blob: da7b1513c85afdff81f50c3be3ee1d8e7e0e54b2 [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) {
Chris Lattner4d391482007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner4d391482007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Naroffa56f6162007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000036 PushDeclContext(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000037
38 // Create Decl objects for each parameter, entrring them in the scope for
39 // binding to their use.
40 struct DeclaratorChunk::ParamInfo PI;
41
42 // Insert the invisible arguments, self and _cmd!
43 PI.Ident = &Context.Idents.get("self");
44 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Chris Lattner04421082008-04-08 04:40:51 +000045 QualType selfTy = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +000046 if (MDecl->isInstance()) {
Gabor Greif30037532008-02-29 20:35:55 +000047 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
48 // There may be no interface context due to error in declaration of the
49 // interface (which has been reported). Recover gracefully
Chris Lattner04421082008-04-08 04:40:51 +000050 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000051 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000052 }
Gabor Greif30037532008-02-29 20:35:55 +000053 }
Chris Lattner04421082008-04-08 04:40:51 +000054 CurMethodDecl->setSelfDecl(CreateImplicitParameter(FnBodyScope, PI.Ident,
55 PI.IdentLoc, selfTy));
Chris Lattner4d391482007-12-12 07:09:47 +000056
57 PI.Ident = &Context.Idents.get("_cmd");
Chris Lattner04421082008-04-08 04:40:51 +000058 CreateImplicitParameter(FnBodyScope, PI.Ident, PI.IdentLoc,
59 Context.getObjCSelType());
60
Chris Lattner8123a952008-04-10 02:22:51 +000061 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000062 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000063 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000064 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000065 if (II)
66 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000067 }
68}
69
70Sema::DeclTy *Sema::ActOnStartClassInterface(
71 SourceLocation AtInterfaceLoc,
72 IdentifierInfo *ClassName, SourceLocation ClassLoc,
73 IdentifierInfo *SuperName, SourceLocation SuperLoc,
74 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
75 SourceLocation EndProtoLoc, AttributeList *AttrList) {
76 assert(ClassName && "Missing class identifier");
77
78 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000079 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +000081 Diag(ClassLoc, diag::err_redefinition_different_kind,
82 ClassName->getName());
83 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
84 }
85
Ted Kremeneka526c5c2008-01-07 19:49:32 +000086 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000087 if (IDecl) {
88 // Class already seen. Is it a forward declaration?
89 if (!IDecl->isForwardDecl())
90 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
91 else {
92 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
94 IDecl->AllocIntfRefProtocols(NumProtocols);
95 }
96 }
97 else {
Chris Lattner0e77ba02008-03-16 01:15:50 +000098 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000099 ClassName, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000100
Steve Naroff31102512008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
103 TUScope->AddDecl(IDecl);
104 }
105
106 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000107 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000109 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000111 Diag(SuperLoc, diag::err_redefinition_different_kind,
112 SuperName->getName());
113 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
114 }
115 else {
116 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000117 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000118
119 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
120 Diag(AtInterfaceLoc, diag::err_undef_superclass,
121 SuperClassEntry ? SuperClassEntry->getName()
122 : SuperName->getName(),
123 ClassName->getName());
124 }
125 }
126 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000127 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000128 IDecl->setLocEnd(SuperLoc);
129 } else { // we have a root class.
130 IDecl->setLocEnd(ClassLoc);
131 }
132
133 /// Check then save referenced protocols
134 if (NumProtocols) {
135 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000136 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000137 if (!RefPDecl || RefPDecl->isForwardDecl())
138 Diag(ClassLoc, diag::warn_undef_protocolref,
139 ProtocolNames[i]->getName(),
140 ClassName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000141 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000142 }
143 IDecl->setLocEnd(EndProtoLoc);
144 }
145 return IDecl;
146}
147
148/// ActOnCompatiblityAlias - this action is called after complete parsing of
149/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000150Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
151 IdentifierInfo *AliasName,
152 SourceLocation AliasLocation,
153 IdentifierInfo *ClassName,
154 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000155 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000156 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000157 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000158 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000159 Diag(AliasLocation, diag::warn_previous_alias_decl);
160 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
161 }
162 else {
163 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
164 AliasName->getName());
165 Diag(ADecl->getLocation(), diag::err_previous_declaration);
166 }
167 return 0;
168 }
169 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000170 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000171 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
172 if (CDecl == 0) {
173 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
174 if (CDeclU)
175 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000176 return 0;
177 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000178
179 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000180 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000181 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
182
183 ObjCAliasDecls[AliasName] = AliasDecl;
184 TUScope->AddDecl(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000185 return AliasDecl;
186}
187
188Sema::DeclTy *Sema::ActOnStartProtocolInterface(
189 SourceLocation AtProtoInterfaceLoc,
190 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
191 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
192 SourceLocation EndProtoLoc) {
193 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000195 if (PDecl) {
196 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000197 if (!PDecl->isForwardDecl()) {
Chris Lattner4d391482007-12-12 07:09:47 +0000198 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
199 ProtocolName->getName());
Chris Lattner439e71f2008-03-16 01:25:17 +0000200 // Just return the protocol we already had.
201 // FIXME: don't leak the objects passed in!
202 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000203 }
Chris Lattner439e71f2008-03-16 01:25:17 +0000204
205 PDecl->setForwardDecl(false);
206 PDecl->AllocReferencedProtocols(NumProtoRefs);
207 } else {
Chris Lattnercca59d72008-03-16 01:23:04 +0000208 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattnerc8581052008-03-16 20:19:15 +0000209 ProtocolName);
210 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000211 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000212 }
Chris Lattner4d391482007-12-12 07:09:47 +0000213
214 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000215 /// Check then save referenced protocols.
Chris Lattner4d391482007-12-12 07:09:47 +0000216 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000217 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000218 if (!RefPDecl || RefPDecl->isForwardDecl())
219 Diag(ProtocolLoc, diag::warn_undef_protocolref,
220 ProtoRefNames[i]->getName(),
221 ProtocolName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000222 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000223 }
224 PDecl->setLocEnd(EndProtoLoc);
225 }
226 return PDecl;
227}
228
229/// FindProtocolDeclaration - This routine looks up protocols and
230/// issuer error if they are not declared. It returns list of protocol
231/// declarations in its 'Protocols' argument.
232void
233Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
234 IdentifierInfo **ProtocolId,
235 unsigned NumProtocols,
236 llvm::SmallVector<DeclTy *,8> &Protocols) {
237 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000238 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000239 if (!PDecl)
240 Diag(TypeLoc, diag::err_undeclared_protocol,
241 ProtocolId[i]->getName());
242 else
243 Protocols.push_back(PDecl);
244 }
245}
246
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000247/// DiagnosePropertyMismatch - Compares two properties for their
248/// attributes and types and warns on a variety of inconsistancies.
249///
250// TODO: Incomplete.
251//
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000252void
253Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
254 ObjCPropertyDecl *SuperProperty,
255 ObjCInterfaceDecl *SuperIDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000256 ObjCPropertyDecl::PropertyAttributeKind CAttr =
257 Property->getPropertyAttributes();
258 ObjCPropertyDecl::PropertyAttributeKind SAttr =
259 SuperProperty->getPropertyAttributes();
260 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
261 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000262 Diag(Property->getLocation(), diag::warn_readonly_property,
263 Property->getName(), SuperIDecl->getName());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000264 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
265 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000266 Diag(Property->getLocation(), diag::warn_property_attribute,
267 Property->getName(), "copy", SuperIDecl->getName(),
268 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000269 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
270 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000271 Diag(Property->getLocation(), diag::warn_property_attribute,
272 Property->getName(), "retain", SuperIDecl->getName(),
273 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000274
275 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
276 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000277 Diag(Property->getLocation(), diag::warn_property_attribute,
278 Property->getName(), "atomic", SuperIDecl->getName(),
279 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000280 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000281 Diag(Property->getLocation(), diag::warn_property_attribute,
282 Property->getName(), "setter", SuperIDecl->getName(),
283 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000284 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000285 Diag(Property->getLocation(), diag::warn_property_attribute,
286 Property->getName(), "getter", SuperIDecl->getName(),
287 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000288
289 if (Property->getCanonicalType() != SuperProperty->getCanonicalType()) {
290 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
291 && (SAttr & ObjCPropertyDecl::OBJC_PR_readonly))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000292 // && objc_compare_types(...))
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000293 ;
294 else
295 ; //
296 }
297
298}
299
300/// ComparePropertiesInBaseAndSuper - This routine compares property
301/// declarations in base and its super class, if any, and issues
302/// diagnostics in a variety of inconsistant situations.
303///
304void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000305Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000306 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
307 if (!SDecl)
308 return;
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000309 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
310 E = SDecl->classprop_end(); S != E; ++S) {
311 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312 // Does property in super class has declaration in current class?
313 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
314 E = IDecl->classprop_end(); I != E; ++I) {
315 ObjCPropertyDecl *PDecl = (*I);
316 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000317 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000318 }
319 }
320}
321
Chris Lattner4d391482007-12-12 07:09:47 +0000322/// ActOnForwardProtocolDeclaration -
323Action::DeclTy *
324Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
325 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000326 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000327
328 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000329 IdentifierInfo *Ident = IdentList[i];
330 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
331 if (PDecl == 0) { // Not already seen?
Chris Lattner4d391482007-12-12 07:09:47 +0000332 // FIXME: Pass in the location of the identifier!
Chris Lattnerc8581052008-03-16 20:19:15 +0000333 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000334 }
335
336 Protocols.push_back(PDecl);
337 }
Chris Lattner61f9d412008-03-16 20:34:23 +0000338 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
339 &Protocols[0], Protocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000340}
341
342Sema::DeclTy *Sema::ActOnStartCategoryInterface(
343 SourceLocation AtInterfaceLoc,
344 IdentifierInfo *ClassName, SourceLocation ClassLoc,
345 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
346 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
347 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000348 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000349
Chris Lattner61f9d412008-03-16 20:34:23 +0000350 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000351 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000352 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000353
354 /// Check that class of this category is already completely declared.
355 if (!IDecl || IDecl->isForwardDecl())
356 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
357 else {
358 /// Check for duplicate interface declaration for this category
359 ObjCCategoryDecl *CDeclChain;
360 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
361 CDeclChain = CDeclChain->getNextClassCategory()) {
362 if (CDeclChain->getIdentifier() == CategoryName) {
363 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
364 CategoryName->getName());
365 break;
366 }
Chris Lattner4d391482007-12-12 07:09:47 +0000367 }
Chris Lattner4d391482007-12-12 07:09:47 +0000368 if (!CDeclChain)
369 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000370 }
Chris Lattner4d391482007-12-12 07:09:47 +0000371
372 if (NumProtoRefs) {
Chris Lattner68c82cf2008-03-16 20:47:45 +0000373 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
374 /// Check and then save the referenced protocols.
Chris Lattner4d391482007-12-12 07:09:47 +0000375 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000376 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000377 if (!RefPDecl || RefPDecl->isForwardDecl()) {
378 Diag(CategoryLoc, diag::warn_undef_protocolref,
379 ProtoRefNames[i]->getName(),
380 CategoryName->getName());
381 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000382 if (RefPDecl)
383 RefProtocols.push_back(RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000384 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000385 if (!RefProtocols.empty())
386 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000387 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000388 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000389 return CDecl;
390}
391
392/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000393/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000394/// object.
395Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
396 SourceLocation AtCatImplLoc,
397 IdentifierInfo *ClassName, SourceLocation ClassLoc,
398 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000399 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000400 ObjCCategoryImplDecl *CDecl =
401 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000402 /// Check that class of this category is already completely declared.
403 if (!IDecl || IDecl->isForwardDecl())
404 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
405
406 /// TODO: Check that CatName, category name, is not used in another
407 // implementation.
408 return CDecl;
409}
410
411Sema::DeclTy *Sema::ActOnStartClassImplementation(
412 SourceLocation AtClassImplLoc,
413 IdentifierInfo *ClassName, SourceLocation ClassLoc,
414 IdentifierInfo *SuperClassname,
415 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000416 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000417 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000418 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000419 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000420 Diag(ClassLoc, diag::err_redefinition_different_kind,
421 ClassName->getName());
422 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
423 }
424 else {
425 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000426 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000427 if (!IDecl)
428 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
429 }
430
431 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000432 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000433 if (SuperClassname) {
434 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000435 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000436 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000437 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
438 SuperClassname->getName());
439 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
440 }
441 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000442 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000443 if (!SDecl)
444 Diag(SuperClassLoc, diag::err_undef_superclass,
445 SuperClassname->getName(), ClassName->getName());
446 else if (IDecl && IDecl->getSuperClass() != SDecl) {
447 // This implementation and its interface do not have the same
448 // super class.
449 Diag(SuperClassLoc, diag::err_conflicting_super_class,
450 SDecl->getName());
451 Diag(SDecl->getLocation(), diag::err_previous_definition);
452 }
453 }
454 }
455
456 if (!IDecl) {
457 // Legacy case of @implementation with no corresponding @interface.
458 // Build, chain & install the interface decl into the identifier.
Chris Lattner0e77ba02008-03-16 01:15:50 +0000459 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000460 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000461 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000462 IDecl->setSuperClass(SDecl);
463 IDecl->setLocEnd(ClassLoc);
464
465 // Remember that this needs to be removed when the scope is popped.
466 TUScope->AddDecl(IDecl);
467 }
468
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000469 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000470 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
471 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000472
473 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000474 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000475 // FIXME: Don't leak everything!
Chris Lattner4d391482007-12-12 07:09:47 +0000476 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
477 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000478 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000479 return IMPDecl;
480}
481
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000482void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
483 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000484 SourceLocation RBrace) {
485 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000487 if (!IDecl)
488 return;
489 /// Check case of non-existing @interface decl.
490 /// (legacy objective-c @implementation decl without an @interface decl).
491 /// Add implementations's ivar to the synthesize class's ivar list.
492 if (IDecl->ImplicitInterfaceDecl()) {
493 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
494 return;
495 }
496 // If implementation has empty ivar list, just return.
497 if (numIvars == 0)
498 return;
499
500 assert(ivars && "missing @implementation ivars");
501
502 // Check interface's Ivar list against those in the implementation.
503 // names and types must match.
504 //
Chris Lattner4d391482007-12-12 07:09:47 +0000505 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000506 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000507 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
508 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000509 ObjCIvarDecl* ImplIvar = ivars[j++];
510 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000511 assert (ImplIvar && "missing implementation ivar");
512 assert (ClsIvar && "missing class ivar");
513 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
514 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
515 ImplIvar->getIdentifier()->getName());
516 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
517 ClsIvar->getIdentifier()->getName());
518 }
519 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
520 // as error.
521 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
522 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
523 ImplIvar->getIdentifier()->getName());
524 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
525 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000526 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000527 }
528 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000529 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000530
531 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000532 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000533 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000534 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000535}
536
Steve Naroff3c2eb662008-02-10 21:38:56 +0000537void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
538 bool &IncompleteImpl) {
539 if (!IncompleteImpl) {
540 Diag(ImpLoc, diag::warn_incomplete_impl);
541 IncompleteImpl = true;
542 }
543 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
544}
545
Steve Naroffefe7f362008-02-08 22:06:17 +0000546/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000547/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000548void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
549 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000550 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000551 const llvm::DenseSet<Selector> &InsMap,
552 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000553 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000554 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000555 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000556 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000557 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000558 method->getImplementationControl() != ObjCMethodDecl::Optional)
559 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000560 }
561 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000562 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000563 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000565 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000566 method->getImplementationControl() != ObjCMethodDecl::Optional)
567 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000568 }
Chris Lattner4d391482007-12-12 07:09:47 +0000569 // Check on this protocols's referenced protocols, recursively
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000571 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000572 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000573}
574
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
576 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000577 llvm::DenseSet<Selector> InsMap;
578 // Check and see if instance methods in class interface have been
579 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000580 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000581 E = IMPDecl->instmeth_end(); I != E; ++I)
582 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000583
584 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000586 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000587 if (!InsMap.count((*I)->getSelector()))
588 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000589
Chris Lattner4d391482007-12-12 07:09:47 +0000590 llvm::DenseSet<Selector> ClsMap;
591 // Check and see if class methods in class interface have been
592 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000593 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000594 E = IMPDecl->classmeth_end(); I != E; ++I)
595 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000596
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000598 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000599 if (!ClsMap.count((*I)->getSelector()))
600 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000601
602 // Check the protocol list for unimplemented methods in the @implementation
603 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000605 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000606 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
607 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000608}
609
610/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
611/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
613 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000614 llvm::DenseSet<Selector> InsMap;
615 // Check and see if instance methods in category interface have been
616 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000618 E = CatImplDecl->instmeth_end(); I != E; ++I)
619 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000620
621 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000623 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000624 if (!InsMap.count((*I)->getSelector()))
625 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
626
Chris Lattner4d391482007-12-12 07:09:47 +0000627 llvm::DenseSet<Selector> ClsMap;
628 // Check and see if class methods in category interface have been
629 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000631 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
632 I != E; ++I)
633 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000634
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000636 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000637 if (!ClsMap.count((*I)->getSelector()))
638 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000639
640 // Check the protocol list for unimplemented methods in the @implementation
641 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000642 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000643 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000644 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffefe7f362008-02-08 22:06:17 +0000645 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
646 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000647 }
Chris Lattner4d391482007-12-12 07:09:47 +0000648}
649
650/// ActOnForwardClassDeclaration -
651Action::DeclTy *
652Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
653 IdentifierInfo **IdentList, unsigned NumElts)
654{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000655 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000656
657 for (unsigned i = 0; i != NumElts; ++i) {
658 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000659 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000660 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000661 Diag(AtClassLoc, diag::err_redefinition_different_kind,
662 IdentList[i]->getName());
663 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
664 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000665 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000666 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0e77ba02008-03-16 01:15:50 +0000667 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000668 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000669 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000670
671 // Remember that this needs to be removed when the scope is popped.
672 TUScope->AddDecl(IDecl);
673 }
674
675 Interfaces.push_back(IDecl);
676 }
677
Chris Lattner61f9d412008-03-16 20:34:23 +0000678 return ObjCClassDecl::Create(Context, AtClassLoc,
679 &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000680}
681
682
683/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
684/// returns true, or false, accordingly.
685/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000686bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
687 const ObjCMethodDecl *PrevMethod) {
Chris Lattner4d391482007-12-12 07:09:47 +0000688 if (Method->getResultType().getCanonicalType() !=
689 PrevMethod->getResultType().getCanonicalType())
690 return false;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000691 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +0000692 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
693 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000694 if (Context.getCanonicalType(ParamDecl->getType()) !=
695 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000696 return false;
697 }
698 return true;
699}
700
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000701void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
702 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000703 if (!FirstMethod.Method) {
704 // Haven't seen a method with this selector name yet - add it.
705 FirstMethod.Method = Method;
706 FirstMethod.Next = 0;
707 } else {
708 // We've seen a method with this name, now check the type signature(s).
709 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
710
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000711 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000712 Next = Next->Next)
713 match = MatchTwoMethodDeclarations(Method, Next->Method);
714
715 if (!match) {
716 // We have a new signature for an existing method - add it.
717 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000719 FirstMethod.Next = OMI;
720 }
721 }
722}
723
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000724void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
725 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000726 if (!FirstMethod.Method) {
727 // Haven't seen a method with this selector name yet - add it.
728 FirstMethod.Method = Method;
729 FirstMethod.Next = 0;
730 } else {
731 // We've seen a method with this name, now check the type signature(s).
732 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
733
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000735 Next = Next->Next)
736 match = MatchTwoMethodDeclarations(Method, Next->Method);
737
738 if (!match) {
739 // We have a new signature for an existing method - add it.
740 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000742 FirstMethod.Next = OMI;
743 }
744 }
745}
746
Steve Naroffa56f6162007-12-18 01:30:32 +0000747// Note: For class/category implemenations, allMethods/allProperties is
748// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000749void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
750 DeclTy **allMethods, unsigned allNum,
751 DeclTy **allProperties, unsigned pNum) {
752 Decl *ClassDecl = static_cast<Decl *>(classDecl);
753
Steve Naroffa56f6162007-12-18 01:30:32 +0000754 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
755 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000756 // should be true.
757 if (!ClassDecl)
758 return;
759
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
761 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000762
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
764 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000765
766 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000767 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
768 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000769 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000770
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000771 if (pNum != 0)
Chris Lattner55d13b42008-03-16 21:23:50 +0000772 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
773 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000774 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
775 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
776 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
777 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000778 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000779 assert(false && "ActOnAtEnd - property declaration misplaced");
Chris Lattner4d391482007-12-12 07:09:47 +0000780
781 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000782 ObjCMethodDecl *Method =
783 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000784
785 if (!Method) continue; // Already issued a diagnostic.
786 if (Method->isInstance()) {
787 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000788 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000789 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
790 : false;
791 if (isInterfaceDeclKind && PrevMethod && !match
792 || checkIdenticalMethods && match) {
793 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
794 Method->getSelector().getName());
795 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
796 } else {
797 insMethods.push_back(Method);
798 InsMap[Method->getSelector()] = Method;
799 /// The following allows us to typecheck messages to "id".
800 AddInstanceMethodToGlobalPool(Method);
801 }
802 }
803 else {
804 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000805 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000806 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
807 : false;
808 if (isInterfaceDeclKind && PrevMethod && !match
809 || checkIdenticalMethods && match) {
810 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
811 Method->getSelector().getName());
812 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
813 } else {
814 clsMethods.push_back(Method);
815 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000816 /// The following allows us to typecheck messages to "Class".
817 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000818 }
819 }
820 }
821
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000823 I->addMethods(&insMethods[0], insMethods.size(),
824 &clsMethods[0], clsMethods.size(), AtEndLoc);
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000825 // Compares properties declaraed in this class to those of its
826 // super class.
827 ComparePropertiesInBaseAndSuper (I);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000829 P->addMethods(&insMethods[0], insMethods.size(),
830 &clsMethods[0], clsMethods.size(), AtEndLoc);
831 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000832 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000833 C->addMethods(&insMethods[0], insMethods.size(),
834 &clsMethods[0], clsMethods.size(), AtEndLoc);
835 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 else if (ObjCImplementationDecl *IC =
837 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000838 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000839 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000840 ImplMethodsVsClassMethods(IC, IDecl);
841 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000842 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000843 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000844 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000845 // Find category interface decl and then check that all methods declared
846 // in this interface is implemented in the category @implementation.
847 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000848 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000849 Categories; Categories = Categories->getNextClassCategory()) {
850 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
851 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
852 break;
853 }
854 }
855 }
856 }
857}
858
859
860/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
861/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000862static Decl::ObjCDeclQualifier
863CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
864 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
865 if (PQTVal & ObjCDeclSpec::DQ_In)
866 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
867 if (PQTVal & ObjCDeclSpec::DQ_Inout)
868 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
869 if (PQTVal & ObjCDeclSpec::DQ_Out)
870 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
871 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
872 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
873 if (PQTVal & ObjCDeclSpec::DQ_Byref)
874 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
875 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
876 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000877
878 return ret;
879}
880
881Sema::DeclTy *Sema::ActOnMethodDeclaration(
882 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000883 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000884 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000885 Selector Sel,
886 // optional arguments. The number of types/arguments is obtained
887 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000888 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000889 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
890 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000891 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +0000892
893 // Make sure we can establish a context for the method.
894 if (!ClassDecl) {
895 Diag(MethodLoc, diag::error_missing_method_context);
896 return 0;
897 }
Chris Lattner4d391482007-12-12 07:09:47 +0000898 QualType resultDeclType;
899
900 if (ReturnType)
901 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
902 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000903 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000904
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000905 ObjCMethodDecl* ObjCMethod =
906 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +0000907 ClassDecl, AttrList,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000908 MethodType == tok::minus, isVariadic,
909 MethodDeclKind == tok::objc_optional ?
910 ObjCMethodDecl::Optional :
911 ObjCMethodDecl::Required);
912
Chris Lattner0ed844b2008-04-04 06:12:32 +0000913 llvm::SmallVector<ParmVarDecl*, 16> Params;
914
915 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
916 // FIXME: arg->AttrList must be stored too!
917 QualType argType;
918
919 if (ArgTypes[i])
920 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
921 else
922 argType = Context.getObjCIdType();
923 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
924 SourceLocation(/*FIXME*/),
925 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +0000926 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000927 Param->setObjCDeclQualifier(
928 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
929 Params.push_back(Param);
930 }
931
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000932 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
933 ObjCMethod->setObjCDeclQualifier(
934 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
935 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000936
937 // For implementations (which can be very "coarse grain"), we add the
938 // method now. This allows the AST to implement lookup methods that work
939 // incrementally (without waiting until we parse the @end). It also allows
940 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000941 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000942 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000943 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000944 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000945 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000946 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000947 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000949 }
950 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000951 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000952 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000953 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000954 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000955 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000956 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000957 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000958 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000959 }
960 }
961 if (PrevMethod) {
962 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000963 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
964 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000965 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
966 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000967 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +0000968}
969
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000970Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
971 FieldDeclarator &FD,
972 ObjCDeclSpec &ODS) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000973 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000974 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
975 FD.D.getIdentifier(), T);
Chris Lattner4d391482007-12-12 07:09:47 +0000976
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000977 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +0000979
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000980 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000981 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000982 PDecl->setGetterName(ODS.getGetterName());
Chris Lattner4d391482007-12-12 07:09:47 +0000983 }
984
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000985 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000987 PDecl->setSetterName(ODS.getSetterName());
Chris Lattner4d391482007-12-12 07:09:47 +0000988 }
989
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000990 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000991 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +0000992
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000993 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +0000995
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000996 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000997 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +0000998
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000999 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001000 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001001
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001002 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001003 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001004
Chris Lattner4d391482007-12-12 07:09:47 +00001005 return PDecl;
1006}
1007
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001008/// ActOnPropertyImplDecl - This routine performs semantic checks and
1009/// builds the AST node for a property implementation declaration; declared
1010/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001011///
1012Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1013 SourceLocation PropertyLoc,
1014 bool Synthesize,
1015 DeclTy *ClassCatImpDecl,
1016 IdentifierInfo *PropertyId,
1017 IdentifierInfo *PropertyIvar) {
1018 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1019 // Make sure we have a context for the property implementation declaration.
1020 if (!ClassImpDecl) {
1021 Diag(AtLoc, diag::error_missing_property_context);
1022 return 0;
1023 }
1024 ObjCPropertyDecl *property = 0;
1025 ObjCInterfaceDecl* IDecl = 0;
1026 // Find the class or category class where this property must have
1027 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001028 ObjCImplementationDecl *IC = 0;
1029 ObjCCategoryImplDecl* CatImplClass = 0;
1030 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001031 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001032 // We always synthesize an interface for an implementation
1033 // without an interface decl. So, IDecl is always non-zero.
1034 assert(IDecl &&
1035 "ActOnPropertyImplDecl - @implementation without @interface");
1036
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001037 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001038 property = IDecl->FindPropertyDeclaration(PropertyId);
1039 if (!property) {
1040 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001041 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001042 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001043 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001044 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001045 if (Synthesize) {
1046 Diag(AtLoc, diag::error_synthesize_category_decl);
1047 return 0;
1048 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001049 IDecl = CatImplClass->getClassInterface();
1050 if (!IDecl) {
1051 Diag(AtLoc, diag::error_missing_property_interface);
1052 return 0;
1053 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001054 ObjCCategoryDecl *Category =
1055 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1056
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001057 // If category for this implementation not found, it is an error which
1058 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001059 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001060 return 0;
1061 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001062 property = Category->FindPropertyDeclaration(PropertyId);
1063 if (!property) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001064 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001065 Category->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001066 return 0;
1067 }
1068 }
1069 else {
1070 Diag(AtLoc, diag::error_bad_property_context);
1071 return 0;
1072 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001073 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001074 // Check that we have a valid, previously declared ivar for @synthesize
1075 if (Synthesize) {
1076 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001077 if (!PropertyIvar)
1078 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001079 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001080 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001081 if (!Ivar) {
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001082 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1083 PropertyId->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001084 return 0;
1085 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001086 // Check that type of property and its ivar match.
1087 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1088 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1089 Ivar->getName());
1090 return 0;
1091 }
1092
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001093 } else if (PropertyIvar) {
1094 // @dynamic
1095 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1096 return 0;
1097 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001098 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001099 ObjCPropertyImplDecl *PIDecl =
1100 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1101 (Synthesize ?
1102 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1103 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1104 Ivar);
1105 if (IC)
1106 IC->addPropertyImplementation(PIDecl);
1107 else
1108 CatImplClass->addPropertyImplementation(PIDecl);
1109
1110 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001111}
Chris Lattner7f925cc2008-04-11 07:00:53 +00001112