blob: 28768fc56b26d8f5d4aa49c3c73e95101938a866 [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"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremeneka526c5c2008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-07-25 17:57:26 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000031
32 // Allow the rest of sema to find private method decl implementations.
33 if (MDecl->isInstance())
34 AddInstanceMethodToGlobalPool(MDecl);
35 else
36 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerb048c982008-04-06 04:47:34 +000039 PushDeclContext(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000040
41 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
43 struct DeclaratorChunk::ParamInfo PI;
44
45 // Insert the invisible arguments, self and _cmd!
46 PI.Ident = &Context.Idents.get("self");
47 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Steve Naroffe2af8b12008-06-05 14:49:39 +000048 QualType selfTy;
Chris Lattner4d391482007-12-12 07:09:47 +000049 if (MDecl->isInstance()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +000050 selfTy = Context.getObjCIdType();
Gabor Greif30037532008-02-29 20:35:55 +000051 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
52 // There may be no interface context due to error in declaration of the
53 // interface (which has been reported). Recover gracefully
Chris Lattner04421082008-04-08 04:40:51 +000054 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000055 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000056 }
Steve Naroffe2af8b12008-06-05 14:49:39 +000057 } else // we have a factory method.
58 selfTy = Context.getObjCClassType();
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000059 getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner41110242008-06-17 18:05:57 +000060 PI.Ident, PI.IdentLoc, selfTy));
Chris Lattner4d391482007-12-12 07:09:47 +000061
62 PI.Ident = &Context.Idents.get("_cmd");
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000063 getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner41110242008-06-17 18:05:57 +000064 PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
Chris Lattner04421082008-04-08 04:40:51 +000065
Chris Lattner8123a952008-04-10 02:22:51 +000066 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000067 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000068 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000069 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000070 if (II)
71 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000072 }
73}
74
Chris Lattner7caeabd2008-07-21 22:17:28 +000075Sema::DeclTy *Sema::
76ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
77 IdentifierInfo *ClassName, SourceLocation ClassLoc,
78 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000079 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000080 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000081 assert(ClassName && "Missing class identifier");
82
83 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000084 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000085 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +000086 Diag(ClassLoc, diag::err_redefinition_different_kind,
87 ClassName->getName());
88 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
89 }
90
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000092 if (IDecl) {
93 // Class already seen. Is it a forward declaration?
94 if (!IDecl->isForwardDecl())
95 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
96 else {
97 IDecl->setLocation(AtInterfaceLoc);
98 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000099 }
Chris Lattnerb752f282008-07-21 07:06:49 +0000100 } else {
101 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000102 ClassName, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000103
Steve Naroff31102512008-04-02 18:30:49 +0000104 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000105 // Remember that this needs to be removed when the scope is popped.
106 TUScope->AddDecl(IDecl);
107 }
108
109 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000111 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000112 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000114 Diag(SuperLoc, diag::err_redefinition_different_kind,
115 SuperName->getName());
116 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
117 }
118 else {
119 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000120 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000121
122 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattnerb752f282008-07-21 07:06:49 +0000123 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner4d391482007-12-12 07:09:47 +0000124 SuperClassEntry ? SuperClassEntry->getName()
125 : SuperName->getName(),
Chris Lattnerb752f282008-07-21 07:06:49 +0000126 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner4d391482007-12-12 07:09:47 +0000127 }
128 }
129 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000130 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000131 IDecl->setLocEnd(SuperLoc);
132 } else { // we have a root class.
133 IDecl->setLocEnd(ClassLoc);
134 }
135
136 /// Check then save referenced protocols
Chris Lattner06036d32008-07-26 04:13:19 +0000137 if (NumProtoRefs) {
138 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000139 IDecl->setLocEnd(EndProtoLoc);
140 }
141 return IDecl;
142}
143
144/// ActOnCompatiblityAlias - this action is called after complete parsing of
145/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000146Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
147 IdentifierInfo *AliasName,
148 SourceLocation AliasLocation,
149 IdentifierInfo *ClassName,
150 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000151 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000152 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000153 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000154 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000155 Diag(AliasLocation, diag::warn_previous_alias_decl);
156 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
157 }
158 else {
159 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
160 AliasName->getName());
161 Diag(ADecl->getLocation(), diag::err_previous_declaration);
162 }
163 return 0;
164 }
165 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000166 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000167 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
168 if (CDecl == 0) {
169 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
170 if (CDeclU)
171 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000172 return 0;
173 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000174
175 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000176 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000177 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
178
179 ObjCAliasDecls[AliasName] = AliasDecl;
180 TUScope->AddDecl(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000181 return AliasDecl;
182}
183
Chris Lattnere13b9592008-07-26 04:03:38 +0000184Sema::DeclTy *
185Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
186 IdentifierInfo *ProtocolName,
187 SourceLocation ProtocolLoc,
188 DeclTy * const *ProtoRefs,
189 unsigned NumProtoRefs,
190 SourceLocation EndProtoLoc) {
Chris Lattner4d391482007-12-12 07:09:47 +0000191 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000193 if (PDecl) {
194 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000195 if (!PDecl->isForwardDecl()) {
Chris Lattner4d391482007-12-12 07:09:47 +0000196 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
197 ProtocolName->getName());
Chris Lattner439e71f2008-03-16 01:25:17 +0000198 // Just return the protocol we already had.
199 // FIXME: don't leak the objects passed in!
200 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000201 }
Steve Narofff11b5082008-08-13 16:39:22 +0000202 // Make sure the cached decl gets a valid start location.
203 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000204 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000205 } else {
Chris Lattner780f3292008-07-21 21:32:27 +0000206 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattnerc8581052008-03-16 20:19:15 +0000207 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000208 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000209 }
Chris Lattner4d391482007-12-12 07:09:47 +0000210
211 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000212 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000213 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000214 PDecl->setLocEnd(EndProtoLoc);
215 }
216 return PDecl;
217}
218
219/// FindProtocolDeclaration - This routine looks up protocols and
220/// issuer error if they are not declared. It returns list of protocol
221/// declarations in its 'Protocols' argument.
222void
Chris Lattnere13b9592008-07-26 04:03:38 +0000223Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000224 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000225 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000226 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000227 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000228 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
229 if (!PDecl) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000230 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
231 ProtocolId[i].first->getName());
Chris Lattnereacc3922008-07-26 03:47:43 +0000232 continue;
233 }
234
235 // If this is a forward declaration and we are supposed to warn in this
236 // case, do it.
237 if (WarnOnDeclarations && PDecl->isForwardDecl())
238 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
239 ProtocolId[i].first->getName());
240 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000241 }
242}
243
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000244/// DiagnosePropertyMismatch - Compares two properties for their
245/// attributes and types and warns on a variety of inconsistancies.
246///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000247void
248Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
249 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000250 const char *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000251 ObjCPropertyDecl::PropertyAttributeKind CAttr =
252 Property->getPropertyAttributes();
253 ObjCPropertyDecl::PropertyAttributeKind SAttr =
254 SuperProperty->getPropertyAttributes();
255 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
256 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000257 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000258 Property->getName(), inheritedName);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000259 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
260 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000261 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000262 Property->getName(), "copy", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000263 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000264 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
265 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000266 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000267 Property->getName(), "retain", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000268 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000269
270 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000272 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000273 Property->getName(), "atomic", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000274 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000275 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000276 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000277 Property->getName(), "setter", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000278 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000279 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000280 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000281 Property->getName(), "getter", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000282 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000283
Chris Lattner717250a2008-07-26 20:50:02 +0000284 if (Context.getCanonicalType(Property->getType()) !=
285 Context.getCanonicalType(SuperProperty->getType()))
Fariborz Jahanian34350962008-05-01 18:05:01 +0000286 Diag(Property->getLocation(), diag::warn_property_type,
287 Property->getType().getAsString(),
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000288 inheritedName);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000289
290}
291
292/// ComparePropertiesInBaseAndSuper - This routine compares property
293/// declarations in base and its super class, if any, and issues
294/// diagnostics in a variety of inconsistant situations.
295///
296void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000297Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000298 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
299 if (!SDecl)
300 return;
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000301 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
302 E = SDecl->classprop_end(); S != E; ++S) {
303 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000304 // Does property in super class has declaration in current class?
305 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
306 E = IDecl->classprop_end(); I != E; ++I) {
307 ObjCPropertyDecl *PDecl = (*I);
308 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000309 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000310 }
311 }
312}
313
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000314/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
315/// of properties declared in a protocol and adds them to the list
316/// of properties for current class if it is not there already.
317void
318Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
319 ObjCProtocolDecl *PDecl)
320{
321 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
322 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
323 E = PDecl->classprop_end(); P != E; ++P) {
324 ObjCPropertyDecl *Pr = (*P);
325 ObjCInterfaceDecl::classprop_iterator CP, CE;
326 // Is this property already in class's list of properties?
327 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
328 CP != CE; ++CP)
329 if ((*CP)->getIdentifier() == Pr->getIdentifier())
330 break;
331 if (CP == CE)
332 // Add this property to list of properties for thie class.
333 mergeProperties.push_back(Pr);
334 else
335 // Property protocol already exist in class. Diagnose any mismatch.
336 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
337 }
338 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
339}
340
341/// MergeProtocolPropertiesIntoClass - This routine merges properties
342/// declared in 'MergeItsProtocols' objects (which can be a class or an
343/// inherited protocol into the list of properties for class 'IDecl'
344///
345
346void
347Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
348 DeclTy *MergeItsProtocols) {
349 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattnerb752f282008-07-21 07:06:49 +0000350 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000351 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
352 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000353 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000354 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
355
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000356 // Go thru the list of protocols for this class and recursively merge
357 // their properties into this class as well.
358 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
359 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000360 MergeProtocolPropertiesIntoClass(IDecl, *P);
361 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000362 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
363 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
364 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000365 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000366 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000367}
368
Chris Lattner4d391482007-12-12 07:09:47 +0000369/// ActOnForwardProtocolDeclaration -
370Action::DeclTy *
371Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000372 const IdentifierLocPair *IdentList,
373 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000374 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000375
376 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000377 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000378 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000379 if (PDecl == 0) // Not already seen?
380 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000381
382 Protocols.push_back(PDecl);
383 }
Chris Lattner61f9d412008-03-16 20:34:23 +0000384 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
385 &Protocols[0], Protocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000386}
387
Chris Lattner7caeabd2008-07-21 22:17:28 +0000388Sema::DeclTy *Sema::
389ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
390 IdentifierInfo *ClassName, SourceLocation ClassLoc,
391 IdentifierInfo *CategoryName,
392 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000393 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000394 unsigned NumProtoRefs,
395 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000397
Chris Lattner61f9d412008-03-16 20:34:23 +0000398 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000399 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000400 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000401
402 /// Check that class of this category is already completely declared.
403 if (!IDecl || IDecl->isForwardDecl())
404 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffd100c802008-06-05 15:03:27 +0000405 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000406 /// Check for duplicate interface declaration for this category
407 ObjCCategoryDecl *CDeclChain;
408 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
409 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000410 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff74199b62008-06-05 04:33:44 +0000411 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000412 CategoryName->getName());
413 break;
414 }
Chris Lattner4d391482007-12-12 07:09:47 +0000415 }
Steve Naroffd100c802008-06-05 15:03:27 +0000416 if (!CDeclChain)
417 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000418 }
Chris Lattner4d391482007-12-12 07:09:47 +0000419
420 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000421 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
422 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000423 }
424 return CDecl;
425}
426
427/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000428/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000429/// object.
430Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
431 SourceLocation AtCatImplLoc,
432 IdentifierInfo *ClassName, SourceLocation ClassLoc,
433 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000434 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000435 ObjCCategoryImplDecl *CDecl =
436 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000437 /// Check that class of this category is already completely declared.
438 if (!IDecl || IDecl->isForwardDecl())
439 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
440
441 /// TODO: Check that CatName, category name, is not used in another
442 // implementation.
443 return CDecl;
444}
445
446Sema::DeclTy *Sema::ActOnStartClassImplementation(
447 SourceLocation AtClassImplLoc,
448 IdentifierInfo *ClassName, SourceLocation ClassLoc,
449 IdentifierInfo *SuperClassname,
450 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000451 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000452 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000453 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000454 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000455 Diag(ClassLoc, diag::err_redefinition_different_kind,
456 ClassName->getName());
457 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
458 }
459 else {
460 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000461 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000462 if (!IDecl)
463 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
464 }
465
466 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000467 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000468 if (SuperClassname) {
469 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000470 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000471 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000472 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
473 SuperClassname->getName());
474 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
475 }
476 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000477 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000478 if (!SDecl)
479 Diag(SuperClassLoc, diag::err_undef_superclass,
480 SuperClassname->getName(), ClassName->getName());
481 else if (IDecl && IDecl->getSuperClass() != SDecl) {
482 // This implementation and its interface do not have the same
483 // super class.
484 Diag(SuperClassLoc, diag::err_conflicting_super_class,
485 SDecl->getName());
486 Diag(SDecl->getLocation(), diag::err_previous_definition);
487 }
488 }
489 }
490
491 if (!IDecl) {
492 // Legacy case of @implementation with no corresponding @interface.
493 // Build, chain & install the interface decl into the identifier.
Chris Lattnerb752f282008-07-21 07:06:49 +0000494 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000495 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000496 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000497 IDecl->setSuperClass(SDecl);
498 IDecl->setLocEnd(ClassLoc);
499
500 // Remember that this needs to be removed when the scope is popped.
501 TUScope->AddDecl(IDecl);
502 }
503
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000504 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000505 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
506 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000507
508 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000509 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000510 // FIXME: Don't leak everything!
Chris Lattner4d391482007-12-12 07:09:47 +0000511 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
512 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000513 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000514 return IMPDecl;
515}
516
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000517void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
518 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000519 SourceLocation RBrace) {
520 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000521 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000522 if (!IDecl)
523 return;
524 /// Check case of non-existing @interface decl.
525 /// (legacy objective-c @implementation decl without an @interface decl).
526 /// Add implementations's ivar to the synthesize class's ivar list.
527 if (IDecl->ImplicitInterfaceDecl()) {
528 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
529 return;
530 }
531 // If implementation has empty ivar list, just return.
532 if (numIvars == 0)
533 return;
534
535 assert(ivars && "missing @implementation ivars");
536
537 // Check interface's Ivar list against those in the implementation.
538 // names and types must match.
539 //
Chris Lattner4d391482007-12-12 07:09:47 +0000540 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000541 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000542 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
543 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000544 ObjCIvarDecl* ImplIvar = ivars[j++];
545 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000546 assert (ImplIvar && "missing implementation ivar");
547 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000548 if (Context.getCanonicalType(ImplIvar->getType()) !=
549 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner4d391482007-12-12 07:09:47 +0000550 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
551 ImplIvar->getIdentifier()->getName());
552 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
553 ClsIvar->getIdentifier()->getName());
554 }
555 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
556 // as error.
557 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
558 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
559 ImplIvar->getIdentifier()->getName());
560 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
561 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000562 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000563 }
564 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000565 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000566
567 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000568 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000569 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000570 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000571}
572
Steve Naroff3c2eb662008-02-10 21:38:56 +0000573void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
574 bool &IncompleteImpl) {
575 if (!IncompleteImpl) {
576 Diag(ImpLoc, diag::warn_incomplete_impl);
577 IncompleteImpl = true;
578 }
579 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
580}
581
Steve Naroffefe7f362008-02-08 22:06:17 +0000582/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000583/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000584void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
585 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000586 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000587 const llvm::DenseSet<Selector> &InsMap,
588 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000589 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000590 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000591 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000593 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000594 method->getImplementationControl() != ObjCMethodDecl::Optional)
595 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000596 }
597 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000598 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000599 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000600 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000601 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000602 method->getImplementationControl() != ObjCMethodDecl::Optional)
603 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000604 }
Chris Lattner780f3292008-07-21 21:32:27 +0000605 // Check on this protocols's referenced protocols, recursively.
606 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
607 E = PDecl->protocol_end(); PI != E; ++PI)
608 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000609}
610
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
612 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000613 llvm::DenseSet<Selector> InsMap;
614 // Check and see if instance methods in class interface have been
615 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000616 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000617 E = IMPDecl->instmeth_end(); I != E; ++I)
618 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000619
620 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000622 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000623 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000624 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000625
Chris Lattner4d391482007-12-12 07:09:47 +0000626 llvm::DenseSet<Selector> ClsMap;
627 // Check and see if class methods in class interface have been
628 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000629 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000630 E = IMPDecl->classmeth_end(); I != E; ++I)
631 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000632
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000633 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000634 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000635 if (!ClsMap.count((*I)->getSelector()))
636 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000637
638 // Check the protocol list for unimplemented methods in the @implementation
639 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000640 const ObjCList<ObjCProtocolDecl> &Protocols =
641 IDecl->getReferencedProtocols();
642 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
643 E = Protocols.end(); I != E; ++I)
644 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffefe7f362008-02-08 22:06:17 +0000645 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000646}
647
648/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
649/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
651 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000652 llvm::DenseSet<Selector> InsMap;
653 // Check and see if instance methods in category interface have been
654 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000655 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000656 E = CatImplDecl->instmeth_end(); I != E; ++I)
657 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000658
659 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000660 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000661 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000662 if (!InsMap.count((*I)->getSelector()))
663 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
664
Chris Lattner4d391482007-12-12 07:09:47 +0000665 llvm::DenseSet<Selector> ClsMap;
666 // Check and see if class methods in category interface have been
667 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000668 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000669 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
670 I != E; ++I)
671 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000672
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000673 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000674 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000675 if (!ClsMap.count((*I)->getSelector()))
676 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000677
678 // Check the protocol list for unimplemented methods in the @implementation
679 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000680 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
681 E = CatClassDecl->protocol_end(); PI != E; ++PI)
682 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000683 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000684}
685
686/// ActOnForwardClassDeclaration -
687Action::DeclTy *
688Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
689 IdentifierInfo **IdentList, unsigned NumElts)
690{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000691 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000692
693 for (unsigned i = 0; i != NumElts; ++i) {
694 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000695 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000697 // GCC apparently allows the following idiom:
698 //
699 // typedef NSObject < XCElementTogglerP > XCElementToggler;
700 // @class XCElementToggler;
701 //
702 // FIXME: Make an extension?
703 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
704 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
705 Diag(AtClassLoc, diag::err_redefinition_different_kind,
706 IdentList[i]->getName());
707 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
708 }
Chris Lattner4d391482007-12-12 07:09:47 +0000709 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000711 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000712 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000713 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000714 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000715
716 // Remember that this needs to be removed when the scope is popped.
717 TUScope->AddDecl(IDecl);
718 }
719
720 Interfaces.push_back(IDecl);
721 }
722
Chris Lattner61f9d412008-03-16 20:34:23 +0000723 return ObjCClassDecl::Create(Context, AtClassLoc,
724 &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000725}
726
727
728/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
729/// returns true, or false, accordingly.
730/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
732 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000733 if (Context.getCanonicalType(Method->getResultType()) !=
734 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000735 return false;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000736 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +0000737 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
738 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000739 if (Context.getCanonicalType(ParamDecl->getType()) !=
740 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000741 return false;
742 }
743 return true;
744}
745
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000746void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
747 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000748 if (!FirstMethod.Method) {
749 // Haven't seen a method with this selector name yet - add it.
750 FirstMethod.Method = Method;
751 FirstMethod.Next = 0;
752 } else {
753 // We've seen a method with this name, now check the type signature(s).
754 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
755
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000756 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000757 Next = Next->Next)
758 match = MatchTwoMethodDeclarations(Method, Next->Method);
759
760 if (!match) {
761 // We have a new signature for an existing method - add it.
762 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000764 FirstMethod.Next = OMI;
765 }
766 }
767}
768
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000769void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
770 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000771 if (!FirstMethod.Method) {
772 // Haven't seen a method with this selector name yet - add it.
773 FirstMethod.Method = Method;
774 FirstMethod.Next = 0;
775 } else {
776 // We've seen a method with this name, now check the type signature(s).
777 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
778
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000779 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000780 Next = Next->Next)
781 match = MatchTwoMethodDeclarations(Method, Next->Method);
782
783 if (!match) {
784 // We have a new signature for an existing method - add it.
785 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000786 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000787 FirstMethod.Next = OMI;
788 }
789 }
790}
791
Steve Naroffa56f6162007-12-18 01:30:32 +0000792// Note: For class/category implemenations, allMethods/allProperties is
793// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000794void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
795 DeclTy **allMethods, unsigned allNum,
796 DeclTy **allProperties, unsigned pNum) {
797 Decl *ClassDecl = static_cast<Decl *>(classDecl);
798
Steve Naroffa56f6162007-12-18 01:30:32 +0000799 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
800 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000801 // should be true.
802 if (!ClassDecl)
803 return;
804
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000805 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
806 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000807
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
809 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000810
811 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000812 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
813 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000815
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000816 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +0000817 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
818 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000819 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
820 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
821 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
822 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000823 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000824 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000825 }
Chris Lattner4d391482007-12-12 07:09:47 +0000826
827 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 ObjCMethodDecl *Method =
829 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000830
831 if (!Method) continue; // Already issued a diagnostic.
832 if (Method->isInstance()) {
833 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000834 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000835 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
836 : false;
837 if (isInterfaceDeclKind && PrevMethod && !match
838 || checkIdenticalMethods && match) {
839 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
840 Method->getSelector().getName());
841 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
842 } else {
843 insMethods.push_back(Method);
844 InsMap[Method->getSelector()] = Method;
845 /// The following allows us to typecheck messages to "id".
846 AddInstanceMethodToGlobalPool(Method);
847 }
848 }
849 else {
850 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000852 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
853 : false;
854 if (isInterfaceDeclKind && PrevMethod && !match
855 || checkIdenticalMethods && match) {
856 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
857 Method->getSelector().getName());
858 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
859 } else {
860 clsMethods.push_back(Method);
861 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000862 /// The following allows us to typecheck messages to "Class".
863 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000864 }
865 }
866 }
867
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000868 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000869 // Compares properties declaraed in this class to those of its
870 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000871 ComparePropertiesInBaseAndSuper(I);
872 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000873 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
874 E = I->classprop_end(); P != E; ++P) {
Steve Naroff8442b5c2008-05-22 23:24:08 +0000875 // FIXME: It would be really nice if we could avoid this. Injecting
876 // methods into the interface makes it hard to distinguish "real" methods
877 // from synthesized "property" methods (that aren't in the source).
878 // This complicicates the rewriter's life.
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000879 I->addPropertyMethods(Context, *P, insMethods);
880 }
881 I->addMethods(&insMethods[0], insMethods.size(),
882 &clsMethods[0], clsMethods.size(), AtEndLoc);
883
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000884 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000885 P->addMethods(&insMethods[0], insMethods.size(),
886 &clsMethods[0], clsMethods.size(), AtEndLoc);
887 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000888 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000889 C->addMethods(&insMethods[0], insMethods.size(),
890 &clsMethods[0], clsMethods.size(), AtEndLoc);
891 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000892 else if (ObjCImplementationDecl *IC =
893 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000894 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000896 ImplMethodsVsClassMethods(IC, IDecl);
897 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000898 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000899 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000900 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000901 // Find category interface decl and then check that all methods declared
902 // in this interface is implemented in the category @implementation.
903 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000905 Categories; Categories = Categories->getNextClassCategory()) {
906 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
907 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
908 break;
909 }
910 }
911 }
912 }
913}
914
915
916/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
917/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918static Decl::ObjCDeclQualifier
919CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
920 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
921 if (PQTVal & ObjCDeclSpec::DQ_In)
922 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
923 if (PQTVal & ObjCDeclSpec::DQ_Inout)
924 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
925 if (PQTVal & ObjCDeclSpec::DQ_Out)
926 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
927 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
928 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
929 if (PQTVal & ObjCDeclSpec::DQ_Byref)
930 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
931 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
932 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000933
934 return ret;
935}
936
937Sema::DeclTy *Sema::ActOnMethodDeclaration(
938 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000939 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000940 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000941 Selector Sel,
942 // optional arguments. The number of types/arguments is obtained
943 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000944 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000945 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
946 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000947 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +0000948
949 // Make sure we can establish a context for the method.
950 if (!ClassDecl) {
951 Diag(MethodLoc, diag::error_missing_method_context);
952 return 0;
953 }
Chris Lattner4d391482007-12-12 07:09:47 +0000954 QualType resultDeclType;
955
956 if (ReturnType)
957 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
958 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000959 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000960
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000961 ObjCMethodDecl* ObjCMethod =
962 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +0000963 ClassDecl, AttrList,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000964 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +0000965 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000966 MethodDeclKind == tok::objc_optional ?
967 ObjCMethodDecl::Optional :
968 ObjCMethodDecl::Required);
969
Chris Lattner0ed844b2008-04-04 06:12:32 +0000970 llvm::SmallVector<ParmVarDecl*, 16> Params;
971
972 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
973 // FIXME: arg->AttrList must be stored too!
974 QualType argType;
975
976 if (ArgTypes[i])
977 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
978 else
979 argType = Context.getObjCIdType();
980 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
981 SourceLocation(/*FIXME*/),
982 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +0000983 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000984 Param->setObjCDeclQualifier(
985 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
986 Params.push_back(Param);
987 }
988
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
990 ObjCMethod->setObjCDeclQualifier(
991 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
992 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000993
994 // For implementations (which can be very "coarse grain"), we add the
995 // method now. This allows the AST to implement lookup methods that work
996 // incrementally (without waiting until we parse the @end). It also allows
997 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000999 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001000 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001001 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001002 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001003 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001004 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001006 }
1007 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001009 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001010 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001011 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001012 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001013 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001014 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001016 }
1017 }
1018 if (PrevMethod) {
1019 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001020 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1021 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +00001022 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1023 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001024 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001025}
1026
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001027Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1028 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001029 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001030 Selector GetterSel,
1031 Selector SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001032 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001033 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001034 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1035 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001036 // Regardless of setter/getter attribute, we save the default getter/setter
1037 // selector names in anticipation of declaration of setter/getter methods.
1038 PDecl->setGetterName(GetterSel);
1039 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001040
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001041 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001042 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001043
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001044 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001045 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001046
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001047 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001048 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001049
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001050 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +00001052
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001053 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001055
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001056 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001057 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001058
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001059 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001060 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001061
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001062 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001063 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001064
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001065 if (MethodImplKind == tok::objc_required)
1066 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1067 else if (MethodImplKind == tok::objc_optional)
1068 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1069
Chris Lattner4d391482007-12-12 07:09:47 +00001070 return PDecl;
1071}
1072
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001073/// ActOnPropertyImplDecl - This routine performs semantic checks and
1074/// builds the AST node for a property implementation declaration; declared
1075/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001076///
1077Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1078 SourceLocation PropertyLoc,
1079 bool Synthesize,
1080 DeclTy *ClassCatImpDecl,
1081 IdentifierInfo *PropertyId,
1082 IdentifierInfo *PropertyIvar) {
1083 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1084 // Make sure we have a context for the property implementation declaration.
1085 if (!ClassImpDecl) {
1086 Diag(AtLoc, diag::error_missing_property_context);
1087 return 0;
1088 }
1089 ObjCPropertyDecl *property = 0;
1090 ObjCInterfaceDecl* IDecl = 0;
1091 // Find the class or category class where this property must have
1092 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001093 ObjCImplementationDecl *IC = 0;
1094 ObjCCategoryImplDecl* CatImplClass = 0;
1095 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001096 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001097 // We always synthesize an interface for an implementation
1098 // without an interface decl. So, IDecl is always non-zero.
1099 assert(IDecl &&
1100 "ActOnPropertyImplDecl - @implementation without @interface");
1101
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001102 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001103 property = IDecl->FindPropertyDeclaration(PropertyId);
1104 if (!property) {
1105 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001106 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001107 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001108 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001109 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001110 if (Synthesize) {
1111 Diag(AtLoc, diag::error_synthesize_category_decl);
1112 return 0;
1113 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001114 IDecl = CatImplClass->getClassInterface();
1115 if (!IDecl) {
1116 Diag(AtLoc, diag::error_missing_property_interface);
1117 return 0;
1118 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001119 ObjCCategoryDecl *Category =
1120 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1121
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001122 // If category for this implementation not found, it is an error which
1123 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001124 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001125 return 0;
1126 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001127 property = Category->FindPropertyDeclaration(PropertyId);
1128 if (!property) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001129 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001130 Category->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001131 return 0;
1132 }
1133 }
1134 else {
1135 Diag(AtLoc, diag::error_bad_property_context);
1136 return 0;
1137 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001138 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001139 // Check that we have a valid, previously declared ivar for @synthesize
1140 if (Synthesize) {
1141 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001142 if (!PropertyIvar)
1143 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001144 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001145 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001146 if (!Ivar) {
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001147 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1148 PropertyId->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001149 return 0;
1150 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001151 // Check that type of property and its ivar match.
Chris Lattner717250a2008-07-26 20:50:02 +00001152 if (Context.getCanonicalType(Ivar->getType()) !=
1153 Context.getCanonicalType(property->getType())) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001154 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1155 Ivar->getName());
1156 return 0;
1157 }
1158
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001159 } else if (PropertyIvar) {
1160 // @dynamic
1161 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1162 return 0;
1163 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001164 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001165 ObjCPropertyImplDecl *PIDecl =
1166 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1167 (Synthesize ?
1168 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1169 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1170 Ivar);
1171 if (IC)
1172 IC->addPropertyImplementation(PIDecl);
1173 else
1174 CatImplClass->addPropertyImplementation(PIDecl);
1175
1176 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001177}