blob: 49b990906a2f580eca1fa95802aff564dff2ff77 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner855e51f2007-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 Dunbar8d03cbe2008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremenek42730c52008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff3ac43f92008-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 Narofffe9eb6a2007-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 Lattner855e51f2007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerf3874bc2008-04-06 04:47:34 +000039 PushDeclContext(MDecl);
Chris Lattner855e51f2007-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 Lattner855e51f2007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Daniel Dunbareaf91c32008-08-26 06:07:48 +000045 MDecl->createImplicitParams(Context);
Chris Lattner855e51f2007-12-12 07:09:47 +000046
Daniel Dunbareaf91c32008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000049
Chris Lattner97316c02008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000056 }
57}
58
Chris Lattnere705e5e2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000069 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000070 Diag(ClassLoc, diag::err_redefinition_different_kind,
71 ClassName->getName());
72 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
73 }
74
Ted Kremenek42730c52008-01-07 19:49:32 +000075 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000076 if (IDecl) {
77 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000078 if (!IDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +000079 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
Steve Naroff1422a622008-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 Lattner855e51f2007-12-12 07:09:47 +000084 IDecl->setLocation(AtInterfaceLoc);
85 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000086 }
Chris Lattner5cece462008-07-21 07:06:49 +000087 } else {
Daniel Dunbard8bd6822008-08-20 18:02:42 +000088 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000089 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000090 if (AttrList)
91 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000092
Steve Naroff15208162008-04-02 18:30:49 +000093 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +000099 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000100 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000101 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000102 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000103 Diag(SuperLoc, diag::err_redefinition_different_kind,
104 SuperName->getName());
105 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
106 }
107 else {
108 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000110
111 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000112 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 SuperClassEntry ? SuperClassEntry->getName()
114 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000115 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000116 }
117 }
118 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000119 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000120 IDecl->setLocEnd(SuperLoc);
121 } else { // we have a root class.
122 IDecl->setLocEnd(ClassLoc);
123 }
124
Steve Naroff1422a622008-11-18 19:15:30 +0000125 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000126 if (NumProtoRefs) {
127 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000128 IDecl->setLocEnd(EndProtoLoc);
129 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000130
131 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000132 return IDecl;
133}
134
135/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000136/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000137Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
138 IdentifierInfo *AliasName,
139 SourceLocation AliasLocation,
140 IdentifierInfo *ClassName,
141 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000142 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000143 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000144 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000145 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000146 Diag(AliasLocation, diag::warn_previous_alias_decl);
147 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
148 }
149 else {
150 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
151 AliasName->getName());
152 Diag(ADecl->getLocation(), diag::err_previous_declaration);
153 }
154 return 0;
155 }
156 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000157 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000158 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
159 if (CDecl == 0) {
160 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
161 if (CDeclU)
162 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000163 return 0;
164 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000165
166 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000168 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
169
170 ObjCAliasDecls[AliasName] = AliasDecl;
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000171
172 if (!CheckObjCDeclScope(AliasDecl))
173 TUScope->AddDecl(AliasDecl);
174
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 return AliasDecl;
176}
177
Chris Lattner2bdedd62008-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 Dunbar28680d12008-09-26 04:48:09 +0000184 SourceLocation EndProtoLoc,
185 AttributeList *AttrList) {
186 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000187 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000188 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000189 if (PDecl) {
190 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000191 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000192 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
193 ProtocolName->getName());
Chris Lattnerc1881852008-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 Lattner855e51f2007-12-12 07:09:47 +0000197 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000198 // Make sure the cached decl gets a valid start location.
199 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000200 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000201 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000202 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000203 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000204 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000205 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000206
207 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000208 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000209 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000210 PDecl->setLocEnd(EndProtoLoc);
211 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000212
213 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000214 return PDecl;
215}
216
217/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-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 Lattner855e51f2007-12-12 07:09:47 +0000220void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000221Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000222 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000223 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000224 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000226 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
227 if (!PDecl) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000228 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
229 ProtocolId[i].first->getName());
Chris Lattner17d50a92008-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())
236 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
237 ProtocolId[i].first->getName());
238 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000239 }
240}
241
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000242/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000243/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000244///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000245void
246Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
247 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000248 const char *inheritedName) {
Fariborz Jahanian1e89de32008-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))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000255 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000256 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000257 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
258 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000259 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000260 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000261 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000262 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
263 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000264 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000265 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000266 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000267
268 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
269 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000270 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000271 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000272 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000273 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000274 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000275 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000276 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000277 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000278 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000279 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000280 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000281
Chris Lattnerc5cff302008-07-26 20:50:02 +0000282 if (Context.getCanonicalType(Property->getType()) !=
283 Context.getCanonicalType(SuperProperty->getType()))
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000284 Diag(Property->getLocation(), diag::warn_property_type,
285 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000286 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000287
288}
289
290/// ComparePropertiesInBaseAndSuper - This routine compares property
291/// declarations in base and its super class, if any, and issues
292/// diagnostics in a variety of inconsistant situations.
293///
294void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000295Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000296 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
297 if (!SDecl)
298 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000299 // FIXME: O(N^2)
Fariborz Jahanianed986602008-05-01 00:03:38 +0000300 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
301 E = SDecl->classprop_end(); S != E; ++S) {
302 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000303 // Does property in super class has declaration in current class?
304 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
305 E = IDecl->classprop_end(); I != E; ++I) {
306 ObjCPropertyDecl *PDecl = (*I);
307 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000308 DiagnosePropertyMismatch(PDecl, SuperPDecl,
309 SDecl->getIdentifierName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000310 }
311 }
312}
313
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000314/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
315/// of properties declared in a protocol and adds them to the list
316/// of properties for current class if it is not there already.
317void
318Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
319 ObjCProtocolDecl *PDecl)
320{
321 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
322 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
323 E = PDecl->classprop_end(); P != E; ++P) {
324 ObjCPropertyDecl *Pr = (*P);
325 ObjCInterfaceDecl::classprop_iterator CP, CE;
326 // Is this property already in class's list of properties?
327 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
328 CP != CE; ++CP)
329 if ((*CP)->getIdentifier() == Pr->getIdentifier())
330 break;
331 if (CP == CE)
332 // Add this property to list of properties for thie class.
333 mergeProperties.push_back(Pr);
334 else
335 // Property protocol already exist in class. Diagnose any mismatch.
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000336 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifierName());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000337 }
338 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
339}
340
341/// MergeProtocolPropertiesIntoClass - This routine merges properties
342/// declared in 'MergeItsProtocols' objects (which can be a class or an
343/// inherited protocol into the list of properties for class 'IDecl'
344///
345
346void
347Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
348 DeclTy *MergeItsProtocols) {
349 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000350 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000351 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
352 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000353 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000354 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
355
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000356 // Go thru the list of protocols for this class and recursively merge
357 // their properties into this class as well.
358 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
359 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000360 MergeProtocolPropertiesIntoClass(IDecl, *P);
361 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000362 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
363 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
364 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000365 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000366 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000367}
368
Chris Lattner855e51f2007-12-12 07:09:47 +0000369/// ActOnForwardProtocolDeclaration -
370Action::DeclTy *
371Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000372 const IdentifierLocPair *IdentList,
373 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000374 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000375
376 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000377 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000378 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000379 if (PDecl == 0) // Not already seen?
380 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000381
382 Protocols.push_back(PDecl);
383 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000384
385 ObjCForwardProtocolDecl *PDecl =
386 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
387 &Protocols[0], Protocols.size());
388
389 CheckObjCDeclScope(PDecl);
390 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000391}
392
Chris Lattnere705e5e2008-07-21 22:17:28 +0000393Sema::DeclTy *Sema::
394ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
395 IdentifierInfo *ClassName, SourceLocation ClassLoc,
396 IdentifierInfo *CategoryName,
397 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000398 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000399 unsigned NumProtoRefs,
400 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000401 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000402
Chris Lattnere29dc832008-03-16 20:34:23 +0000403 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000404 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000405 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000406
407 /// Check that class of this category is already completely declared.
408 if (!IDecl || IDecl->isForwardDecl())
409 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000410 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000411 /// Check for duplicate interface declaration for this category
412 ObjCCategoryDecl *CDeclChain;
413 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
414 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000415 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000416 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000417 CategoryName->getName());
418 break;
419 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000420 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000421 if (!CDeclChain)
422 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000423 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000424
425 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000426 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
427 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000428 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000429
430 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000431 return CDecl;
432}
433
434/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000435/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000436/// object.
437Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
438 SourceLocation AtCatImplLoc,
439 IdentifierInfo *ClassName, SourceLocation ClassLoc,
440 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000441 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000442 ObjCCategoryImplDecl *CDecl =
443 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000444 /// Check that class of this category is already completely declared.
445 if (!IDecl || IDecl->isForwardDecl())
446 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
447
448 /// TODO: Check that CatName, category name, is not used in another
449 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000450 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000451
452 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000453 return CDecl;
454}
455
456Sema::DeclTy *Sema::ActOnStartClassImplementation(
457 SourceLocation AtClassImplLoc,
458 IdentifierInfo *ClassName, SourceLocation ClassLoc,
459 IdentifierInfo *SuperClassname,
460 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000461 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000462 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000463 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000464 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000465 Diag(ClassLoc, diag::err_redefinition_different_kind,
466 ClassName->getName());
467 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
468 }
469 else {
470 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000471 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000472 if (!IDecl)
473 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
474 }
475
476 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000477 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000478 if (SuperClassname) {
479 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000480 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000481 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000482 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
483 SuperClassname->getName());
484 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
485 }
486 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000487 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000488 if (!SDecl)
489 Diag(SuperClassLoc, diag::err_undef_superclass,
490 SuperClassname->getName(), ClassName->getName());
491 else if (IDecl && IDecl->getSuperClass() != SDecl) {
492 // This implementation and its interface do not have the same
493 // super class.
494 Diag(SuperClassLoc, diag::err_conflicting_super_class,
495 SDecl->getName());
496 Diag(SDecl->getLocation(), diag::err_previous_definition);
497 }
498 }
499 }
500
501 if (!IDecl) {
502 // Legacy case of @implementation with no corresponding @interface.
503 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000504
505 // FIXME: Do we support attributes on the @implementation? If so
506 // we should copy them over.
Chris Lattner5cece462008-07-21 07:06:49 +0000507 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000508 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000509 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000510 IDecl->setSuperClass(SDecl);
511 IDecl->setLocEnd(ClassLoc);
512
513 // Remember that this needs to be removed when the scope is popped.
514 TUScope->AddDecl(IDecl);
515 }
516
Ted Kremenek42730c52008-01-07 19:49:32 +0000517 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000518 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
519 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000520
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000521 if (CheckObjCDeclScope(IMPDecl))
522 return IMPDecl;
523
Chris Lattner855e51f2007-12-12 07:09:47 +0000524 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000525 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000526 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000527 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
528 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000529 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000530 return IMPDecl;
531}
532
Ted Kremenek42730c52008-01-07 19:49:32 +0000533void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
534 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000535 SourceLocation RBrace) {
536 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000537 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 if (!IDecl)
539 return;
540 /// Check case of non-existing @interface decl.
541 /// (legacy objective-c @implementation decl without an @interface decl).
542 /// Add implementations's ivar to the synthesize class's ivar list.
543 if (IDecl->ImplicitInterfaceDecl()) {
544 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
545 return;
546 }
547 // If implementation has empty ivar list, just return.
548 if (numIvars == 0)
549 return;
550
551 assert(ivars && "missing @implementation ivars");
552
553 // Check interface's Ivar list against those in the implementation.
554 // names and types must match.
555 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000556 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000557 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000558 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
559 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000560 ObjCIvarDecl* ImplIvar = ivars[j++];
561 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000562 assert (ImplIvar && "missing implementation ivar");
563 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000564 if (Context.getCanonicalType(ImplIvar->getType()) !=
565 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000566 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
567 ImplIvar->getIdentifier()->getName());
568 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
569 ClsIvar->getIdentifier()->getName());
570 }
571 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
572 // as error.
573 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
574 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
575 ImplIvar->getIdentifier()->getName());
576 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
577 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000578 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000579 }
580 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000581 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000582
583 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000584 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000585 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000586 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000587}
588
Steve Naroffb4f48512008-02-10 21:38:56 +0000589void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
590 bool &IncompleteImpl) {
591 if (!IncompleteImpl) {
592 Diag(ImpLoc, diag::warn_incomplete_impl);
593 IncompleteImpl = true;
594 }
595 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
596}
597
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000598/// FIXME: Type hierarchies in Objective-C can be deep. We could most
599/// likely improve the efficiency of selector lookups and type
600/// checking by associating with each protocol / interface / category
601/// the flattened instance tables. If we used an immutable set to keep
602/// the table then it wouldn't add significant memory cost and it
603/// would be handy for lookups.
604
Steve Naroffb268d2a2008-02-08 22:06:17 +0000605/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000606/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000607void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
608 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000609 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000610 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000611 const llvm::DenseSet<Selector> &ClsMap,
612 ObjCInterfaceDecl *IDecl) {
613 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
614
615 // If a method lookup fails locally we still need to look and see if
616 // the method was implemented by a base class or an inherited
617 // protocol. This lookup is slow, but occurs rarely in correct code
618 // and otherwise would terminate in a warning.
619
Chris Lattner855e51f2007-12-12 07:09:47 +0000620 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000621 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000622 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000623 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000624 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
625 !InsMap.count(method->getSelector()) &&
626 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000627 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000628 }
629 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000631 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000632 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000633 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
634 !ClsMap.count(method->getSelector()) &&
635 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000636 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000637 }
Chris Lattner0be08822008-07-21 21:32:27 +0000638 // Check on this protocols's referenced protocols, recursively.
639 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
640 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000641 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000642}
643
Ted Kremenek42730c52008-01-07 19:49:32 +0000644void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
645 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000646 llvm::DenseSet<Selector> InsMap;
647 // Check and see if instance methods in class interface have been
648 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000649 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000650 E = IMPDecl->instmeth_end(); I != E; ++I)
651 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000652
653 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000654 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000655 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000656 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000657 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000658
Chris Lattner855e51f2007-12-12 07:09:47 +0000659 llvm::DenseSet<Selector> ClsMap;
660 // Check and see if class methods in class interface have been
661 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000662 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000663 E = IMPDecl->classmeth_end(); I != E; ++I)
664 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000665
Ted Kremenek42730c52008-01-07 19:49:32 +0000666 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000667 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000668 if (!ClsMap.count((*I)->getSelector()))
669 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000670
671 // Check the protocol list for unimplemented methods in the @implementation
672 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000673 const ObjCList<ObjCProtocolDecl> &Protocols =
674 IDecl->getReferencedProtocols();
675 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
676 E = Protocols.end(); I != E; ++I)
677 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000678 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000679}
680
681/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000682/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000683void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
684 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000685 llvm::DenseSet<Selector> InsMap;
686 // Check and see if instance methods in category interface have been
687 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000688 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000689 E = CatImplDecl->instmeth_end(); I != E; ++I)
690 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000691
692 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000693 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000694 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000695 if (!InsMap.count((*I)->getSelector()))
696 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
697
Chris Lattner855e51f2007-12-12 07:09:47 +0000698 llvm::DenseSet<Selector> ClsMap;
699 // Check and see if class methods in category interface have been
700 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000701 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000702 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
703 I != E; ++I)
704 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000705
Ted Kremenek42730c52008-01-07 19:49:32 +0000706 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000707 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000708 if (!ClsMap.count((*I)->getSelector()))
709 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000710
711 // Check the protocol list for unimplemented methods in the @implementation
712 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000713 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
714 E = CatClassDecl->protocol_end(); PI != E; ++PI)
715 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000716 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000717}
718
719/// ActOnForwardClassDeclaration -
720Action::DeclTy *
721Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
722 IdentifierInfo **IdentList, unsigned NumElts)
723{
Ted Kremenek42730c52008-01-07 19:49:32 +0000724 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000725
726 for (unsigned i = 0; i != NumElts; ++i) {
727 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000728 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000729 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000730 // GCC apparently allows the following idiom:
731 //
732 // typedef NSObject < XCElementTogglerP > XCElementToggler;
733 // @class XCElementToggler;
734 //
735 // FIXME: Make an extension?
736 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
737 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
738 Diag(AtClassLoc, diag::err_redefinition_different_kind,
739 IdentList[i]->getName());
740 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
741 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000742 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000743 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000744 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000745 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000746 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000747 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000748
749 // Remember that this needs to be removed when the scope is popped.
750 TUScope->AddDecl(IDecl);
751 }
752
753 Interfaces.push_back(IDecl);
754 }
755
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000756 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
757 &Interfaces[0],
758 Interfaces.size());
759
760 CheckObjCDeclScope(CDecl);
761 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000762}
763
764
765/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
766/// returns true, or false, accordingly.
767/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000768bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000769 const ObjCMethodDecl *PrevMethod,
770 bool matchBasedOnSizeAndAlignment) {
771 QualType T1 = Context.getCanonicalType(Method->getResultType());
772 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
773
774 if (T1 != T2) {
775 // The result types are different.
776 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000777 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000778 // Incomplete types don't have a size and alignment.
779 if (T1->isIncompleteType() || T2->isIncompleteType())
780 return false;
781 // Check is based on size and alignment.
782 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
783 return false;
784 }
785 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
786 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
787 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
788 if (T1 != T2) {
789 // The result types are different.
790 if (!matchBasedOnSizeAndAlignment)
791 return false;
792 // Incomplete types don't have a size and alignment.
793 if (T1->isIncompleteType() || T2->isIncompleteType())
794 return false;
795 // Check is based on size and alignment.
796 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
797 return false;
798 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000799 }
800 return true;
801}
802
Ted Kremenek42730c52008-01-07 19:49:32 +0000803void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
804 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000805 if (!FirstMethod.Method) {
806 // Haven't seen a method with this selector name yet - add it.
807 FirstMethod.Method = Method;
808 FirstMethod.Next = 0;
809 } else {
810 // We've seen a method with this name, now check the type signature(s).
811 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
812
Ted Kremenek42730c52008-01-07 19:49:32 +0000813 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000814 Next = Next->Next)
815 match = MatchTwoMethodDeclarations(Method, Next->Method);
816
817 if (!match) {
818 // We have a new signature for an existing method - add it.
819 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000820 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000821 FirstMethod.Next = OMI;
822 }
823 }
824}
825
Steve Naroffec7e0882008-10-21 10:50:19 +0000826// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +0000827ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
828 SourceRange R) {
829 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +0000830 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +0000831
832 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +0000833 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
834 // This checks if the methods differ by size & alignment.
835 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
836 issueWarning = true;
837 }
838 if (issueWarning && (MethList.Method && MethList.Next)) {
Steve Naroff68354f32008-09-30 14:38:43 +0000839 Diag(R.getBegin(), diag::warn_multiple_method_decl, Sel.getName(), R);
840 Diag(MethList.Method->getLocStart(), diag::warn_using_decl,
841 MethList.Method->getSourceRange());
842 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
843 Diag(Next->Method->getLocStart(), diag::warn_also_found_decl,
844 Next->Method->getSourceRange());
845 }
846 return MethList.Method;
847}
848
Ted Kremenek42730c52008-01-07 19:49:32 +0000849void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
850 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000851 if (!FirstMethod.Method) {
852 // Haven't seen a method with this selector name yet - add it.
853 FirstMethod.Method = Method;
854 FirstMethod.Next = 0;
855 } else {
856 // We've seen a method with this name, now check the type signature(s).
857 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
858
Ted Kremenek42730c52008-01-07 19:49:32 +0000859 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000860 Next = Next->Next)
861 match = MatchTwoMethodDeclarations(Method, Next->Method);
862
863 if (!match) {
864 // We have a new signature for an existing method - add it.
865 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000866 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000867 FirstMethod.Next = OMI;
868 }
869 }
870}
871
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000872// Note: For class/category implemenations, allMethods/allProperties is
873// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000874void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
875 DeclTy **allMethods, unsigned allNum,
876 DeclTy **allProperties, unsigned pNum) {
877 Decl *ClassDecl = static_cast<Decl *>(classDecl);
878
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000879 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
880 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000881 // should be true.
882 if (!ClassDecl)
883 return;
884
Ted Kremenek42730c52008-01-07 19:49:32 +0000885 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
886 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000887
Ted Kremenek42730c52008-01-07 19:49:32 +0000888 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
889 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000890
891 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000892 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
893 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000894 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000895
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000896 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000897 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
898 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000899 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
900 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
901 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000902 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000903 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000904 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000905 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000906
907 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000908 ObjCMethodDecl *Method =
909 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000910
911 if (!Method) continue; // Already issued a diagnostic.
912 if (Method->isInstance()) {
913 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000914 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000915 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
916 : false;
917 if (isInterfaceDeclKind && PrevMethod && !match
918 || checkIdenticalMethods && match) {
919 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
920 Method->getSelector().getName());
921 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
922 } else {
923 insMethods.push_back(Method);
924 InsMap[Method->getSelector()] = Method;
925 /// The following allows us to typecheck messages to "id".
926 AddInstanceMethodToGlobalPool(Method);
927 }
928 }
929 else {
930 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000931 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000932 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
933 : false;
934 if (isInterfaceDeclKind && PrevMethod && !match
935 || checkIdenticalMethods && match) {
936 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
937 Method->getSelector().getName());
938 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
939 } else {
940 clsMethods.push_back(Method);
941 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000942 /// The following allows us to typecheck messages to "Class".
943 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000944 }
945 }
946 }
Steve Naroffe2e61e72008-09-29 14:20:56 +0000947 // Save the size so we can detect if we've added any property methods.
948 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
949 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner855e51f2007-12-12 07:09:47 +0000950
Ted Kremenek42730c52008-01-07 19:49:32 +0000951 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000952 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +0000953 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000954 ComparePropertiesInBaseAndSuper(I);
955 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000956 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
957 e = I->classprop_end(); i != e; ++i)
958 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000959 I->addMethods(&insMethods[0], insMethods.size(),
960 &clsMethods[0], clsMethods.size(), AtEndLoc);
961
Ted Kremenek42730c52008-01-07 19:49:32 +0000962 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000963 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
964 e = P->classprop_end(); i != e; ++i)
965 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner855e51f2007-12-12 07:09:47 +0000966 P->addMethods(&insMethods[0], insMethods.size(),
967 &clsMethods[0], clsMethods.size(), AtEndLoc);
968 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000969 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000970 // FIXME: Need to compare properties to those in interface?
971
972 // FIXME: If we merge properties into class we should probably
973 // merge them into category as well?
974 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
975 e = C->classprop_end(); i != e; ++i)
976 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner855e51f2007-12-12 07:09:47 +0000977 C->addMethods(&insMethods[0], insMethods.size(),
978 &clsMethods[0], clsMethods.size(), AtEndLoc);
979 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000980 else if (ObjCImplementationDecl *IC =
981 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000982 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000983 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000984 ImplMethodsVsClassMethods(IC, IDecl);
985 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000986 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000987 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000988 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000989 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000990 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +0000991 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000992 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000993 Categories; Categories = Categories->getNextClassCategory()) {
994 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
995 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
996 break;
997 }
998 }
999 }
1000 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001001 // Add any synthesized methods to the global pool. This allows us to
1002 // handle the following, which is supported by GCC (and part of the design).
1003 //
1004 // @interface Foo
1005 // @property double bar;
1006 // @end
1007 //
1008 // void thisIsUnfortunate() {
1009 // id foo;
1010 // double bar = [foo bar];
1011 // }
1012 //
1013 if (insMethodsSizePriorToPropAdds < insMethods.size())
1014 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1015 AddInstanceMethodToGlobalPool(insMethods[i]);
1016 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1017 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1018 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner855e51f2007-12-12 07:09:47 +00001019}
1020
1021
1022/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1023/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001024static Decl::ObjCDeclQualifier
1025CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1026 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1027 if (PQTVal & ObjCDeclSpec::DQ_In)
1028 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1029 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1030 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1031 if (PQTVal & ObjCDeclSpec::DQ_Out)
1032 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1033 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1034 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1035 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1036 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1037 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1038 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001039
1040 return ret;
1041}
1042
1043Sema::DeclTy *Sema::ActOnMethodDeclaration(
1044 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001045 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001046 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001047 Selector Sel,
1048 // optional arguments. The number of types/arguments is obtained
1049 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001050 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +00001051 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1052 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001053 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001054
1055 // Make sure we can establish a context for the method.
1056 if (!ClassDecl) {
1057 Diag(MethodLoc, diag::error_missing_method_context);
1058 return 0;
1059 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001060 QualType resultDeclType;
1061
1062 if (ReturnType)
1063 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1064 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001065 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001066
Chris Lattner114add62008-03-16 00:49:28 +00001067 ObjCMethodDecl* ObjCMethod =
1068 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +00001069 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +00001070 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001071 false,
Chris Lattner114add62008-03-16 00:49:28 +00001072 MethodDeclKind == tok::objc_optional ?
1073 ObjCMethodDecl::Optional :
1074 ObjCMethodDecl::Required);
1075
Chris Lattnereee57c02008-04-04 06:12:32 +00001076 llvm::SmallVector<ParmVarDecl*, 16> Params;
1077
1078 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1079 // FIXME: arg->AttrList must be stored too!
1080 QualType argType;
1081
1082 if (ArgTypes[i])
1083 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1084 else
1085 argType = Context.getObjCIdType();
1086 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1087 SourceLocation(/*FIXME*/),
1088 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001089 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001090 Param->setObjCDeclQualifier(
1091 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1092 Params.push_back(Param);
1093 }
1094
Ted Kremenek42730c52008-01-07 19:49:32 +00001095 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1096 ObjCMethod->setObjCDeclQualifier(
1097 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1098 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001099
1100 if (AttrList)
1101 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001102
1103 // For implementations (which can be very "coarse grain"), we add the
1104 // method now. This allows the AST to implement lookup methods that work
1105 // incrementally (without waiting until we parse the @end). It also allows
1106 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001107 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001108 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001109 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001110 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001111 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001112 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001113 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001114 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001115 }
1116 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001117 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001118 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001119 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001120 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001121 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001122 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001123 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001124 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001125 }
1126 }
1127 if (PrevMethod) {
1128 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001129 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1130 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001131 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1132 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001133 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001134}
1135
Daniel Dunbar540ff472008-09-23 21:53:23 +00001136void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1137 SourceLocation Loc,
1138 unsigned &Attributes) {
1139 // FIXME: Improve the reported location.
1140
1141 // readonly and readwrite conflict.
1142 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1143 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
1144 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1145 "readonly", "readwrite");
1146 Attributes ^= ObjCDeclSpec::DQ_PR_readonly;
1147 }
1148
1149 // Check for copy or retain on non-object types.
1150 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1151 !Context.isObjCObjectPointerType(PropertyTy)) {
1152 Diag(Loc, diag::err_objc_property_requires_object,
1153 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1154 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1155 }
1156
1157 // Check for more than one of { assign, copy, retain }.
1158 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1159 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1160 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1161 "assign", "copy");
1162 Attributes ^= ObjCDeclSpec::DQ_PR_copy;
1163 }
1164 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1165 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1166 "assign", "retain");
1167 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1168 }
1169 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1170 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1171 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1172 "copy", "retain");
1173 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1174 }
1175 }
1176
1177 // Warn if user supplied no assignment attribute, property is
1178 // readwrite, and this is an object type.
1179 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1180 ObjCDeclSpec::DQ_PR_retain)) &&
1181 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1182 Context.isObjCObjectPointerType(PropertyTy)) {
1183 // Skip this warning in gc-only mode.
1184 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1185 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1186
1187 // If non-gc code warn that this is likely inappropriate.
1188 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1189 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1190
1191 // FIXME: Implement warning dependent on NSCopying being
1192 // implemented. See also:
1193 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1194 // (please trim this list while you are at it).
1195 }
1196}
1197
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001198Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1199 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001200 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001201 Selector GetterSel,
1202 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001203 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001204 QualType T = GetTypeForDeclarator(FD.D, S);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001205 unsigned Attributes = ODS.getPropertyAttributes();
1206
1207 // May modify Attributes.
1208 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1209
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001210 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1211 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001212 // Regardless of setter/getter attribute, we save the default getter/setter
1213 // selector names in anticipation of declaration of setter/getter methods.
1214 PDecl->setGetterName(GetterSel);
1215 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001216
Daniel Dunbar540ff472008-09-23 21:53:23 +00001217 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001218 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001219
Daniel Dunbar540ff472008-09-23 21:53:23 +00001220 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001221 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001222
Daniel Dunbar540ff472008-09-23 21:53:23 +00001223 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001224 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001225
Daniel Dunbar540ff472008-09-23 21:53:23 +00001226 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001227 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001228
Daniel Dunbar540ff472008-09-23 21:53:23 +00001229 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001230 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001231
Daniel Dunbar540ff472008-09-23 21:53:23 +00001232 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001233 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001234
Daniel Dunbar540ff472008-09-23 21:53:23 +00001235 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001236 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001237
Daniel Dunbar540ff472008-09-23 21:53:23 +00001238 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001239 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001240
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001241 if (MethodImplKind == tok::objc_required)
1242 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1243 else if (MethodImplKind == tok::objc_optional)
1244 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1245
Chris Lattner855e51f2007-12-12 07:09:47 +00001246 return PDecl;
1247}
1248
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001249/// ActOnPropertyImplDecl - This routine performs semantic checks and
1250/// builds the AST node for a property implementation declaration; declared
1251/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001252///
1253Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1254 SourceLocation PropertyLoc,
1255 bool Synthesize,
1256 DeclTy *ClassCatImpDecl,
1257 IdentifierInfo *PropertyId,
1258 IdentifierInfo *PropertyIvar) {
1259 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1260 // Make sure we have a context for the property implementation declaration.
1261 if (!ClassImpDecl) {
1262 Diag(AtLoc, diag::error_missing_property_context);
1263 return 0;
1264 }
1265 ObjCPropertyDecl *property = 0;
1266 ObjCInterfaceDecl* IDecl = 0;
1267 // Find the class or category class where this property must have
1268 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001269 ObjCImplementationDecl *IC = 0;
1270 ObjCCategoryImplDecl* CatImplClass = 0;
1271 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001272 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001273 // We always synthesize an interface for an implementation
1274 // without an interface decl. So, IDecl is always non-zero.
1275 assert(IDecl &&
1276 "ActOnPropertyImplDecl - @implementation without @interface");
1277
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001278 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001279 property = IDecl->FindPropertyDeclaration(PropertyId);
1280 if (!property) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001281 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001282 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001283 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001284 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001285 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001286 if (Synthesize) {
1287 Diag(AtLoc, diag::error_synthesize_category_decl);
1288 return 0;
1289 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001290 IDecl = CatImplClass->getClassInterface();
1291 if (!IDecl) {
1292 Diag(AtLoc, diag::error_missing_property_interface);
1293 return 0;
1294 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001295 ObjCCategoryDecl *Category =
1296 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1297
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001298 // If category for this implementation not found, it is an error which
1299 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001300 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001301 return 0;
1302 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001303 property = Category->FindPropertyDeclaration(PropertyId);
1304 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001305 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001306 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001307 return 0;
1308 }
1309 }
1310 else {
1311 Diag(AtLoc, diag::error_bad_property_context);
1312 return 0;
1313 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001314 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001315 // Check that we have a valid, previously declared ivar for @synthesize
1316 if (Synthesize) {
1317 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001318 if (!PropertyIvar)
1319 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001320 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001321 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001322 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001323 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1324 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001325 return 0;
1326 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001327 QualType PropType = Context.getCanonicalType(property->getType());
1328 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1329
Steve Naroff631e3922008-09-30 00:24:17 +00001330 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001331 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001332 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Steve Naroffc32e45c2008-09-30 10:07:56 +00001333 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1334 Ivar->getName());
1335 return 0;
1336 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001337 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001338 } else if (PropertyIvar) {
1339 // @dynamic
1340 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1341 return 0;
1342 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001343 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001344 ObjCPropertyImplDecl *PIDecl =
1345 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1346 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001347 ObjCPropertyImplDecl::Synthesize
1348 : ObjCPropertyImplDecl::Dynamic),
1349 Ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001350 if (IC)
1351 IC->addPropertyImplementation(PIDecl);
1352 else
1353 CatImplClass->addPropertyImplementation(PIDecl);
1354
1355 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001356}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001357
1358bool Sema::CheckObjCDeclScope(Decl *D)
1359{
1360 if (isa<TranslationUnitDecl>(CurContext))
1361 return false;
1362
1363 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1364 D->setInvalidDecl();
1365
1366 return true;
1367}