blob: d3e2ed3b1d6a22be512cf7b9c3d6cd4a0a3391e0 [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"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremenek42730c52008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff3ac43f92008-07-25 17:57:26 +000025 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
26
27 // If we don't have a valid method decl, simply return.
28 if (!MDecl)
29 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000030
31 // Allow the rest of sema to find private method decl implementations.
32 if (MDecl->isInstance())
33 AddInstanceMethodToGlobalPool(MDecl);
34 else
35 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerf3874bc2008-04-06 04:47:34 +000038 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000039
40 // Create Decl objects for each parameter, entrring them in the scope for
41 // binding to their use.
42 struct DeclaratorChunk::ParamInfo PI;
43
44 // Insert the invisible arguments, self and _cmd!
45 PI.Ident = &Context.Idents.get("self");
46 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Steve Naroff1a5027c2008-06-05 14:49:39 +000047 QualType selfTy;
Chris Lattner855e51f2007-12-12 07:09:47 +000048 if (MDecl->isInstance()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +000049 selfTy = Context.getObjCIdType();
Gabor Greif9b2e0f92008-02-29 20:35:55 +000050 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
51 // There may be no interface context due to error in declaration of the
52 // interface (which has been reported). Recover gracefully
Chris Lattner3e254fb2008-04-08 04:40:51 +000053 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000054 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000055 }
Steve Naroff1a5027c2008-06-05 14:49:39 +000056 } else // we have a factory method.
57 selfTy = Context.getObjCClassType();
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000058 getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000059 PI.Ident, PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000060
61 PI.Ident = &Context.Idents.get("_cmd");
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000062 getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000063 PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
Chris Lattner3e254fb2008-04-08 04:40:51 +000064
Chris Lattner97316c02008-04-10 02:22:51 +000065 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000066 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000067 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000068 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000069 if (II)
70 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000071 }
72}
73
Chris Lattnere705e5e2008-07-21 22:17:28 +000074Sema::DeclTy *Sema::
75ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
76 IdentifierInfo *ClassName, SourceLocation ClassLoc,
77 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000078 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000079 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000080 assert(ClassName && "Missing class identifier");
81
82 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000083 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000084 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000085 Diag(ClassLoc, diag::err_redefinition_different_kind,
86 ClassName->getName());
87 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
88 }
89
Ted Kremenek42730c52008-01-07 19:49:32 +000090 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000091 if (IDecl) {
92 // Class already seen. Is it a forward declaration?
93 if (!IDecl->isForwardDecl())
94 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
95 else {
96 IDecl->setLocation(AtInterfaceLoc);
97 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000098 }
Chris Lattner5cece462008-07-21 07:06:49 +000099 } else {
100 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +0000101 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102
Steve Naroff15208162008-04-02 18:30:49 +0000103 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 Diag(SuperLoc, diag::err_redefinition_different_kind,
114 SuperName->getName());
115 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
116 }
117 else {
118 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000119 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000120
121 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000122 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000123 SuperClassEntry ? SuperClassEntry->getName()
124 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000125 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000126 }
127 }
128 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000129 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000130 IDecl->setLocEnd(SuperLoc);
131 } else { // we have a root class.
132 IDecl->setLocEnd(ClassLoc);
133 }
134
135 /// Check then save referenced protocols
Chris Lattnerae1ae492008-07-26 04:13:19 +0000136 if (NumProtoRefs) {
137 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000138 IDecl->setLocEnd(EndProtoLoc);
139 }
140 return IDecl;
141}
142
143/// ActOnCompatiblityAlias - this action is called after complete parsing of
144/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000145Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
146 IdentifierInfo *AliasName,
147 SourceLocation AliasLocation,
148 IdentifierInfo *ClassName,
149 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000150 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000151 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000152 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000153 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000154 Diag(AliasLocation, diag::warn_previous_alias_decl);
155 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
156 }
157 else {
158 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
159 AliasName->getName());
160 Diag(ADecl->getLocation(), diag::err_previous_declaration);
161 }
162 return 0;
163 }
164 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000165 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000166 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
167 if (CDecl == 0) {
168 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
169 if (CDeclU)
170 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000171 return 0;
172 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000173
174 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000175 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000176 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
177
178 ObjCAliasDecls[AliasName] = AliasDecl;
179 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000180 return AliasDecl;
181}
182
Chris Lattner2bdedd62008-07-26 04:03:38 +0000183Sema::DeclTy *
184Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
185 IdentifierInfo *ProtocolName,
186 SourceLocation ProtocolLoc,
187 DeclTy * const *ProtoRefs,
188 unsigned NumProtoRefs,
189 SourceLocation EndProtoLoc) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000190 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000191 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000192 if (PDecl) {
193 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000194 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000195 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
196 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000197 // Just return the protocol we already had.
198 // FIXME: don't leak the objects passed in!
199 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000201
202 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000203 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000204 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000205 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000206 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000207 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000208
209 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000210 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000211 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000212 PDecl->setLocEnd(EndProtoLoc);
213 }
214 return PDecl;
215}
216
217/// FindProtocolDeclaration - This routine looks up protocols and
218/// issuer error if they are not declared. It returns list of protocol
219/// declarations in its 'Protocols' argument.
220void
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
243/// attributes and types and warns on a variety of inconsistancies.
244///
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;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000299 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
300 E = SDecl->classprop_end(); S != E; ++S) {
301 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000302 // Does property in super class has declaration in current class?
303 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
304 E = IDecl->classprop_end(); I != E; ++I) {
305 ObjCPropertyDecl *PDecl = (*I);
306 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000307 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000308 }
309 }
310}
311
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000312/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
313/// of properties declared in a protocol and adds them to the list
314/// of properties for current class if it is not there already.
315void
316Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
317 ObjCProtocolDecl *PDecl)
318{
319 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
320 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
321 E = PDecl->classprop_end(); P != E; ++P) {
322 ObjCPropertyDecl *Pr = (*P);
323 ObjCInterfaceDecl::classprop_iterator CP, CE;
324 // Is this property already in class's list of properties?
325 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
326 CP != CE; ++CP)
327 if ((*CP)->getIdentifier() == Pr->getIdentifier())
328 break;
329 if (CP == CE)
330 // Add this property to list of properties for thie class.
331 mergeProperties.push_back(Pr);
332 else
333 // Property protocol already exist in class. Diagnose any mismatch.
334 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
335 }
336 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
337}
338
339/// MergeProtocolPropertiesIntoClass - This routine merges properties
340/// declared in 'MergeItsProtocols' objects (which can be a class or an
341/// inherited protocol into the list of properties for class 'IDecl'
342///
343
344void
345Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
346 DeclTy *MergeItsProtocols) {
347 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000348 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000349 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
350 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000351 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000352 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
353
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000354 // Go thru the list of protocols for this class and recursively merge
355 // their properties into this class as well.
356 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
357 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000358 MergeProtocolPropertiesIntoClass(IDecl, *P);
359 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000360 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
361 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
362 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000363 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000364 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000365}
366
Chris Lattner855e51f2007-12-12 07:09:47 +0000367/// ActOnForwardProtocolDeclaration -
368Action::DeclTy *
369Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000370 const IdentifierLocPair *IdentList,
371 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000372 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000373
374 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000375 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000376 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000377 if (PDecl == 0) // Not already seen?
378 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000379
380 Protocols.push_back(PDecl);
381 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000382 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
383 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000384}
385
Chris Lattnere705e5e2008-07-21 22:17:28 +0000386Sema::DeclTy *Sema::
387ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
388 IdentifierInfo *ClassName, SourceLocation ClassLoc,
389 IdentifierInfo *CategoryName,
390 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000391 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000392 unsigned NumProtoRefs,
393 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000394 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000395
Chris Lattnere29dc832008-03-16 20:34:23 +0000396 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000397 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000398 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000399
400 /// Check that class of this category is already completely declared.
401 if (!IDecl || IDecl->isForwardDecl())
402 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000403 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000404 /// Check for duplicate interface declaration for this category
405 ObjCCategoryDecl *CDeclChain;
406 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
407 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000408 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000409 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000410 CategoryName->getName());
411 break;
412 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000413 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000414 if (!CDeclChain)
415 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000416 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000417
418 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000419 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
420 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000421 }
422 return CDecl;
423}
424
425/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000426/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000427/// object.
428Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
429 SourceLocation AtCatImplLoc,
430 IdentifierInfo *ClassName, SourceLocation ClassLoc,
431 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000432 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000433 ObjCCategoryImplDecl *CDecl =
434 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000435 /// Check that class of this category is already completely declared.
436 if (!IDecl || IDecl->isForwardDecl())
437 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
438
439 /// TODO: Check that CatName, category name, is not used in another
440 // implementation.
441 return CDecl;
442}
443
444Sema::DeclTy *Sema::ActOnStartClassImplementation(
445 SourceLocation AtClassImplLoc,
446 IdentifierInfo *ClassName, SourceLocation ClassLoc,
447 IdentifierInfo *SuperClassname,
448 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000449 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000450 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000451 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000452 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000453 Diag(ClassLoc, diag::err_redefinition_different_kind,
454 ClassName->getName());
455 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
456 }
457 else {
458 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000459 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000460 if (!IDecl)
461 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
462 }
463
464 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000465 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000466 if (SuperClassname) {
467 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000468 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000469 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000470 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
471 SuperClassname->getName());
472 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
473 }
474 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000475 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000476 if (!SDecl)
477 Diag(SuperClassLoc, diag::err_undef_superclass,
478 SuperClassname->getName(), ClassName->getName());
479 else if (IDecl && IDecl->getSuperClass() != SDecl) {
480 // This implementation and its interface do not have the same
481 // super class.
482 Diag(SuperClassLoc, diag::err_conflicting_super_class,
483 SDecl->getName());
484 Diag(SDecl->getLocation(), diag::err_previous_definition);
485 }
486 }
487 }
488
489 if (!IDecl) {
490 // Legacy case of @implementation with no corresponding @interface.
491 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000492 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000493 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000494 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000495 IDecl->setSuperClass(SDecl);
496 IDecl->setLocEnd(ClassLoc);
497
498 // Remember that this needs to be removed when the scope is popped.
499 TUScope->AddDecl(IDecl);
500 }
501
Ted Kremenek42730c52008-01-07 19:49:32 +0000502 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000503 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
504 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000505
506 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000507 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000508 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000509 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
510 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000511 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000512 return IMPDecl;
513}
514
Ted Kremenek42730c52008-01-07 19:49:32 +0000515void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
516 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000517 SourceLocation RBrace) {
518 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000519 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000520 if (!IDecl)
521 return;
522 /// Check case of non-existing @interface decl.
523 /// (legacy objective-c @implementation decl without an @interface decl).
524 /// Add implementations's ivar to the synthesize class's ivar list.
525 if (IDecl->ImplicitInterfaceDecl()) {
526 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
527 return;
528 }
529 // If implementation has empty ivar list, just return.
530 if (numIvars == 0)
531 return;
532
533 assert(ivars && "missing @implementation ivars");
534
535 // Check interface's Ivar list against those in the implementation.
536 // names and types must match.
537 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000539 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000540 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
541 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000542 ObjCIvarDecl* ImplIvar = ivars[j++];
543 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000544 assert (ImplIvar && "missing implementation ivar");
545 assert (ClsIvar && "missing class ivar");
546 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
547 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
548 ImplIvar->getIdentifier()->getName());
549 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
550 ClsIvar->getIdentifier()->getName());
551 }
552 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
553 // as error.
554 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
555 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
556 ImplIvar->getIdentifier()->getName());
557 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
558 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000559 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000560 }
561 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000562 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000563
564 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000565 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000566 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000567 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000568}
569
Steve Naroffb4f48512008-02-10 21:38:56 +0000570void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
571 bool &IncompleteImpl) {
572 if (!IncompleteImpl) {
573 Diag(ImpLoc, diag::warn_incomplete_impl);
574 IncompleteImpl = true;
575 }
576 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
577}
578
Steve Naroffb268d2a2008-02-08 22:06:17 +0000579/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000580/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000581void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
582 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000583 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000584 const llvm::DenseSet<Selector> &InsMap,
585 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000586 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000587 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000588 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000590 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000591 method->getImplementationControl() != ObjCMethodDecl::Optional)
592 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000593 }
594 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000595 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000596 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000598 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000599 method->getImplementationControl() != ObjCMethodDecl::Optional)
600 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000601 }
Chris Lattner0be08822008-07-21 21:32:27 +0000602 // Check on this protocols's referenced protocols, recursively.
603 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
604 E = PDecl->protocol_end(); PI != E; ++PI)
605 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000606}
607
Ted Kremenek42730c52008-01-07 19:49:32 +0000608void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
609 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000610 llvm::DenseSet<Selector> InsMap;
611 // Check and see if instance methods in class interface have been
612 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000613 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000614 E = IMPDecl->instmeth_end(); I != E; ++I)
615 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000616
617 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000618 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000619 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000620 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000621 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000622
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 llvm::DenseSet<Selector> ClsMap;
624 // Check and see if class methods in class interface have been
625 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000626 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000627 E = IMPDecl->classmeth_end(); I != E; ++I)
628 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000629
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000631 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000632 if (!ClsMap.count((*I)->getSelector()))
633 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000634
635 // Check the protocol list for unimplemented methods in the @implementation
636 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000637 const ObjCList<ObjCProtocolDecl> &Protocols =
638 IDecl->getReferencedProtocols();
639 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
640 E = Protocols.end(); I != E; ++I)
641 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000642 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000643}
644
645/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
646/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000647void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
648 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000649 llvm::DenseSet<Selector> InsMap;
650 // Check and see if instance methods in category interface have been
651 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000652 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000653 E = CatImplDecl->instmeth_end(); I != E; ++I)
654 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000655
656 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000657 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000658 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000659 if (!InsMap.count((*I)->getSelector()))
660 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
661
Chris Lattner855e51f2007-12-12 07:09:47 +0000662 llvm::DenseSet<Selector> ClsMap;
663 // Check and see if class methods in category interface have been
664 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000665 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000666 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
667 I != E; ++I)
668 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000669
Ted Kremenek42730c52008-01-07 19:49:32 +0000670 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000671 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000672 if (!ClsMap.count((*I)->getSelector()))
673 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000674
675 // Check the protocol list for unimplemented methods in the @implementation
676 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000677 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
678 E = CatClassDecl->protocol_end(); PI != E; ++PI)
679 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000680 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000681}
682
683/// ActOnForwardClassDeclaration -
684Action::DeclTy *
685Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
686 IdentifierInfo **IdentList, unsigned NumElts)
687{
Ted Kremenek42730c52008-01-07 19:49:32 +0000688 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000689
690 for (unsigned i = 0; i != NumElts; ++i) {
691 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000692 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000693 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000694 // GCC apparently allows the following idiom:
695 //
696 // typedef NSObject < XCElementTogglerP > XCElementToggler;
697 // @class XCElementToggler;
698 //
699 // FIXME: Make an extension?
700 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
701 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
702 Diag(AtClassLoc, diag::err_redefinition_different_kind,
703 IdentList[i]->getName());
704 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
705 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000706 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000707 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000708 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000709 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000710 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000711 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000712
713 // Remember that this needs to be removed when the scope is popped.
714 TUScope->AddDecl(IDecl);
715 }
716
717 Interfaces.push_back(IDecl);
718 }
719
Chris Lattnere29dc832008-03-16 20:34:23 +0000720 return ObjCClassDecl::Create(Context, AtClassLoc,
721 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000722}
723
724
725/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
726/// returns true, or false, accordingly.
727/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000728bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
729 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000730 if (Method->getResultType().getCanonicalType() !=
731 PrevMethod->getResultType().getCanonicalType())
732 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000733 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000734 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
735 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000736 if (Context.getCanonicalType(ParamDecl->getType()) !=
737 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000738 return false;
739 }
740 return true;
741}
742
Ted Kremenek42730c52008-01-07 19:49:32 +0000743void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
744 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000745 if (!FirstMethod.Method) {
746 // Haven't seen a method with this selector name yet - add it.
747 FirstMethod.Method = Method;
748 FirstMethod.Next = 0;
749 } else {
750 // We've seen a method with this name, now check the type signature(s).
751 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
752
Ted Kremenek42730c52008-01-07 19:49:32 +0000753 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000754 Next = Next->Next)
755 match = MatchTwoMethodDeclarations(Method, Next->Method);
756
757 if (!match) {
758 // We have a new signature for an existing method - add it.
759 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000760 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 FirstMethod.Next = OMI;
762 }
763 }
764}
765
Ted Kremenek42730c52008-01-07 19:49:32 +0000766void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
767 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000768 if (!FirstMethod.Method) {
769 // Haven't seen a method with this selector name yet - add it.
770 FirstMethod.Method = Method;
771 FirstMethod.Next = 0;
772 } else {
773 // We've seen a method with this name, now check the type signature(s).
774 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
775
Ted Kremenek42730c52008-01-07 19:49:32 +0000776 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000777 Next = Next->Next)
778 match = MatchTwoMethodDeclarations(Method, Next->Method);
779
780 if (!match) {
781 // We have a new signature for an existing method - add it.
782 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000783 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000784 FirstMethod.Next = OMI;
785 }
786 }
787}
788
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000789// Note: For class/category implemenations, allMethods/allProperties is
790// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000791void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
792 DeclTy **allMethods, unsigned allNum,
793 DeclTy **allProperties, unsigned pNum) {
794 Decl *ClassDecl = static_cast<Decl *>(classDecl);
795
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000796 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
797 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000798 // should be true.
799 if (!ClassDecl)
800 return;
801
Ted Kremenek42730c52008-01-07 19:49:32 +0000802 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
803 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000804
Ted Kremenek42730c52008-01-07 19:49:32 +0000805 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
806 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000807
808 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000809 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
810 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000811 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000812
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000813 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000814 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
815 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000816 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
817 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
818 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
819 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000820 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000821 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000822 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000823
824 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000825 ObjCMethodDecl *Method =
826 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000827
828 if (!Method) continue; // Already issued a diagnostic.
829 if (Method->isInstance()) {
830 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000831 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000832 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
833 : false;
834 if (isInterfaceDeclKind && PrevMethod && !match
835 || checkIdenticalMethods && match) {
836 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
837 Method->getSelector().getName());
838 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
839 } else {
840 insMethods.push_back(Method);
841 InsMap[Method->getSelector()] = Method;
842 /// The following allows us to typecheck messages to "id".
843 AddInstanceMethodToGlobalPool(Method);
844 }
845 }
846 else {
847 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000848 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000849 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
850 : false;
851 if (isInterfaceDeclKind && PrevMethod && !match
852 || checkIdenticalMethods && match) {
853 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
854 Method->getSelector().getName());
855 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
856 } else {
857 clsMethods.push_back(Method);
858 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000859 /// The following allows us to typecheck messages to "Class".
860 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000861 }
862 }
863 }
864
Ted Kremenek42730c52008-01-07 19:49:32 +0000865 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000866 // Compares properties declaraed in this class to those of its
867 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000868 ComparePropertiesInBaseAndSuper(I);
869 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000870 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
871 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000872 // FIXME: It would be really nice if we could avoid this. Injecting
873 // methods into the interface makes it hard to distinguish "real" methods
874 // from synthesized "property" methods (that aren't in the source).
875 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000876 I->addPropertyMethods(Context, *P, insMethods);
877 }
878 I->addMethods(&insMethods[0], insMethods.size(),
879 &clsMethods[0], clsMethods.size(), AtEndLoc);
880
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000882 P->addMethods(&insMethods[0], insMethods.size(),
883 &clsMethods[0], clsMethods.size(), AtEndLoc);
884 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000885 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000886 C->addMethods(&insMethods[0], insMethods.size(),
887 &clsMethods[0], clsMethods.size(), AtEndLoc);
888 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 else if (ObjCImplementationDecl *IC =
890 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000891 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000892 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000893 ImplMethodsVsClassMethods(IC, IDecl);
894 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000896 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000897 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000898 // Find category interface decl and then check that all methods declared
899 // in this interface is implemented in the category @implementation.
900 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000901 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000902 Categories; Categories = Categories->getNextClassCategory()) {
903 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
904 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
905 break;
906 }
907 }
908 }
909 }
910}
911
912
913/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
914/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000915static Decl::ObjCDeclQualifier
916CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
917 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
918 if (PQTVal & ObjCDeclSpec::DQ_In)
919 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
920 if (PQTVal & ObjCDeclSpec::DQ_Inout)
921 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
922 if (PQTVal & ObjCDeclSpec::DQ_Out)
923 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
924 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
925 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
926 if (PQTVal & ObjCDeclSpec::DQ_Byref)
927 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
928 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
929 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000930
931 return ret;
932}
933
934Sema::DeclTy *Sema::ActOnMethodDeclaration(
935 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000936 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000937 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000938 Selector Sel,
939 // optional arguments. The number of types/arguments is obtained
940 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000941 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000942 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
943 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000944 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000945
946 // Make sure we can establish a context for the method.
947 if (!ClassDecl) {
948 Diag(MethodLoc, diag::error_missing_method_context);
949 return 0;
950 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000951 QualType resultDeclType;
952
953 if (ReturnType)
954 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
955 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000956 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000957
Chris Lattner114add62008-03-16 00:49:28 +0000958 ObjCMethodDecl* ObjCMethod =
959 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000960 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000961 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000962 false,
Chris Lattner114add62008-03-16 00:49:28 +0000963 MethodDeclKind == tok::objc_optional ?
964 ObjCMethodDecl::Optional :
965 ObjCMethodDecl::Required);
966
Chris Lattnereee57c02008-04-04 06:12:32 +0000967 llvm::SmallVector<ParmVarDecl*, 16> Params;
968
969 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
970 // FIXME: arg->AttrList must be stored too!
971 QualType argType;
972
973 if (ArgTypes[i])
974 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
975 else
976 argType = Context.getObjCIdType();
977 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
978 SourceLocation(/*FIXME*/),
979 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000980 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000981 Param->setObjCDeclQualifier(
982 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
983 Params.push_back(Param);
984 }
985
Ted Kremenek42730c52008-01-07 19:49:32 +0000986 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
987 ObjCMethod->setObjCDeclQualifier(
988 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
989 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000990
991 // For implementations (which can be very "coarse grain"), we add the
992 // method now. This allows the AST to implement lookup methods that work
993 // incrementally (without waiting until we parse the @end). It also allows
994 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000995 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000996 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000997 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000998 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000999 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001000 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001001 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001002 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001003 }
1004 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001005 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001006 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001007 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001008 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001009 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001010 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001011 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001012 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001013 }
1014 }
1015 if (PrevMethod) {
1016 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001017 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1018 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001019 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1020 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001021 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001022}
1023
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001024Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1025 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001026 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001027 Selector GetterSel,
1028 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001029 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001030 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001031 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1032 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001033 // Regardless of setter/getter attribute, we save the default getter/setter
1034 // selector names in anticipation of declaration of setter/getter methods.
1035 PDecl->setGetterName(GetterSel);
1036 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001037
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001038 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001039 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001040
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001041 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001043
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001044 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001045 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001046
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001047 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001048 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001049
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001050 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001052
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001053 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001054 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001055
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001056 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001057 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001058
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001059 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001060 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001061
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001062 if (MethodImplKind == tok::objc_required)
1063 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1064 else if (MethodImplKind == tok::objc_optional)
1065 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1066
Chris Lattner855e51f2007-12-12 07:09:47 +00001067 return PDecl;
1068}
1069
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001070/// ActOnPropertyImplDecl - This routine performs semantic checks and
1071/// builds the AST node for a property implementation declaration; declared
1072/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001073///
1074Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1075 SourceLocation PropertyLoc,
1076 bool Synthesize,
1077 DeclTy *ClassCatImpDecl,
1078 IdentifierInfo *PropertyId,
1079 IdentifierInfo *PropertyIvar) {
1080 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1081 // Make sure we have a context for the property implementation declaration.
1082 if (!ClassImpDecl) {
1083 Diag(AtLoc, diag::error_missing_property_context);
1084 return 0;
1085 }
1086 ObjCPropertyDecl *property = 0;
1087 ObjCInterfaceDecl* IDecl = 0;
1088 // Find the class or category class where this property must have
1089 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001090 ObjCImplementationDecl *IC = 0;
1091 ObjCCategoryImplDecl* CatImplClass = 0;
1092 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001093 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001094 // We always synthesize an interface for an implementation
1095 // without an interface decl. So, IDecl is always non-zero.
1096 assert(IDecl &&
1097 "ActOnPropertyImplDecl - @implementation without @interface");
1098
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001099 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001100 property = IDecl->FindPropertyDeclaration(PropertyId);
1101 if (!property) {
1102 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001103 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001104 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001105 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001106 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001107 if (Synthesize) {
1108 Diag(AtLoc, diag::error_synthesize_category_decl);
1109 return 0;
1110 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001111 IDecl = CatImplClass->getClassInterface();
1112 if (!IDecl) {
1113 Diag(AtLoc, diag::error_missing_property_interface);
1114 return 0;
1115 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001116 ObjCCategoryDecl *Category =
1117 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1118
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001119 // If category for this implementation not found, it is an error which
1120 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001121 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001122 return 0;
1123 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001124 property = Category->FindPropertyDeclaration(PropertyId);
1125 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001126 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001127 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001128 return 0;
1129 }
1130 }
1131 else {
1132 Diag(AtLoc, diag::error_bad_property_context);
1133 return 0;
1134 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001135 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001136 // Check that we have a valid, previously declared ivar for @synthesize
1137 if (Synthesize) {
1138 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001139 if (!PropertyIvar)
1140 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001141 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001142 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001143 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001144 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1145 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001146 return 0;
1147 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001148 // Check that type of property and its ivar match.
Chris Lattnerc5cff302008-07-26 20:50:02 +00001149 if (Context.getCanonicalType(Ivar->getType()) !=
1150 Context.getCanonicalType(property->getType())) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001151 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1152 Ivar->getName());
1153 return 0;
1154 }
1155
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001156 } else if (PropertyIvar) {
1157 // @dynamic
1158 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1159 return 0;
1160 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001161 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001162 ObjCPropertyImplDecl *PIDecl =
1163 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1164 (Synthesize ?
1165 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1166 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1167 Ivar);
1168 if (IC)
1169 IC->addPropertyImplementation(PIDecl);
1170 else
1171 CatImplClass->addPropertyImplementation(PIDecl);
1172
1173 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001174}