blob: 07cc890fde3c92eb5eafdba1b7be13599abeedc5 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremeneka526c5c2008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-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 Naroffa56f6162007-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 Lattner4d391482007-12-12 07:09:47 +000036
37 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerb048c982008-04-06 04:47:34 +000038 PushDeclContext(MDecl);
Chris Lattner4d391482007-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 Naroffe2af8b12008-06-05 14:49:39 +000047 QualType selfTy;
Chris Lattner4d391482007-12-12 07:09:47 +000048 if (MDecl->isInstance()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +000049 selfTy = Context.getObjCIdType();
Gabor Greif30037532008-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 Lattner04421082008-04-08 04:40:51 +000053 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000054 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000055 }
Steve Naroffe2af8b12008-06-05 14:49:39 +000056 } else // we have a factory method.
57 selfTy = Context.getObjCClassType();
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000058 getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner41110242008-06-17 18:05:57 +000059 PI.Ident, PI.IdentLoc, selfTy));
Chris Lattner4d391482007-12-12 07:09:47 +000060
61 PI.Ident = &Context.Idents.get("_cmd");
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000062 getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner41110242008-06-17 18:05:57 +000063 PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
Chris Lattner04421082008-04-08 04:40:51 +000064
Chris Lattner8123a952008-04-10 02:22:51 +000065 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000066 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000067 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000068 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000069 if (II)
70 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000071 }
72}
73
Chris Lattner7caeabd2008-07-21 22:17:28 +000074Sema::DeclTy *Sema::
75ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
76 IdentifierInfo *ClassName, SourceLocation ClassLoc,
77 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000078 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000079 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000080 assert(ClassName && "Missing class identifier");
81
82 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000083 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000084 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +000090 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-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 Lattner4d391482007-12-12 07:09:47 +000098 }
Chris Lattnerb752f282008-07-21 07:06:49 +000099 } else {
100 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000101 ClassName, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000102
Steve Naroff31102512008-04-02 18:30:49 +0000103 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000119 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000120
121 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattnerb752f282008-07-21 07:06:49 +0000122 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner4d391482007-12-12 07:09:47 +0000123 SuperClassEntry ? SuperClassEntry->getName()
124 : SuperName->getName(),
Chris Lattnerb752f282008-07-21 07:06:49 +0000125 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner4d391482007-12-12 07:09:47 +0000126 }
127 }
128 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000129 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-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 Lattner06036d32008-07-26 04:13:19 +0000136 if (NumProtoRefs) {
137 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-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 Naroffe8043c32008-04-01 23:04:06 +0000145Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
146 IdentifierInfo *AliasName,
147 SourceLocation AliasLocation,
148 IdentifierInfo *ClassName,
149 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000150 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000151 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000153 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-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 Naroffb327ce02008-04-02 14:35:35 +0000165 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-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 Lattner4d391482007-12-12 07:09:47 +0000171 return 0;
172 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000173
174 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000176 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
177
178 ObjCAliasDecls[AliasName] = AliasDecl;
179 TUScope->AddDecl(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000180 return AliasDecl;
181}
182
Chris Lattnere13b9592008-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 Lattner4d391482007-12-12 07:09:47 +0000190 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000191 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000192 if (PDecl) {
193 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000194 if (!PDecl->isForwardDecl()) {
Chris Lattner4d391482007-12-12 07:09:47 +0000195 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
196 ProtocolName->getName());
Chris Lattner439e71f2008-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 Lattner4d391482007-12-12 07:09:47 +0000200 }
Chris Lattner439e71f2008-03-16 01:25:17 +0000201
202 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000203 } else {
Chris Lattner780f3292008-07-21 21:32:27 +0000204 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattnerc8581052008-03-16 20:19:15 +0000205 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000206 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000207 }
Chris Lattner4d391482007-12-12 07:09:47 +0000208
209 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000210 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000211 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-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 Lattnere13b9592008-07-26 04:03:38 +0000221Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000222 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000223 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000224 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000225 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000226 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
227 if (!PDecl) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000228 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
229 ProtocolId[i].first->getName());
Chris Lattnereacc3922008-07-26 03:47:43 +0000230 continue;
231 }
232
233 // If this is a forward declaration and we are supposed to warn in this
234 // case, do it.
235 if (WarnOnDeclarations && PDecl->isForwardDecl())
236 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
237 ProtocolId[i].first->getName());
238 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000239 }
240}
241
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000242/// DiagnosePropertyMismatch - Compares two properties for their
243/// attributes and types and warns on a variety of inconsistancies.
244///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000245void
246Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
247 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000248 const char *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000249 ObjCPropertyDecl::PropertyAttributeKind CAttr =
250 Property->getPropertyAttributes();
251 ObjCPropertyDecl::PropertyAttributeKind SAttr =
252 SuperProperty->getPropertyAttributes();
253 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
254 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000255 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000256 Property->getName(), inheritedName);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000257 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
258 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000259 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000260 Property->getName(), "copy", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000261 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000262 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
263 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000264 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000265 Property->getName(), "retain", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000266 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000267
268 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
269 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000270 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000271 Property->getName(), "atomic", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000272 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000273 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000274 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000275 Property->getName(), "setter", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000276 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000277 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000278 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000279 Property->getName(), "getter", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000280 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000281
Fariborz Jahanian34350962008-05-01 18:05:01 +0000282 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
283 Diag(Property->getLocation(), diag::warn_property_type,
284 Property->getType().getAsString(),
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000285 inheritedName);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000286
287}
288
289/// ComparePropertiesInBaseAndSuper - This routine compares property
290/// declarations in base and its super class, if any, and issues
291/// diagnostics in a variety of inconsistant situations.
292///
293void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000294Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000295 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
296 if (!SDecl)
297 return;
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000298 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
299 E = SDecl->classprop_end(); S != E; ++S) {
300 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000301 // Does property in super class has declaration in current class?
302 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
303 E = IDecl->classprop_end(); I != E; ++I) {
304 ObjCPropertyDecl *PDecl = (*I);
305 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000306 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000307 }
308 }
309}
310
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000311/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
312/// of properties declared in a protocol and adds them to the list
313/// of properties for current class if it is not there already.
314void
315Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
316 ObjCProtocolDecl *PDecl)
317{
318 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
319 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
320 E = PDecl->classprop_end(); P != E; ++P) {
321 ObjCPropertyDecl *Pr = (*P);
322 ObjCInterfaceDecl::classprop_iterator CP, CE;
323 // Is this property already in class's list of properties?
324 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
325 CP != CE; ++CP)
326 if ((*CP)->getIdentifier() == Pr->getIdentifier())
327 break;
328 if (CP == CE)
329 // Add this property to list of properties for thie class.
330 mergeProperties.push_back(Pr);
331 else
332 // Property protocol already exist in class. Diagnose any mismatch.
333 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
334 }
335 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
336}
337
338/// MergeProtocolPropertiesIntoClass - This routine merges properties
339/// declared in 'MergeItsProtocols' objects (which can be a class or an
340/// inherited protocol into the list of properties for class 'IDecl'
341///
342
343void
344Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
345 DeclTy *MergeItsProtocols) {
346 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattnerb752f282008-07-21 07:06:49 +0000347 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000348 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
349 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000350 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000351 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
352
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000353 // Go thru the list of protocols for this class and recursively merge
354 // their properties into this class as well.
355 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
356 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000357 MergeProtocolPropertiesIntoClass(IDecl, *P);
358 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000359 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
360 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
361 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000362 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000363 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000364}
365
Chris Lattner4d391482007-12-12 07:09:47 +0000366/// ActOnForwardProtocolDeclaration -
367Action::DeclTy *
368Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000369 const IdentifierLocPair *IdentList,
370 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000371 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000372
373 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000374 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000375 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000376 if (PDecl == 0) // Not already seen?
377 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000378
379 Protocols.push_back(PDecl);
380 }
Chris Lattner61f9d412008-03-16 20:34:23 +0000381 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
382 &Protocols[0], Protocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000383}
384
Chris Lattner7caeabd2008-07-21 22:17:28 +0000385Sema::DeclTy *Sema::
386ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
387 IdentifierInfo *ClassName, SourceLocation ClassLoc,
388 IdentifierInfo *CategoryName,
389 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000390 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000391 unsigned NumProtoRefs,
392 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000393 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000394
Chris Lattner61f9d412008-03-16 20:34:23 +0000395 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000396 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000397 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000398
399 /// Check that class of this category is already completely declared.
400 if (!IDecl || IDecl->isForwardDecl())
401 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffd100c802008-06-05 15:03:27 +0000402 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000403 /// Check for duplicate interface declaration for this category
404 ObjCCategoryDecl *CDeclChain;
405 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
406 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000407 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff74199b62008-06-05 04:33:44 +0000408 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000409 CategoryName->getName());
410 break;
411 }
Chris Lattner4d391482007-12-12 07:09:47 +0000412 }
Steve Naroffd100c802008-06-05 15:03:27 +0000413 if (!CDeclChain)
414 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000415 }
Chris Lattner4d391482007-12-12 07:09:47 +0000416
417 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000418 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
419 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000420 }
421 return CDecl;
422}
423
424/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000425/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000426/// object.
427Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
428 SourceLocation AtCatImplLoc,
429 IdentifierInfo *ClassName, SourceLocation ClassLoc,
430 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000431 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000432 ObjCCategoryImplDecl *CDecl =
433 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000434 /// Check that class of this category is already completely declared.
435 if (!IDecl || IDecl->isForwardDecl())
436 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
437
438 /// TODO: Check that CatName, category name, is not used in another
439 // implementation.
440 return CDecl;
441}
442
443Sema::DeclTy *Sema::ActOnStartClassImplementation(
444 SourceLocation AtClassImplLoc,
445 IdentifierInfo *ClassName, SourceLocation ClassLoc,
446 IdentifierInfo *SuperClassname,
447 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000449 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000450 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000451 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000452 Diag(ClassLoc, diag::err_redefinition_different_kind,
453 ClassName->getName());
454 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
455 }
456 else {
457 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000458 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000459 if (!IDecl)
460 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
461 }
462
463 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000464 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000465 if (SuperClassname) {
466 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000467 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000468 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000469 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
470 SuperClassname->getName());
471 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
472 }
473 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000474 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000475 if (!SDecl)
476 Diag(SuperClassLoc, diag::err_undef_superclass,
477 SuperClassname->getName(), ClassName->getName());
478 else if (IDecl && IDecl->getSuperClass() != SDecl) {
479 // This implementation and its interface do not have the same
480 // super class.
481 Diag(SuperClassLoc, diag::err_conflicting_super_class,
482 SDecl->getName());
483 Diag(SDecl->getLocation(), diag::err_previous_definition);
484 }
485 }
486 }
487
488 if (!IDecl) {
489 // Legacy case of @implementation with no corresponding @interface.
490 // Build, chain & install the interface decl into the identifier.
Chris Lattnerb752f282008-07-21 07:06:49 +0000491 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000492 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000493 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000494 IDecl->setSuperClass(SDecl);
495 IDecl->setLocEnd(ClassLoc);
496
497 // Remember that this needs to be removed when the scope is popped.
498 TUScope->AddDecl(IDecl);
499 }
500
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000501 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000502 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
503 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000504
505 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000506 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000507 // FIXME: Don't leak everything!
Chris Lattner4d391482007-12-12 07:09:47 +0000508 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
509 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000510 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000511 return IMPDecl;
512}
513
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000514void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
515 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000516 SourceLocation RBrace) {
517 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000518 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000519 if (!IDecl)
520 return;
521 /// Check case of non-existing @interface decl.
522 /// (legacy objective-c @implementation decl without an @interface decl).
523 /// Add implementations's ivar to the synthesize class's ivar list.
524 if (IDecl->ImplicitInterfaceDecl()) {
525 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
526 return;
527 }
528 // If implementation has empty ivar list, just return.
529 if (numIvars == 0)
530 return;
531
532 assert(ivars && "missing @implementation ivars");
533
534 // Check interface's Ivar list against those in the implementation.
535 // names and types must match.
536 //
Chris Lattner4d391482007-12-12 07:09:47 +0000537 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000538 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000539 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
540 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000541 ObjCIvarDecl* ImplIvar = ivars[j++];
542 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000543 assert (ImplIvar && "missing implementation ivar");
544 assert (ClsIvar && "missing class ivar");
545 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
546 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
547 ImplIvar->getIdentifier()->getName());
548 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
549 ClsIvar->getIdentifier()->getName());
550 }
551 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
552 // as error.
553 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
554 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
555 ImplIvar->getIdentifier()->getName());
556 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
557 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000558 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000559 }
560 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000561 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000562
563 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000564 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000565 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000566 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000567}
568
Steve Naroff3c2eb662008-02-10 21:38:56 +0000569void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
570 bool &IncompleteImpl) {
571 if (!IncompleteImpl) {
572 Diag(ImpLoc, diag::warn_incomplete_impl);
573 IncompleteImpl = true;
574 }
575 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
576}
577
Steve Naroffefe7f362008-02-08 22:06:17 +0000578/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000579/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000580void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
581 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000582 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000583 const llvm::DenseSet<Selector> &InsMap,
584 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000585 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000586 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000587 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000588 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000589 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000590 method->getImplementationControl() != ObjCMethodDecl::Optional)
591 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000592 }
593 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000595 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000596 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000597 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000598 method->getImplementationControl() != ObjCMethodDecl::Optional)
599 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000600 }
Chris Lattner780f3292008-07-21 21:32:27 +0000601 // Check on this protocols's referenced protocols, recursively.
602 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
603 E = PDecl->protocol_end(); PI != E; ++PI)
604 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000605}
606
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
608 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000609 llvm::DenseSet<Selector> InsMap;
610 // Check and see if instance methods in class interface have been
611 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000613 E = IMPDecl->instmeth_end(); I != E; ++I)
614 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000615
616 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000618 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000619 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000620 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000621
Chris Lattner4d391482007-12-12 07:09:47 +0000622 llvm::DenseSet<Selector> ClsMap;
623 // Check and see if class methods in class interface have been
624 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000626 E = IMPDecl->classmeth_end(); I != E; ++I)
627 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000628
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000629 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000630 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000631 if (!ClsMap.count((*I)->getSelector()))
632 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000633
634 // Check the protocol list for unimplemented methods in the @implementation
635 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000636 const ObjCList<ObjCProtocolDecl> &Protocols =
637 IDecl->getReferencedProtocols();
638 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
639 E = Protocols.end(); I != E; ++I)
640 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffefe7f362008-02-08 22:06:17 +0000641 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000642}
643
644/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
645/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000646void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
647 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000648 llvm::DenseSet<Selector> InsMap;
649 // Check and see if instance methods in category interface have been
650 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000652 E = CatImplDecl->instmeth_end(); I != E; ++I)
653 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000654
655 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000656 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000657 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000658 if (!InsMap.count((*I)->getSelector()))
659 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
660
Chris Lattner4d391482007-12-12 07:09:47 +0000661 llvm::DenseSet<Selector> ClsMap;
662 // Check and see if class methods in category interface have been
663 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000664 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000665 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
666 I != E; ++I)
667 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000668
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000670 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000671 if (!ClsMap.count((*I)->getSelector()))
672 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000673
674 // Check the protocol list for unimplemented methods in the @implementation
675 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000676 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
677 E = CatClassDecl->protocol_end(); PI != E; ++PI)
678 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000679 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000680}
681
682/// ActOnForwardClassDeclaration -
683Action::DeclTy *
684Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
685 IdentifierInfo **IdentList, unsigned NumElts)
686{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000688
689 for (unsigned i = 0; i != NumElts; ++i) {
690 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000691 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000692 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000693 // GCC apparently allows the following idiom:
694 //
695 // typedef NSObject < XCElementTogglerP > XCElementToggler;
696 // @class XCElementToggler;
697 //
698 // FIXME: Make an extension?
699 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
700 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
701 Diag(AtClassLoc, diag::err_redefinition_different_kind,
702 IdentList[i]->getName());
703 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
704 }
Chris Lattner4d391482007-12-12 07:09:47 +0000705 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000706 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000707 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000708 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000709 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000710 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000711
712 // Remember that this needs to be removed when the scope is popped.
713 TUScope->AddDecl(IDecl);
714 }
715
716 Interfaces.push_back(IDecl);
717 }
718
Chris Lattner61f9d412008-03-16 20:34:23 +0000719 return ObjCClassDecl::Create(Context, AtClassLoc,
720 &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000721}
722
723
724/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
725/// returns true, or false, accordingly.
726/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
728 const ObjCMethodDecl *PrevMethod) {
Chris Lattner4d391482007-12-12 07:09:47 +0000729 if (Method->getResultType().getCanonicalType() !=
730 PrevMethod->getResultType().getCanonicalType())
731 return false;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000732 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +0000733 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
734 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000735 if (Context.getCanonicalType(ParamDecl->getType()) !=
736 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000737 return false;
738 }
739 return true;
740}
741
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000742void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
743 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000744 if (!FirstMethod.Method) {
745 // Haven't seen a method with this selector name yet - add it.
746 FirstMethod.Method = Method;
747 FirstMethod.Next = 0;
748 } else {
749 // We've seen a method with this name, now check the type signature(s).
750 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
751
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000752 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000753 Next = Next->Next)
754 match = MatchTwoMethodDeclarations(Method, Next->Method);
755
756 if (!match) {
757 // We have a new signature for an existing method - add it.
758 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000759 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000760 FirstMethod.Next = OMI;
761 }
762 }
763}
764
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000765void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
766 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000767 if (!FirstMethod.Method) {
768 // Haven't seen a method with this selector name yet - add it.
769 FirstMethod.Method = Method;
770 FirstMethod.Next = 0;
771 } else {
772 // We've seen a method with this name, now check the type signature(s).
773 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
774
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000775 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000776 Next = Next->Next)
777 match = MatchTwoMethodDeclarations(Method, Next->Method);
778
779 if (!match) {
780 // We have a new signature for an existing method - add it.
781 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000782 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000783 FirstMethod.Next = OMI;
784 }
785 }
786}
787
Steve Naroffa56f6162007-12-18 01:30:32 +0000788// Note: For class/category implemenations, allMethods/allProperties is
789// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000790void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
791 DeclTy **allMethods, unsigned allNum,
792 DeclTy **allProperties, unsigned pNum) {
793 Decl *ClassDecl = static_cast<Decl *>(classDecl);
794
Steve Naroffa56f6162007-12-18 01:30:32 +0000795 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
796 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000797 // should be true.
798 if (!ClassDecl)
799 return;
800
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
802 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000803
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
805 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000806
807 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000808 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
809 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000810 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000811
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000812 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +0000813 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
814 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000815 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
816 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
817 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
818 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000819 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000820 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000821 }
Chris Lattner4d391482007-12-12 07:09:47 +0000822
823 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000824 ObjCMethodDecl *Method =
825 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000826
827 if (!Method) continue; // Already issued a diagnostic.
828 if (Method->isInstance()) {
829 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000830 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000831 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
832 : false;
833 if (isInterfaceDeclKind && PrevMethod && !match
834 || checkIdenticalMethods && match) {
835 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
836 Method->getSelector().getName());
837 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
838 } else {
839 insMethods.push_back(Method);
840 InsMap[Method->getSelector()] = Method;
841 /// The following allows us to typecheck messages to "id".
842 AddInstanceMethodToGlobalPool(Method);
843 }
844 }
845 else {
846 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000847 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000848 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
849 : false;
850 if (isInterfaceDeclKind && PrevMethod && !match
851 || checkIdenticalMethods && match) {
852 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
853 Method->getSelector().getName());
854 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
855 } else {
856 clsMethods.push_back(Method);
857 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000858 /// The following allows us to typecheck messages to "Class".
859 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000860 }
861 }
862 }
863
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000865 // Compares properties declaraed in this class to those of its
866 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000867 ComparePropertiesInBaseAndSuper(I);
868 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000869 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
870 E = I->classprop_end(); P != E; ++P) {
Steve Naroff8442b5c2008-05-22 23:24:08 +0000871 // FIXME: It would be really nice if we could avoid this. Injecting
872 // methods into the interface makes it hard to distinguish "real" methods
873 // from synthesized "property" methods (that aren't in the source).
874 // This complicicates the rewriter's life.
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000875 I->addPropertyMethods(Context, *P, insMethods);
876 }
877 I->addMethods(&insMethods[0], insMethods.size(),
878 &clsMethods[0], clsMethods.size(), AtEndLoc);
879
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000880 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000881 P->addMethods(&insMethods[0], insMethods.size(),
882 &clsMethods[0], clsMethods.size(), AtEndLoc);
883 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000884 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000885 C->addMethods(&insMethods[0], insMethods.size(),
886 &clsMethods[0], clsMethods.size(), AtEndLoc);
887 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000888 else if (ObjCImplementationDecl *IC =
889 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000890 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000891 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000892 ImplMethodsVsClassMethods(IC, IDecl);
893 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000894 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000895 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000896 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000897 // Find category interface decl and then check that all methods declared
898 // in this interface is implemented in the category @implementation.
899 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000900 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000901 Categories; Categories = Categories->getNextClassCategory()) {
902 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
903 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
904 break;
905 }
906 }
907 }
908 }
909}
910
911
912/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
913/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000914static Decl::ObjCDeclQualifier
915CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
916 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
917 if (PQTVal & ObjCDeclSpec::DQ_In)
918 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
919 if (PQTVal & ObjCDeclSpec::DQ_Inout)
920 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
921 if (PQTVal & ObjCDeclSpec::DQ_Out)
922 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
923 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
924 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
925 if (PQTVal & ObjCDeclSpec::DQ_Byref)
926 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
927 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
928 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000929
930 return ret;
931}
932
933Sema::DeclTy *Sema::ActOnMethodDeclaration(
934 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000935 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000937 Selector Sel,
938 // optional arguments. The number of types/arguments is obtained
939 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000940 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000941 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
942 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000943 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +0000944
945 // Make sure we can establish a context for the method.
946 if (!ClassDecl) {
947 Diag(MethodLoc, diag::error_missing_method_context);
948 return 0;
949 }
Chris Lattner4d391482007-12-12 07:09:47 +0000950 QualType resultDeclType;
951
952 if (ReturnType)
953 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
954 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000955 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000956
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000957 ObjCMethodDecl* ObjCMethod =
958 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +0000959 ClassDecl, AttrList,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000960 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +0000961 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000962 MethodDeclKind == tok::objc_optional ?
963 ObjCMethodDecl::Optional :
964 ObjCMethodDecl::Required);
965
Chris Lattner0ed844b2008-04-04 06:12:32 +0000966 llvm::SmallVector<ParmVarDecl*, 16> Params;
967
968 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
969 // FIXME: arg->AttrList must be stored too!
970 QualType argType;
971
972 if (ArgTypes[i])
973 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
974 else
975 argType = Context.getObjCIdType();
976 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
977 SourceLocation(/*FIXME*/),
978 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +0000979 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000980 Param->setObjCDeclQualifier(
981 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
982 Params.push_back(Param);
983 }
984
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000985 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
986 ObjCMethod->setObjCDeclQualifier(
987 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
988 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000989
990 // For implementations (which can be very "coarse grain"), we add the
991 // method now. This allows the AST to implement lookup methods that work
992 // incrementally (without waiting until we parse the @end). It also allows
993 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000995 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000996 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000997 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000999 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001000 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001001 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001002 }
1003 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001005 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001006 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001007 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001009 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001010 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001012 }
1013 }
1014 if (PrevMethod) {
1015 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1017 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +00001018 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1019 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001020 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001021}
1022
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001023Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1024 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001025 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001026 Selector GetterSel,
1027 Selector SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001028 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001029 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001030 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1031 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001032 // Regardless of setter/getter attribute, we save the default getter/setter
1033 // selector names in anticipation of declaration of setter/getter methods.
1034 PDecl->setGetterName(GetterSel);
1035 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001036
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001037 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001038 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001039
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001040 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001041 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001042
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001043 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001044 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001045
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001046 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001047 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +00001048
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001049 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001050 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001051
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001052 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001053 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001054
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001055 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001056 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001057
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001058 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001059 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001060
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001061 if (MethodImplKind == tok::objc_required)
1062 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1063 else if (MethodImplKind == tok::objc_optional)
1064 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1065
Chris Lattner4d391482007-12-12 07:09:47 +00001066 return PDecl;
1067}
1068
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001069/// ActOnPropertyImplDecl - This routine performs semantic checks and
1070/// builds the AST node for a property implementation declaration; declared
1071/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001072///
1073Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1074 SourceLocation PropertyLoc,
1075 bool Synthesize,
1076 DeclTy *ClassCatImpDecl,
1077 IdentifierInfo *PropertyId,
1078 IdentifierInfo *PropertyIvar) {
1079 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1080 // Make sure we have a context for the property implementation declaration.
1081 if (!ClassImpDecl) {
1082 Diag(AtLoc, diag::error_missing_property_context);
1083 return 0;
1084 }
1085 ObjCPropertyDecl *property = 0;
1086 ObjCInterfaceDecl* IDecl = 0;
1087 // Find the class or category class where this property must have
1088 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001089 ObjCImplementationDecl *IC = 0;
1090 ObjCCategoryImplDecl* CatImplClass = 0;
1091 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001092 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001093 // We always synthesize an interface for an implementation
1094 // without an interface decl. So, IDecl is always non-zero.
1095 assert(IDecl &&
1096 "ActOnPropertyImplDecl - @implementation without @interface");
1097
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001098 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001099 property = IDecl->FindPropertyDeclaration(PropertyId);
1100 if (!property) {
1101 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001102 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001103 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001104 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001105 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001106 if (Synthesize) {
1107 Diag(AtLoc, diag::error_synthesize_category_decl);
1108 return 0;
1109 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001110 IDecl = CatImplClass->getClassInterface();
1111 if (!IDecl) {
1112 Diag(AtLoc, diag::error_missing_property_interface);
1113 return 0;
1114 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001115 ObjCCategoryDecl *Category =
1116 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1117
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001118 // If category for this implementation not found, it is an error which
1119 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001120 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001121 return 0;
1122 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001123 property = Category->FindPropertyDeclaration(PropertyId);
1124 if (!property) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001125 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001126 Category->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001127 return 0;
1128 }
1129 }
1130 else {
1131 Diag(AtLoc, diag::error_bad_property_context);
1132 return 0;
1133 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001134 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001135 // Check that we have a valid, previously declared ivar for @synthesize
1136 if (Synthesize) {
1137 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001138 if (!PropertyIvar)
1139 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001140 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001141 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001142 if (!Ivar) {
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001143 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1144 PropertyId->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001145 return 0;
1146 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001147 // Check that type of property and its ivar match.
1148 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1149 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1150 Ivar->getName());
1151 return 0;
1152 }
1153
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001154 } else if (PropertyIvar) {
1155 // @dynamic
1156 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1157 return 0;
1158 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001159 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001160 ObjCPropertyImplDecl *PIDecl =
1161 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1162 (Synthesize ?
1163 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1164 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1165 Ivar);
1166 if (IC)
1167 IC->addPropertyImplementation(PIDecl);
1168 else
1169 CatImplClass->addPropertyImplementation(PIDecl);
1170
1171 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001172}