blob: 234bead575e232d7b0a4d3cafcffbda6677261ba [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremeneka526c5c2008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Chris Lattner4d391482007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner4d391482007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Naroffa56f6162007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000036 PushDeclContext(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000037
38 // Create Decl objects for each parameter, entrring them in the scope for
39 // binding to their use.
40 struct DeclaratorChunk::ParamInfo PI;
41
42 // Insert the invisible arguments, self and _cmd!
43 PI.Ident = &Context.Idents.get("self");
44 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Chris Lattner04421082008-04-08 04:40:51 +000045 QualType selfTy = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +000046 if (MDecl->isInstance()) {
Gabor Greif30037532008-02-29 20:35:55 +000047 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
48 // There may be no interface context due to error in declaration of the
49 // interface (which has been reported). Recover gracefully
Chris Lattner04421082008-04-08 04:40:51 +000050 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000051 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian20552d22008-01-10 20:33:58 +000052 }
Gabor Greif30037532008-02-29 20:35:55 +000053 }
Chris Lattner04421082008-04-08 04:40:51 +000054 CurMethodDecl->setSelfDecl(CreateImplicitParameter(FnBodyScope, PI.Ident,
55 PI.IdentLoc, selfTy));
Chris Lattner4d391482007-12-12 07:09:47 +000056
57 PI.Ident = &Context.Idents.get("_cmd");
Chris Lattner04421082008-04-08 04:40:51 +000058 CreateImplicitParameter(FnBodyScope, PI.Ident, PI.IdentLoc,
59 Context.getObjCSelType());
60
Chris Lattner8123a952008-04-10 02:22:51 +000061 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000062 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000063 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000064 IdentifierInfo *II = PDecl->getIdentifier();
65 if (II) {
Chris Lattner7f925cc2008-04-11 07:00:53 +000066 IdResolver.AddDecl(PDecl, FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000067 FnBodyScope->AddDecl(PDecl);
68 }
Chris Lattner4d391482007-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 Naroffb327ce02008-04-02 14:35:35 +000081 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000082 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +000088 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-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 Lattner0e77ba02008-03-16 01:15:50 +0000100 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000101 ClassName, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000102
Steve Naroff31102512008-04-02 18:30:49 +0000103 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000113 Diag(SuperLoc, diag::err_redefinition_different_kind,
114 SuperName->getName());
115 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
116 }
117 else {
118 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000119 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000120
121 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
122 Diag(AtInterfaceLoc, diag::err_undef_superclass,
123 SuperClassEntry ? SuperClassEntry->getName()
124 : SuperName->getName(),
125 ClassName->getName());
126 }
127 }
128 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000129 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000130 IDecl->setLocEnd(SuperLoc);
131 } else { // we have a root class.
132 IDecl->setLocEnd(ClassLoc);
133 }
134
135 /// Check then save referenced protocols
136 if (NumProtocols) {
137 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000138 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner4d391482007-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 Jahanianc395bda2007-12-20 19:24:10 +0000143 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner4d391482007-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 Naroffe8043c32008-04-01 23:04:06 +0000152Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
153 IdentifierInfo *AliasName,
154 SourceLocation AliasLocation,
155 IdentifierInfo *ClassName,
156 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000157 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000158 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000159 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000160 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-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 Naroffb327ce02008-04-02 14:35:35 +0000172 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-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 Lattner4d391482007-12-12 07:09:47 +0000178 return 0;
179 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000180
181 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000182 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000183 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
184
185 ObjCAliasDecls[AliasName] = AliasDecl;
186 TUScope->AddDecl(AliasDecl);
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000197 if (PDecl) {
198 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000199 if (!PDecl->isForwardDecl()) {
Chris Lattner4d391482007-12-12 07:09:47 +0000200 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
201 ProtocolName->getName());
Chris Lattner439e71f2008-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 Lattner4d391482007-12-12 07:09:47 +0000205 }
Chris Lattner439e71f2008-03-16 01:25:17 +0000206
207 PDecl->setForwardDecl(false);
208 PDecl->AllocReferencedProtocols(NumProtoRefs);
209 } else {
Chris Lattnercca59d72008-03-16 01:23:04 +0000210 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattnerc8581052008-03-16 20:19:15 +0000211 ProtocolName);
212 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000213 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000214 }
Chris Lattner4d391482007-12-12 07:09:47 +0000215
216 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000217 /// Check then save referenced protocols.
Chris Lattner4d391482007-12-12 07:09:47 +0000218 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000219 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-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 Jahanianc395bda2007-12-20 19:24:10 +0000224 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000240 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner4d391482007-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
249/// ActOnForwardProtocolDeclaration -
250Action::DeclTy *
251Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
252 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000253 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000254
255 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000256 IdentifierInfo *Ident = IdentList[i];
257 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
258 if (PDecl == 0) { // Not already seen?
Chris Lattner4d391482007-12-12 07:09:47 +0000259 // FIXME: Pass in the location of the identifier!
Chris Lattnerc8581052008-03-16 20:19:15 +0000260 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000261 }
262
263 Protocols.push_back(PDecl);
264 }
Chris Lattner61f9d412008-03-16 20:34:23 +0000265 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
266 &Protocols[0], Protocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000267}
268
269Sema::DeclTy *Sema::ActOnStartCategoryInterface(
270 SourceLocation AtInterfaceLoc,
271 IdentifierInfo *ClassName, SourceLocation ClassLoc,
272 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
273 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
274 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000276
Chris Lattner61f9d412008-03-16 20:34:23 +0000277 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000278 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000279 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000280
281 /// Check that class of this category is already completely declared.
282 if (!IDecl || IDecl->isForwardDecl())
283 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
284 else {
285 /// Check for duplicate interface declaration for this category
286 ObjCCategoryDecl *CDeclChain;
287 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
288 CDeclChain = CDeclChain->getNextClassCategory()) {
289 if (CDeclChain->getIdentifier() == CategoryName) {
290 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
291 CategoryName->getName());
292 break;
293 }
Chris Lattner4d391482007-12-12 07:09:47 +0000294 }
Chris Lattner4d391482007-12-12 07:09:47 +0000295 if (!CDeclChain)
296 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000297 }
Chris Lattner4d391482007-12-12 07:09:47 +0000298
299 if (NumProtoRefs) {
Chris Lattner68c82cf2008-03-16 20:47:45 +0000300 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
301 /// Check and then save the referenced protocols.
Chris Lattner4d391482007-12-12 07:09:47 +0000302 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000303 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000304 if (!RefPDecl || RefPDecl->isForwardDecl()) {
305 Diag(CategoryLoc, diag::warn_undef_protocolref,
306 ProtoRefNames[i]->getName(),
307 CategoryName->getName());
308 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000309 if (RefPDecl)
310 RefProtocols.push_back(RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000311 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000312 if (!RefProtocols.empty())
313 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000314 }
Chris Lattner68c82cf2008-03-16 20:47:45 +0000315 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000316 return CDecl;
317}
318
319/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000320/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000321/// object.
322Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
323 SourceLocation AtCatImplLoc,
324 IdentifierInfo *ClassName, SourceLocation ClassLoc,
325 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000326 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000327 ObjCCategoryImplDecl *CDecl =
328 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000329 /// Check that class of this category is already completely declared.
330 if (!IDecl || IDecl->isForwardDecl())
331 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
332
333 /// TODO: Check that CatName, category name, is not used in another
334 // implementation.
335 return CDecl;
336}
337
338Sema::DeclTy *Sema::ActOnStartClassImplementation(
339 SourceLocation AtClassImplLoc,
340 IdentifierInfo *ClassName, SourceLocation ClassLoc,
341 IdentifierInfo *SuperClassname,
342 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000343 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000344 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000345 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000346 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000347 Diag(ClassLoc, diag::err_redefinition_different_kind,
348 ClassName->getName());
349 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
350 }
351 else {
352 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000353 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000354 if (!IDecl)
355 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
356 }
357
358 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000359 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000360 if (SuperClassname) {
361 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000362 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000363 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000364 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
365 SuperClassname->getName());
366 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
367 }
368 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000369 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000370 if (!SDecl)
371 Diag(SuperClassLoc, diag::err_undef_superclass,
372 SuperClassname->getName(), ClassName->getName());
373 else if (IDecl && IDecl->getSuperClass() != SDecl) {
374 // This implementation and its interface do not have the same
375 // super class.
376 Diag(SuperClassLoc, diag::err_conflicting_super_class,
377 SDecl->getName());
378 Diag(SDecl->getLocation(), diag::err_previous_definition);
379 }
380 }
381 }
382
383 if (!IDecl) {
384 // Legacy case of @implementation with no corresponding @interface.
385 // Build, chain & install the interface decl into the identifier.
Chris Lattner0e77ba02008-03-16 01:15:50 +0000386 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000387 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000388 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000389 IDecl->setSuperClass(SDecl);
390 IDecl->setLocEnd(ClassLoc);
391
392 // Remember that this needs to be removed when the scope is popped.
393 TUScope->AddDecl(IDecl);
394 }
395
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000397 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
398 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000399
400 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000401 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000402 // FIXME: Don't leak everything!
Chris Lattner4d391482007-12-12 07:09:47 +0000403 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
404 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000405 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000406 return IMPDecl;
407}
408
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000409void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
410 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000411 SourceLocation RBrace) {
412 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000413 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000414 if (!IDecl)
415 return;
416 /// Check case of non-existing @interface decl.
417 /// (legacy objective-c @implementation decl without an @interface decl).
418 /// Add implementations's ivar to the synthesize class's ivar list.
419 if (IDecl->ImplicitInterfaceDecl()) {
420 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
421 return;
422 }
423 // If implementation has empty ivar list, just return.
424 if (numIvars == 0)
425 return;
426
427 assert(ivars && "missing @implementation ivars");
428
429 // Check interface's Ivar list against those in the implementation.
430 // names and types must match.
431 //
Chris Lattner4d391482007-12-12 07:09:47 +0000432 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000433 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000434 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
435 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000436 ObjCIvarDecl* ImplIvar = ivars[j++];
437 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000438 assert (ImplIvar && "missing implementation ivar");
439 assert (ClsIvar && "missing class ivar");
440 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
441 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
442 ImplIvar->getIdentifier()->getName());
443 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
444 ClsIvar->getIdentifier()->getName());
445 }
446 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
447 // as error.
448 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
449 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
450 ImplIvar->getIdentifier()->getName());
451 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
452 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000453 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000454 }
455 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000456 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000457
458 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000459 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000460 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000461 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000462}
463
Steve Naroff3c2eb662008-02-10 21:38:56 +0000464void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
465 bool &IncompleteImpl) {
466 if (!IncompleteImpl) {
467 Diag(ImpLoc, diag::warn_incomplete_impl);
468 IncompleteImpl = true;
469 }
470 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
471}
472
Steve Naroffefe7f362008-02-08 22:06:17 +0000473/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000474/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000475void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
476 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000477 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000478 const llvm::DenseSet<Selector> &InsMap,
479 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000480 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000481 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000482 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000484 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000485 method->getImplementationControl() != ObjCMethodDecl::Optional)
486 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000487 }
488 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000489 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000490 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000491 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000492 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000493 method->getImplementationControl() != ObjCMethodDecl::Optional)
494 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000495 }
Chris Lattner4d391482007-12-12 07:09:47 +0000496 // Check on this protocols's referenced protocols, recursively
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000497 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000498 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000499 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000500}
501
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000502void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
503 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000504 llvm::DenseSet<Selector> InsMap;
505 // Check and see if instance methods in class interface have been
506 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000507 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000508 E = IMPDecl->instmeth_end(); I != E; ++I)
509 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000510
511 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000512 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000513 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000514 if (!InsMap.count((*I)->getSelector()))
515 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000516
Chris Lattner4d391482007-12-12 07:09:47 +0000517 llvm::DenseSet<Selector> ClsMap;
518 // Check and see if class methods in class interface have been
519 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000521 E = IMPDecl->classmeth_end(); I != E; ++I)
522 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000523
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000524 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000525 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000526 if (!ClsMap.count((*I)->getSelector()))
527 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000528
529 // Check the protocol list for unimplemented methods in the @implementation
530 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000531 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000532 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000533 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
534 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000535}
536
537/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
538/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000539void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
540 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000541 llvm::DenseSet<Selector> InsMap;
542 // Check and see if instance methods in category interface have been
543 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000544 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000545 E = CatImplDecl->instmeth_end(); I != E; ++I)
546 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000547
548 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000549 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000550 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000551 if (!InsMap.count((*I)->getSelector()))
552 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
553
Chris Lattner4d391482007-12-12 07:09:47 +0000554 llvm::DenseSet<Selector> ClsMap;
555 // Check and see if class methods in category interface have been
556 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000557 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000558 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
559 I != E; ++I)
560 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000561
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000562 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000563 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000564 if (!ClsMap.count((*I)->getSelector()))
565 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000566
567 // Check the protocol list for unimplemented methods in the @implementation
568 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000569 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000570 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000571 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffefe7f362008-02-08 22:06:17 +0000572 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
573 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000574 }
Chris Lattner4d391482007-12-12 07:09:47 +0000575}
576
577/// ActOnForwardClassDeclaration -
578Action::DeclTy *
579Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
580 IdentifierInfo **IdentList, unsigned NumElts)
581{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000583
584 for (unsigned i = 0; i != NumElts; ++i) {
585 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000586 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000588 Diag(AtClassLoc, diag::err_redefinition_different_kind,
589 IdentList[i]->getName());
590 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
591 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000593 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0e77ba02008-03-16 01:15:50 +0000594 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000595 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000596 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000597
598 // Remember that this needs to be removed when the scope is popped.
599 TUScope->AddDecl(IDecl);
600 }
601
602 Interfaces.push_back(IDecl);
603 }
604
Chris Lattner61f9d412008-03-16 20:34:23 +0000605 return ObjCClassDecl::Create(Context, AtClassLoc,
606 &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000607}
608
609
610/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
611/// returns true, or false, accordingly.
612/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000613bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
614 const ObjCMethodDecl *PrevMethod) {
Chris Lattner4d391482007-12-12 07:09:47 +0000615 if (Method->getResultType().getCanonicalType() !=
616 PrevMethod->getResultType().getCanonicalType())
617 return false;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000618 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +0000619 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
620 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000621 if (Context.getCanonicalType(ParamDecl->getType()) !=
622 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000623 return false;
624 }
625 return true;
626}
627
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
629 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000630 if (!FirstMethod.Method) {
631 // Haven't seen a method with this selector name yet - add it.
632 FirstMethod.Method = Method;
633 FirstMethod.Next = 0;
634 } else {
635 // We've seen a method with this name, now check the type signature(s).
636 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
637
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000638 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000639 Next = Next->Next)
640 match = MatchTwoMethodDeclarations(Method, Next->Method);
641
642 if (!match) {
643 // We have a new signature for an existing method - add it.
644 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000645 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000646 FirstMethod.Next = OMI;
647 }
648 }
649}
650
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000651void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
652 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000653 if (!FirstMethod.Method) {
654 // Haven't seen a method with this selector name yet - add it.
655 FirstMethod.Method = Method;
656 FirstMethod.Next = 0;
657 } else {
658 // We've seen a method with this name, now check the type signature(s).
659 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
660
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000661 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000662 Next = Next->Next)
663 match = MatchTwoMethodDeclarations(Method, Next->Method);
664
665 if (!match) {
666 // We have a new signature for an existing method - add it.
667 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000668 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000669 FirstMethod.Next = OMI;
670 }
671 }
672}
673
Steve Naroffa56f6162007-12-18 01:30:32 +0000674// Note: For class/category implemenations, allMethods/allProperties is
675// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000676void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
677 DeclTy **allMethods, unsigned allNum,
678 DeclTy **allProperties, unsigned pNum) {
679 Decl *ClassDecl = static_cast<Decl *>(classDecl);
680
Steve Naroffa56f6162007-12-18 01:30:32 +0000681 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
682 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000683 // should be true.
684 if (!ClassDecl)
685 return;
686
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
688 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000689
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
691 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000692
693 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000694 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
695 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000697
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000698 if (pNum != 0)
Chris Lattner55d13b42008-03-16 21:23:50 +0000699 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
700 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000701 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
702 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
703 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
704 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000705 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000706 assert(false && "ActOnAtEnd - property declaration misplaced");
Chris Lattner4d391482007-12-12 07:09:47 +0000707
708 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000709 ObjCMethodDecl *Method =
710 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000711
712 if (!Method) continue; // Already issued a diagnostic.
713 if (Method->isInstance()) {
714 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000715 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000716 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
717 : false;
718 if (isInterfaceDeclKind && PrevMethod && !match
719 || checkIdenticalMethods && match) {
720 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
721 Method->getSelector().getName());
722 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
723 } else {
724 insMethods.push_back(Method);
725 InsMap[Method->getSelector()] = Method;
726 /// The following allows us to typecheck messages to "id".
727 AddInstanceMethodToGlobalPool(Method);
728 }
729 }
730 else {
731 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000732 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000733 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
734 : false;
735 if (isInterfaceDeclKind && PrevMethod && !match
736 || checkIdenticalMethods && match) {
737 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
738 Method->getSelector().getName());
739 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
740 } else {
741 clsMethods.push_back(Method);
742 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000743 /// The following allows us to typecheck messages to "Class".
744 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000745 }
746 }
747 }
748
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000749 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000750 I->addMethods(&insMethods[0], insMethods.size(),
751 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000752 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000753 P->addMethods(&insMethods[0], insMethods.size(),
754 &clsMethods[0], clsMethods.size(), AtEndLoc);
755 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000756 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000757 C->addMethods(&insMethods[0], insMethods.size(),
758 &clsMethods[0], clsMethods.size(), AtEndLoc);
759 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760 else if (ObjCImplementationDecl *IC =
761 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000762 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000764 ImplMethodsVsClassMethods(IC, IDecl);
765 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000766 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000767 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000768 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000769 // Find category interface decl and then check that all methods declared
770 // in this interface is implemented in the category @implementation.
771 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000772 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000773 Categories; Categories = Categories->getNextClassCategory()) {
774 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
775 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
776 break;
777 }
778 }
779 }
780 }
781}
782
783
784/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
785/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000786static Decl::ObjCDeclQualifier
787CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
788 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
789 if (PQTVal & ObjCDeclSpec::DQ_In)
790 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
791 if (PQTVal & ObjCDeclSpec::DQ_Inout)
792 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
793 if (PQTVal & ObjCDeclSpec::DQ_Out)
794 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
795 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
796 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
797 if (PQTVal & ObjCDeclSpec::DQ_Byref)
798 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
799 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
800 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000801
802 return ret;
803}
804
805Sema::DeclTy *Sema::ActOnMethodDeclaration(
806 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000807 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000809 Selector Sel,
810 // optional arguments. The number of types/arguments is obtained
811 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000813 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
814 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000815 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +0000816
817 // Make sure we can establish a context for the method.
818 if (!ClassDecl) {
819 Diag(MethodLoc, diag::error_missing_method_context);
820 return 0;
821 }
Chris Lattner4d391482007-12-12 07:09:47 +0000822 QualType resultDeclType;
823
824 if (ReturnType)
825 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
826 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000827 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000828
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000829 ObjCMethodDecl* ObjCMethod =
830 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerb06fa3b2008-03-16 00:58:16 +0000831 ClassDecl, AttrList,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000832 MethodType == tok::minus, isVariadic,
833 MethodDeclKind == tok::objc_optional ?
834 ObjCMethodDecl::Optional :
835 ObjCMethodDecl::Required);
836
Chris Lattner0ed844b2008-04-04 06:12:32 +0000837 llvm::SmallVector<ParmVarDecl*, 16> Params;
838
839 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
840 // FIXME: arg->AttrList must be stored too!
841 QualType argType;
842
843 if (ArgTypes[i])
844 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
845 else
846 argType = Context.getObjCIdType();
847 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
848 SourceLocation(/*FIXME*/),
849 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +0000850 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000851 Param->setObjCDeclQualifier(
852 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
853 Params.push_back(Param);
854 }
855
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000856 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
857 ObjCMethod->setObjCDeclQualifier(
858 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
859 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000860
861 // For implementations (which can be very "coarse grain"), we add the
862 // method now. This allows the AST to implement lookup methods that work
863 // incrementally (without waiting until we parse the @end). It also allows
864 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000865 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000866 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000867 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000868 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000869 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000870 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000871 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000872 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000873 }
874 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000876 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000877 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000878 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000880 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000881 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000883 }
884 }
885 if (PrevMethod) {
886 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000887 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
888 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000889 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
890 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000891 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +0000892}
893
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000894Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
895 FieldDeclarator &FD,
896 ObjCDeclSpec &ODS) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000897 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000898 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
899 FD.D.getIdentifier(), T);
Chris Lattner4d391482007-12-12 07:09:47 +0000900
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000901 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000902 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +0000903
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000904 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000905 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000906 PDecl->setGetterName(ODS.getGetterName());
Chris Lattner4d391482007-12-12 07:09:47 +0000907 }
908
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000909 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000910 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000911 PDecl->setSetterName(ODS.getSetterName());
Chris Lattner4d391482007-12-12 07:09:47 +0000912 }
913
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000914 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000915 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +0000916
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000917 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +0000919
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000920 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000921 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +0000922
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000923 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000924 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +0000925
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +0000926 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000927 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +0000928
Chris Lattner4d391482007-12-12 07:09:47 +0000929 return PDecl;
930}
931
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000932/// ActOnPropertyImplDecl - This routine performs semantic checks and
933/// builds the AST node for a property implementation declaration; declared
934/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000935///
936Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
937 SourceLocation PropertyLoc,
938 bool Synthesize,
939 DeclTy *ClassCatImpDecl,
940 IdentifierInfo *PropertyId,
941 IdentifierInfo *PropertyIvar) {
942 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
943 // Make sure we have a context for the property implementation declaration.
944 if (!ClassImpDecl) {
945 Diag(AtLoc, diag::error_missing_property_context);
946 return 0;
947 }
948 ObjCPropertyDecl *property = 0;
949 ObjCInterfaceDecl* IDecl = 0;
950 // Find the class or category class where this property must have
951 // a declaration.
952 if (ObjCImplementationDecl *IC =
953 dyn_cast<ObjCImplementationDecl>(ClassImpDecl)) {
954 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +0000955 // We always synthesize an interface for an implementation
956 // without an interface decl. So, IDecl is always non-zero.
957 assert(IDecl &&
958 "ActOnPropertyImplDecl - @implementation without @interface");
959
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000960 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000961 property = IDecl->FindPropertyDeclaration(PropertyId);
962 if (!property) {
963 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000964 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000965 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000966 }
967 else if (ObjCCategoryImplDecl* CatImplClass =
968 dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +0000969 if (Synthesize) {
970 Diag(AtLoc, diag::error_synthesize_category_decl);
971 return 0;
972 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000973 IDecl = CatImplClass->getClassInterface();
974 if (!IDecl) {
975 Diag(AtLoc, diag::error_missing_property_interface);
976 return 0;
977 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000978 ObjCCategoryDecl *Category =
979 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
980
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000981 // If category for this implementation not found, it is an error which
982 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000983 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000984 return 0;
985 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000986 property = Category->FindPropertyDeclaration(PropertyId);
987 if (!property) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +0000988 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanian559c0c42008-04-21 19:04:53 +0000989 Category->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +0000990 return 0;
991 }
992 }
993 else {
994 Diag(AtLoc, diag::error_bad_property_context);
995 return 0;
996 }
997
998 // Check that we have a valid, previously declared ivar for @synthesize
999 if (Synthesize) {
1000 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001001 if (!PropertyIvar)
1002 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001003 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001004 ObjCIvarDecl *Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
1005 if (!Ivar) {
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001006 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1007 PropertyId->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001008 return 0;
1009 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001010 // Check that type of property and its ivar match.
1011 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1012 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1013 Ivar->getName());
1014 return 0;
1015 }
1016
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001017 } else if (PropertyIvar) {
1018 // @dynamic
1019 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1020 return 0;
1021 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001022 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1023 // TODO: Build the property implementation AST, pushes it into its
1024 // class/cateogory implementation's vector of property implementations
1025#if 0
1026 ObjCCategoryImplDecl *PIDecl =
1027 ObjCCategoryImplDecl::Create(AtLoc, PropertyLoc, property,
1028 Synthesize ? ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1029 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC,
1030 PropertyId, PropertyIvar);
1031#endif
1032 return 0;
1033}
Chris Lattner7f925cc2008-04-11 07:00:53 +00001034