blob: d98c3065f52a3abfa9375a39537d1c55fd5a17e5 [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?
78 if (!IDecl->isForwardDecl())
79 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
80 else {
81 IDecl->setLocation(AtInterfaceLoc);
82 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000083 }
Chris Lattner5cece462008-07-21 07:06:49 +000084 } else {
Daniel Dunbard8bd6822008-08-20 18:02:42 +000085 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000086 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000087 if (AttrList)
88 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000089
Steve Naroff15208162008-04-02 18:30:49 +000090 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +000091 // Remember that this needs to be removed when the scope is popped.
92 TUScope->AddDecl(IDecl);
93 }
94
95 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +000097 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +000098 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000099 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000100 Diag(SuperLoc, diag::err_redefinition_different_kind,
101 SuperName->getName());
102 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
103 }
104 else {
105 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000106 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000107
108 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000109 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 SuperClassEntry ? SuperClassEntry->getName()
111 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000112 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 }
114 }
115 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000116 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000117 IDecl->setLocEnd(SuperLoc);
118 } else { // we have a root class.
119 IDecl->setLocEnd(ClassLoc);
120 }
121
122 /// Check then save referenced protocols
Chris Lattnerae1ae492008-07-26 04:13:19 +0000123 if (NumProtoRefs) {
124 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000125 IDecl->setLocEnd(EndProtoLoc);
126 }
127 return IDecl;
128}
129
130/// ActOnCompatiblityAlias - this action is called after complete parsing of
131/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000132Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
133 IdentifierInfo *AliasName,
134 SourceLocation AliasLocation,
135 IdentifierInfo *ClassName,
136 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000137 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000138 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000140 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000141 Diag(AliasLocation, diag::warn_previous_alias_decl);
142 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
143 }
144 else {
145 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
146 AliasName->getName());
147 Diag(ADecl->getLocation(), diag::err_previous_declaration);
148 }
149 return 0;
150 }
151 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000152 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000153 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
154 if (CDecl == 0) {
155 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
156 if (CDeclU)
157 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000158 return 0;
159 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000160
161 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000162 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000163 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
164
165 ObjCAliasDecls[AliasName] = AliasDecl;
166 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000167 return AliasDecl;
168}
169
Chris Lattner2bdedd62008-07-26 04:03:38 +0000170Sema::DeclTy *
171Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
172 IdentifierInfo *ProtocolName,
173 SourceLocation ProtocolLoc,
174 DeclTy * const *ProtoRefs,
175 unsigned NumProtoRefs,
176 SourceLocation EndProtoLoc) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000177 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000178 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000179 if (PDecl) {
180 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000181 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000182 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
183 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000184 // Just return the protocol we already had.
185 // FIXME: don't leak the objects passed in!
186 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000187 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000188 // Make sure the cached decl gets a valid start location.
189 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000190 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000191 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000192 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000193 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000195 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000196
197 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000198 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000199 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 PDecl->setLocEnd(EndProtoLoc);
201 }
202 return PDecl;
203}
204
205/// FindProtocolDeclaration - This routine looks up protocols and
206/// issuer error if they are not declared. It returns list of protocol
207/// declarations in its 'Protocols' argument.
208void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000209Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000210 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000211 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000212 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000213 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000214 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
215 if (!PDecl) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000216 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
217 ProtocolId[i].first->getName());
Chris Lattner17d50a92008-07-26 03:47:43 +0000218 continue;
219 }
220
221 // If this is a forward declaration and we are supposed to warn in this
222 // case, do it.
223 if (WarnOnDeclarations && PDecl->isForwardDecl())
224 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
225 ProtocolId[i].first->getName());
226 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000227 }
228}
229
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000230/// DiagnosePropertyMismatch - Compares two properties for their
231/// attributes and types and warns on a variety of inconsistancies.
232///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000233void
234Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
235 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000236 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000237 ObjCPropertyDecl::PropertyAttributeKind CAttr =
238 Property->getPropertyAttributes();
239 ObjCPropertyDecl::PropertyAttributeKind SAttr =
240 SuperProperty->getPropertyAttributes();
241 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
242 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000243 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000244 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000245 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
246 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000247 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000248 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000249 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000250 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
251 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000252 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000253 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000254 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000255
256 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
257 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000258 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000259 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000260 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000261 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000262 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000263 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000264 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000265 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000266 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000267 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000268 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000269
Chris Lattnerc5cff302008-07-26 20:50:02 +0000270 if (Context.getCanonicalType(Property->getType()) !=
271 Context.getCanonicalType(SuperProperty->getType()))
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000272 Diag(Property->getLocation(), diag::warn_property_type,
273 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000274 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000275
276}
277
278/// ComparePropertiesInBaseAndSuper - This routine compares property
279/// declarations in base and its super class, if any, and issues
280/// diagnostics in a variety of inconsistant situations.
281///
282void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000283Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000284 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
285 if (!SDecl)
286 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000287 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
288 E = SDecl->classprop_end(); S != E; ++S) {
289 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000290 // Does property in super class has declaration in current class?
291 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
292 E = IDecl->classprop_end(); I != E; ++I) {
293 ObjCPropertyDecl *PDecl = (*I);
294 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000295 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000296 }
297 }
298}
299
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000300/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
301/// of properties declared in a protocol and adds them to the list
302/// of properties for current class if it is not there already.
303void
304Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
305 ObjCProtocolDecl *PDecl)
306{
307 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
308 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
309 E = PDecl->classprop_end(); P != E; ++P) {
310 ObjCPropertyDecl *Pr = (*P);
311 ObjCInterfaceDecl::classprop_iterator CP, CE;
312 // Is this property already in class's list of properties?
313 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
314 CP != CE; ++CP)
315 if ((*CP)->getIdentifier() == Pr->getIdentifier())
316 break;
317 if (CP == CE)
318 // Add this property to list of properties for thie class.
319 mergeProperties.push_back(Pr);
320 else
321 // Property protocol already exist in class. Diagnose any mismatch.
322 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
323 }
324 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
325}
326
327/// MergeProtocolPropertiesIntoClass - This routine merges properties
328/// declared in 'MergeItsProtocols' objects (which can be a class or an
329/// inherited protocol into the list of properties for class 'IDecl'
330///
331
332void
333Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
334 DeclTy *MergeItsProtocols) {
335 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000336 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000337 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
338 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000339 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000340 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
341
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000342 // Go thru the list of protocols for this class and recursively merge
343 // their properties into this class as well.
344 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
345 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000346 MergeProtocolPropertiesIntoClass(IDecl, *P);
347 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000348 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
349 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
350 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000351 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000352 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000353}
354
Chris Lattner855e51f2007-12-12 07:09:47 +0000355/// ActOnForwardProtocolDeclaration -
356Action::DeclTy *
357Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000358 const IdentifierLocPair *IdentList,
359 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000360 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000361
362 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000363 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000364 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000365 if (PDecl == 0) // Not already seen?
366 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000367
368 Protocols.push_back(PDecl);
369 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000370 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
371 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000372}
373
Chris Lattnere705e5e2008-07-21 22:17:28 +0000374Sema::DeclTy *Sema::
375ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
376 IdentifierInfo *ClassName, SourceLocation ClassLoc,
377 IdentifierInfo *CategoryName,
378 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000379 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000380 unsigned NumProtoRefs,
381 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000382 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000383
Chris Lattnere29dc832008-03-16 20:34:23 +0000384 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000385 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000386 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000387
388 /// Check that class of this category is already completely declared.
389 if (!IDecl || IDecl->isForwardDecl())
390 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000391 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000392 /// Check for duplicate interface declaration for this category
393 ObjCCategoryDecl *CDeclChain;
394 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
395 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000396 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000397 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000398 CategoryName->getName());
399 break;
400 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000401 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000402 if (!CDeclChain)
403 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000404 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000405
406 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000407 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
408 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000409 }
410 return CDecl;
411}
412
413/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000414/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000415/// object.
416Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
417 SourceLocation AtCatImplLoc,
418 IdentifierInfo *ClassName, SourceLocation ClassLoc,
419 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000420 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000421 ObjCCategoryImplDecl *CDecl =
422 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000423 /// Check that class of this category is already completely declared.
424 if (!IDecl || IDecl->isForwardDecl())
425 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
426
427 /// TODO: Check that CatName, category name, is not used in another
428 // implementation.
429 return CDecl;
430}
431
432Sema::DeclTy *Sema::ActOnStartClassImplementation(
433 SourceLocation AtClassImplLoc,
434 IdentifierInfo *ClassName, SourceLocation ClassLoc,
435 IdentifierInfo *SuperClassname,
436 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000437 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000438 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000439 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000440 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000441 Diag(ClassLoc, diag::err_redefinition_different_kind,
442 ClassName->getName());
443 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
444 }
445 else {
446 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000447 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000448 if (!IDecl)
449 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
450 }
451
452 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000454 if (SuperClassname) {
455 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000456 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000457 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000458 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
459 SuperClassname->getName());
460 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
461 }
462 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000463 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000464 if (!SDecl)
465 Diag(SuperClassLoc, diag::err_undef_superclass,
466 SuperClassname->getName(), ClassName->getName());
467 else if (IDecl && IDecl->getSuperClass() != SDecl) {
468 // This implementation and its interface do not have the same
469 // super class.
470 Diag(SuperClassLoc, diag::err_conflicting_super_class,
471 SDecl->getName());
472 Diag(SDecl->getLocation(), diag::err_previous_definition);
473 }
474 }
475 }
476
477 if (!IDecl) {
478 // Legacy case of @implementation with no corresponding @interface.
479 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000480
481 // FIXME: Do we support attributes on the @implementation? If so
482 // we should copy them over.
Chris Lattner5cece462008-07-21 07:06:49 +0000483 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000484 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000485 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000486 IDecl->setSuperClass(SDecl);
487 IDecl->setLocEnd(ClassLoc);
488
489 // Remember that this needs to be removed when the scope is popped.
490 TUScope->AddDecl(IDecl);
491 }
492
Ted Kremenek42730c52008-01-07 19:49:32 +0000493 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000494 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
495 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000496
497 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000498 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000499 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000500 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
501 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000502 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 return IMPDecl;
504}
505
Ted Kremenek42730c52008-01-07 19:49:32 +0000506void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
507 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000508 SourceLocation RBrace) {
509 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000510 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000511 if (!IDecl)
512 return;
513 /// Check case of non-existing @interface decl.
514 /// (legacy objective-c @implementation decl without an @interface decl).
515 /// Add implementations's ivar to the synthesize class's ivar list.
516 if (IDecl->ImplicitInterfaceDecl()) {
517 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
518 return;
519 }
520 // If implementation has empty ivar list, just return.
521 if (numIvars == 0)
522 return;
523
524 assert(ivars && "missing @implementation ivars");
525
526 // Check interface's Ivar list against those in the implementation.
527 // names and types must match.
528 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000529 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000530 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000531 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
532 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000533 ObjCIvarDecl* ImplIvar = ivars[j++];
534 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000535 assert (ImplIvar && "missing implementation ivar");
536 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000537 if (Context.getCanonicalType(ImplIvar->getType()) !=
538 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000539 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
540 ImplIvar->getIdentifier()->getName());
541 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
542 ClsIvar->getIdentifier()->getName());
543 }
544 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
545 // as error.
546 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
547 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
548 ImplIvar->getIdentifier()->getName());
549 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
550 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000551 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000552 }
553 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000554 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000555
556 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000557 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000558 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000559 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000560}
561
Steve Naroffb4f48512008-02-10 21:38:56 +0000562void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
563 bool &IncompleteImpl) {
564 if (!IncompleteImpl) {
565 Diag(ImpLoc, diag::warn_incomplete_impl);
566 IncompleteImpl = true;
567 }
568 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
569}
570
Steve Naroffb268d2a2008-02-08 22:06:17 +0000571/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000572/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000573void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
574 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000575 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000576 const llvm::DenseSet<Selector> &InsMap,
577 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000578 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000579 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000580 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000581 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000582 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000583 method->getImplementationControl() != ObjCMethodDecl::Optional)
584 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 }
586 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000587 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000588 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000590 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000591 method->getImplementationControl() != ObjCMethodDecl::Optional)
592 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000593 }
Chris Lattner0be08822008-07-21 21:32:27 +0000594 // Check on this protocols's referenced protocols, recursively.
595 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
596 E = PDecl->protocol_end(); PI != E; ++PI)
597 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000598}
599
Ted Kremenek42730c52008-01-07 19:49:32 +0000600void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
601 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000602 llvm::DenseSet<Selector> InsMap;
603 // Check and see if instance methods in class interface have been
604 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000605 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000606 E = IMPDecl->instmeth_end(); I != E; ++I)
607 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000608
609 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000610 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000611 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000612 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000613 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000614
Chris Lattner855e51f2007-12-12 07:09:47 +0000615 llvm::DenseSet<Selector> ClsMap;
616 // Check and see if class methods in class interface have been
617 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000618 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000619 E = IMPDecl->classmeth_end(); I != E; ++I)
620 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000621
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000623 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000624 if (!ClsMap.count((*I)->getSelector()))
625 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000626
627 // Check the protocol list for unimplemented methods in the @implementation
628 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000629 const ObjCList<ObjCProtocolDecl> &Protocols =
630 IDecl->getReferencedProtocols();
631 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
632 E = Protocols.end(); I != E; ++I)
633 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000634 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000635}
636
637/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
638/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000639void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
640 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000641 llvm::DenseSet<Selector> InsMap;
642 // Check and see if instance methods in category interface have been
643 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000644 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000645 E = CatImplDecl->instmeth_end(); I != E; ++I)
646 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000647
648 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000649 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000650 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000651 if (!InsMap.count((*I)->getSelector()))
652 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
653
Chris Lattner855e51f2007-12-12 07:09:47 +0000654 llvm::DenseSet<Selector> ClsMap;
655 // Check and see if class methods in category interface have been
656 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000657 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000658 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
659 I != E; ++I)
660 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000661
Ted Kremenek42730c52008-01-07 19:49:32 +0000662 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000663 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000664 if (!ClsMap.count((*I)->getSelector()))
665 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000666
667 // Check the protocol list for unimplemented methods in the @implementation
668 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000669 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
670 E = CatClassDecl->protocol_end(); PI != E; ++PI)
671 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000672 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000673}
674
675/// ActOnForwardClassDeclaration -
676Action::DeclTy *
677Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
678 IdentifierInfo **IdentList, unsigned NumElts)
679{
Ted Kremenek42730c52008-01-07 19:49:32 +0000680 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000681
682 for (unsigned i = 0; i != NumElts; ++i) {
683 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000684 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000686 // GCC apparently allows the following idiom:
687 //
688 // typedef NSObject < XCElementTogglerP > XCElementToggler;
689 // @class XCElementToggler;
690 //
691 // FIXME: Make an extension?
692 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
693 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
694 Diag(AtClassLoc, diag::err_redefinition_different_kind,
695 IdentList[i]->getName());
696 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
697 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000698 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000699 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000700 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000701 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000702 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000703 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000704
705 // Remember that this needs to be removed when the scope is popped.
706 TUScope->AddDecl(IDecl);
707 }
708
709 Interfaces.push_back(IDecl);
710 }
711
Chris Lattnere29dc832008-03-16 20:34:23 +0000712 return ObjCClassDecl::Create(Context, AtClassLoc,
713 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000714}
715
716
717/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
718/// returns true, or false, accordingly.
719/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000720bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
721 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000722 if (Context.getCanonicalType(Method->getResultType()) !=
723 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000724 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000725 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000726 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
727 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000728 if (Context.getCanonicalType(ParamDecl->getType()) !=
729 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000730 return false;
731 }
732 return true;
733}
734
Ted Kremenek42730c52008-01-07 19:49:32 +0000735void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
736 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000737 if (!FirstMethod.Method) {
738 // Haven't seen a method with this selector name yet - add it.
739 FirstMethod.Method = Method;
740 FirstMethod.Next = 0;
741 } else {
742 // We've seen a method with this name, now check the type signature(s).
743 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
744
Ted Kremenek42730c52008-01-07 19:49:32 +0000745 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000746 Next = Next->Next)
747 match = MatchTwoMethodDeclarations(Method, Next->Method);
748
749 if (!match) {
750 // We have a new signature for an existing method - add it.
751 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000752 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000753 FirstMethod.Next = OMI;
754 }
755 }
756}
757
Ted Kremenek42730c52008-01-07 19:49:32 +0000758void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
759 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000760 if (!FirstMethod.Method) {
761 // Haven't seen a method with this selector name yet - add it.
762 FirstMethod.Method = Method;
763 FirstMethod.Next = 0;
764 } else {
765 // We've seen a method with this name, now check the type signature(s).
766 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
767
Ted Kremenek42730c52008-01-07 19:49:32 +0000768 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000769 Next = Next->Next)
770 match = MatchTwoMethodDeclarations(Method, Next->Method);
771
772 if (!match) {
773 // We have a new signature for an existing method - add it.
774 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000775 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000776 FirstMethod.Next = OMI;
777 }
778 }
779}
780
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000781// Note: For class/category implemenations, allMethods/allProperties is
782// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000783void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
784 DeclTy **allMethods, unsigned allNum,
785 DeclTy **allProperties, unsigned pNum) {
786 Decl *ClassDecl = static_cast<Decl *>(classDecl);
787
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000788 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
789 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000790 // should be true.
791 if (!ClassDecl)
792 return;
793
Ted Kremenek42730c52008-01-07 19:49:32 +0000794 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
795 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000796
Ted Kremenek42730c52008-01-07 19:49:32 +0000797 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
798 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000799
800 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000801 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
802 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000803 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000804
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000805 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000806 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
807 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000808 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
809 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
810 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
811 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000812 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000813 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000814 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000815
816 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000817 ObjCMethodDecl *Method =
818 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000819
820 if (!Method) continue; // Already issued a diagnostic.
821 if (Method->isInstance()) {
822 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000823 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000824 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
825 : false;
826 if (isInterfaceDeclKind && PrevMethod && !match
827 || checkIdenticalMethods && match) {
828 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
829 Method->getSelector().getName());
830 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
831 } else {
832 insMethods.push_back(Method);
833 InsMap[Method->getSelector()] = Method;
834 /// The following allows us to typecheck messages to "id".
835 AddInstanceMethodToGlobalPool(Method);
836 }
837 }
838 else {
839 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000840 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000841 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
842 : false;
843 if (isInterfaceDeclKind && PrevMethod && !match
844 || checkIdenticalMethods && match) {
845 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
846 Method->getSelector().getName());
847 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
848 } else {
849 clsMethods.push_back(Method);
850 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000851 /// The following allows us to typecheck messages to "Class".
852 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000853 }
854 }
855 }
856
Ted Kremenek42730c52008-01-07 19:49:32 +0000857 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000858 // Compares properties declaraed in this class to those of its
859 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000860 ComparePropertiesInBaseAndSuper(I);
861 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000862 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
863 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000864 // FIXME: It would be really nice if we could avoid this. Injecting
865 // methods into the interface makes it hard to distinguish "real" methods
866 // from synthesized "property" methods (that aren't in the source).
867 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000868 I->addPropertyMethods(Context, *P, insMethods);
869 }
870 I->addMethods(&insMethods[0], insMethods.size(),
871 &clsMethods[0], clsMethods.size(), AtEndLoc);
872
Ted Kremenek42730c52008-01-07 19:49:32 +0000873 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000874 P->addMethods(&insMethods[0], insMethods.size(),
875 &clsMethods[0], clsMethods.size(), AtEndLoc);
876 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000877 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000878 C->addMethods(&insMethods[0], insMethods.size(),
879 &clsMethods[0], clsMethods.size(), AtEndLoc);
880 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 else if (ObjCImplementationDecl *IC =
882 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000883 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000884 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000885 ImplMethodsVsClassMethods(IC, IDecl);
886 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000887 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000888 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000890 // Find category interface decl and then check that all methods declared
891 // in this interface is implemented in the category @implementation.
892 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000893 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000894 Categories; Categories = Categories->getNextClassCategory()) {
895 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
896 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
897 break;
898 }
899 }
900 }
901 }
902}
903
904
905/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
906/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000907static Decl::ObjCDeclQualifier
908CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
909 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
910 if (PQTVal & ObjCDeclSpec::DQ_In)
911 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
912 if (PQTVal & ObjCDeclSpec::DQ_Inout)
913 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
914 if (PQTVal & ObjCDeclSpec::DQ_Out)
915 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
916 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
917 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
918 if (PQTVal & ObjCDeclSpec::DQ_Byref)
919 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
920 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
921 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000922
923 return ret;
924}
925
926Sema::DeclTy *Sema::ActOnMethodDeclaration(
927 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000928 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000929 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000930 Selector Sel,
931 // optional arguments. The number of types/arguments is obtained
932 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000933 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000934 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
935 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000936 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000937
938 // Make sure we can establish a context for the method.
939 if (!ClassDecl) {
940 Diag(MethodLoc, diag::error_missing_method_context);
941 return 0;
942 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000943 QualType resultDeclType;
944
945 if (ReturnType)
946 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
947 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000948 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000949
Chris Lattner114add62008-03-16 00:49:28 +0000950 ObjCMethodDecl* ObjCMethod =
951 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000952 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +0000953 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000954 false,
Chris Lattner114add62008-03-16 00:49:28 +0000955 MethodDeclKind == tok::objc_optional ?
956 ObjCMethodDecl::Optional :
957 ObjCMethodDecl::Required);
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000958 if (AttrList)
959 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner114add62008-03-16 00:49:28 +0000960
Chris Lattnereee57c02008-04-04 06:12:32 +0000961 llvm::SmallVector<ParmVarDecl*, 16> Params;
962
963 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
964 // FIXME: arg->AttrList must be stored too!
965 QualType argType;
966
967 if (ArgTypes[i])
968 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
969 else
970 argType = Context.getObjCIdType();
971 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
972 SourceLocation(/*FIXME*/),
973 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000974 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000975 Param->setObjCDeclQualifier(
976 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
977 Params.push_back(Param);
978 }
979
Ted Kremenek42730c52008-01-07 19:49:32 +0000980 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
981 ObjCMethod->setObjCDeclQualifier(
982 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
983 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000984
985 // For implementations (which can be very "coarse grain"), we add the
986 // method now. This allows the AST to implement lookup methods that work
987 // incrementally (without waiting until we parse the @end). It also allows
988 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000989 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000990 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000991 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000992 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000993 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000994 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000995 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000996 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000997 }
998 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000999 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001000 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001001 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001002 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001003 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001004 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001005 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001006 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001007 }
1008 }
1009 if (PrevMethod) {
1010 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001011 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1012 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001013 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1014 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001015 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001016}
1017
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001018Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1019 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001020 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001021 Selector GetterSel,
1022 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001023 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001024 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001025 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1026 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001027 // Regardless of setter/getter attribute, we save the default getter/setter
1028 // selector names in anticipation of declaration of setter/getter methods.
1029 PDecl->setGetterName(GetterSel);
1030 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001031
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001032 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001033 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001034
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001035 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001036 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001037
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001038 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001039 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001040
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001041 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001043
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001044 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001045 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001046
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001047 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001048 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001049
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001050 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001052
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001053 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001054 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001055
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001056 if (MethodImplKind == tok::objc_required)
1057 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1058 else if (MethodImplKind == tok::objc_optional)
1059 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1060
Chris Lattner855e51f2007-12-12 07:09:47 +00001061 return PDecl;
1062}
1063
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001064/// ActOnPropertyImplDecl - This routine performs semantic checks and
1065/// builds the AST node for a property implementation declaration; declared
1066/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001067///
1068Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1069 SourceLocation PropertyLoc,
1070 bool Synthesize,
1071 DeclTy *ClassCatImpDecl,
1072 IdentifierInfo *PropertyId,
1073 IdentifierInfo *PropertyIvar) {
1074 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1075 // Make sure we have a context for the property implementation declaration.
1076 if (!ClassImpDecl) {
1077 Diag(AtLoc, diag::error_missing_property_context);
1078 return 0;
1079 }
1080 ObjCPropertyDecl *property = 0;
1081 ObjCInterfaceDecl* IDecl = 0;
1082 // Find the class or category class where this property must have
1083 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001084 ObjCImplementationDecl *IC = 0;
1085 ObjCCategoryImplDecl* CatImplClass = 0;
1086 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001087 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001088 // We always synthesize an interface for an implementation
1089 // without an interface decl. So, IDecl is always non-zero.
1090 assert(IDecl &&
1091 "ActOnPropertyImplDecl - @implementation without @interface");
1092
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001093 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001094 property = IDecl->FindPropertyDeclaration(PropertyId);
1095 if (!property) {
1096 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001097 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001098 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001099 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001100 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001101 if (Synthesize) {
1102 Diag(AtLoc, diag::error_synthesize_category_decl);
1103 return 0;
1104 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001105 IDecl = CatImplClass->getClassInterface();
1106 if (!IDecl) {
1107 Diag(AtLoc, diag::error_missing_property_interface);
1108 return 0;
1109 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001110 ObjCCategoryDecl *Category =
1111 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1112
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001113 // If category for this implementation not found, it is an error which
1114 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001115 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001116 return 0;
1117 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001118 property = Category->FindPropertyDeclaration(PropertyId);
1119 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001120 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001121 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001122 return 0;
1123 }
1124 }
1125 else {
1126 Diag(AtLoc, diag::error_bad_property_context);
1127 return 0;
1128 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001129 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001130 // Check that we have a valid, previously declared ivar for @synthesize
1131 if (Synthesize) {
1132 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001133 if (!PropertyIvar)
1134 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001135 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001136 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001137 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001138 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1139 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001140 return 0;
1141 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001142 // Check that type of property and its ivar match.
Chris Lattnerc5cff302008-07-26 20:50:02 +00001143 if (Context.getCanonicalType(Ivar->getType()) !=
1144 Context.getCanonicalType(property->getType())) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001145 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1146 Ivar->getName());
1147 return 0;
1148 }
1149
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001150 } else if (PropertyIvar) {
1151 // @dynamic
1152 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1153 return 0;
1154 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001155 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001156 ObjCPropertyImplDecl *PIDecl =
1157 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1158 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001159 ObjCPropertyImplDecl::Synthesize
1160 : ObjCPropertyImplDecl::Dynamic),
1161 Ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001162 if (IC)
1163 IC->addPropertyImplementation(PIDecl);
1164 else
1165 CatImplClass->addPropertyImplementation(PIDecl);
1166
1167 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001168}