blob: 3478de5bf534a2c992012d8837368b7529ad048d [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");
Chris Lattnerb5457862008-07-27 00:05:05 +0000546 if (Context.getCanonicalType(ImplIvar->getType()) !=
547 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000548 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
549 ImplIvar->getIdentifier()->getName());
550 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
551 ClsIvar->getIdentifier()->getName());
552 }
553 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
554 // as error.
555 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
556 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
557 ImplIvar->getIdentifier()->getName());
558 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
559 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000560 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000561 }
562 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000563 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000564
565 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000566 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000567 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000568 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000569}
570
Steve Naroffb4f48512008-02-10 21:38:56 +0000571void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
572 bool &IncompleteImpl) {
573 if (!IncompleteImpl) {
574 Diag(ImpLoc, diag::warn_incomplete_impl);
575 IncompleteImpl = true;
576 }
577 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
578}
579
Steve Naroffb268d2a2008-02-08 22:06:17 +0000580/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000581/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000582void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
583 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000584 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000585 const llvm::DenseSet<Selector> &InsMap,
586 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000588 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000589 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000590 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000591 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000592 method->getImplementationControl() != ObjCMethodDecl::Optional)
593 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000594 }
595 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000596 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000597 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000598 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000599 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000600 method->getImplementationControl() != ObjCMethodDecl::Optional)
601 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000602 }
Chris Lattner0be08822008-07-21 21:32:27 +0000603 // Check on this protocols's referenced protocols, recursively.
604 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
605 E = PDecl->protocol_end(); PI != E; ++PI)
606 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000607}
608
Ted Kremenek42730c52008-01-07 19:49:32 +0000609void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
610 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000611 llvm::DenseSet<Selector> InsMap;
612 // Check and see if instance methods in class interface have been
613 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000614 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000615 E = IMPDecl->instmeth_end(); I != E; ++I)
616 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000617
618 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000619 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000620 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000621 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000622 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000623
Chris Lattner855e51f2007-12-12 07:09:47 +0000624 llvm::DenseSet<Selector> ClsMap;
625 // Check and see if class methods in class interface have been
626 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000627 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000628 E = IMPDecl->classmeth_end(); I != E; ++I)
629 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000630
Ted Kremenek42730c52008-01-07 19:49:32 +0000631 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000632 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000633 if (!ClsMap.count((*I)->getSelector()))
634 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000635
636 // Check the protocol list for unimplemented methods in the @implementation
637 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000638 const ObjCList<ObjCProtocolDecl> &Protocols =
639 IDecl->getReferencedProtocols();
640 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
641 E = Protocols.end(); I != E; ++I)
642 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000643 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000644}
645
646/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
647/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000648void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
649 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000650 llvm::DenseSet<Selector> InsMap;
651 // Check and see if instance methods in category interface have been
652 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000653 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000654 E = CatImplDecl->instmeth_end(); I != E; ++I)
655 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000656
657 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000658 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000659 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000660 if (!InsMap.count((*I)->getSelector()))
661 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
662
Chris Lattner855e51f2007-12-12 07:09:47 +0000663 llvm::DenseSet<Selector> ClsMap;
664 // Check and see if class methods in category interface have been
665 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000666 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000667 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
668 I != E; ++I)
669 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000670
Ted Kremenek42730c52008-01-07 19:49:32 +0000671 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000672 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000673 if (!ClsMap.count((*I)->getSelector()))
674 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000675
676 // Check the protocol list for unimplemented methods in the @implementation
677 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000678 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
679 E = CatClassDecl->protocol_end(); PI != E; ++PI)
680 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000681 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000682}
683
684/// ActOnForwardClassDeclaration -
685Action::DeclTy *
686Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
687 IdentifierInfo **IdentList, unsigned NumElts)
688{
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000690
691 for (unsigned i = 0; i != NumElts; ++i) {
692 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000693 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000694 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000695 // GCC apparently allows the following idiom:
696 //
697 // typedef NSObject < XCElementTogglerP > XCElementToggler;
698 // @class XCElementToggler;
699 //
700 // FIXME: Make an extension?
701 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
702 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
703 Diag(AtClassLoc, diag::err_redefinition_different_kind,
704 IdentList[i]->getName());
705 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
706 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000707 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000708 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000709 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000710 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000711 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000712 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000713
714 // Remember that this needs to be removed when the scope is popped.
715 TUScope->AddDecl(IDecl);
716 }
717
718 Interfaces.push_back(IDecl);
719 }
720
Chris Lattnere29dc832008-03-16 20:34:23 +0000721 return ObjCClassDecl::Create(Context, AtClassLoc,
722 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000723}
724
725
726/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
727/// returns true, or false, accordingly.
728/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000729bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
730 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000731 if (Context.getCanonicalType(Method->getResultType()) !=
732 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000733 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000734 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000735 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
736 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000737 if (Context.getCanonicalType(ParamDecl->getType()) !=
738 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000739 return false;
740 }
741 return true;
742}
743
Ted Kremenek42730c52008-01-07 19:49:32 +0000744void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
745 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000746 if (!FirstMethod.Method) {
747 // Haven't seen a method with this selector name yet - add it.
748 FirstMethod.Method = Method;
749 FirstMethod.Next = 0;
750 } else {
751 // We've seen a method with this name, now check the type signature(s).
752 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
753
Ted Kremenek42730c52008-01-07 19:49:32 +0000754 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000755 Next = Next->Next)
756 match = MatchTwoMethodDeclarations(Method, Next->Method);
757
758 if (!match) {
759 // We have a new signature for an existing method - add it.
760 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000761 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000762 FirstMethod.Next = OMI;
763 }
764 }
765}
766
Ted Kremenek42730c52008-01-07 19:49:32 +0000767void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
768 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000769 if (!FirstMethod.Method) {
770 // Haven't seen a method with this selector name yet - add it.
771 FirstMethod.Method = Method;
772 FirstMethod.Next = 0;
773 } else {
774 // We've seen a method with this name, now check the type signature(s).
775 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
776
Ted Kremenek42730c52008-01-07 19:49:32 +0000777 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000778 Next = Next->Next)
779 match = MatchTwoMethodDeclarations(Method, Next->Method);
780
781 if (!match) {
782 // We have a new signature for an existing method - add it.
783 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000784 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000785 FirstMethod.Next = OMI;
786 }
787 }
788}
789
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000790// Note: For class/category implemenations, allMethods/allProperties is
791// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000792void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
793 DeclTy **allMethods, unsigned allNum,
794 DeclTy **allProperties, unsigned pNum) {
795 Decl *ClassDecl = static_cast<Decl *>(classDecl);
796
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000797 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
798 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000799 // should be true.
800 if (!ClassDecl)
801 return;
802
Ted Kremenek42730c52008-01-07 19:49:32 +0000803 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
804 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000805
Ted Kremenek42730c52008-01-07 19:49:32 +0000806 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
807 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000808
809 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000810 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
811 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000812 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000813
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000814 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000815 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
816 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000817 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
818 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
819 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
820 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000821 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000822 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000823 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000824
825 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000826 ObjCMethodDecl *Method =
827 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000828
829 if (!Method) continue; // Already issued a diagnostic.
830 if (Method->isInstance()) {
831 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000833 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
834 : false;
835 if (isInterfaceDeclKind && PrevMethod && !match
836 || checkIdenticalMethods && match) {
837 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
838 Method->getSelector().getName());
839 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
840 } else {
841 insMethods.push_back(Method);
842 InsMap[Method->getSelector()] = Method;
843 /// The following allows us to typecheck messages to "id".
844 AddInstanceMethodToGlobalPool(Method);
845 }
846 }
847 else {
848 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000849 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000850 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
851 : false;
852 if (isInterfaceDeclKind && PrevMethod && !match
853 || checkIdenticalMethods && match) {
854 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
855 Method->getSelector().getName());
856 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
857 } else {
858 clsMethods.push_back(Method);
859 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000860 /// The following allows us to typecheck messages to "Class".
861 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000862 }
863 }
864 }
865
Ted Kremenek42730c52008-01-07 19:49:32 +0000866 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000867 // Compares properties declaraed in this class to those of its
868 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000869 ComparePropertiesInBaseAndSuper(I);
870 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000871 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
872 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000873 // FIXME: It would be really nice if we could avoid this. Injecting
874 // methods into the interface makes it hard to distinguish "real" methods
875 // from synthesized "property" methods (that aren't in the source).
876 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000877 I->addPropertyMethods(Context, *P, insMethods);
878 }
879 I->addMethods(&insMethods[0], insMethods.size(),
880 &clsMethods[0], clsMethods.size(), AtEndLoc);
881
Ted Kremenek42730c52008-01-07 19:49:32 +0000882 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000883 P->addMethods(&insMethods[0], insMethods.size(),
884 &clsMethods[0], clsMethods.size(), AtEndLoc);
885 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000886 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000887 C->addMethods(&insMethods[0], insMethods.size(),
888 &clsMethods[0], clsMethods.size(), AtEndLoc);
889 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000890 else if (ObjCImplementationDecl *IC =
891 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000892 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000893 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000894 ImplMethodsVsClassMethods(IC, IDecl);
895 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000896 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000897 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000899 // Find category interface decl and then check that all methods declared
900 // in this interface is implemented in the category @implementation.
901 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000902 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000903 Categories; Categories = Categories->getNextClassCategory()) {
904 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
905 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
906 break;
907 }
908 }
909 }
910 }
911}
912
913
914/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
915/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000916static Decl::ObjCDeclQualifier
917CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
918 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
919 if (PQTVal & ObjCDeclSpec::DQ_In)
920 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
921 if (PQTVal & ObjCDeclSpec::DQ_Inout)
922 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
923 if (PQTVal & ObjCDeclSpec::DQ_Out)
924 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
925 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
926 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
927 if (PQTVal & ObjCDeclSpec::DQ_Byref)
928 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
929 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
930 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000931
932 return ret;
933}
934
935Sema::DeclTy *Sema::ActOnMethodDeclaration(
936 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000937 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000938 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000939 Selector Sel,
940 // optional arguments. The number of types/arguments is obtained
941 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000942 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000943 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
944 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000945 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000946
947 // Make sure we can establish a context for the method.
948 if (!ClassDecl) {
949 Diag(MethodLoc, diag::error_missing_method_context);
950 return 0;
951 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000952 QualType resultDeclType;
953
954 if (ReturnType)
955 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
956 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000957 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000958
Chris Lattner114add62008-03-16 00:49:28 +0000959 ObjCMethodDecl* ObjCMethod =
960 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000961 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000962 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000963 false,
Chris Lattner114add62008-03-16 00:49:28 +0000964 MethodDeclKind == tok::objc_optional ?
965 ObjCMethodDecl::Optional :
966 ObjCMethodDecl::Required);
967
Chris Lattnereee57c02008-04-04 06:12:32 +0000968 llvm::SmallVector<ParmVarDecl*, 16> Params;
969
970 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
971 // FIXME: arg->AttrList must be stored too!
972 QualType argType;
973
974 if (ArgTypes[i])
975 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
976 else
977 argType = Context.getObjCIdType();
978 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
979 SourceLocation(/*FIXME*/),
980 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000981 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000982 Param->setObjCDeclQualifier(
983 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
984 Params.push_back(Param);
985 }
986
Ted Kremenek42730c52008-01-07 19:49:32 +0000987 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
988 ObjCMethod->setObjCDeclQualifier(
989 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
990 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000991
992 // For implementations (which can be very "coarse grain"), we add the
993 // method now. This allows the AST to implement lookup methods that work
994 // incrementally (without waiting until we parse the @end). It also allows
995 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000996 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000997 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000998 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000999 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001000 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001001 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001002 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001003 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001004 }
1005 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001006 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001007 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001008 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001009 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001010 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001011 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001012 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001013 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001014 }
1015 }
1016 if (PrevMethod) {
1017 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001018 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1019 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001020 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1021 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001022 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001023}
1024
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001025Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1026 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001027 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001028 Selector GetterSel,
1029 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001030 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001031 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001032 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1033 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001034 // Regardless of setter/getter attribute, we save the default getter/setter
1035 // selector names in anticipation of declaration of setter/getter methods.
1036 PDecl->setGetterName(GetterSel);
1037 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001038
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001039 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001040 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001041
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001042 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001043 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001044
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001045 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001046 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001047
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001048 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001049 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001050
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001051 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001052 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001053
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001054 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001055 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001056
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001057 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001058 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001059
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001060 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001061 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001062
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001063 if (MethodImplKind == tok::objc_required)
1064 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1065 else if (MethodImplKind == tok::objc_optional)
1066 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1067
Chris Lattner855e51f2007-12-12 07:09:47 +00001068 return PDecl;
1069}
1070
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001071/// ActOnPropertyImplDecl - This routine performs semantic checks and
1072/// builds the AST node for a property implementation declaration; declared
1073/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001074///
1075Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1076 SourceLocation PropertyLoc,
1077 bool Synthesize,
1078 DeclTy *ClassCatImpDecl,
1079 IdentifierInfo *PropertyId,
1080 IdentifierInfo *PropertyIvar) {
1081 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1082 // Make sure we have a context for the property implementation declaration.
1083 if (!ClassImpDecl) {
1084 Diag(AtLoc, diag::error_missing_property_context);
1085 return 0;
1086 }
1087 ObjCPropertyDecl *property = 0;
1088 ObjCInterfaceDecl* IDecl = 0;
1089 // Find the class or category class where this property must have
1090 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001091 ObjCImplementationDecl *IC = 0;
1092 ObjCCategoryImplDecl* CatImplClass = 0;
1093 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001094 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001095 // We always synthesize an interface for an implementation
1096 // without an interface decl. So, IDecl is always non-zero.
1097 assert(IDecl &&
1098 "ActOnPropertyImplDecl - @implementation without @interface");
1099
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001100 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001101 property = IDecl->FindPropertyDeclaration(PropertyId);
1102 if (!property) {
1103 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001104 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001105 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001106 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001107 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001108 if (Synthesize) {
1109 Diag(AtLoc, diag::error_synthesize_category_decl);
1110 return 0;
1111 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001112 IDecl = CatImplClass->getClassInterface();
1113 if (!IDecl) {
1114 Diag(AtLoc, diag::error_missing_property_interface);
1115 return 0;
1116 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001117 ObjCCategoryDecl *Category =
1118 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1119
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001120 // If category for this implementation not found, it is an error which
1121 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001122 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001123 return 0;
1124 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001125 property = Category->FindPropertyDeclaration(PropertyId);
1126 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001127 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001128 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001129 return 0;
1130 }
1131 }
1132 else {
1133 Diag(AtLoc, diag::error_bad_property_context);
1134 return 0;
1135 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001136 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001137 // Check that we have a valid, previously declared ivar for @synthesize
1138 if (Synthesize) {
1139 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001140 if (!PropertyIvar)
1141 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001142 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001143 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001144 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001145 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1146 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001147 return 0;
1148 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001149 // Check that type of property and its ivar match.
Chris Lattnerc5cff302008-07-26 20:50:02 +00001150 if (Context.getCanonicalType(Ivar->getType()) !=
1151 Context.getCanonicalType(property->getType())) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001152 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1153 Ivar->getName());
1154 return 0;
1155 }
1156
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001157 } else if (PropertyIvar) {
1158 // @dynamic
1159 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1160 return 0;
1161 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001162 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001163 ObjCPropertyImplDecl *PIDecl =
1164 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1165 (Synthesize ?
1166 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1167 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1168 Ivar);
1169 if (IC)
1170 IC->addPropertyImplementation(PIDecl);
1171 else
1172 CatImplClass->addPropertyImplementation(PIDecl);
1173
1174 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001175}