blob: 63c1350743ce68423744fbe07b8c6ebd1babcac4 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner855e51f2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremenek42730c52008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Chris Lattner855e51f2007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremenek42730c52008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner855e51f2007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Narofffe9eb6a2007-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 Lattner855e51f2007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
Chris Lattnerf3874bc2008-04-06 04:47:34 +000036 PushDeclContext(MDecl);
Chris Lattner855e51f2007-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 Lattner3e254fb2008-04-08 04:40:51 +000045 QualType selfTy = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +000046 if (MDecl->isInstance()) {
Gabor Greif9b2e0f92008-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 Lattner3e254fb2008-04-08 04:40:51 +000050 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000051 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000052 }
Gabor Greif9b2e0f92008-02-29 20:35:55 +000053 }
Chris Lattner3e254fb2008-04-08 04:40:51 +000054 CurMethodDecl->setSelfDecl(CreateImplicitParameter(FnBodyScope, PI.Ident,
55 PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000056
57 PI.Ident = &Context.Idents.get("_cmd");
Chris Lattner3e254fb2008-04-08 04:40:51 +000058 CreateImplicitParameter(FnBodyScope, PI.Ident, PI.IdentLoc,
59 Context.getObjCSelType());
60
Chris Lattner97316c02008-04-10 02:22:51 +000061 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000062 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000063 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000064 IdentifierInfo *II = PDecl->getIdentifier();
65 if (II) {
Chris Lattner2a1e2ed2008-04-11 07:00:53 +000066 IdResolver.AddDecl(PDecl, FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000067 FnBodyScope->AddDecl(PDecl);
68 }
Chris Lattner855e51f2007-12-12 07:09:47 +000069 }
70}
71
72Sema::DeclTy *Sema::ActOnStartClassInterface(
73 SourceLocation AtInterfaceLoc,
74 IdentifierInfo *ClassName, SourceLocation ClassLoc,
75 IdentifierInfo *SuperName, SourceLocation SuperLoc,
76 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
77 SourceLocation EndProtoLoc, AttributeList *AttrList) {
78 assert(ClassName && "Missing class identifier");
79
80 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000081 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000082 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000083 Diag(ClassLoc, diag::err_redefinition_different_kind,
84 ClassName->getName());
85 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
86 }
87
Ted Kremenek42730c52008-01-07 19:49:32 +000088 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000089 if (IDecl) {
90 // Class already seen. Is it a forward declaration?
91 if (!IDecl->isForwardDecl())
92 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
93 else {
94 IDecl->setLocation(AtInterfaceLoc);
95 IDecl->setForwardDecl(false);
96 IDecl->AllocIntfRefProtocols(NumProtocols);
97 }
98 }
99 else {
Chris Lattner0db541b2008-03-16 01:15:50 +0000100 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
Steve Naroff7c371742008-04-11 19:35:35 +0000101 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102
Steve Naroff15208162008-04-02 18:30:49 +0000103 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 Diag(SuperLoc, diag::err_redefinition_different_kind,
114 SuperName->getName());
115 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
116 }
117 else {
118 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000119 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000120
121 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
122 Diag(AtInterfaceLoc, diag::err_undef_superclass,
123 SuperClassEntry ? SuperClassEntry->getName()
124 : SuperName->getName(),
125 ClassName->getName());
126 }
127 }
128 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000129 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000130 IDecl->setLocEnd(SuperLoc);
131 } else { // we have a root class.
132 IDecl->setLocEnd(ClassLoc);
133 }
134
135 /// Check then save referenced protocols
136 if (NumProtocols) {
137 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000138 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 if (!RefPDecl || RefPDecl->isForwardDecl())
140 Diag(ClassLoc, diag::warn_undef_protocolref,
141 ProtocolNames[i]->getName(),
142 ClassName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000143 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000144 }
145 IDecl->setLocEnd(EndProtoLoc);
146 }
147 return IDecl;
148}
149
150/// ActOnCompatiblityAlias - this action is called after complete parsing of
151/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000152Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
153 IdentifierInfo *AliasName,
154 SourceLocation AliasLocation,
155 IdentifierInfo *ClassName,
156 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000157 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000158 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000159 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000160 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000161 Diag(AliasLocation, diag::warn_previous_alias_decl);
162 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
163 }
164 else {
165 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
166 AliasName->getName());
167 Diag(ADecl->getLocation(), diag::err_previous_declaration);
168 }
169 return 0;
170 }
171 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000172 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000173 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
174 if (CDecl == 0) {
175 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
176 if (CDeclU)
177 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 return 0;
179 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000180
181 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000182 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000183 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
184
185 ObjCAliasDecls[AliasName] = AliasDecl;
186 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000187 return AliasDecl;
188}
189
190Sema::DeclTy *Sema::ActOnStartProtocolInterface(
191 SourceLocation AtProtoInterfaceLoc,
192 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
193 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
194 SourceLocation EndProtoLoc) {
195 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000196 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000197 if (PDecl) {
198 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000199 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
201 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000202 // Just return the protocol we already had.
203 // FIXME: don't leak the objects passed in!
204 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000205 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000206
207 PDecl->setForwardDecl(false);
208 PDecl->AllocReferencedProtocols(NumProtoRefs);
209 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000210 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000211 ProtocolName);
212 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000213 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000214 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000215
216 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000217 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000218 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000219 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000220 if (!RefPDecl || RefPDecl->isForwardDecl())
221 Diag(ProtocolLoc, diag::warn_undef_protocolref,
222 ProtoRefNames[i]->getName(),
223 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000224 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 }
226 PDecl->setLocEnd(EndProtoLoc);
227 }
228 return PDecl;
229}
230
231/// FindProtocolDeclaration - This routine looks up protocols and
232/// issuer error if they are not declared. It returns list of protocol
233/// declarations in its 'Protocols' argument.
234void
235Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
236 IdentifierInfo **ProtocolId,
237 unsigned NumProtocols,
238 llvm::SmallVector<DeclTy *,8> &Protocols) {
239 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000241 if (!PDecl)
242 Diag(TypeLoc, diag::err_undeclared_protocol,
243 ProtocolId[i]->getName());
244 else
245 Protocols.push_back(PDecl);
246 }
247}
248
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000249/// DiagnosePropertyMismatch - Compares two properties for their
250/// attributes and types and warns on a variety of inconsistancies.
251///
252// TODO: Incomplete.
253//
254static void
255DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
256 ObjCPropertyDecl *SuperProperty) {
257 ObjCPropertyDecl::PropertyAttributeKind CAttr =
258 Property->getPropertyAttributes();
259 ObjCPropertyDecl::PropertyAttributeKind SAttr =
260 SuperProperty->getPropertyAttributes();
261 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
262 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
263 ; // ???
264
265 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
266 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
267 ; //
268 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
269 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
270 ; // ???
271
272 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
273 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
274 ; //
275
276 if (Property->getSetterName() != SuperProperty->getSetterName())
277 ; //
278 if (Property->getGetterName() != SuperProperty->getGetterName())
279 ; //
280
281 if (Property->getCanonicalType() != SuperProperty->getCanonicalType()) {
282 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
283 && (SAttr & ObjCPropertyDecl::OBJC_PR_readonly))
284 // && objc_compare_types(...))
285 ;
286 else
287 ; //
288 }
289
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
297Sema::ComparePropertiesInBaseAndSuper(SourceLocation *PropertyLoc,
298 DeclTy *D) {
299 ObjCInterfaceDecl *IDecl =
300 dyn_cast<ObjCInterfaceDecl>(static_cast<Decl *>(D));
301 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
302 if (!SDecl)
303 return;
304 for (ObjCInterfaceDecl::classprop_iterator I = SDecl->classprop_begin(),
305 E = SDecl->classprop_end(); I != E; ++I) {
306 ObjCPropertyDecl *SuperPDecl = (*I);
307 // Does property in super class has declaration in current class?
308 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
309 E = IDecl->classprop_end(); I != E; ++I) {
310 ObjCPropertyDecl *PDecl = (*I);
311 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
312 DiagnosePropertyMismatch(PDecl, SuperPDecl);
313 }
314 }
315}
316
Chris Lattner855e51f2007-12-12 07:09:47 +0000317/// ActOnForwardProtocolDeclaration -
318Action::DeclTy *
319Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
320 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000321 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000322
323 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000324 IdentifierInfo *Ident = IdentList[i];
325 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
326 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000327 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000328 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000329 }
330
331 Protocols.push_back(PDecl);
332 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000333 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
334 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000335}
336
337Sema::DeclTy *Sema::ActOnStartCategoryInterface(
338 SourceLocation AtInterfaceLoc,
339 IdentifierInfo *ClassName, SourceLocation ClassLoc,
340 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
341 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
342 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000343 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000344
Chris Lattnere29dc832008-03-16 20:34:23 +0000345 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000346 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000347 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000348
349 /// Check that class of this category is already completely declared.
350 if (!IDecl || IDecl->isForwardDecl())
351 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
352 else {
353 /// Check for duplicate interface declaration for this category
354 ObjCCategoryDecl *CDeclChain;
355 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
356 CDeclChain = CDeclChain->getNextClassCategory()) {
357 if (CDeclChain->getIdentifier() == CategoryName) {
358 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
359 CategoryName->getName());
360 break;
361 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000362 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000363 if (!CDeclChain)
364 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000365 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000366
367 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000368 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
369 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000370 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000371 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000372 if (!RefPDecl || RefPDecl->isForwardDecl()) {
373 Diag(CategoryLoc, diag::warn_undef_protocolref,
374 ProtoRefNames[i]->getName(),
375 CategoryName->getName());
376 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000377 if (RefPDecl)
378 RefProtocols.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000379 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000380 if (!RefProtocols.empty())
381 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000382 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000383 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000384 return CDecl;
385}
386
387/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000388/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000389/// object.
390Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
391 SourceLocation AtCatImplLoc,
392 IdentifierInfo *ClassName, SourceLocation ClassLoc,
393 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000394 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000395 ObjCCategoryImplDecl *CDecl =
396 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000397 /// Check that class of this category is already completely declared.
398 if (!IDecl || IDecl->isForwardDecl())
399 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
400
401 /// TODO: Check that CatName, category name, is not used in another
402 // implementation.
403 return CDecl;
404}
405
406Sema::DeclTy *Sema::ActOnStartClassImplementation(
407 SourceLocation AtClassImplLoc,
408 IdentifierInfo *ClassName, SourceLocation ClassLoc,
409 IdentifierInfo *SuperClassname,
410 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000411 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000412 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000413 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000414 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000415 Diag(ClassLoc, diag::err_redefinition_different_kind,
416 ClassName->getName());
417 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
418 }
419 else {
420 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000421 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000422 if (!IDecl)
423 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
424 }
425
426 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000427 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000428 if (SuperClassname) {
429 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000430 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000431 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000432 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
433 SuperClassname->getName());
434 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
435 }
436 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000437 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000438 if (!SDecl)
439 Diag(SuperClassLoc, diag::err_undef_superclass,
440 SuperClassname->getName(), ClassName->getName());
441 else if (IDecl && IDecl->getSuperClass() != SDecl) {
442 // This implementation and its interface do not have the same
443 // super class.
444 Diag(SuperClassLoc, diag::err_conflicting_super_class,
445 SDecl->getName());
446 Diag(SDecl->getLocation(), diag::err_previous_definition);
447 }
448 }
449 }
450
451 if (!IDecl) {
452 // Legacy case of @implementation with no corresponding @interface.
453 // Build, chain & install the interface decl into the identifier.
Chris Lattner0db541b2008-03-16 01:15:50 +0000454 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000455 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000456 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000457 IDecl->setSuperClass(SDecl);
458 IDecl->setLocEnd(ClassLoc);
459
460 // Remember that this needs to be removed when the scope is popped.
461 TUScope->AddDecl(IDecl);
462 }
463
Ted Kremenek42730c52008-01-07 19:49:32 +0000464 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000465 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
466 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000467
468 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000469 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000470 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000471 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
472 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000473 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000474 return IMPDecl;
475}
476
Ted Kremenek42730c52008-01-07 19:49:32 +0000477void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
478 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000479 SourceLocation RBrace) {
480 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000481 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000482 if (!IDecl)
483 return;
484 /// Check case of non-existing @interface decl.
485 /// (legacy objective-c @implementation decl without an @interface decl).
486 /// Add implementations's ivar to the synthesize class's ivar list.
487 if (IDecl->ImplicitInterfaceDecl()) {
488 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
489 return;
490 }
491 // If implementation has empty ivar list, just return.
492 if (numIvars == 0)
493 return;
494
495 assert(ivars && "missing @implementation ivars");
496
497 // Check interface's Ivar list against those in the implementation.
498 // names and types must match.
499 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000500 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000501 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000502 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
503 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000504 ObjCIvarDecl* ImplIvar = ivars[j++];
505 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000506 assert (ImplIvar && "missing implementation ivar");
507 assert (ClsIvar && "missing class ivar");
508 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
509 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
510 ImplIvar->getIdentifier()->getName());
511 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
512 ClsIvar->getIdentifier()->getName());
513 }
514 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
515 // as error.
516 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
517 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
518 ImplIvar->getIdentifier()->getName());
519 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
520 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000521 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000522 }
523 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000524 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000525
526 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000527 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000528 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000529 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000530}
531
Steve Naroffb4f48512008-02-10 21:38:56 +0000532void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
533 bool &IncompleteImpl) {
534 if (!IncompleteImpl) {
535 Diag(ImpLoc, diag::warn_incomplete_impl);
536 IncompleteImpl = true;
537 }
538 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
539}
540
Steve Naroffb268d2a2008-02-08 22:06:17 +0000541/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000542/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000543void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
544 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000545 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000546 const llvm::DenseSet<Selector> &InsMap,
547 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000548 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000549 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000550 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000551 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000552 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000553 method->getImplementationControl() != ObjCMethodDecl::Optional)
554 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000555 }
556 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000557 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000558 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000559 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000560 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000561 method->getImplementationControl() != ObjCMethodDecl::Optional)
562 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000563 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000564 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000565 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000566 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000567 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000568}
569
Ted Kremenek42730c52008-01-07 19:49:32 +0000570void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
571 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000572 llvm::DenseSet<Selector> InsMap;
573 // Check and see if instance methods in class interface have been
574 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000575 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000576 E = IMPDecl->instmeth_end(); I != E; ++I)
577 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000578
579 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000580 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000581 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000582 if (!InsMap.count((*I)->getSelector()))
583 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000584
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 llvm::DenseSet<Selector> ClsMap;
586 // Check and see if class methods in class interface have been
587 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000588 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000589 E = IMPDecl->classmeth_end(); I != E; ++I)
590 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000591
Ted Kremenek42730c52008-01-07 19:49:32 +0000592 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000593 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000594 if (!ClsMap.count((*I)->getSelector()))
595 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000596
597 // Check the protocol list for unimplemented methods in the @implementation
598 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000599 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000600 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000601 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
602 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000603}
604
605/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
606/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000607void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
608 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000609 llvm::DenseSet<Selector> InsMap;
610 // Check and see if instance methods in category interface have been
611 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000612 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000613 E = CatImplDecl->instmeth_end(); I != E; ++I)
614 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000615
616 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000618 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000619 if (!InsMap.count((*I)->getSelector()))
620 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
621
Chris Lattner855e51f2007-12-12 07:09:47 +0000622 llvm::DenseSet<Selector> ClsMap;
623 // Check and see if class methods in category interface have been
624 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000625 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000626 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
627 I != E; ++I)
628 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000629
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000631 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000632 if (!ClsMap.count((*I)->getSelector()))
633 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000634
635 // Check the protocol list for unimplemented methods in the @implementation
636 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000637 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000638 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000640 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
641 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000642 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000643}
644
645/// ActOnForwardClassDeclaration -
646Action::DeclTy *
647Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
648 IdentifierInfo **IdentList, unsigned NumElts)
649{
Ted Kremenek42730c52008-01-07 19:49:32 +0000650 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000651
652 for (unsigned i = 0; i != NumElts; ++i) {
653 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000654 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000655 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000656 Diag(AtClassLoc, diag::err_redefinition_different_kind,
657 IdentList[i]->getName());
658 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
659 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000660 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000661 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0db541b2008-03-16 01:15:50 +0000662 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000663 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000664 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000665
666 // Remember that this needs to be removed when the scope is popped.
667 TUScope->AddDecl(IDecl);
668 }
669
670 Interfaces.push_back(IDecl);
671 }
672
Chris Lattnere29dc832008-03-16 20:34:23 +0000673 return ObjCClassDecl::Create(Context, AtClassLoc,
674 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000675}
676
677
678/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
679/// returns true, or false, accordingly.
680/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000681bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
682 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000683 if (Method->getResultType().getCanonicalType() !=
684 PrevMethod->getResultType().getCanonicalType())
685 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000686 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000687 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
688 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000689 if (Context.getCanonicalType(ParamDecl->getType()) !=
690 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000691 return false;
692 }
693 return true;
694}
695
Ted Kremenek42730c52008-01-07 19:49:32 +0000696void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
697 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000698 if (!FirstMethod.Method) {
699 // Haven't seen a method with this selector name yet - add it.
700 FirstMethod.Method = Method;
701 FirstMethod.Next = 0;
702 } else {
703 // We've seen a method with this name, now check the type signature(s).
704 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
705
Ted Kremenek42730c52008-01-07 19:49:32 +0000706 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000707 Next = Next->Next)
708 match = MatchTwoMethodDeclarations(Method, Next->Method);
709
710 if (!match) {
711 // We have a new signature for an existing method - add it.
712 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000713 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000714 FirstMethod.Next = OMI;
715 }
716 }
717}
718
Ted Kremenek42730c52008-01-07 19:49:32 +0000719void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
720 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000721 if (!FirstMethod.Method) {
722 // Haven't seen a method with this selector name yet - add it.
723 FirstMethod.Method = Method;
724 FirstMethod.Next = 0;
725 } else {
726 // We've seen a method with this name, now check the type signature(s).
727 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
728
Ted Kremenek42730c52008-01-07 19:49:32 +0000729 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000730 Next = Next->Next)
731 match = MatchTwoMethodDeclarations(Method, Next->Method);
732
733 if (!match) {
734 // We have a new signature for an existing method - add it.
735 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000736 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000737 FirstMethod.Next = OMI;
738 }
739 }
740}
741
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000742// Note: For class/category implemenations, allMethods/allProperties is
743// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000744void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
745 DeclTy **allMethods, unsigned allNum,
746 DeclTy **allProperties, unsigned pNum) {
747 Decl *ClassDecl = static_cast<Decl *>(classDecl);
748
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000749 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
750 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000751 // should be true.
752 if (!ClassDecl)
753 return;
754
Ted Kremenek42730c52008-01-07 19:49:32 +0000755 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
756 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000757
Ted Kremenek42730c52008-01-07 19:49:32 +0000758 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
759 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000760
761 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000762 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
763 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000764 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000765
Chris Lattner2d1c4312008-03-16 21:17:37 +0000766 if (pNum != 0)
Chris Lattnercffe3662008-03-16 21:23:50 +0000767 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
768 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000769 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
770 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
771 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
772 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000773 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000774 assert(false && "ActOnAtEnd - property declaration misplaced");
Chris Lattner855e51f2007-12-12 07:09:47 +0000775
776 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000777 ObjCMethodDecl *Method =
778 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000779
780 if (!Method) continue; // Already issued a diagnostic.
781 if (Method->isInstance()) {
782 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000783 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000784 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
785 : false;
786 if (isInterfaceDeclKind && PrevMethod && !match
787 || checkIdenticalMethods && match) {
788 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
789 Method->getSelector().getName());
790 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
791 } else {
792 insMethods.push_back(Method);
793 InsMap[Method->getSelector()] = Method;
794 /// The following allows us to typecheck messages to "id".
795 AddInstanceMethodToGlobalPool(Method);
796 }
797 }
798 else {
799 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000800 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000801 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
802 : false;
803 if (isInterfaceDeclKind && PrevMethod && !match
804 || checkIdenticalMethods && match) {
805 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
806 Method->getSelector().getName());
807 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
808 } else {
809 clsMethods.push_back(Method);
810 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000811 /// The following allows us to typecheck messages to "Class".
812 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000813 }
814 }
815 }
816
Ted Kremenek42730c52008-01-07 19:49:32 +0000817 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000818 I->addMethods(&insMethods[0], insMethods.size(),
819 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000820 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000821 P->addMethods(&insMethods[0], insMethods.size(),
822 &clsMethods[0], clsMethods.size(), AtEndLoc);
823 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000824 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000825 C->addMethods(&insMethods[0], insMethods.size(),
826 &clsMethods[0], clsMethods.size(), AtEndLoc);
827 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000828 else if (ObjCImplementationDecl *IC =
829 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000830 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000831 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000832 ImplMethodsVsClassMethods(IC, IDecl);
833 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000834 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000835 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000836 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000837 // Find category interface decl and then check that all methods declared
838 // in this interface is implemented in the category @implementation.
839 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000840 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000841 Categories; Categories = Categories->getNextClassCategory()) {
842 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
843 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
844 break;
845 }
846 }
847 }
848 }
849}
850
851
852/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
853/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000854static Decl::ObjCDeclQualifier
855CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
856 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
857 if (PQTVal & ObjCDeclSpec::DQ_In)
858 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
859 if (PQTVal & ObjCDeclSpec::DQ_Inout)
860 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
861 if (PQTVal & ObjCDeclSpec::DQ_Out)
862 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
863 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
864 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
865 if (PQTVal & ObjCDeclSpec::DQ_Byref)
866 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
867 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
868 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000869
870 return ret;
871}
872
873Sema::DeclTy *Sema::ActOnMethodDeclaration(
874 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000875 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000876 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000877 Selector Sel,
878 // optional arguments. The number of types/arguments is obtained
879 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000880 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000881 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
882 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000883 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000884
885 // Make sure we can establish a context for the method.
886 if (!ClassDecl) {
887 Diag(MethodLoc, diag::error_missing_method_context);
888 return 0;
889 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000890 QualType resultDeclType;
891
892 if (ReturnType)
893 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
894 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000896
Chris Lattner114add62008-03-16 00:49:28 +0000897 ObjCMethodDecl* ObjCMethod =
898 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000899 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000900 MethodType == tok::minus, isVariadic,
901 MethodDeclKind == tok::objc_optional ?
902 ObjCMethodDecl::Optional :
903 ObjCMethodDecl::Required);
904
Chris Lattnereee57c02008-04-04 06:12:32 +0000905 llvm::SmallVector<ParmVarDecl*, 16> Params;
906
907 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
908 // FIXME: arg->AttrList must be stored too!
909 QualType argType;
910
911 if (ArgTypes[i])
912 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
913 else
914 argType = Context.getObjCIdType();
915 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
916 SourceLocation(/*FIXME*/),
917 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000918 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000919 Param->setObjCDeclQualifier(
920 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
921 Params.push_back(Param);
922 }
923
Ted Kremenek42730c52008-01-07 19:49:32 +0000924 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
925 ObjCMethod->setObjCDeclQualifier(
926 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
927 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000928
929 // For implementations (which can be very "coarse grain"), we add the
930 // method now. This allows the AST to implement lookup methods that work
931 // incrementally (without waiting until we parse the @end). It also allows
932 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000933 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000934 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000935 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000936 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000937 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000938 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000939 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000940 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000941 }
942 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000943 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000944 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000945 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000946 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000947 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000948 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000949 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000950 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000951 }
952 }
953 if (PrevMethod) {
954 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +0000955 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
956 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000957 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
958 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000959 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +0000960}
961
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000962Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
963 FieldDeclarator &FD,
964 ObjCDeclSpec &ODS) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000965 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000966 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
967 FD.D.getIdentifier(), T);
Chris Lattner855e51f2007-12-12 07:09:47 +0000968
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000969 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +0000970 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +0000971
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000972 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000973 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000974 PDecl->setGetterName(ODS.getGetterName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000975 }
976
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000977 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000978 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000979 PDecl->setSetterName(ODS.getSetterName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000980 }
981
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000982 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +0000983 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +0000984
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000985 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +0000986 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +0000987
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000988 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +0000989 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +0000990
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000991 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +0000992 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +0000993
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000994 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +0000995 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +0000996
Chris Lattner855e51f2007-12-12 07:09:47 +0000997 return PDecl;
998}
999
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001000/// ActOnPropertyImplDecl - This routine performs semantic checks and
1001/// builds the AST node for a property implementation declaration; declared
1002/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001003///
1004Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1005 SourceLocation PropertyLoc,
1006 bool Synthesize,
1007 DeclTy *ClassCatImpDecl,
1008 IdentifierInfo *PropertyId,
1009 IdentifierInfo *PropertyIvar) {
1010 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1011 // Make sure we have a context for the property implementation declaration.
1012 if (!ClassImpDecl) {
1013 Diag(AtLoc, diag::error_missing_property_context);
1014 return 0;
1015 }
1016 ObjCPropertyDecl *property = 0;
1017 ObjCInterfaceDecl* IDecl = 0;
1018 // Find the class or category class where this property must have
1019 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001020 ObjCImplementationDecl *IC = 0;
1021 ObjCCategoryImplDecl* CatImplClass = 0;
1022 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001023 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001024 // We always synthesize an interface for an implementation
1025 // without an interface decl. So, IDecl is always non-zero.
1026 assert(IDecl &&
1027 "ActOnPropertyImplDecl - @implementation without @interface");
1028
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001029 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001030 property = IDecl->FindPropertyDeclaration(PropertyId);
1031 if (!property) {
1032 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001033 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001034 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001035 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001036 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001037 if (Synthesize) {
1038 Diag(AtLoc, diag::error_synthesize_category_decl);
1039 return 0;
1040 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001041 IDecl = CatImplClass->getClassInterface();
1042 if (!IDecl) {
1043 Diag(AtLoc, diag::error_missing_property_interface);
1044 return 0;
1045 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001046 ObjCCategoryDecl *Category =
1047 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1048
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001049 // If category for this implementation not found, it is an error which
1050 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001051 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001052 return 0;
1053 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001054 property = Category->FindPropertyDeclaration(PropertyId);
1055 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001056 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001057 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001058 return 0;
1059 }
1060 }
1061 else {
1062 Diag(AtLoc, diag::error_bad_property_context);
1063 return 0;
1064 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001065 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001066 // Check that we have a valid, previously declared ivar for @synthesize
1067 if (Synthesize) {
1068 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001069 if (!PropertyIvar)
1070 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001071 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001072 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001073 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001074 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1075 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001076 return 0;
1077 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001078 // Check that type of property and its ivar match.
1079 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1080 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1081 Ivar->getName());
1082 return 0;
1083 }
1084
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001085 } else if (PropertyIvar) {
1086 // @dynamic
1087 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1088 return 0;
1089 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001090 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001091 ObjCPropertyImplDecl *PIDecl =
1092 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1093 (Synthesize ?
1094 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1095 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1096 Ivar);
1097 if (IC)
1098 IC->addPropertyImplementation(PIDecl);
1099 else
1100 CatImplClass->addPropertyImplementation(PIDecl);
1101
1102 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001103}
Chris Lattner2a1e2ed2008-04-11 07:00:53 +00001104