blob: 2385cd07bd881799b5b25fec3c4301c317db7ee9 [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;
36
37 // Create Decl objects for each parameter, entrring them in the scope for
38 // binding to their use.
39 struct DeclaratorChunk::ParamInfo PI;
40
41 // Insert the invisible arguments, self and _cmd!
42 PI.Ident = &Context.Idents.get("self");
43 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
44 PI.InvalidType = false;
Nate Begeman9f3c4bb2008-02-17 21:20:31 +000045 PI.AttrList = 0;
Gabor Greif9b2e0f92008-02-29 20:35:55 +000046 PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr();
47
Chris Lattner855e51f2007-12-12 07:09:47 +000048 if (MDecl->isInstance()) {
Gabor Greif9b2e0f92008-02-29 20:35:55 +000049 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
50 // There may be no interface context due to error in declaration of the
51 // interface (which has been reported). Recover gracefully
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000052 QualType selfTy = Context.getObjCInterfaceType(OID);
53 selfTy = Context.getPointerType(selfTy);
54 PI.TypeInfo = selfTy.getAsOpaquePtr();
55 }
Gabor Greif9b2e0f92008-02-29 20:35:55 +000056 }
57
Chris Lattner855e51f2007-12-12 07:09:47 +000058 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
59
60 PI.Ident = &Context.Idents.get("_cmd");
Ted Kremenek42730c52008-01-07 19:49:32 +000061 PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr();
Chris Lattner855e51f2007-12-12 07:09:47 +000062 ActOnParamDeclarator(PI, FnBodyScope);
63
Chris Lattner685d7922008-03-16 01:07:14 +000064 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
66 PI.Ident = PDecl->getIdentifier();
67 PI.IdentLoc = PDecl->getLocation(); // user vars have a real location.
68 PI.TypeInfo = PDecl->getType().getAsOpaquePtr();
Fariborz Jahanian00a313b2008-01-21 22:59:53 +000069 MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope));
Chris Lattner855e51f2007-12-12 07:09:47 +000070 }
71}
72
73Sema::DeclTy *Sema::ActOnStartClassInterface(
74 SourceLocation AtInterfaceLoc,
75 IdentifierInfo *ClassName, SourceLocation ClassLoc,
76 IdentifierInfo *SuperName, SourceLocation SuperLoc,
77 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
78 SourceLocation EndProtoLoc, AttributeList *AttrList) {
79 assert(ClassName && "Missing class identifier");
80
81 // Check for another declaration kind with the same name.
Steve Naroffe57c21a2008-04-01 23:04:06 +000082 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, ClassLoc,TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000083 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000084 Diag(ClassLoc, diag::err_redefinition_different_kind,
85 ClassName->getName());
86 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
87 }
88
Ted Kremenek42730c52008-01-07 19:49:32 +000089 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000090 if (IDecl) {
91 // Class already seen. Is it a forward declaration?
92 if (!IDecl->isForwardDecl())
93 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
94 else {
95 IDecl->setLocation(AtInterfaceLoc);
96 IDecl->setForwardDecl(false);
97 IDecl->AllocIntfRefProtocols(NumProtocols);
98 }
99 }
100 else {
Chris Lattner0db541b2008-03-16 01:15:50 +0000101 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
102 ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000103
104 // Chain & install the interface decl into the identifier.
105 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
106 ClassName->setFETokenInfo(IDecl);
107
108 // Remember that this needs to be removed when the scope is popped.
109 TUScope->AddDecl(IDecl);
110 }
111
112 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000114 // Check if a different kind of symbol declared in this scope.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000115 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, SuperLoc, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000116 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000117 Diag(SuperLoc, diag::err_redefinition_different_kind,
118 SuperName->getName());
119 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
120 }
121 else {
122 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000123 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000124
125 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
126 Diag(AtInterfaceLoc, diag::err_undef_superclass,
127 SuperClassEntry ? SuperClassEntry->getName()
128 : SuperName->getName(),
129 ClassName->getName());
130 }
131 }
132 IDecl->setSuperClass(SuperClassEntry);
133 IDecl->setLocEnd(SuperLoc);
134 } else { // we have a root class.
135 IDecl->setLocEnd(ClassLoc);
136 }
137
138 /// Check then save referenced protocols
139 if (NumProtocols) {
140 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000141 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000142 if (!RefPDecl || RefPDecl->isForwardDecl())
143 Diag(ClassLoc, diag::warn_undef_protocolref,
144 ProtocolNames[i]->getName(),
145 ClassName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000146 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000147 }
148 IDecl->setLocEnd(EndProtoLoc);
149 }
150 return IDecl;
151}
152
153/// ActOnCompatiblityAlias - this action is called after complete parsing of
154/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000155Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
156 IdentifierInfo *AliasName,
157 SourceLocation AliasLocation,
158 IdentifierInfo *ClassName,
159 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 // Look for previous declaration of alias name
Steve Naroffe57c21a2008-04-01 23:04:06 +0000161 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary,
162 AliasLocation, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000163 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000164 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000165 Diag(AliasLocation, diag::warn_previous_alias_decl);
166 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
167 }
168 else {
169 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
170 AliasName->getName());
171 Diag(ADecl->getLocation(), diag::err_previous_declaration);
172 }
173 return 0;
174 }
175 // Check for class declaration
Steve Naroffe57c21a2008-04-01 23:04:06 +0000176 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary,
177 ClassLocation, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000178 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
179 if (CDecl == 0) {
180 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
181 if (CDeclU)
182 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000183 return 0;
184 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000185
186 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000187 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000188 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
189
190 ObjCAliasDecls[AliasName] = AliasDecl;
191 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000192 return AliasDecl;
193}
194
195Sema::DeclTy *Sema::ActOnStartProtocolInterface(
196 SourceLocation AtProtoInterfaceLoc,
197 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
198 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
199 SourceLocation EndProtoLoc) {
200 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000201 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000202 if (PDecl) {
203 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000204 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000205 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
206 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000207 // Just return the protocol we already had.
208 // FIXME: don't leak the objects passed in!
209 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000210 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000211
212 PDecl->setForwardDecl(false);
213 PDecl->AllocReferencedProtocols(NumProtoRefs);
214 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000215 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000216 ProtocolName);
217 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000218 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000219 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000220
221 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000222 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000223 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000224 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 if (!RefPDecl || RefPDecl->isForwardDecl())
226 Diag(ProtocolLoc, diag::warn_undef_protocolref,
227 ProtoRefNames[i]->getName(),
228 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000229 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000230 }
231 PDecl->setLocEnd(EndProtoLoc);
232 }
233 return PDecl;
234}
235
236/// FindProtocolDeclaration - This routine looks up protocols and
237/// issuer error if they are not declared. It returns list of protocol
238/// declarations in its 'Protocols' argument.
239void
240Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
241 IdentifierInfo **ProtocolId,
242 unsigned NumProtocols,
243 llvm::SmallVector<DeclTy *,8> &Protocols) {
244 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000245 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000246 if (!PDecl)
247 Diag(TypeLoc, diag::err_undeclared_protocol,
248 ProtocolId[i]->getName());
249 else
250 Protocols.push_back(PDecl);
251 }
252}
253
254/// ActOnForwardProtocolDeclaration -
255Action::DeclTy *
256Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
257 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000258 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000259
260 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000261 IdentifierInfo *Ident = IdentList[i];
262 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
263 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000264 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000265 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000266 }
267
268 Protocols.push_back(PDecl);
269 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000270 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
271 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000272}
273
274Sema::DeclTy *Sema::ActOnStartCategoryInterface(
275 SourceLocation AtInterfaceLoc,
276 IdentifierInfo *ClassName, SourceLocation ClassLoc,
277 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
278 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
279 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000280 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000281
Chris Lattnere29dc832008-03-16 20:34:23 +0000282 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000283 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000284 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000285
286 /// Check that class of this category is already completely declared.
287 if (!IDecl || IDecl->isForwardDecl())
288 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
289 else {
290 /// Check for duplicate interface declaration for this category
291 ObjCCategoryDecl *CDeclChain;
292 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
293 CDeclChain = CDeclChain->getNextClassCategory()) {
294 if (CDeclChain->getIdentifier() == CategoryName) {
295 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
296 CategoryName->getName());
297 break;
298 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000299 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000300 if (!CDeclChain)
301 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000302 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000303
304 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000305 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
306 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000307 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000308 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000309 if (!RefPDecl || RefPDecl->isForwardDecl()) {
310 Diag(CategoryLoc, diag::warn_undef_protocolref,
311 ProtoRefNames[i]->getName(),
312 CategoryName->getName());
313 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000314 if (RefPDecl)
315 RefProtocols.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000316 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000317 if (!RefProtocols.empty())
318 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000319 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000320 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000321 return CDecl;
322}
323
324/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000325/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000326/// object.
327Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
328 SourceLocation AtCatImplLoc,
329 IdentifierInfo *ClassName, SourceLocation ClassLoc,
330 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000331 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000332 ObjCCategoryImplDecl *CDecl =
333 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000334 /// Check that class of this category is already completely declared.
335 if (!IDecl || IDecl->isForwardDecl())
336 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
337
338 /// TODO: Check that CatName, category name, is not used in another
339 // implementation.
340 return CDecl;
341}
342
343Sema::DeclTy *Sema::ActOnStartClassImplementation(
344 SourceLocation AtClassImplLoc,
345 IdentifierInfo *ClassName, SourceLocation ClassLoc,
346 IdentifierInfo *SuperClassname,
347 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000348 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000349 // Check for another declaration kind with the same name.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000350 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, ClassLoc,TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000351 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000352 Diag(ClassLoc, diag::err_redefinition_different_kind,
353 ClassName->getName());
354 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
355 }
356 else {
357 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000358 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000359 if (!IDecl)
360 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
361 }
362
363 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000364 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000365 if (SuperClassname) {
366 // Check if a different kind of symbol declared in this scope.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000367 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary,
368 SuperClassLoc, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000369 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000370 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
371 SuperClassname->getName());
372 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
373 }
374 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000375 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000376 if (!SDecl)
377 Diag(SuperClassLoc, diag::err_undef_superclass,
378 SuperClassname->getName(), ClassName->getName());
379 else if (IDecl && IDecl->getSuperClass() != SDecl) {
380 // This implementation and its interface do not have the same
381 // super class.
382 Diag(SuperClassLoc, diag::err_conflicting_super_class,
383 SDecl->getName());
384 Diag(SDecl->getLocation(), diag::err_previous_definition);
385 }
386 }
387 }
388
389 if (!IDecl) {
390 // Legacy case of @implementation with no corresponding @interface.
391 // Build, chain & install the interface decl into the identifier.
Chris Lattner0db541b2008-03-16 01:15:50 +0000392 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
393 false, true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000394 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
395 ClassName->setFETokenInfo(IDecl);
396 IDecl->setSuperClass(SDecl);
397 IDecl->setLocEnd(ClassLoc);
398
399 // Remember that this needs to be removed when the scope is popped.
400 TUScope->AddDecl(IDecl);
401 }
402
Ted Kremenek42730c52008-01-07 19:49:32 +0000403 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000404 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
405 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000406
407 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000408 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000409 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000410 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
411 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000413 return IMPDecl;
414}
415
Ted Kremenek42730c52008-01-07 19:49:32 +0000416void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
417 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000418 SourceLocation RBrace) {
419 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000420 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000421 if (!IDecl)
422 return;
423 /// Check case of non-existing @interface decl.
424 /// (legacy objective-c @implementation decl without an @interface decl).
425 /// Add implementations's ivar to the synthesize class's ivar list.
426 if (IDecl->ImplicitInterfaceDecl()) {
427 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
428 return;
429 }
430 // If implementation has empty ivar list, just return.
431 if (numIvars == 0)
432 return;
433
434 assert(ivars && "missing @implementation ivars");
435
436 // Check interface's Ivar list against those in the implementation.
437 // names and types must match.
438 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000439 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000440 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000441 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
442 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000443 ObjCIvarDecl* ImplIvar = ivars[j++];
444 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000445 assert (ImplIvar && "missing implementation ivar");
446 assert (ClsIvar && "missing class ivar");
447 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
448 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
449 ImplIvar->getIdentifier()->getName());
450 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
451 ClsIvar->getIdentifier()->getName());
452 }
453 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
454 // as error.
455 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
456 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
457 ImplIvar->getIdentifier()->getName());
458 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
459 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000460 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000461 }
462 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000463 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000464
465 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000466 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000467 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000468 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000469}
470
Steve Naroffb4f48512008-02-10 21:38:56 +0000471void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
472 bool &IncompleteImpl) {
473 if (!IncompleteImpl) {
474 Diag(ImpLoc, diag::warn_incomplete_impl);
475 IncompleteImpl = true;
476 }
477 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
478}
479
Steve Naroffb268d2a2008-02-08 22:06:17 +0000480/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000481/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000482void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
483 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000484 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000485 const llvm::DenseSet<Selector> &InsMap,
486 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000487 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000488 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000489 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000490 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000491 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000492 method->getImplementationControl() != ObjCMethodDecl::Optional)
493 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000494 }
495 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000496 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000497 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000498 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000499 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000500 method->getImplementationControl() != ObjCMethodDecl::Optional)
501 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000502 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000504 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000505 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000506 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000507}
508
Ted Kremenek42730c52008-01-07 19:49:32 +0000509void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
510 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000511 llvm::DenseSet<Selector> InsMap;
512 // Check and see if instance methods in class interface have been
513 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000514 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000515 E = IMPDecl->instmeth_end(); I != E; ++I)
516 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000517
518 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000519 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000520 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000521 if (!InsMap.count((*I)->getSelector()))
522 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000523
Chris Lattner855e51f2007-12-12 07:09:47 +0000524 llvm::DenseSet<Selector> ClsMap;
525 // Check and see if class methods in class interface have been
526 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000527 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000528 E = IMPDecl->classmeth_end(); I != E; ++I)
529 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000530
Ted Kremenek42730c52008-01-07 19:49:32 +0000531 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000532 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000533 if (!ClsMap.count((*I)->getSelector()))
534 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000535
536 // Check the protocol list for unimplemented methods in the @implementation
537 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000538 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000539 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000540 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
541 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000542}
543
544/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
545/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000546void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
547 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000548 llvm::DenseSet<Selector> InsMap;
549 // Check and see if instance methods in category interface have been
550 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000551 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000552 E = CatImplDecl->instmeth_end(); I != E; ++I)
553 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000554
555 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000556 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000557 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000558 if (!InsMap.count((*I)->getSelector()))
559 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
560
Chris Lattner855e51f2007-12-12 07:09:47 +0000561 llvm::DenseSet<Selector> ClsMap;
562 // Check and see if class methods in category interface have been
563 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000564 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000565 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
566 I != E; ++I)
567 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000568
Ted Kremenek42730c52008-01-07 19:49:32 +0000569 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000570 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000571 if (!ClsMap.count((*I)->getSelector()))
572 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000573
574 // Check the protocol list for unimplemented methods in the @implementation
575 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000576 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000577 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000578 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000579 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
580 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000581 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000582}
583
584/// ActOnForwardClassDeclaration -
585Action::DeclTy *
586Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
587 IdentifierInfo **IdentList, unsigned NumElts)
588{
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000590
591 for (unsigned i = 0; i != NumElts; ++i) {
592 // Check for another declaration kind with the same name.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000593 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary,
594 AtClassLoc, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000595 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000596 Diag(AtClassLoc, diag::err_redefinition_different_kind,
597 IdentList[i]->getName());
598 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
599 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000600 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000601 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0db541b2008-03-16 01:15:50 +0000602 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
603 true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000604 // Chain & install the interface decl into the identifier.
605 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
606 IdentList[i]->setFETokenInfo(IDecl);
607
608 // Remember that this needs to be removed when the scope is popped.
609 TUScope->AddDecl(IDecl);
610 }
611
612 Interfaces.push_back(IDecl);
613 }
614
Chris Lattnere29dc832008-03-16 20:34:23 +0000615 return ObjCClassDecl::Create(Context, AtClassLoc,
616 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000617}
618
619
620/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
621/// returns true, or false, accordingly.
622/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000623bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
624 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000625 if (Method->getResultType().getCanonicalType() !=
626 PrevMethod->getResultType().getCanonicalType())
627 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000628 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000629 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
630 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
631 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
632 return false;
633 }
634 return true;
635}
636
Ted Kremenek42730c52008-01-07 19:49:32 +0000637void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
638 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000639 if (!FirstMethod.Method) {
640 // Haven't seen a method with this selector name yet - add it.
641 FirstMethod.Method = Method;
642 FirstMethod.Next = 0;
643 } else {
644 // We've seen a method with this name, now check the type signature(s).
645 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
646
Ted Kremenek42730c52008-01-07 19:49:32 +0000647 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000648 Next = Next->Next)
649 match = MatchTwoMethodDeclarations(Method, Next->Method);
650
651 if (!match) {
652 // We have a new signature for an existing method - add it.
653 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000654 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000655 FirstMethod.Next = OMI;
656 }
657 }
658}
659
Ted Kremenek42730c52008-01-07 19:49:32 +0000660void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
661 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000662 if (!FirstMethod.Method) {
663 // Haven't seen a method with this selector name yet - add it.
664 FirstMethod.Method = Method;
665 FirstMethod.Next = 0;
666 } else {
667 // We've seen a method with this name, now check the type signature(s).
668 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
669
Ted Kremenek42730c52008-01-07 19:49:32 +0000670 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000671 Next = Next->Next)
672 match = MatchTwoMethodDeclarations(Method, Next->Method);
673
674 if (!match) {
675 // We have a new signature for an existing method - add it.
676 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000677 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000678 FirstMethod.Next = OMI;
679 }
680 }
681}
682
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000683// Note: For class/category implemenations, allMethods/allProperties is
684// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000685void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
686 DeclTy **allMethods, unsigned allNum,
687 DeclTy **allProperties, unsigned pNum) {
688 Decl *ClassDecl = static_cast<Decl *>(classDecl);
689
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000690 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
691 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000692 // should be true.
693 if (!ClassDecl)
694 return;
695
Ted Kremenek42730c52008-01-07 19:49:32 +0000696 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
697 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000698
Ted Kremenek42730c52008-01-07 19:49:32 +0000699 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
700 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000701
702 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000703 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
704 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000705 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000706
707 // TODO: property declaration in category and protocols.
Chris Lattner2d1c4312008-03-16 21:17:37 +0000708 if (pNum != 0)
Chris Lattnercffe3662008-03-16 21:23:50 +0000709 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
710 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Chris Lattner855e51f2007-12-12 07:09:47 +0000711
712 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000713 ObjCMethodDecl *Method =
714 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000715
716 if (!Method) continue; // Already issued a diagnostic.
717 if (Method->isInstance()) {
718 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000719 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000720 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
721 : false;
722 if (isInterfaceDeclKind && PrevMethod && !match
723 || checkIdenticalMethods && match) {
724 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
725 Method->getSelector().getName());
726 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
727 } else {
728 insMethods.push_back(Method);
729 InsMap[Method->getSelector()] = Method;
730 /// The following allows us to typecheck messages to "id".
731 AddInstanceMethodToGlobalPool(Method);
732 }
733 }
734 else {
735 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000736 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000737 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
738 : false;
739 if (isInterfaceDeclKind && PrevMethod && !match
740 || checkIdenticalMethods && match) {
741 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
742 Method->getSelector().getName());
743 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
744 } else {
745 clsMethods.push_back(Method);
746 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000747 /// The following allows us to typecheck messages to "Class".
748 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000749 }
750 }
751 }
752
Ted Kremenek42730c52008-01-07 19:49:32 +0000753 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000754 I->addMethods(&insMethods[0], insMethods.size(),
755 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000756 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000757 P->addMethods(&insMethods[0], insMethods.size(),
758 &clsMethods[0], clsMethods.size(), AtEndLoc);
759 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000760 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 C->addMethods(&insMethods[0], insMethods.size(),
762 &clsMethods[0], clsMethods.size(), AtEndLoc);
763 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000764 else if (ObjCImplementationDecl *IC =
765 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000766 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000767 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000768 ImplMethodsVsClassMethods(IC, IDecl);
769 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000770 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000771 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000772 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000773 // Find category interface decl and then check that all methods declared
774 // in this interface is implemented in the category @implementation.
775 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000776 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000777 Categories; Categories = Categories->getNextClassCategory()) {
778 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
779 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
780 break;
781 }
782 }
783 }
784 }
785}
786
787
788/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
789/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000790static Decl::ObjCDeclQualifier
791CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
792 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
793 if (PQTVal & ObjCDeclSpec::DQ_In)
794 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
795 if (PQTVal & ObjCDeclSpec::DQ_Inout)
796 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
797 if (PQTVal & ObjCDeclSpec::DQ_Out)
798 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
799 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
800 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
801 if (PQTVal & ObjCDeclSpec::DQ_Byref)
802 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
803 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
804 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000805
806 return ret;
807}
808
809Sema::DeclTy *Sema::ActOnMethodDeclaration(
810 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000811 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000812 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000813 Selector Sel,
814 // optional arguments. The number of types/arguments is obtained
815 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000816 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000817 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
818 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000819 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000820
821 // Make sure we can establish a context for the method.
822 if (!ClassDecl) {
823 Diag(MethodLoc, diag::error_missing_method_context);
824 return 0;
825 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000826 llvm::SmallVector<ParmVarDecl*, 16> Params;
827
828 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
829 // FIXME: arg->AttrList must be stored too!
830 QualType argType;
831
832 if (ArgTypes[i])
833 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
834 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 argType = Context.getObjCIdType();
Chris Lattner58114f02008-03-15 21:32:50 +0000836 ParmVarDecl* Param = ParmVarDecl::Create(Context, SourceLocation(/*FIXME*/),
Chris Lattner48d225c2008-03-15 21:10:16 +0000837 ArgNames[i], argType,
Chris Lattner58114f02008-03-15 21:32:50 +0000838 VarDecl::None, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000839 Param->setObjCDeclQualifier(
840 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
Chris Lattner855e51f2007-12-12 07:09:47 +0000841 Params.push_back(Param);
842 }
843 QualType resultDeclType;
844
845 if (ReturnType)
846 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
847 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000848 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000849
Chris Lattner114add62008-03-16 00:49:28 +0000850 ObjCMethodDecl* ObjCMethod =
851 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000852 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000853 MethodType == tok::minus, isVariadic,
854 MethodDeclKind == tok::objc_optional ?
855 ObjCMethodDecl::Optional :
856 ObjCMethodDecl::Required);
857
Ted Kremenek42730c52008-01-07 19:49:32 +0000858 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
859 ObjCMethod->setObjCDeclQualifier(
860 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
861 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000862
863 // For implementations (which can be very "coarse grain"), we add the
864 // method now. This allows the AST to implement lookup methods that work
865 // incrementally (without waiting until we parse the @end). It also allows
866 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000867 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000868 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000869 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000870 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000871 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000872 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000873 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000874 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000875 }
876 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000877 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000878 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000879 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000880 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000882 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000883 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000884 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000885 }
886 }
887 if (PrevMethod) {
888 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
890 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000891 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
892 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000893 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +0000894}
895
Ted Kremenek42730c52008-01-07 19:49:32 +0000896Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
Chris Lattner44859612008-03-17 01:19:02 +0000897 DeclTy **allProperties,
898 unsigned NumProperties,
899 ObjCDeclSpec &DS) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000900 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000901
Chris Lattner44859612008-03-17 01:19:02 +0000902 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +0000904
Chris Lattner44859612008-03-17 01:19:02 +0000905 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000907 PDecl->setGetterName(DS.getGetterName());
908 }
909
Chris Lattner44859612008-03-17 01:19:02 +0000910 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000911 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000912 PDecl->setSetterName(DS.getSetterName());
913 }
914
Chris Lattner44859612008-03-17 01:19:02 +0000915 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +0000916 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +0000917
Chris Lattner44859612008-03-17 01:19:02 +0000918 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +0000919 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +0000920
Chris Lattner44859612008-03-17 01:19:02 +0000921 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +0000923
Chris Lattner44859612008-03-17 01:19:02 +0000924 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +0000925 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +0000926
Chris Lattner44859612008-03-17 01:19:02 +0000927 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +0000928 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +0000929
Chris Lattner44859612008-03-17 01:19:02 +0000930 if (NumProperties != 0)
931 PDecl->setPropertyDeclLists((ObjCIvarDecl**)allProperties, NumProperties);
932
Chris Lattner855e51f2007-12-12 07:09:47 +0000933 return PDecl;
934}
935