blob: 127645044c3df2939dcb504a4682b907b60a39fa [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner855e51f2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremenek42730c52008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Chris Lattner855e51f2007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremenek42730c52008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner855e51f2007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Narofffe9eb6a2007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
Chris Lattnerf3874bc2008-04-06 04:47:34 +000036 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000037
38 // Create Decl objects for each parameter, entrring them in the scope for
39 // binding to their use.
40 struct DeclaratorChunk::ParamInfo PI;
41
42 // Insert the invisible arguments, self and _cmd!
43 PI.Ident = &Context.Idents.get("self");
44 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Chris Lattner3e254fb2008-04-08 04:40:51 +000045 QualType selfTy = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +000046 if (MDecl->isInstance()) {
Gabor Greif9b2e0f92008-02-29 20:35:55 +000047 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
48 // There may be no interface context due to error in declaration of the
49 // interface (which has been reported). Recover gracefully
Chris Lattner3e254fb2008-04-08 04:40:51 +000050 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000051 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000052 }
Gabor Greif9b2e0f92008-02-29 20:35:55 +000053 }
Chris Lattner3e254fb2008-04-08 04:40:51 +000054 CurMethodDecl->setSelfDecl(CreateImplicitParameter(FnBodyScope, PI.Ident,
55 PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000056
57 PI.Ident = &Context.Idents.get("_cmd");
Chris Lattner3e254fb2008-04-08 04:40:51 +000058 CreateImplicitParameter(FnBodyScope, PI.Ident, PI.IdentLoc,
59 Context.getObjCSelType());
60
Chris Lattner97316c02008-04-10 02:22:51 +000061 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000062 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000063 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000064 IdentifierInfo *II = PDecl->getIdentifier();
65 if (II) {
Chris Lattner2a1e2ed2008-04-11 07:00:53 +000066 IdResolver.AddDecl(PDecl, FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000067 FnBodyScope->AddDecl(PDecl);
68 }
Chris Lattner855e51f2007-12-12 07:09:47 +000069 }
70}
71
72Sema::DeclTy *Sema::ActOnStartClassInterface(
73 SourceLocation AtInterfaceLoc,
74 IdentifierInfo *ClassName, SourceLocation ClassLoc,
75 IdentifierInfo *SuperName, SourceLocation SuperLoc,
76 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
77 SourceLocation EndProtoLoc, AttributeList *AttrList) {
78 assert(ClassName && "Missing class identifier");
79
80 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000081 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000082 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000083 Diag(ClassLoc, diag::err_redefinition_different_kind,
84 ClassName->getName());
85 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
86 }
87
Ted Kremenek42730c52008-01-07 19:49:32 +000088 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000089 if (IDecl) {
90 // Class already seen. Is it a forward declaration?
91 if (!IDecl->isForwardDecl())
92 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
93 else {
94 IDecl->setLocation(AtInterfaceLoc);
95 IDecl->setForwardDecl(false);
96 IDecl->AllocIntfRefProtocols(NumProtocols);
97 }
98 }
99 else {
Chris Lattner0db541b2008-03-16 01:15:50 +0000100 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
101 ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102
Steve Naroff15208162008-04-02 18:30:49 +0000103 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 Diag(SuperLoc, diag::err_redefinition_different_kind,
114 SuperName->getName());
115 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
116 }
117 else {
118 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000119 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000120
121 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
122 Diag(AtInterfaceLoc, diag::err_undef_superclass,
123 SuperClassEntry ? SuperClassEntry->getName()
124 : SuperName->getName(),
125 ClassName->getName());
126 }
127 }
128 IDecl->setSuperClass(SuperClassEntry);
129 IDecl->setLocEnd(SuperLoc);
130 } else { // we have a root class.
131 IDecl->setLocEnd(ClassLoc);
132 }
133
134 /// Check then save referenced protocols
135 if (NumProtocols) {
136 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000137 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000138 if (!RefPDecl || RefPDecl->isForwardDecl())
139 Diag(ClassLoc, diag::warn_undef_protocolref,
140 ProtocolNames[i]->getName(),
141 ClassName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000142 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000143 }
144 IDecl->setLocEnd(EndProtoLoc);
145 }
146 return IDecl;
147}
148
149/// ActOnCompatiblityAlias - this action is called after complete parsing of
150/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000151Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
152 IdentifierInfo *AliasName,
153 SourceLocation AliasLocation,
154 IdentifierInfo *ClassName,
155 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000156 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000157 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000158 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000159 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 Diag(AliasLocation, diag::warn_previous_alias_decl);
161 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
162 }
163 else {
164 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
165 AliasName->getName());
166 Diag(ADecl->getLocation(), diag::err_previous_declaration);
167 }
168 return 0;
169 }
170 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000171 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000172 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
173 if (CDecl == 0) {
174 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
175 if (CDeclU)
176 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000177 return 0;
178 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000179
180 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000181 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000182 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
183
184 ObjCAliasDecls[AliasName] = AliasDecl;
185 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000186 return AliasDecl;
187}
188
189Sema::DeclTy *Sema::ActOnStartProtocolInterface(
190 SourceLocation AtProtoInterfaceLoc,
191 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
192 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
193 SourceLocation EndProtoLoc) {
194 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000195 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000196 if (PDecl) {
197 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000198 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000199 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
200 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000201 // Just return the protocol we already had.
202 // FIXME: don't leak the objects passed in!
203 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000204 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000205
206 PDecl->setForwardDecl(false);
207 PDecl->AllocReferencedProtocols(NumProtoRefs);
208 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000209 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000210 ProtocolName);
211 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000212 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000213 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000214
215 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000216 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000217 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000218 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000219 if (!RefPDecl || RefPDecl->isForwardDecl())
220 Diag(ProtocolLoc, diag::warn_undef_protocolref,
221 ProtoRefNames[i]->getName(),
222 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000223 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000224 }
225 PDecl->setLocEnd(EndProtoLoc);
226 }
227 return PDecl;
228}
229
230/// FindProtocolDeclaration - This routine looks up protocols and
231/// issuer error if they are not declared. It returns list of protocol
232/// declarations in its 'Protocols' argument.
233void
234Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
235 IdentifierInfo **ProtocolId,
236 unsigned NumProtocols,
237 llvm::SmallVector<DeclTy *,8> &Protocols) {
238 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000239 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000240 if (!PDecl)
241 Diag(TypeLoc, diag::err_undeclared_protocol,
242 ProtocolId[i]->getName());
243 else
244 Protocols.push_back(PDecl);
245 }
246}
247
248/// ActOnForwardProtocolDeclaration -
249Action::DeclTy *
250Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
251 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000252 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000253
254 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000255 IdentifierInfo *Ident = IdentList[i];
256 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
257 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000258 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000259 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000260 }
261
262 Protocols.push_back(PDecl);
263 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000264 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
265 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000266}
267
268Sema::DeclTy *Sema::ActOnStartCategoryInterface(
269 SourceLocation AtInterfaceLoc,
270 IdentifierInfo *ClassName, SourceLocation ClassLoc,
271 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
272 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
273 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000274 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000275
Chris Lattnere29dc832008-03-16 20:34:23 +0000276 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000277 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000278 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000279
280 /// Check that class of this category is already completely declared.
281 if (!IDecl || IDecl->isForwardDecl())
282 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
283 else {
284 /// Check for duplicate interface declaration for this category
285 ObjCCategoryDecl *CDeclChain;
286 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
287 CDeclChain = CDeclChain->getNextClassCategory()) {
288 if (CDeclChain->getIdentifier() == CategoryName) {
289 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
290 CategoryName->getName());
291 break;
292 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000293 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000294 if (!CDeclChain)
295 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000296 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000297
298 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000299 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
300 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000301 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000302 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000303 if (!RefPDecl || RefPDecl->isForwardDecl()) {
304 Diag(CategoryLoc, diag::warn_undef_protocolref,
305 ProtoRefNames[i]->getName(),
306 CategoryName->getName());
307 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000308 if (RefPDecl)
309 RefProtocols.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000310 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000311 if (!RefProtocols.empty())
312 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000313 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000314 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000315 return CDecl;
316}
317
318/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000319/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000320/// object.
321Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
322 SourceLocation AtCatImplLoc,
323 IdentifierInfo *ClassName, SourceLocation ClassLoc,
324 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000325 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000326 ObjCCategoryImplDecl *CDecl =
327 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000328 /// Check that class of this category is already completely declared.
329 if (!IDecl || IDecl->isForwardDecl())
330 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
331
332 /// TODO: Check that CatName, category name, is not used in another
333 // implementation.
334 return CDecl;
335}
336
337Sema::DeclTy *Sema::ActOnStartClassImplementation(
338 SourceLocation AtClassImplLoc,
339 IdentifierInfo *ClassName, SourceLocation ClassLoc,
340 IdentifierInfo *SuperClassname,
341 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000342 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000343 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000344 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000345 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000346 Diag(ClassLoc, diag::err_redefinition_different_kind,
347 ClassName->getName());
348 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
349 }
350 else {
351 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000352 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000353 if (!IDecl)
354 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
355 }
356
357 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000358 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000359 if (SuperClassname) {
360 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000361 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000362 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000363 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
364 SuperClassname->getName());
365 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
366 }
367 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000368 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000369 if (!SDecl)
370 Diag(SuperClassLoc, diag::err_undef_superclass,
371 SuperClassname->getName(), ClassName->getName());
372 else if (IDecl && IDecl->getSuperClass() != SDecl) {
373 // This implementation and its interface do not have the same
374 // super class.
375 Diag(SuperClassLoc, diag::err_conflicting_super_class,
376 SDecl->getName());
377 Diag(SDecl->getLocation(), diag::err_previous_definition);
378 }
379 }
380 }
381
382 if (!IDecl) {
383 // Legacy case of @implementation with no corresponding @interface.
384 // Build, chain & install the interface decl into the identifier.
Chris Lattner0db541b2008-03-16 01:15:50 +0000385 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
386 false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000387 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000388 IDecl->setSuperClass(SDecl);
389 IDecl->setLocEnd(ClassLoc);
390
391 // Remember that this needs to be removed when the scope is popped.
392 TUScope->AddDecl(IDecl);
393 }
394
Ted Kremenek42730c52008-01-07 19:49:32 +0000395 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000396 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
397 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000398
399 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000400 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000401 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000402 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
403 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000404 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000405 return IMPDecl;
406}
407
Ted Kremenek42730c52008-01-07 19:49:32 +0000408void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
409 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000410 SourceLocation RBrace) {
411 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000413 if (!IDecl)
414 return;
415 /// Check case of non-existing @interface decl.
416 /// (legacy objective-c @implementation decl without an @interface decl).
417 /// Add implementations's ivar to the synthesize class's ivar list.
418 if (IDecl->ImplicitInterfaceDecl()) {
419 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
420 return;
421 }
422 // If implementation has empty ivar list, just return.
423 if (numIvars == 0)
424 return;
425
426 assert(ivars && "missing @implementation ivars");
427
428 // Check interface's Ivar list against those in the implementation.
429 // names and types must match.
430 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000431 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000432 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000433 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
434 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000435 ObjCIvarDecl* ImplIvar = ivars[j++];
436 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000437 assert (ImplIvar && "missing implementation ivar");
438 assert (ClsIvar && "missing class ivar");
439 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
440 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
441 ImplIvar->getIdentifier()->getName());
442 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
443 ClsIvar->getIdentifier()->getName());
444 }
445 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
446 // as error.
447 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
448 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
449 ImplIvar->getIdentifier()->getName());
450 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
451 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000452 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000453 }
454 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000455 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000456
457 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000458 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000459 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000460 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000461}
462
Steve Naroffb4f48512008-02-10 21:38:56 +0000463void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
464 bool &IncompleteImpl) {
465 if (!IncompleteImpl) {
466 Diag(ImpLoc, diag::warn_incomplete_impl);
467 IncompleteImpl = true;
468 }
469 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
470}
471
Steve Naroffb268d2a2008-02-08 22:06:17 +0000472/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000473/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000474void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
475 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000476 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000477 const llvm::DenseSet<Selector> &InsMap,
478 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000479 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000480 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000481 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000482 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000483 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000484 method->getImplementationControl() != ObjCMethodDecl::Optional)
485 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000486 }
487 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000488 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000489 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000490 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000491 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000492 method->getImplementationControl() != ObjCMethodDecl::Optional)
493 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000494 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000495 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000496 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000497 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000498 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000499}
500
Ted Kremenek42730c52008-01-07 19:49:32 +0000501void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
502 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 llvm::DenseSet<Selector> InsMap;
504 // Check and see if instance methods in class interface have been
505 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000506 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000507 E = IMPDecl->instmeth_end(); I != E; ++I)
508 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000509
510 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000511 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000512 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000513 if (!InsMap.count((*I)->getSelector()))
514 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000515
Chris Lattner855e51f2007-12-12 07:09:47 +0000516 llvm::DenseSet<Selector> ClsMap;
517 // Check and see if class methods in class interface have been
518 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000519 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000520 E = IMPDecl->classmeth_end(); I != E; ++I)
521 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000522
Ted Kremenek42730c52008-01-07 19:49:32 +0000523 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000524 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000525 if (!ClsMap.count((*I)->getSelector()))
526 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000527
528 // Check the protocol list for unimplemented methods in the @implementation
529 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000530 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000531 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000532 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
533 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000534}
535
536/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
537/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000538void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
539 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000540 llvm::DenseSet<Selector> InsMap;
541 // Check and see if instance methods in category interface have been
542 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000543 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000544 E = CatImplDecl->instmeth_end(); I != E; ++I)
545 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000546
547 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000548 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000549 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000550 if (!InsMap.count((*I)->getSelector()))
551 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
552
Chris Lattner855e51f2007-12-12 07:09:47 +0000553 llvm::DenseSet<Selector> ClsMap;
554 // Check and see if class methods in category interface have been
555 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000556 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000557 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
558 I != E; ++I)
559 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000560
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000562 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000563 if (!ClsMap.count((*I)->getSelector()))
564 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000565
566 // Check the protocol list for unimplemented methods in the @implementation
567 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000568 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000569 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000570 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000571 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
572 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000573 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000574}
575
576/// ActOnForwardClassDeclaration -
577Action::DeclTy *
578Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
579 IdentifierInfo **IdentList, unsigned NumElts)
580{
Ted Kremenek42730c52008-01-07 19:49:32 +0000581 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000582
583 for (unsigned i = 0; i != NumElts; ++i) {
584 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000585 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000586 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 Diag(AtClassLoc, diag::err_redefinition_different_kind,
588 IdentList[i]->getName());
589 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
590 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000591 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000592 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0db541b2008-03-16 01:15:50 +0000593 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
594 true);
Steve Naroff15208162008-04-02 18:30:49 +0000595 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000596
597 // Remember that this needs to be removed when the scope is popped.
598 TUScope->AddDecl(IDecl);
599 }
600
601 Interfaces.push_back(IDecl);
602 }
603
Chris Lattnere29dc832008-03-16 20:34:23 +0000604 return ObjCClassDecl::Create(Context, AtClassLoc,
605 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000606}
607
608
609/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
610/// returns true, or false, accordingly.
611/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000612bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
613 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000614 if (Method->getResultType().getCanonicalType() !=
615 PrevMethod->getResultType().getCanonicalType())
616 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000617 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000618 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
619 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000620 if (Context.getCanonicalType(ParamDecl->getType()) !=
621 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000622 return false;
623 }
624 return true;
625}
626
Ted Kremenek42730c52008-01-07 19:49:32 +0000627void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
628 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000629 if (!FirstMethod.Method) {
630 // Haven't seen a method with this selector name yet - add it.
631 FirstMethod.Method = Method;
632 FirstMethod.Next = 0;
633 } else {
634 // We've seen a method with this name, now check the type signature(s).
635 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
636
Ted Kremenek42730c52008-01-07 19:49:32 +0000637 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000638 Next = Next->Next)
639 match = MatchTwoMethodDeclarations(Method, Next->Method);
640
641 if (!match) {
642 // We have a new signature for an existing method - add it.
643 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000644 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000645 FirstMethod.Next = OMI;
646 }
647 }
648}
649
Ted Kremenek42730c52008-01-07 19:49:32 +0000650void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
651 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000652 if (!FirstMethod.Method) {
653 // Haven't seen a method with this selector name yet - add it.
654 FirstMethod.Method = Method;
655 FirstMethod.Next = 0;
656 } else {
657 // We've seen a method with this name, now check the type signature(s).
658 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
659
Ted Kremenek42730c52008-01-07 19:49:32 +0000660 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000661 Next = Next->Next)
662 match = MatchTwoMethodDeclarations(Method, Next->Method);
663
664 if (!match) {
665 // We have a new signature for an existing method - add it.
666 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000667 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000668 FirstMethod.Next = OMI;
669 }
670 }
671}
672
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000673// Note: For class/category implemenations, allMethods/allProperties is
674// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000675void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
676 DeclTy **allMethods, unsigned allNum,
677 DeclTy **allProperties, unsigned pNum) {
678 Decl *ClassDecl = static_cast<Decl *>(classDecl);
679
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000680 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
681 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000682 // should be true.
683 if (!ClassDecl)
684 return;
685
Ted Kremenek42730c52008-01-07 19:49:32 +0000686 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
687 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000688
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
690 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000691
692 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000693 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
694 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000695 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000696
697 // TODO: property declaration in category and protocols.
Chris Lattner2d1c4312008-03-16 21:17:37 +0000698 if (pNum != 0)
Chris Lattnercffe3662008-03-16 21:23:50 +0000699 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
700 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Chris Lattner855e51f2007-12-12 07:09:47 +0000701
702 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000703 ObjCMethodDecl *Method =
704 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000705
706 if (!Method) continue; // Already issued a diagnostic.
707 if (Method->isInstance()) {
708 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000709 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000710 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
711 : false;
712 if (isInterfaceDeclKind && PrevMethod && !match
713 || checkIdenticalMethods && match) {
714 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
715 Method->getSelector().getName());
716 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
717 } else {
718 insMethods.push_back(Method);
719 InsMap[Method->getSelector()] = Method;
720 /// The following allows us to typecheck messages to "id".
721 AddInstanceMethodToGlobalPool(Method);
722 }
723 }
724 else {
725 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000726 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000727 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
728 : false;
729 if (isInterfaceDeclKind && PrevMethod && !match
730 || checkIdenticalMethods && match) {
731 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
732 Method->getSelector().getName());
733 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
734 } else {
735 clsMethods.push_back(Method);
736 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000737 /// The following allows us to typecheck messages to "Class".
738 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000739 }
740 }
741 }
742
Ted Kremenek42730c52008-01-07 19:49:32 +0000743 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000744 I->addMethods(&insMethods[0], insMethods.size(),
745 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000746 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000747 P->addMethods(&insMethods[0], insMethods.size(),
748 &clsMethods[0], clsMethods.size(), AtEndLoc);
749 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000750 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000751 C->addMethods(&insMethods[0], insMethods.size(),
752 &clsMethods[0], clsMethods.size(), AtEndLoc);
753 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000754 else if (ObjCImplementationDecl *IC =
755 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000756 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000757 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000758 ImplMethodsVsClassMethods(IC, IDecl);
759 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000760 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000762 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000763 // Find category interface decl and then check that all methods declared
764 // in this interface is implemented in the category @implementation.
765 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000766 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000767 Categories; Categories = Categories->getNextClassCategory()) {
768 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
769 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
770 break;
771 }
772 }
773 }
774 }
775}
776
777
778/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
779/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000780static Decl::ObjCDeclQualifier
781CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
782 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
783 if (PQTVal & ObjCDeclSpec::DQ_In)
784 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
785 if (PQTVal & ObjCDeclSpec::DQ_Inout)
786 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
787 if (PQTVal & ObjCDeclSpec::DQ_Out)
788 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
789 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
790 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
791 if (PQTVal & ObjCDeclSpec::DQ_Byref)
792 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
793 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
794 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000795
796 return ret;
797}
798
799Sema::DeclTy *Sema::ActOnMethodDeclaration(
800 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000801 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000802 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000803 Selector Sel,
804 // optional arguments. The number of types/arguments is obtained
805 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000806 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000807 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
808 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000809 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000810
811 // Make sure we can establish a context for the method.
812 if (!ClassDecl) {
813 Diag(MethodLoc, diag::error_missing_method_context);
814 return 0;
815 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000816 QualType resultDeclType;
817
818 if (ReturnType)
819 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
820 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000821 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000822
Chris Lattner114add62008-03-16 00:49:28 +0000823 ObjCMethodDecl* ObjCMethod =
824 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000825 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000826 MethodType == tok::minus, isVariadic,
827 MethodDeclKind == tok::objc_optional ?
828 ObjCMethodDecl::Optional :
829 ObjCMethodDecl::Required);
830
Chris Lattnereee57c02008-04-04 06:12:32 +0000831 llvm::SmallVector<ParmVarDecl*, 16> Params;
832
833 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
834 // FIXME: arg->AttrList must be stored too!
835 QualType argType;
836
837 if (ArgTypes[i])
838 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
839 else
840 argType = Context.getObjCIdType();
841 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
842 SourceLocation(/*FIXME*/),
843 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000844 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000845 Param->setObjCDeclQualifier(
846 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
847 Params.push_back(Param);
848 }
849
Ted Kremenek42730c52008-01-07 19:49:32 +0000850 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
851 ObjCMethod->setObjCDeclQualifier(
852 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
853 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000854
855 // For implementations (which can be very "coarse grain"), we add the
856 // method now. This allows the AST to implement lookup methods that work
857 // incrementally (without waiting until we parse the @end). It also allows
858 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000859 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000860 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000861 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000862 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000863 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000864 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000865 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000866 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000867 }
868 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000869 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000870 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000871 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000872 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000873 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000874 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000875 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000876 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000877 }
878 }
879 if (PrevMethod) {
880 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
882 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000883 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
884 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000885 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +0000886}
887
Ted Kremenek42730c52008-01-07 19:49:32 +0000888Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
Chris Lattner44859612008-03-17 01:19:02 +0000889 DeclTy **allProperties,
890 unsigned NumProperties,
891 ObjCDeclSpec &DS) {
Chris Lattner2d1c4312008-03-16 21:17:37 +0000892 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000893
Chris Lattner44859612008-03-17 01:19:02 +0000894 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +0000896
Chris Lattner44859612008-03-17 01:19:02 +0000897 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000899 PDecl->setGetterName(DS.getGetterName());
900 }
901
Chris Lattner44859612008-03-17 01:19:02 +0000902 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000904 PDecl->setSetterName(DS.getSetterName());
905 }
906
Chris Lattner44859612008-03-17 01:19:02 +0000907 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +0000908 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +0000909
Chris Lattner44859612008-03-17 01:19:02 +0000910 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +0000911 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +0000912
Chris Lattner44859612008-03-17 01:19:02 +0000913 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +0000914 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +0000915
Chris Lattner44859612008-03-17 01:19:02 +0000916 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +0000917 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +0000918
Chris Lattner44859612008-03-17 01:19:02 +0000919 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +0000920 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +0000921
Chris Lattner44859612008-03-17 01:19:02 +0000922 if (NumProperties != 0)
923 PDecl->setPropertyDeclLists((ObjCIvarDecl**)allProperties, NumProperties);
924
Chris Lattner855e51f2007-12-12 07:09:47 +0000925 return PDecl;
926}
927
Chris Lattner2a1e2ed2008-04-11 07:00:53 +0000928
929