blob: 3b5be29730ba8b8e391e4903c31acf53280c1e96 [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();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000065 if (II)
66 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-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 Naroff6384a012008-04-02 14:35:35 +000079 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000080 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +000086 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-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 Lattner0db541b2008-03-16 01:15:50 +000098 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
Steve Naroff7c371742008-04-11 19:35:35 +000099 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
Steve Naroff15208162008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +0000107 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000109 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +0000117 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-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 Naroff7c371742008-04-11 19:35:35 +0000127 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +0000136 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-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 Jahanian87829072007-12-20 19:24:10 +0000141 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-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 Naroffe57c21a2008-04-01 23:04:06 +0000150Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
151 IdentifierInfo *AliasName,
152 SourceLocation AliasLocation,
153 IdentifierInfo *ClassName,
154 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000155 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000156 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000157 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000158 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-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 Naroff6384a012008-04-02 14:35:35 +0000170 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-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 Lattner855e51f2007-12-12 07:09:47 +0000176 return 0;
177 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000178
179 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000180 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000181 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
182
183 ObjCAliasDecls[AliasName] = AliasDecl;
184 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +0000194 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000195 if (PDecl) {
196 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000197 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000198 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
199 ProtocolName->getName());
Chris Lattnerc1881852008-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 Lattner855e51f2007-12-12 07:09:47 +0000203 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000204
205 PDecl->setForwardDecl(false);
206 PDecl->AllocReferencedProtocols(NumProtoRefs);
207 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000208 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000209 ProtocolName);
210 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000211 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000212 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000213
214 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000215 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000216 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000217 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-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 Jahanian87829072007-12-20 19:24:10 +0000222 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-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 Kremenek42730c52008-01-07 19:49:32 +0000238 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-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 Jahanian1e89de32008-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//
252static void
253DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
254 ObjCPropertyDecl *SuperProperty) {
255 ObjCPropertyDecl::PropertyAttributeKind CAttr =
256 Property->getPropertyAttributes();
257 ObjCPropertyDecl::PropertyAttributeKind SAttr =
258 SuperProperty->getPropertyAttributes();
259 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
260 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
261 ; // ???
262
263 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
264 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
265 ; //
266 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
267 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
268 ; // ???
269
270 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
272 ; //
273
274 if (Property->getSetterName() != SuperProperty->getSetterName())
275 ; //
276 if (Property->getGetterName() != SuperProperty->getGetterName())
277 ; //
278
279 if (Property->getCanonicalType() != SuperProperty->getCanonicalType()) {
280 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
281 && (SAttr & ObjCPropertyDecl::OBJC_PR_readonly))
282 // && objc_compare_types(...))
283 ;
284 else
285 ; //
286 }
287
288}
289
290/// ComparePropertiesInBaseAndSuper - This routine compares property
291/// declarations in base and its super class, if any, and issues
292/// diagnostics in a variety of inconsistant situations.
293///
294void
295Sema::ComparePropertiesInBaseAndSuper(SourceLocation *PropertyLoc,
296 DeclTy *D) {
297 ObjCInterfaceDecl *IDecl =
298 dyn_cast<ObjCInterfaceDecl>(static_cast<Decl *>(D));
299 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
300 if (!SDecl)
301 return;
302 for (ObjCInterfaceDecl::classprop_iterator I = SDecl->classprop_begin(),
303 E = SDecl->classprop_end(); I != E; ++I) {
304 ObjCPropertyDecl *SuperPDecl = (*I);
305 // Does property in super class has declaration in current class?
306 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
307 E = IDecl->classprop_end(); I != E; ++I) {
308 ObjCPropertyDecl *PDecl = (*I);
309 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
310 DiagnosePropertyMismatch(PDecl, SuperPDecl);
311 }
312 }
313}
314
Chris Lattner855e51f2007-12-12 07:09:47 +0000315/// ActOnForwardProtocolDeclaration -
316Action::DeclTy *
317Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
318 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000319 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000320
321 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000322 IdentifierInfo *Ident = IdentList[i];
323 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
324 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000325 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000326 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000327 }
328
329 Protocols.push_back(PDecl);
330 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000331 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
332 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000333}
334
335Sema::DeclTy *Sema::ActOnStartCategoryInterface(
336 SourceLocation AtInterfaceLoc,
337 IdentifierInfo *ClassName, SourceLocation ClassLoc,
338 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
339 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
340 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000341 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000342
Chris Lattnere29dc832008-03-16 20:34:23 +0000343 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000344 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000345 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000346
347 /// Check that class of this category is already completely declared.
348 if (!IDecl || IDecl->isForwardDecl())
349 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
350 else {
351 /// Check for duplicate interface declaration for this category
352 ObjCCategoryDecl *CDeclChain;
353 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
354 CDeclChain = CDeclChain->getNextClassCategory()) {
355 if (CDeclChain->getIdentifier() == CategoryName) {
356 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
357 CategoryName->getName());
358 break;
359 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000360 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000361 if (!CDeclChain)
362 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000363 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000364
365 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000366 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
367 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000368 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000369 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000370 if (!RefPDecl || RefPDecl->isForwardDecl()) {
371 Diag(CategoryLoc, diag::warn_undef_protocolref,
372 ProtoRefNames[i]->getName(),
373 CategoryName->getName());
374 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000375 if (RefPDecl)
376 RefProtocols.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000377 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000378 if (!RefProtocols.empty())
379 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000380 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000381 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000382 return CDecl;
383}
384
385/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000386/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000387/// object.
388Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
389 SourceLocation AtCatImplLoc,
390 IdentifierInfo *ClassName, SourceLocation ClassLoc,
391 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000392 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000393 ObjCCategoryImplDecl *CDecl =
394 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000395 /// Check that class of this category is already completely declared.
396 if (!IDecl || IDecl->isForwardDecl())
397 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
398
399 /// TODO: Check that CatName, category name, is not used in another
400 // implementation.
401 return CDecl;
402}
403
404Sema::DeclTy *Sema::ActOnStartClassImplementation(
405 SourceLocation AtClassImplLoc,
406 IdentifierInfo *ClassName, SourceLocation ClassLoc,
407 IdentifierInfo *SuperClassname,
408 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000409 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000410 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000411 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000413 Diag(ClassLoc, diag::err_redefinition_different_kind,
414 ClassName->getName());
415 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
416 }
417 else {
418 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000419 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000420 if (!IDecl)
421 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
422 }
423
424 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000425 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000426 if (SuperClassname) {
427 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000428 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000429 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000430 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
431 SuperClassname->getName());
432 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
433 }
434 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000435 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000436 if (!SDecl)
437 Diag(SuperClassLoc, diag::err_undef_superclass,
438 SuperClassname->getName(), ClassName->getName());
439 else if (IDecl && IDecl->getSuperClass() != SDecl) {
440 // This implementation and its interface do not have the same
441 // super class.
442 Diag(SuperClassLoc, diag::err_conflicting_super_class,
443 SDecl->getName());
444 Diag(SDecl->getLocation(), diag::err_previous_definition);
445 }
446 }
447 }
448
449 if (!IDecl) {
450 // Legacy case of @implementation with no corresponding @interface.
451 // Build, chain & install the interface decl into the identifier.
Chris Lattner0db541b2008-03-16 01:15:50 +0000452 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000453 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000454 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000455 IDecl->setSuperClass(SDecl);
456 IDecl->setLocEnd(ClassLoc);
457
458 // Remember that this needs to be removed when the scope is popped.
459 TUScope->AddDecl(IDecl);
460 }
461
Ted Kremenek42730c52008-01-07 19:49:32 +0000462 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000463 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
464 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000465
466 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000467 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000468 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000469 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
470 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000471 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000472 return IMPDecl;
473}
474
Ted Kremenek42730c52008-01-07 19:49:32 +0000475void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
476 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000477 SourceLocation RBrace) {
478 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000479 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000480 if (!IDecl)
481 return;
482 /// Check case of non-existing @interface decl.
483 /// (legacy objective-c @implementation decl without an @interface decl).
484 /// Add implementations's ivar to the synthesize class's ivar list.
485 if (IDecl->ImplicitInterfaceDecl()) {
486 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
487 return;
488 }
489 // If implementation has empty ivar list, just return.
490 if (numIvars == 0)
491 return;
492
493 assert(ivars && "missing @implementation ivars");
494
495 // Check interface's Ivar list against those in the implementation.
496 // names and types must match.
497 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000498 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000499 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000500 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
501 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000502 ObjCIvarDecl* ImplIvar = ivars[j++];
503 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000504 assert (ImplIvar && "missing implementation ivar");
505 assert (ClsIvar && "missing class ivar");
506 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
507 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
508 ImplIvar->getIdentifier()->getName());
509 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
510 ClsIvar->getIdentifier()->getName());
511 }
512 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
513 // as error.
514 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
515 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
516 ImplIvar->getIdentifier()->getName());
517 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
518 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000519 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000520 }
521 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000522 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000523
524 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000525 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000526 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000527 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000528}
529
Steve Naroffb4f48512008-02-10 21:38:56 +0000530void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
531 bool &IncompleteImpl) {
532 if (!IncompleteImpl) {
533 Diag(ImpLoc, diag::warn_incomplete_impl);
534 IncompleteImpl = true;
535 }
536 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
537}
538
Steve Naroffb268d2a2008-02-08 22:06:17 +0000539/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000540/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000541void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
542 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000543 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000544 const llvm::DenseSet<Selector> &InsMap,
545 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000546 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000547 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000548 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000549 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000550 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000551 method->getImplementationControl() != ObjCMethodDecl::Optional)
552 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000553 }
554 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000555 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000556 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000557 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000558 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000559 method->getImplementationControl() != ObjCMethodDecl::Optional)
560 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000561 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000562 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000563 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000564 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000565 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000566}
567
Ted Kremenek42730c52008-01-07 19:49:32 +0000568void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
569 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000570 llvm::DenseSet<Selector> InsMap;
571 // Check and see if instance methods in class interface have been
572 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000573 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000574 E = IMPDecl->instmeth_end(); I != E; ++I)
575 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000576
577 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000578 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000579 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000580 if (!InsMap.count((*I)->getSelector()))
581 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000582
Chris Lattner855e51f2007-12-12 07:09:47 +0000583 llvm::DenseSet<Selector> ClsMap;
584 // Check and see if class methods in class interface have been
585 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000586 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000587 E = IMPDecl->classmeth_end(); I != E; ++I)
588 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000589
Ted Kremenek42730c52008-01-07 19:49:32 +0000590 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000591 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000592 if (!ClsMap.count((*I)->getSelector()))
593 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000594
595 // Check the protocol list for unimplemented methods in the @implementation
596 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000598 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000599 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
600 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000601}
602
603/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
604/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000605void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
606 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000607 llvm::DenseSet<Selector> InsMap;
608 // Check and see if instance methods in category interface have been
609 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000610 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000611 E = CatImplDecl->instmeth_end(); I != E; ++I)
612 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000613
614 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000615 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000616 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000617 if (!InsMap.count((*I)->getSelector()))
618 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
619
Chris Lattner855e51f2007-12-12 07:09:47 +0000620 llvm::DenseSet<Selector> ClsMap;
621 // Check and see if class methods in category interface have been
622 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000623 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000624 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
625 I != E; ++I)
626 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000627
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000629 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000630 if (!ClsMap.count((*I)->getSelector()))
631 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000632
633 // Check the protocol list for unimplemented methods in the @implementation
634 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000635 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000636 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000637 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000638 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
639 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000641}
642
643/// ActOnForwardClassDeclaration -
644Action::DeclTy *
645Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
646 IdentifierInfo **IdentList, unsigned NumElts)
647{
Ted Kremenek42730c52008-01-07 19:49:32 +0000648 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000649
650 for (unsigned i = 0; i != NumElts; ++i) {
651 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000652 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000653 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000654 Diag(AtClassLoc, diag::err_redefinition_different_kind,
655 IdentList[i]->getName());
656 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
657 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000658 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000659 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0db541b2008-03-16 01:15:50 +0000660 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000661 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000662 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000663
664 // Remember that this needs to be removed when the scope is popped.
665 TUScope->AddDecl(IDecl);
666 }
667
668 Interfaces.push_back(IDecl);
669 }
670
Chris Lattnere29dc832008-03-16 20:34:23 +0000671 return ObjCClassDecl::Create(Context, AtClassLoc,
672 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000673}
674
675
676/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
677/// returns true, or false, accordingly.
678/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000679bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
680 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000681 if (Method->getResultType().getCanonicalType() !=
682 PrevMethod->getResultType().getCanonicalType())
683 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000684 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000685 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
686 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000687 if (Context.getCanonicalType(ParamDecl->getType()) !=
688 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000689 return false;
690 }
691 return true;
692}
693
Ted Kremenek42730c52008-01-07 19:49:32 +0000694void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
695 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000696 if (!FirstMethod.Method) {
697 // Haven't seen a method with this selector name yet - add it.
698 FirstMethod.Method = Method;
699 FirstMethod.Next = 0;
700 } else {
701 // We've seen a method with this name, now check the type signature(s).
702 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
703
Ted Kremenek42730c52008-01-07 19:49:32 +0000704 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000705 Next = Next->Next)
706 match = MatchTwoMethodDeclarations(Method, Next->Method);
707
708 if (!match) {
709 // We have a new signature for an existing method - add it.
710 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000711 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000712 FirstMethod.Next = OMI;
713 }
714 }
715}
716
Ted Kremenek42730c52008-01-07 19:49:32 +0000717void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
718 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000719 if (!FirstMethod.Method) {
720 // Haven't seen a method with this selector name yet - add it.
721 FirstMethod.Method = Method;
722 FirstMethod.Next = 0;
723 } else {
724 // We've seen a method with this name, now check the type signature(s).
725 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
726
Ted Kremenek42730c52008-01-07 19:49:32 +0000727 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000728 Next = Next->Next)
729 match = MatchTwoMethodDeclarations(Method, Next->Method);
730
731 if (!match) {
732 // We have a new signature for an existing method - add it.
733 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000734 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000735 FirstMethod.Next = OMI;
736 }
737 }
738}
739
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000740// Note: For class/category implemenations, allMethods/allProperties is
741// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000742void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
743 DeclTy **allMethods, unsigned allNum,
744 DeclTy **allProperties, unsigned pNum) {
745 Decl *ClassDecl = static_cast<Decl *>(classDecl);
746
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000747 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
748 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000749 // should be true.
750 if (!ClassDecl)
751 return;
752
Ted Kremenek42730c52008-01-07 19:49:32 +0000753 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
754 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000755
Ted Kremenek42730c52008-01-07 19:49:32 +0000756 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
757 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000758
759 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000760 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
761 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000762 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000763
Chris Lattner2d1c4312008-03-16 21:17:37 +0000764 if (pNum != 0)
Chris Lattnercffe3662008-03-16 21:23:50 +0000765 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
766 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000767 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
768 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
769 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
770 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000771 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000772 assert(false && "ActOnAtEnd - property declaration misplaced");
Chris Lattner855e51f2007-12-12 07:09:47 +0000773
774 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000775 ObjCMethodDecl *Method =
776 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000777
778 if (!Method) continue; // Already issued a diagnostic.
779 if (Method->isInstance()) {
780 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000781 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000782 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
783 : false;
784 if (isInterfaceDeclKind && PrevMethod && !match
785 || checkIdenticalMethods && match) {
786 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
787 Method->getSelector().getName());
788 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
789 } else {
790 insMethods.push_back(Method);
791 InsMap[Method->getSelector()] = Method;
792 /// The following allows us to typecheck messages to "id".
793 AddInstanceMethodToGlobalPool(Method);
794 }
795 }
796 else {
797 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000798 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000799 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
800 : false;
801 if (isInterfaceDeclKind && PrevMethod && !match
802 || checkIdenticalMethods && match) {
803 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
804 Method->getSelector().getName());
805 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
806 } else {
807 clsMethods.push_back(Method);
808 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000809 /// The following allows us to typecheck messages to "Class".
810 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000811 }
812 }
813 }
814
Ted Kremenek42730c52008-01-07 19:49:32 +0000815 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000816 I->addMethods(&insMethods[0], insMethods.size(),
817 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000818 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000819 P->addMethods(&insMethods[0], insMethods.size(),
820 &clsMethods[0], clsMethods.size(), AtEndLoc);
821 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000822 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000823 C->addMethods(&insMethods[0], insMethods.size(),
824 &clsMethods[0], clsMethods.size(), AtEndLoc);
825 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000826 else if (ObjCImplementationDecl *IC =
827 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000828 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000829 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000830 ImplMethodsVsClassMethods(IC, IDecl);
831 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000833 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000834 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000835 // Find category interface decl and then check that all methods declared
836 // in this interface is implemented in the category @implementation.
837 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000838 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000839 Categories; Categories = Categories->getNextClassCategory()) {
840 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
841 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
842 break;
843 }
844 }
845 }
846 }
847}
848
849
850/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
851/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000852static Decl::ObjCDeclQualifier
853CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
854 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
855 if (PQTVal & ObjCDeclSpec::DQ_In)
856 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
857 if (PQTVal & ObjCDeclSpec::DQ_Inout)
858 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
859 if (PQTVal & ObjCDeclSpec::DQ_Out)
860 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
861 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
862 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
863 if (PQTVal & ObjCDeclSpec::DQ_Byref)
864 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
865 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
866 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000867
868 return ret;
869}
870
871Sema::DeclTy *Sema::ActOnMethodDeclaration(
872 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000873 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000874 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000875 Selector Sel,
876 // optional arguments. The number of types/arguments is obtained
877 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000878 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000879 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
880 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000881 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000882
883 // Make sure we can establish a context for the method.
884 if (!ClassDecl) {
885 Diag(MethodLoc, diag::error_missing_method_context);
886 return 0;
887 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000888 QualType resultDeclType;
889
890 if (ReturnType)
891 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
892 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000893 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000894
Chris Lattner114add62008-03-16 00:49:28 +0000895 ObjCMethodDecl* ObjCMethod =
896 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000897 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000898 MethodType == tok::minus, isVariadic,
899 MethodDeclKind == tok::objc_optional ?
900 ObjCMethodDecl::Optional :
901 ObjCMethodDecl::Required);
902
Chris Lattnereee57c02008-04-04 06:12:32 +0000903 llvm::SmallVector<ParmVarDecl*, 16> Params;
904
905 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
906 // FIXME: arg->AttrList must be stored too!
907 QualType argType;
908
909 if (ArgTypes[i])
910 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
911 else
912 argType = Context.getObjCIdType();
913 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
914 SourceLocation(/*FIXME*/),
915 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000916 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000917 Param->setObjCDeclQualifier(
918 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
919 Params.push_back(Param);
920 }
921
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
923 ObjCMethod->setObjCDeclQualifier(
924 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
925 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000926
927 // For implementations (which can be very "coarse grain"), we add the
928 // method now. This allows the AST to implement lookup methods that work
929 // incrementally (without waiting until we parse the @end). It also allows
930 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000931 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000932 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000933 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000934 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000935 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000936 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000937 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000938 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000939 }
940 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000941 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000942 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000943 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000944 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000945 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000946 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000947 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000948 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000949 }
950 }
951 if (PrevMethod) {
952 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +0000953 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
954 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000955 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
956 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000957 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +0000958}
959
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000960Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
961 FieldDeclarator &FD,
962 ObjCDeclSpec &ODS) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000963 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +0000964 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
965 FD.D.getIdentifier(), T);
Chris Lattner855e51f2007-12-12 07:09:47 +0000966
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000967 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +0000968 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +0000969
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000970 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000971 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000972 PDecl->setGetterName(ODS.getGetterName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000973 }
974
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000975 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000976 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000977 PDecl->setSetterName(ODS.getSetterName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000978 }
979
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000980 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +0000981 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +0000982
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000983 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +0000984 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +0000985
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000986 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +0000987 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +0000988
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000989 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +0000990 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +0000991
Fariborz Jahaniane7071722008-04-11 23:40:25 +0000992 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +0000993 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +0000994
Chris Lattner855e51f2007-12-12 07:09:47 +0000995 return PDecl;
996}
997
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +0000998/// ActOnPropertyImplDecl - This routine performs semantic checks and
999/// builds the AST node for a property implementation declaration; declared
1000/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001001///
1002Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1003 SourceLocation PropertyLoc,
1004 bool Synthesize,
1005 DeclTy *ClassCatImpDecl,
1006 IdentifierInfo *PropertyId,
1007 IdentifierInfo *PropertyIvar) {
1008 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1009 // Make sure we have a context for the property implementation declaration.
1010 if (!ClassImpDecl) {
1011 Diag(AtLoc, diag::error_missing_property_context);
1012 return 0;
1013 }
1014 ObjCPropertyDecl *property = 0;
1015 ObjCInterfaceDecl* IDecl = 0;
1016 // Find the class or category class where this property must have
1017 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001018 ObjCImplementationDecl *IC = 0;
1019 ObjCCategoryImplDecl* CatImplClass = 0;
1020 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001021 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001022 // We always synthesize an interface for an implementation
1023 // without an interface decl. So, IDecl is always non-zero.
1024 assert(IDecl &&
1025 "ActOnPropertyImplDecl - @implementation without @interface");
1026
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001027 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001028 property = IDecl->FindPropertyDeclaration(PropertyId);
1029 if (!property) {
1030 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001031 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001032 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001033 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001034 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001035 if (Synthesize) {
1036 Diag(AtLoc, diag::error_synthesize_category_decl);
1037 return 0;
1038 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001039 IDecl = CatImplClass->getClassInterface();
1040 if (!IDecl) {
1041 Diag(AtLoc, diag::error_missing_property_interface);
1042 return 0;
1043 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001044 ObjCCategoryDecl *Category =
1045 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1046
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001047 // If category for this implementation not found, it is an error which
1048 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001049 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001050 return 0;
1051 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001052 property = Category->FindPropertyDeclaration(PropertyId);
1053 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001054 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001055 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001056 return 0;
1057 }
1058 }
1059 else {
1060 Diag(AtLoc, diag::error_bad_property_context);
1061 return 0;
1062 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001063 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001064 // Check that we have a valid, previously declared ivar for @synthesize
1065 if (Synthesize) {
1066 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001067 if (!PropertyIvar)
1068 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001069 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001070 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001071 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001072 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1073 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001074 return 0;
1075 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001076 // Check that type of property and its ivar match.
1077 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1078 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1079 Ivar->getName());
1080 return 0;
1081 }
1082
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001083 } else if (PropertyIvar) {
1084 // @dynamic
1085 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1086 return 0;
1087 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001088 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001089 ObjCPropertyImplDecl *PIDecl =
1090 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1091 (Synthesize ?
1092 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1093 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1094 Ivar);
1095 if (IC)
1096 IC->addPropertyImplementation(PIDecl);
1097 else
1098 CatImplClass->addPropertyImplementation(PIDecl);
1099
1100 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001101}
Chris Lattner2a1e2ed2008-04-11 07:00:53 +00001102