blob: cae6fe649a2efd675dde51de0d193b3c01518c07 [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 Lattnerfa25bbb2008-11-19 05:08:23 +000070 Diag(ClassLoc, diag::err_redefinition_different_kind)
71 << ClassName->getName();
Chris Lattner4d391482007-12-12 07:09:47 +000072 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
73 }
74
Ted Kremeneka526c5c2008-01-07 19:49:32 +000075 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000076 if (IDecl) {
77 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000078 if (!IDecl->isForwardDecl()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000079 Diag(AtInterfaceLoc, diag::err_duplicate_class_def) << IDecl->getName();
Steve Naroffcfe8bf32008-11-18 19:15:30 +000080 // Return the previous class interface.
81 // FIXME: don't leak the objects passed in!
82 return IDecl;
83 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000084 IDecl->setLocation(AtInterfaceLoc);
85 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000086 }
Chris Lattnerb752f282008-07-21 07:06:49 +000087 } else {
Daniel Dunbarf6414922008-08-20 18:02:42 +000088 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000089 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000090 if (AttrList)
91 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000092
Steve Naroff31102512008-04-02 18:30:49 +000093 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +000094 // Remember that this needs to be removed when the scope is popped.
95 TUScope->AddDecl(IDecl);
96 }
97
98 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000099 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000100 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000101 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000103 Diag(SuperLoc, diag::err_redefinition_different_kind)
104 << SuperName->getName();
Chris Lattner4d391482007-12-12 07:09:47 +0000105 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
106 }
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 Lattner4d391482007-12-12 07:09:47 +0000110
111 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000112 Diag(SuperLoc, diag::err_undef_superclass)
113 << (SuperClassEntry ? SuperClassEntry->getName()
114 : SuperName->getName())
115 << ClassName->getName() << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000116 }
117 }
118 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000119 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000120 IDecl->setLocEnd(SuperLoc);
121 } else { // we have a root class.
122 IDecl->setLocEnd(ClassLoc);
123 }
124
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000125 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000126 if (NumProtoRefs) {
127 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000128 IDecl->setLocEnd(EndProtoLoc);
129 }
Anders Carlsson15281452008-11-04 16:57:32 +0000130
131 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000132 return IDecl;
133}
134
135/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000136/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000137Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
138 IdentifierInfo *AliasName,
139 SourceLocation AliasLocation,
140 IdentifierInfo *ClassName,
141 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000142 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000143 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000144 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000145 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000146 Diag(AliasLocation, diag::warn_previous_alias_decl);
147 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
148 }
149 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000150 Diag(AliasLocation, diag::err_conflicting_aliasing_type)
151 << AliasName->getName();
Chris Lattner4d391482007-12-12 07:09:47 +0000152 Diag(ADecl->getLocation(), diag::err_previous_declaration);
153 }
154 return 0;
155 }
156 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000157 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000158 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
159 if (CDecl == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000160 Diag(ClassLocation, diag::warn_undef_interface) << ClassName->getName();
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000161 if (CDeclU)
162 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000163 return 0;
164 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000165
166 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000167 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000168 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
169
170 ObjCAliasDecls[AliasName] = AliasDecl;
Anders Carlsson15281452008-11-04 16:57:32 +0000171
172 if (!CheckObjCDeclScope(AliasDecl))
173 TUScope->AddDecl(AliasDecl);
174
Chris Lattner4d391482007-12-12 07:09:47 +0000175 return AliasDecl;
176}
177
Chris Lattnere13b9592008-07-26 04:03:38 +0000178Sema::DeclTy *
179Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
180 IdentifierInfo *ProtocolName,
181 SourceLocation ProtocolLoc,
182 DeclTy * const *ProtoRefs,
183 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000184 SourceLocation EndProtoLoc,
185 AttributeList *AttrList) {
186 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000187 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000188 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000189 if (PDecl) {
190 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000191 if (!PDecl->isForwardDecl()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000192 Diag(ProtocolLoc, diag::err_duplicate_protocol_def)
193 << ProtocolName->getName();
Chris Lattner439e71f2008-03-16 01:25:17 +0000194 // Just return the protocol we already had.
195 // FIXME: don't leak the objects passed in!
196 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000197 }
Steve Narofff11b5082008-08-13 16:39:22 +0000198 // Make sure the cached decl gets a valid start location.
199 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000200 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000201 } else {
Chris Lattner780f3292008-07-21 21:32:27 +0000202 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattnerc8581052008-03-16 20:19:15 +0000203 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000204 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000205 }
Chris Lattner4d391482007-12-12 07:09:47 +0000206
207 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000208 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000209 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000210 PDecl->setLocEnd(EndProtoLoc);
211 }
Anders Carlsson15281452008-11-04 16:57:32 +0000212
213 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000214 return PDecl;
215}
216
217/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000218/// issues an error if they are not declared. It returns list of
219/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000220void
Chris Lattnere13b9592008-07-26 04:03:38 +0000221Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000222 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000223 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000224 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000225 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000226 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
227 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000228 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
229 << ProtocolId[i].first->getName();
Chris Lattnereacc3922008-07-26 03:47:43 +0000230 continue;
231 }
232
233 // If this is a forward declaration and we are supposed to warn in this
234 // case, do it.
235 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000236 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
237 << ProtocolId[i].first->getName();
Chris Lattnereacc3922008-07-26 03:47:43 +0000238 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000239 }
240}
241
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000242/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000243/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000244///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000245void
246Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
247 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000248 const char *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000249 ObjCPropertyDecl::PropertyAttributeKind CAttr =
250 Property->getPropertyAttributes();
251 ObjCPropertyDecl::PropertyAttributeKind SAttr =
252 SuperProperty->getPropertyAttributes();
253 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
254 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000255 Diag(Property->getLocation(), diag::warn_readonly_property)
256 << Property->getName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000257 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
258 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000259 Diag(Property->getLocation(), diag::warn_property_attribute)
260 << Property->getName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000261 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
262 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000263 Diag(Property->getLocation(), diag::warn_property_attribute)
264 << Property->getName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000265
266 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
267 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000268 Diag(Property->getLocation(), diag::warn_property_attribute)
269 << Property->getName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000270 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000271 Diag(Property->getLocation(), diag::warn_property_attribute)
272 << Property->getName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000273 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000274 Diag(Property->getLocation(), diag::warn_property_attribute)
275 << Property->getName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000276
Chris Lattner717250a2008-07-26 20:50:02 +0000277 if (Context.getCanonicalType(Property->getType()) !=
278 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000279 Diag(Property->getLocation(), diag::warn_property_type)
280 << Property->getType().getAsString() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000281
282}
283
284/// ComparePropertiesInBaseAndSuper - This routine compares property
285/// declarations in base and its super class, if any, and issues
286/// diagnostics in a variety of inconsistant situations.
287///
288void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000289Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000290 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
291 if (!SDecl)
292 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000293 // FIXME: O(N^2)
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000294 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
295 E = SDecl->classprop_end(); S != E; ++S) {
296 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000297 // Does property in super class has declaration in current class?
298 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
299 E = IDecl->classprop_end(); I != E; ++I) {
300 ObjCPropertyDecl *PDecl = (*I);
301 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000302 DiagnosePropertyMismatch(PDecl, SuperPDecl,
303 SDecl->getIdentifierName());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000304 }
305 }
306}
307
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000308/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
309/// of properties declared in a protocol and adds them to the list
310/// of properties for current class if it is not there already.
311void
312Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
313 ObjCProtocolDecl *PDecl)
314{
315 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
316 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
317 E = PDecl->classprop_end(); P != E; ++P) {
318 ObjCPropertyDecl *Pr = (*P);
319 ObjCInterfaceDecl::classprop_iterator CP, CE;
320 // Is this property already in class's list of properties?
321 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
322 CP != CE; ++CP)
323 if ((*CP)->getIdentifier() == Pr->getIdentifier())
324 break;
325 if (CP == CE)
326 // Add this property to list of properties for thie class.
327 mergeProperties.push_back(Pr);
328 else
329 // Property protocol already exist in class. Diagnose any mismatch.
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000330 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifierName());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000331 }
332 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
333}
334
335/// MergeProtocolPropertiesIntoClass - This routine merges properties
336/// declared in 'MergeItsProtocols' objects (which can be a class or an
337/// inherited protocol into the list of properties for class 'IDecl'
338///
339
340void
341Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
342 DeclTy *MergeItsProtocols) {
343 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattnerb752f282008-07-21 07:06:49 +0000344 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000345 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
346 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000347 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000348 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
349
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000350 // Go thru the list of protocols for this class and recursively merge
351 // their properties into this class as well.
352 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
353 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000354 MergeProtocolPropertiesIntoClass(IDecl, *P);
355 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000356 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
357 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
358 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000359 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000360 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000361}
362
Chris Lattner4d391482007-12-12 07:09:47 +0000363/// ActOnForwardProtocolDeclaration -
364Action::DeclTy *
365Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000366 const IdentifierLocPair *IdentList,
367 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000368 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000369
370 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000371 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000372 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000373 if (PDecl == 0) // Not already seen?
374 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000375
376 Protocols.push_back(PDecl);
377 }
Anders Carlsson15281452008-11-04 16:57:32 +0000378
379 ObjCForwardProtocolDecl *PDecl =
380 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
381 &Protocols[0], Protocols.size());
382
383 CheckObjCDeclScope(PDecl);
384 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000385}
386
Chris Lattner7caeabd2008-07-21 22:17:28 +0000387Sema::DeclTy *Sema::
388ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
389 IdentifierInfo *ClassName, SourceLocation ClassLoc,
390 IdentifierInfo *CategoryName,
391 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000392 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000393 unsigned NumProtoRefs,
394 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000395 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000396
Chris Lattner61f9d412008-03-16 20:34:23 +0000397 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000398 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000399 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000400
401 /// Check that class of this category is already completely declared.
402 if (!IDecl || IDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000403 Diag(ClassLoc, diag::err_undef_interface) << ClassName->getName();
Steve Naroffd100c802008-06-05 15:03:27 +0000404 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000405 /// Check for duplicate interface declaration for this category
406 ObjCCategoryDecl *CDeclChain;
407 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
408 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000409 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000410 Diag(CategoryLoc, diag::warn_dup_category_def)
411 << ClassName->getName() << CategoryName->getName();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000412 break;
413 }
Chris Lattner4d391482007-12-12 07:09:47 +0000414 }
Steve Naroffd100c802008-06-05 15:03:27 +0000415 if (!CDeclChain)
416 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000417 }
Chris Lattner4d391482007-12-12 07:09:47 +0000418
419 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000420 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
421 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000422 }
Anders Carlsson15281452008-11-04 16:57:32 +0000423
424 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000425 return CDecl;
426}
427
428/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000429/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000430/// object.
431Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
432 SourceLocation AtCatImplLoc,
433 IdentifierInfo *ClassName, SourceLocation ClassLoc,
434 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000435 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000436 ObjCCategoryImplDecl *CDecl =
437 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000438 /// Check that class of this category is already completely declared.
439 if (!IDecl || IDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000440 Diag(ClassLoc, diag::err_undef_interface) << ClassName->getName();
Chris Lattner4d391482007-12-12 07:09:47 +0000441
442 /// TODO: Check that CatName, category name, is not used in another
443 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000444 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000445
446 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000447 return CDecl;
448}
449
450Sema::DeclTy *Sema::ActOnStartClassImplementation(
451 SourceLocation AtClassImplLoc,
452 IdentifierInfo *ClassName, SourceLocation ClassLoc,
453 IdentifierInfo *SuperClassname,
454 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000456 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000457 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000458 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000459 Diag(ClassLoc, diag::err_redefinition_different_kind,
460 ClassName->getName());
461 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
462 }
463 else {
464 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000465 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000466 if (!IDecl)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000467 Diag(ClassLoc, diag::warn_undef_interface) << ClassName->getName();
Chris Lattner4d391482007-12-12 07:09:47 +0000468 }
469
470 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000471 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000472 if (SuperClassname) {
473 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000474 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000475 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000476 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
477 SuperClassname->getName());
478 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
479 }
480 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000481 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000482 if (!SDecl)
483 Diag(SuperClassLoc, diag::err_undef_superclass,
484 SuperClassname->getName(), ClassName->getName());
485 else if (IDecl && IDecl->getSuperClass() != SDecl) {
486 // This implementation and its interface do not have the same
487 // super class.
488 Diag(SuperClassLoc, diag::err_conflicting_super_class,
489 SDecl->getName());
490 Diag(SDecl->getLocation(), diag::err_previous_definition);
491 }
492 }
493 }
494
495 if (!IDecl) {
496 // Legacy case of @implementation with no corresponding @interface.
497 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000498
499 // FIXME: Do we support attributes on the @implementation? If so
500 // we should copy them over.
Chris Lattnerb752f282008-07-21 07:06:49 +0000501 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000502 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000503 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000504 IDecl->setSuperClass(SDecl);
505 IDecl->setLocEnd(ClassLoc);
506
507 // Remember that this needs to be removed when the scope is popped.
508 TUScope->AddDecl(IDecl);
509 }
510
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000511 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000512 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
513 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000514
Anders Carlsson15281452008-11-04 16:57:32 +0000515 if (CheckObjCDeclScope(IMPDecl))
516 return IMPDecl;
517
Chris Lattner4d391482007-12-12 07:09:47 +0000518 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000519 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000520 // FIXME: Don't leak everything!
Chris Lattner4d391482007-12-12 07:09:47 +0000521 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
522 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000524 return IMPDecl;
525}
526
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000527void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
528 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000529 SourceLocation RBrace) {
530 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000531 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000532 if (!IDecl)
533 return;
534 /// Check case of non-existing @interface decl.
535 /// (legacy objective-c @implementation decl without an @interface decl).
536 /// Add implementations's ivar to the synthesize class's ivar list.
537 if (IDecl->ImplicitInterfaceDecl()) {
538 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
539 return;
540 }
541 // If implementation has empty ivar list, just return.
542 if (numIvars == 0)
543 return;
544
545 assert(ivars && "missing @implementation ivars");
546
547 // Check interface's Ivar list against those in the implementation.
548 // names and types must match.
549 //
Chris Lattner4d391482007-12-12 07:09:47 +0000550 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000551 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000552 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
553 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000554 ObjCIvarDecl* ImplIvar = ivars[j++];
555 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000556 assert (ImplIvar && "missing implementation ivar");
557 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000558 if (Context.getCanonicalType(ImplIvar->getType()) !=
559 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000560 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
561 << ImplIvar->getIdentifier()->getName();
562 Diag(ClsIvar->getLocation(), diag::err_previous_definition)
563 << ClsIvar->getIdentifier()->getName();
Chris Lattner4d391482007-12-12 07:09:47 +0000564 }
565 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
566 // as error.
567 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000568 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
569 << ImplIvar->getIdentifier()->getName();
570 Diag(ClsIvar->getLocation(), diag::err_previous_definition)
571 << ClsIvar->getIdentifier()->getName();
Chris Lattner609e4c72007-12-12 18:11:49 +0000572 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000573 }
574 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000575 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000576
577 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000578 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000579 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000580 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000581}
582
Steve Naroff3c2eb662008-02-10 21:38:56 +0000583void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
584 bool &IncompleteImpl) {
585 if (!IncompleteImpl) {
586 Diag(ImpLoc, diag::warn_incomplete_impl);
587 IncompleteImpl = true;
588 }
589 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
590}
591
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000592/// FIXME: Type hierarchies in Objective-C can be deep. We could most
593/// likely improve the efficiency of selector lookups and type
594/// checking by associating with each protocol / interface / category
595/// the flattened instance tables. If we used an immutable set to keep
596/// the table then it wouldn't add significant memory cost and it
597/// would be handy for lookups.
598
Steve Naroffefe7f362008-02-08 22:06:17 +0000599/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000600/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000601void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
602 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000603 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000604 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000605 const llvm::DenseSet<Selector> &ClsMap,
606 ObjCInterfaceDecl *IDecl) {
607 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
608
609 // If a method lookup fails locally we still need to look and see if
610 // the method was implemented by a base class or an inherited
611 // protocol. This lookup is slow, but occurs rarely in correct code
612 // and otherwise would terminate in a warning.
613
Chris Lattner4d391482007-12-12 07:09:47 +0000614 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000615 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000616 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000618 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
619 !InsMap.count(method->getSelector()) &&
620 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000621 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000622 }
623 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000625 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000626 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000627 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
628 !ClsMap.count(method->getSelector()) &&
629 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000630 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000631 }
Chris Lattner780f3292008-07-21 21:32:27 +0000632 // Check on this protocols's referenced protocols, recursively.
633 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
634 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000635 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000636}
637
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000638void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
639 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000640 llvm::DenseSet<Selector> InsMap;
641 // Check and see if instance methods in class interface have been
642 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000643 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000644 E = IMPDecl->instmeth_end(); I != E; ++I)
645 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000646
647 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000648 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000649 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000650 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000651 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000652
Chris Lattner4d391482007-12-12 07:09:47 +0000653 llvm::DenseSet<Selector> ClsMap;
654 // Check and see if class methods in class interface have been
655 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000656 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000657 E = IMPDecl->classmeth_end(); I != E; ++I)
658 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000659
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000660 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000661 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000662 if (!ClsMap.count((*I)->getSelector()))
663 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000664
665 // Check the protocol list for unimplemented methods in the @implementation
666 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000667 const ObjCList<ObjCProtocolDecl> &Protocols =
668 IDecl->getReferencedProtocols();
669 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
670 E = Protocols.end(); I != E; ++I)
671 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000672 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000673}
674
675/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000676/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000677void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
678 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000679 llvm::DenseSet<Selector> InsMap;
680 // Check and see if instance methods in category interface have been
681 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000683 E = CatImplDecl->instmeth_end(); I != E; ++I)
684 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000685
686 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000688 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000689 if (!InsMap.count((*I)->getSelector()))
690 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
691
Chris Lattner4d391482007-12-12 07:09:47 +0000692 llvm::DenseSet<Selector> ClsMap;
693 // Check and see if class methods in category interface have been
694 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000695 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000696 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
697 I != E; ++I)
698 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000699
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000700 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000701 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000702 if (!ClsMap.count((*I)->getSelector()))
703 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000704
705 // Check the protocol list for unimplemented methods in the @implementation
706 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000707 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
708 E = CatClassDecl->protocol_end(); PI != E; ++PI)
709 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000710 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000711}
712
713/// ActOnForwardClassDeclaration -
714Action::DeclTy *
715Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
716 IdentifierInfo **IdentList, unsigned NumElts)
717{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000719
720 for (unsigned i = 0; i != NumElts; ++i) {
721 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000722 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000723 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000724 // GCC apparently allows the following idiom:
725 //
726 // typedef NSObject < XCElementTogglerP > XCElementToggler;
727 // @class XCElementToggler;
728 //
729 // FIXME: Make an extension?
730 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
731 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
732 Diag(AtClassLoc, diag::err_redefinition_different_kind,
733 IdentList[i]->getName());
734 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
735 }
Chris Lattner4d391482007-12-12 07:09:47 +0000736 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000737 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000738 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000739 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000740 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000741 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000742
743 // Remember that this needs to be removed when the scope is popped.
744 TUScope->AddDecl(IDecl);
745 }
746
747 Interfaces.push_back(IDecl);
748 }
749
Anders Carlsson15281452008-11-04 16:57:32 +0000750 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
751 &Interfaces[0],
752 Interfaces.size());
753
754 CheckObjCDeclScope(CDecl);
755 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000756}
757
758
759/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
760/// returns true, or false, accordingly.
761/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000762bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000763 const ObjCMethodDecl *PrevMethod,
764 bool matchBasedOnSizeAndAlignment) {
765 QualType T1 = Context.getCanonicalType(Method->getResultType());
766 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
767
768 if (T1 != T2) {
769 // The result types are different.
770 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000771 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000772 // Incomplete types don't have a size and alignment.
773 if (T1->isIncompleteType() || T2->isIncompleteType())
774 return false;
775 // Check is based on size and alignment.
776 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
777 return false;
778 }
779 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
780 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
781 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
782 if (T1 != T2) {
783 // The result types are different.
784 if (!matchBasedOnSizeAndAlignment)
785 return false;
786 // Incomplete types don't have a size and alignment.
787 if (T1->isIncompleteType() || T2->isIncompleteType())
788 return false;
789 // Check is based on size and alignment.
790 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
791 return false;
792 }
Chris Lattner4d391482007-12-12 07:09:47 +0000793 }
794 return true;
795}
796
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000797void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
798 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000799 if (!FirstMethod.Method) {
800 // Haven't seen a method with this selector name yet - add it.
801 FirstMethod.Method = Method;
802 FirstMethod.Next = 0;
803 } else {
804 // We've seen a method with this name, now check the type signature(s).
805 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
806
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000808 Next = Next->Next)
809 match = MatchTwoMethodDeclarations(Method, Next->Method);
810
811 if (!match) {
812 // We have a new signature for an existing method - add it.
813 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000815 FirstMethod.Next = OMI;
816 }
817 }
818}
819
Steve Naroff6f5f41c2008-10-21 10:50:19 +0000820// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +0000821ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
822 SourceRange R) {
823 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000824 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +0000825
826 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000827 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
828 // This checks if the methods differ by size & alignment.
829 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
830 issueWarning = true;
831 }
832 if (issueWarning && (MethList.Method && MethList.Next)) {
Steve Naroff037cda52008-09-30 14:38:43 +0000833 Diag(R.getBegin(), diag::warn_multiple_method_decl, Sel.getName(), R);
834 Diag(MethList.Method->getLocStart(), diag::warn_using_decl,
835 MethList.Method->getSourceRange());
836 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
837 Diag(Next->Method->getLocStart(), diag::warn_also_found_decl,
838 Next->Method->getSourceRange());
839 }
840 return MethList.Method;
841}
842
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000843void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
844 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000845 if (!FirstMethod.Method) {
846 // Haven't seen a method with this selector name yet - add it.
847 FirstMethod.Method = Method;
848 FirstMethod.Next = 0;
849 } else {
850 // We've seen a method with this name, now check the type signature(s).
851 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
852
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000854 Next = Next->Next)
855 match = MatchTwoMethodDeclarations(Method, Next->Method);
856
857 if (!match) {
858 // We have a new signature for an existing method - add it.
859 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000860 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000861 FirstMethod.Next = OMI;
862 }
863 }
864}
865
Steve Naroffa56f6162007-12-18 01:30:32 +0000866// Note: For class/category implemenations, allMethods/allProperties is
867// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000868void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
869 DeclTy **allMethods, unsigned allNum,
870 DeclTy **allProperties, unsigned pNum) {
871 Decl *ClassDecl = static_cast<Decl *>(classDecl);
872
Steve Naroffa56f6162007-12-18 01:30:32 +0000873 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
874 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000875 // should be true.
876 if (!ClassDecl)
877 return;
878
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
880 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000881
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
883 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000884
885 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000886 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
887 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000888 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000889
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000890 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +0000891 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
892 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000893 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
894 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
895 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000896 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000897 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000898 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000899 }
Chris Lattner4d391482007-12-12 07:09:47 +0000900
901 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000902 ObjCMethodDecl *Method =
903 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000904
905 if (!Method) continue; // Already issued a diagnostic.
906 if (Method->isInstance()) {
907 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000909 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
910 : false;
911 if (isInterfaceDeclKind && PrevMethod && !match
912 || checkIdenticalMethods && match) {
913 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
914 Method->getSelector().getName());
915 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
916 } else {
917 insMethods.push_back(Method);
918 InsMap[Method->getSelector()] = Method;
919 /// The following allows us to typecheck messages to "id".
920 AddInstanceMethodToGlobalPool(Method);
921 }
922 }
923 else {
924 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000925 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000926 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
927 : false;
928 if (isInterfaceDeclKind && PrevMethod && !match
929 || checkIdenticalMethods && match) {
930 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
931 Method->getSelector().getName());
932 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
933 } else {
934 clsMethods.push_back(Method);
935 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000936 /// The following allows us to typecheck messages to "Class".
937 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000938 }
939 }
940 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +0000941 // Save the size so we can detect if we've added any property methods.
942 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
943 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner4d391482007-12-12 07:09:47 +0000944
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000945 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000946 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000947 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000948 ComparePropertiesInBaseAndSuper(I);
949 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000950 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
951 e = I->classprop_end(); i != e; ++i)
952 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000953 I->addMethods(&insMethods[0], insMethods.size(),
954 &clsMethods[0], clsMethods.size(), AtEndLoc);
955
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000956 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000957 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
958 e = P->classprop_end(); i != e; ++i)
959 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000960 P->addMethods(&insMethods[0], insMethods.size(),
961 &clsMethods[0], clsMethods.size(), AtEndLoc);
962 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000963 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000964 // FIXME: Need to compare properties to those in interface?
965
966 // FIXME: If we merge properties into class we should probably
967 // merge them into category as well?
968 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
969 e = C->classprop_end(); i != e; ++i)
970 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000971 C->addMethods(&insMethods[0], insMethods.size(),
972 &clsMethods[0], clsMethods.size(), AtEndLoc);
973 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974 else if (ObjCImplementationDecl *IC =
975 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000976 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000977 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000978 ImplMethodsVsClassMethods(IC, IDecl);
979 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000980 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000981 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000982 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000983 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000984 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +0000985 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000987 Categories; Categories = Categories->getNextClassCategory()) {
988 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
989 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
990 break;
991 }
992 }
993 }
994 }
Steve Naroff5e0a74f2008-09-29 14:20:56 +0000995 // Add any synthesized methods to the global pool. This allows us to
996 // handle the following, which is supported by GCC (and part of the design).
997 //
998 // @interface Foo
999 // @property double bar;
1000 // @end
1001 //
1002 // void thisIsUnfortunate() {
1003 // id foo;
1004 // double bar = [foo bar];
1005 // }
1006 //
1007 if (insMethodsSizePriorToPropAdds < insMethods.size())
1008 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1009 AddInstanceMethodToGlobalPool(insMethods[i]);
1010 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1011 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1012 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001013}
1014
1015
1016/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1017/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001018static Decl::ObjCDeclQualifier
1019CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1020 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1021 if (PQTVal & ObjCDeclSpec::DQ_In)
1022 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1023 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1024 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1025 if (PQTVal & ObjCDeclSpec::DQ_Out)
1026 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1027 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1028 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1029 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1030 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1031 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1032 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001033
1034 return ret;
1035}
1036
1037Sema::DeclTy *Sema::ActOnMethodDeclaration(
1038 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001039 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001040 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001041 Selector Sel,
1042 // optional arguments. The number of types/arguments is obtained
1043 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001044 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +00001045 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1046 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001047 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001048
1049 // Make sure we can establish a context for the method.
1050 if (!ClassDecl) {
1051 Diag(MethodLoc, diag::error_missing_method_context);
1052 return 0;
1053 }
Chris Lattner4d391482007-12-12 07:09:47 +00001054 QualType resultDeclType;
1055
1056 if (ReturnType)
1057 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1058 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001059 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001060
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001061 ObjCMethodDecl* ObjCMethod =
1062 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +00001063 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001064 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001065 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001066 MethodDeclKind == tok::objc_optional ?
1067 ObjCMethodDecl::Optional :
1068 ObjCMethodDecl::Required);
1069
Chris Lattner0ed844b2008-04-04 06:12:32 +00001070 llvm::SmallVector<ParmVarDecl*, 16> Params;
1071
1072 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1073 // FIXME: arg->AttrList must be stored too!
1074 QualType argType;
1075
1076 if (ArgTypes[i])
1077 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1078 else
1079 argType = Context.getObjCIdType();
1080 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1081 SourceLocation(/*FIXME*/),
1082 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +00001083 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001084 Param->setObjCDeclQualifier(
1085 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1086 Params.push_back(Param);
1087 }
1088
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001089 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1090 ObjCMethod->setObjCDeclQualifier(
1091 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1092 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001093
1094 if (AttrList)
1095 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001096
1097 // For implementations (which can be very "coarse grain"), we add the
1098 // method now. This allows the AST to implement lookup methods that work
1099 // incrementally (without waiting until we parse the @end). It also allows
1100 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001101 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001102 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001103 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001104 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001105 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001106 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001107 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001109 }
1110 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001111 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001112 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001113 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001114 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001115 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001116 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001117 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001118 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001119 }
1120 }
1121 if (PrevMethod) {
1122 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001123 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1124 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +00001125 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1126 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001127 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001128}
1129
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001130void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1131 SourceLocation Loc,
1132 unsigned &Attributes) {
1133 // FIXME: Improve the reported location.
1134
1135 // readonly and readwrite conflict.
1136 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1137 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
1138 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1139 "readonly", "readwrite");
1140 Attributes ^= ObjCDeclSpec::DQ_PR_readonly;
1141 }
1142
1143 // Check for copy or retain on non-object types.
1144 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1145 !Context.isObjCObjectPointerType(PropertyTy)) {
1146 Diag(Loc, diag::err_objc_property_requires_object,
1147 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1148 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1149 }
1150
1151 // Check for more than one of { assign, copy, retain }.
1152 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1153 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1154 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1155 "assign", "copy");
1156 Attributes ^= ObjCDeclSpec::DQ_PR_copy;
1157 }
1158 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1159 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1160 "assign", "retain");
1161 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1162 }
1163 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1164 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1165 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1166 "copy", "retain");
1167 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1168 }
1169 }
1170
1171 // Warn if user supplied no assignment attribute, property is
1172 // readwrite, and this is an object type.
1173 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1174 ObjCDeclSpec::DQ_PR_retain)) &&
1175 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1176 Context.isObjCObjectPointerType(PropertyTy)) {
1177 // Skip this warning in gc-only mode.
1178 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1179 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1180
1181 // If non-gc code warn that this is likely inappropriate.
1182 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1183 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1184
1185 // FIXME: Implement warning dependent on NSCopying being
1186 // implemented. See also:
1187 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1188 // (please trim this list while you are at it).
1189 }
1190}
1191
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001192Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1193 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001194 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001195 Selector GetterSel,
1196 Selector SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001197 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001198 QualType T = GetTypeForDeclarator(FD.D, S);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001199 unsigned Attributes = ODS.getPropertyAttributes();
1200
1201 // May modify Attributes.
1202 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1203
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001204 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1205 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001206 // Regardless of setter/getter attribute, we save the default getter/setter
1207 // selector names in anticipation of declaration of setter/getter methods.
1208 PDecl->setGetterName(GetterSel);
1209 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001210
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001211 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001212 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001213
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001214 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001215 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001216
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001217 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001218 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001219
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001220 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +00001222
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001223 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001224 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001225
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001226 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001227 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001228
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001229 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001230 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001231
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001232 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001233 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001234
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001235 if (MethodImplKind == tok::objc_required)
1236 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1237 else if (MethodImplKind == tok::objc_optional)
1238 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1239
Chris Lattner4d391482007-12-12 07:09:47 +00001240 return PDecl;
1241}
1242
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001243/// ActOnPropertyImplDecl - This routine performs semantic checks and
1244/// builds the AST node for a property implementation declaration; declared
1245/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001246///
1247Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1248 SourceLocation PropertyLoc,
1249 bool Synthesize,
1250 DeclTy *ClassCatImpDecl,
1251 IdentifierInfo *PropertyId,
1252 IdentifierInfo *PropertyIvar) {
1253 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1254 // Make sure we have a context for the property implementation declaration.
1255 if (!ClassImpDecl) {
1256 Diag(AtLoc, diag::error_missing_property_context);
1257 return 0;
1258 }
1259 ObjCPropertyDecl *property = 0;
1260 ObjCInterfaceDecl* IDecl = 0;
1261 // Find the class or category class where this property must have
1262 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001263 ObjCImplementationDecl *IC = 0;
1264 ObjCCategoryImplDecl* CatImplClass = 0;
1265 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001266 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001267 // We always synthesize an interface for an implementation
1268 // without an interface decl. So, IDecl is always non-zero.
1269 assert(IDecl &&
1270 "ActOnPropertyImplDecl - @implementation without @interface");
1271
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001272 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001273 property = IDecl->FindPropertyDeclaration(PropertyId);
1274 if (!property) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001275 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001276 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001277 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001278 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001279 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001280 if (Synthesize) {
1281 Diag(AtLoc, diag::error_synthesize_category_decl);
1282 return 0;
1283 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001284 IDecl = CatImplClass->getClassInterface();
1285 if (!IDecl) {
1286 Diag(AtLoc, diag::error_missing_property_interface);
1287 return 0;
1288 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001289 ObjCCategoryDecl *Category =
1290 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1291
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001292 // If category for this implementation not found, it is an error which
1293 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001294 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001295 return 0;
1296 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001297 property = Category->FindPropertyDeclaration(PropertyId);
1298 if (!property) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001299 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001300 Category->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001301 return 0;
1302 }
1303 }
1304 else {
1305 Diag(AtLoc, diag::error_bad_property_context);
1306 return 0;
1307 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001308 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001309 // Check that we have a valid, previously declared ivar for @synthesize
1310 if (Synthesize) {
1311 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001312 if (!PropertyIvar)
1313 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001314 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001315 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001316 if (!Ivar) {
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001317 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1318 PropertyId->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001319 return 0;
1320 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001321 QualType PropType = Context.getCanonicalType(property->getType());
1322 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1323
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001324 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001325 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001326 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Steve Naroff3ce52d62008-09-30 10:07:56 +00001327 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1328 Ivar->getName());
1329 return 0;
1330 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001331 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001332 } else if (PropertyIvar) {
1333 // @dynamic
1334 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1335 return 0;
1336 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001337 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001338 ObjCPropertyImplDecl *PIDecl =
1339 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1340 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001341 ObjCPropertyImplDecl::Synthesize
1342 : ObjCPropertyImplDecl::Dynamic),
1343 Ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001344 if (IC)
1345 IC->addPropertyImplementation(PIDecl);
1346 else
1347 CatImplClass->addPropertyImplementation(PIDecl);
1348
1349 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001350}
Anders Carlsson15281452008-11-04 16:57:32 +00001351
1352bool Sema::CheckObjCDeclScope(Decl *D)
1353{
1354 if (isa<TranslationUnitDecl>(CurContext))
1355 return false;
1356
1357 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1358 D->setInvalidDecl();
1359
1360 return true;
1361}