blob: 04ce426dec976fdfc0fd19aebdb11e3d82278c46 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremeneka526c5c2008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Chris Lattner4d391482007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremeneka526c5c2008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner4d391482007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Naroffa56f6162007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
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 Begemanb7894b52008-02-17 21:20:31 +000045 PI.AttrList = 0;
Gabor Greif30037532008-02-29 20:35:55 +000046 PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr();
47
Chris Lattner4d391482007-12-12 07:09:47 +000048 if (MDecl->isInstance()) {
Gabor Greif30037532008-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 Jahanian20552d22008-01-10 20:33:58 +000052 QualType selfTy = Context.getObjCInterfaceType(OID);
53 selfTy = Context.getPointerType(selfTy);
54 PI.TypeInfo = selfTy.getAsOpaquePtr();
55 }
Gabor Greif30037532008-02-29 20:35:55 +000056 }
57
Chris Lattner4d391482007-12-12 07:09:47 +000058 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
59
60 PI.Ident = &Context.Idents.get("_cmd");
Ted Kremeneka526c5c2008-01-07 19:49:32 +000061 PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr();
Chris Lattner4d391482007-12-12 07:09:47 +000062 ActOnParamDeclarator(PI, FnBodyScope);
63
64 for (int i = 0; i < MDecl->getNumParams(); i++) {
65 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 Jahanian2338d582008-01-21 22:59:53 +000069 MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope));
Chris Lattner4d391482007-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.
82 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000083 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +000089 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-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 {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000101 IDecl = new ObjCInterfaceDecl(AtInterfaceLoc, NumProtocols, ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000102
103 // Chain & install the interface decl into the identifier.
104 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
105 ClassName->setFETokenInfo(IDecl);
106
107 // Remember that this needs to be removed when the scope is popped.
108 TUScope->AddDecl(IDecl);
109 }
110
111 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000113 // Check if a different kind of symbol declared in this scope.
114 PrevDecl = LookupInterfaceDecl(SuperName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000116 Diag(SuperLoc, diag::err_redefinition_different_kind,
117 SuperName->getName());
118 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
119 }
120 else {
121 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000122 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000123
124 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
125 Diag(AtInterfaceLoc, diag::err_undef_superclass,
126 SuperClassEntry ? SuperClassEntry->getName()
127 : SuperName->getName(),
128 ClassName->getName());
129 }
130 }
131 IDecl->setSuperClass(SuperClassEntry);
132 IDecl->setLocEnd(SuperLoc);
133 } else { // we have a root class.
134 IDecl->setLocEnd(ClassLoc);
135 }
136
137 /// Check then save referenced protocols
138 if (NumProtocols) {
139 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000140 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000141 if (!RefPDecl || RefPDecl->isForwardDecl())
142 Diag(ClassLoc, diag::warn_undef_protocolref,
143 ProtocolNames[i]->getName(),
144 ClassName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000145 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000146 }
147 IDecl->setLocEnd(EndProtoLoc);
148 }
149 return IDecl;
150}
151
152/// ActOnCompatiblityAlias - this action is called after complete parsing of
153/// @compaatibility_alias declaration. It sets up the alias relationships.
154Sema::DeclTy *Sema::ActOnCompatiblityAlias(
155 SourceLocation AtCompatibilityAliasLoc,
156 IdentifierInfo *AliasName, SourceLocation AliasLocation,
157 IdentifierInfo *ClassName, SourceLocation ClassLocation) {
158 // Look for previous declaration of alias name
159 ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary,
160 AliasLocation, TUScope);
161 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000162 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000163 Diag(AliasLocation, diag::warn_previous_alias_decl);
164 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
165 }
166 else {
167 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
168 AliasName->getName());
169 Diag(ADecl->getLocation(), diag::err_previous_declaration);
170 }
171 return 0;
172 }
173 // Check for class declaration
174 ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary,
175 ClassLocation, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000176 if (!CDecl || !isa<ObjCInterfaceDecl>(CDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000177 Diag(ClassLocation, diag::warn_undef_interface,
178 ClassName->getName());
179 if (CDecl)
180 Diag(CDecl->getLocation(), diag::warn_previous_declaration);
181 return 0;
182 }
183 // Everything checked out, instantiate a new alias declaration ast
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000184 ObjCCompatibleAliasDecl *AliasDecl =
185 new ObjCCompatibleAliasDecl(AtCompatibilityAliasLoc,
Chris Lattner4d391482007-12-12 07:09:47 +0000186 AliasName,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000187 dyn_cast<ObjCInterfaceDecl>(CDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000188
189 // Chain & install the interface decl into the identifier.
190 AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>());
191 AliasName->setFETokenInfo(AliasDecl);
192 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 Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000202 if (PDecl) {
203 // Protocol already seen. Better be a forward protocol declaration
204 if (!PDecl->isForwardDecl())
205 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
206 ProtocolName->getName());
207 else {
208 PDecl->setForwardDecl(false);
209 PDecl->AllocReferencedProtocols(NumProtoRefs);
210 }
211 }
212 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000213 PDecl = new ObjCProtocolDecl(AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner4d391482007-12-12 07:09:47 +0000214 ProtocolName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000215 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000216 }
217
218 if (NumProtoRefs) {
219 /// Check then save referenced protocols
220 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000221 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000222 if (!RefPDecl || RefPDecl->isForwardDecl())
223 Diag(ProtocolLoc, diag::warn_undef_protocolref,
224 ProtoRefNames[i]->getName(),
225 ProtocolName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000226 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000227 }
228 PDecl->setLocEnd(EndProtoLoc);
229 }
230 return PDecl;
231}
232
233/// FindProtocolDeclaration - This routine looks up protocols and
234/// issuer error if they are not declared. It returns list of protocol
235/// declarations in its 'Protocols' argument.
236void
237Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
238 IdentifierInfo **ProtocolId,
239 unsigned NumProtocols,
240 llvm::SmallVector<DeclTy *,8> &Protocols) {
241 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000242 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000243 if (!PDecl)
244 Diag(TypeLoc, diag::err_undeclared_protocol,
245 ProtocolId[i]->getName());
246 else
247 Protocols.push_back(PDecl);
248 }
249}
250
251/// ActOnForwardProtocolDeclaration -
252Action::DeclTy *
253Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
254 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000255 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000256
257 for (unsigned i = 0; i != NumElts; ++i) {
258 IdentifierInfo *P = IdentList[i];
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 ObjCProtocolDecl *PDecl = ObjCProtocols[P];
Chris Lattner4d391482007-12-12 07:09:47 +0000260 if (!PDecl) { // Not already seen?
261 // FIXME: Pass in the location of the identifier!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000262 PDecl = new ObjCProtocolDecl(AtProtocolLoc, 0, P, true);
263 ObjCProtocols[P] = PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000264 }
265
266 Protocols.push_back(PDecl);
267 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000268 return new ObjCForwardProtocolDecl(AtProtocolLoc,
Chris Lattner4d391482007-12-12 07:09:47 +0000269 &Protocols[0], Protocols.size());
270}
271
272Sema::DeclTy *Sema::ActOnStartCategoryInterface(
273 SourceLocation AtInterfaceLoc,
274 IdentifierInfo *ClassName, SourceLocation ClassLoc,
275 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
276 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
277 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000278 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000279
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000280 ObjCCategoryDecl *CDecl = new ObjCCategoryDecl(AtInterfaceLoc, NumProtoRefs,
Chris Lattner4d391482007-12-12 07:09:47 +0000281 CategoryName);
282 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000283
284 /// Check that class of this category is already completely declared.
285 if (!IDecl || IDecl->isForwardDecl())
286 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
287 else {
288 /// Check for duplicate interface declaration for this category
289 ObjCCategoryDecl *CDeclChain;
290 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
291 CDeclChain = CDeclChain->getNextClassCategory()) {
292 if (CDeclChain->getIdentifier() == CategoryName) {
293 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
294 CategoryName->getName());
295 break;
296 }
Chris Lattner4d391482007-12-12 07:09:47 +0000297 }
Chris Lattner4d391482007-12-12 07:09:47 +0000298 if (!CDeclChain)
299 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000300 }
Chris Lattner4d391482007-12-12 07:09:47 +0000301
302 if (NumProtoRefs) {
303 /// Check then save referenced protocols
304 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000305 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000306 if (!RefPDecl || RefPDecl->isForwardDecl()) {
307 Diag(CategoryLoc, diag::warn_undef_protocolref,
308 ProtoRefNames[i]->getName(),
309 CategoryName->getName());
310 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000311 CDecl->setCatReferencedProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000312 }
313 CDecl->setLocEnd(EndProtoLoc);
314 }
315 return CDecl;
316}
317
318/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000319/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000325 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
326 ObjCCategoryImplDecl *CDecl = new ObjCCategoryImplDecl(AtCatImplLoc,
Chris Lattner4d391482007-12-12 07:09:47 +0000327 CatName, IDecl);
328 /// 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 Kremeneka526c5c2008-01-07 19:49:32 +0000342 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000343 // Check for another declaration kind with the same name.
344 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000345 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000352 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000358 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000359 if (SuperClassname) {
360 // Check if a different kind of symbol declared in this scope.
361 PrevDecl = LookupInterfaceDecl(SuperClassname);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000362 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000368 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-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.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000385 IDecl = new ObjCInterfaceDecl(AtClassImplLoc, 0, ClassName,
Chris Lattnerc81c8142008-02-25 21:04:36 +0000386 false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000387 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
388 ClassName->setFETokenInfo(IDecl);
389 IDecl->setSuperClass(SDecl);
390 IDecl->setLocEnd(ClassLoc);
391
392 // Remember that this needs to be removed when the scope is popped.
393 TUScope->AddDecl(IDecl);
394 }
395
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 ObjCImplementationDecl* IMPDecl =
397 new ObjCImplementationDecl(AtClassImplLoc, ClassName, IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000398
399 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000400 if (ObjCImplementations[ClassName])
Chris Lattner4d391482007-12-12 07:09:47 +0000401 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
402 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000403 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000404 return IMPDecl;
405}
406
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000407void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
408 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000409 SourceLocation RBrace) {
410 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000411 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000412 if (!IDecl)
413 return;
414 /// Check case of non-existing @interface decl.
415 /// (legacy objective-c @implementation decl without an @interface decl).
416 /// Add implementations's ivar to the synthesize class's ivar list.
417 if (IDecl->ImplicitInterfaceDecl()) {
418 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
419 return;
420 }
421 // If implementation has empty ivar list, just return.
422 if (numIvars == 0)
423 return;
424
425 assert(ivars && "missing @implementation ivars");
426
427 // Check interface's Ivar list against those in the implementation.
428 // names and types must match.
429 //
Chris Lattner4d391482007-12-12 07:09:47 +0000430 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000431 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000432 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
433 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000434 ObjCIvarDecl* ImplIvar = ivars[j++];
435 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000436 assert (ImplIvar && "missing implementation ivar");
437 assert (ClsIvar && "missing class ivar");
438 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
439 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
440 ImplIvar->getIdentifier()->getName());
441 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
442 ClsIvar->getIdentifier()->getName());
443 }
444 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
445 // as error.
446 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
447 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
448 ImplIvar->getIdentifier()->getName());
449 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
450 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000451 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000452 }
453 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000454 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000455
456 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000457 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000458 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000459 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000460}
461
Steve Naroff3c2eb662008-02-10 21:38:56 +0000462void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
463 bool &IncompleteImpl) {
464 if (!IncompleteImpl) {
465 Diag(ImpLoc, diag::warn_incomplete_impl);
466 IncompleteImpl = true;
467 }
468 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
469}
470
Steve Naroffefe7f362008-02-08 22:06:17 +0000471/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000472/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000473void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
474 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000475 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000476 const llvm::DenseSet<Selector> &InsMap,
477 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000478 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000479 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000480 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000481 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000482 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000483 method->getImplementationControl() != ObjCMethodDecl::Optional)
484 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000485 }
486 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000487 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000488 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000489 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000490 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000491 method->getImplementationControl() != ObjCMethodDecl::Optional)
492 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000493 }
Chris Lattner4d391482007-12-12 07:09:47 +0000494 // Check on this protocols's referenced protocols, recursively
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000495 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000496 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000497 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000498}
499
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000500void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
501 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000502 llvm::DenseSet<Selector> InsMap;
503 // Check and see if instance methods in class interface have been
504 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000505 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000506 E = IMPDecl->instmeth_end(); I != E; ++I)
507 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000508
509 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000510 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000511 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000512 if (!InsMap.count((*I)->getSelector()))
513 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000514
Chris Lattner4d391482007-12-12 07:09:47 +0000515 llvm::DenseSet<Selector> ClsMap;
516 // Check and see if class methods in class interface have been
517 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000518 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000519 E = IMPDecl->classmeth_end(); I != E; ++I)
520 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000521
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000523 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000524 if (!ClsMap.count((*I)->getSelector()))
525 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000526
527 // Check the protocol list for unimplemented methods in the @implementation
528 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000530 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000531 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
532 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000533}
534
535/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
536/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000537void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
538 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000539 llvm::DenseSet<Selector> InsMap;
540 // Check and see if instance methods in category interface have been
541 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000542 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000543 E = CatImplDecl->instmeth_end(); I != E; ++I)
544 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000545
546 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000547 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000548 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000549 if (!InsMap.count((*I)->getSelector()))
550 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
551
Chris Lattner4d391482007-12-12 07:09:47 +0000552 llvm::DenseSet<Selector> ClsMap;
553 // Check and see if class methods in category interface have been
554 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000555 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000556 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
557 I != E; ++I)
558 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000559
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000560 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000561 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000562 if (!ClsMap.count((*I)->getSelector()))
563 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000564
565 // Check the protocol list for unimplemented methods in the @implementation
566 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000567 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000568 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000569 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffefe7f362008-02-08 22:06:17 +0000570 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
571 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000572 }
Chris Lattner4d391482007-12-12 07:09:47 +0000573}
574
575/// ActOnForwardClassDeclaration -
576Action::DeclTy *
577Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
578 IdentifierInfo **IdentList, unsigned NumElts)
579{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000580 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000581
582 for (unsigned i = 0; i != NumElts; ++i) {
583 // Check for another declaration kind with the same name.
584 ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000586 Diag(AtClassLoc, diag::err_redefinition_different_kind,
587 IdentList[i]->getName());
588 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
589 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000590 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000591 if (!IDecl) { // Not already seen? Make a forward decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 IDecl = new ObjCInterfaceDecl(AtClassLoc, 0, IdentList[i], true);
Chris Lattner4d391482007-12-12 07:09:47 +0000593 // Chain & install the interface decl into the identifier.
594 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
595 IdentList[i]->setFETokenInfo(IDecl);
596
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
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604 return new ObjCClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000605}
606
607
608/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
609/// returns true, or false, accordingly.
610/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
612 const ObjCMethodDecl *PrevMethod) {
Chris Lattner4d391482007-12-12 07:09:47 +0000613 if (Method->getResultType().getCanonicalType() !=
614 PrevMethod->getResultType().getCanonicalType())
615 return false;
616 for (int i = 0; i < Method->getNumParams(); i++) {
617 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
618 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
619 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
620 return false;
621 }
622 return true;
623}
624
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
626 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000627 if (!FirstMethod.Method) {
628 // Haven't seen a method with this selector name yet - add it.
629 FirstMethod.Method = Method;
630 FirstMethod.Next = 0;
631 } else {
632 // We've seen a method with this name, now check the type signature(s).
633 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
634
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000636 Next = Next->Next)
637 match = MatchTwoMethodDeclarations(Method, Next->Method);
638
639 if (!match) {
640 // We have a new signature for an existing method - add it.
641 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000642 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000643 FirstMethod.Next = OMI;
644 }
645 }
646}
647
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000648void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
649 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000650 if (!FirstMethod.Method) {
651 // Haven't seen a method with this selector name yet - add it.
652 FirstMethod.Method = Method;
653 FirstMethod.Next = 0;
654 } else {
655 // We've seen a method with this name, now check the type signature(s).
656 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
657
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000658 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000659 Next = Next->Next)
660 match = MatchTwoMethodDeclarations(Method, Next->Method);
661
662 if (!match) {
663 // We have a new signature for an existing method - add it.
664 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000665 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000666 FirstMethod.Next = OMI;
667 }
668 }
669}
670
Steve Naroffa56f6162007-12-18 01:30:32 +0000671// Note: For class/category implemenations, allMethods/allProperties is
672// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000673void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
674 DeclTy **allMethods, unsigned allNum,
675 DeclTy **allProperties, unsigned pNum) {
676 Decl *ClassDecl = static_cast<Decl *>(classDecl);
677
Steve Naroffa56f6162007-12-18 01:30:32 +0000678 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
679 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000680 // should be true.
681 if (!ClassDecl)
682 return;
683
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000684 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
685 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000686
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000687 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
688 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000689
690 bool isInterfaceDeclKind =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000691 (isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
692 || isa<ObjCProtocolDecl>(ClassDecl));
693 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000694
695 // TODO: property declaration in category and protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 if (pNum != 0 && isa<ObjCInterfaceDecl>(ClassDecl)) {
697 ObjCPropertyDecl **properties = new ObjCPropertyDecl*[pNum];
698 memcpy(properties, allProperties, pNum*sizeof(ObjCPropertyDecl*));
699 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setPropertyDecls(properties);
700 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum);
Chris Lattner4d391482007-12-12 07:09:47 +0000701 }
702
703 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000704 ObjCMethodDecl *Method =
705 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000706
707 if (!Method) continue; // Already issued a diagnostic.
708 if (Method->isInstance()) {
709 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000711 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
712 : false;
713 if (isInterfaceDeclKind && PrevMethod && !match
714 || checkIdenticalMethods && match) {
715 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
716 Method->getSelector().getName());
717 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
718 } else {
719 insMethods.push_back(Method);
720 InsMap[Method->getSelector()] = Method;
721 /// The following allows us to typecheck messages to "id".
722 AddInstanceMethodToGlobalPool(Method);
723 }
724 }
725 else {
726 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000728 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
729 : false;
730 if (isInterfaceDeclKind && PrevMethod && !match
731 || checkIdenticalMethods && match) {
732 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
733 Method->getSelector().getName());
734 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
735 } else {
736 clsMethods.push_back(Method);
737 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000738 /// The following allows us to typecheck messages to "Class".
739 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000740 }
741 }
742 }
743
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000745 I->addMethods(&insMethods[0], insMethods.size(),
746 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000747 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000748 P->addMethods(&insMethods[0], insMethods.size(),
749 &clsMethods[0], clsMethods.size(), AtEndLoc);
750 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000752 C->addMethods(&insMethods[0], insMethods.size(),
753 &clsMethods[0], clsMethods.size(), AtEndLoc);
754 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000755 else if (ObjCImplementationDecl *IC =
756 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000757 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000758 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000759 ImplMethodsVsClassMethods(IC, IDecl);
760 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000762 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000764 // Find category interface decl and then check that all methods declared
765 // in this interface is implemented in the category @implementation.
766 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000767 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000768 Categories; Categories = Categories->getNextClassCategory()) {
769 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
770 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
771 break;
772 }
773 }
774 }
775 }
776}
777
778
779/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
780/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000781static Decl::ObjCDeclQualifier
782CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
783 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
784 if (PQTVal & ObjCDeclSpec::DQ_In)
785 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
786 if (PQTVal & ObjCDeclSpec::DQ_Inout)
787 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
788 if (PQTVal & ObjCDeclSpec::DQ_Out)
789 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
790 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
791 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
792 if (PQTVal & ObjCDeclSpec::DQ_Byref)
793 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
794 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
795 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000796
797 return ret;
798}
799
800Sema::DeclTy *Sema::ActOnMethodDeclaration(
801 SourceLocation MethodLoc, SourceLocation EndLoc,
802 tok::TokenKind MethodType, DeclTy *ClassDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000804 Selector Sel,
805 // optional arguments. The number of types/arguments is obtained
806 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000808 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
809 bool isVariadic) {
Steve Naroffda323ad2008-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 Lattner4d391482007-12-12 07:09:47 +0000816 llvm::SmallVector<ParmVarDecl*, 16> Params;
817
818 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
819 // FIXME: arg->AttrList must be stored too!
820 QualType argType;
821
822 if (ArgTypes[i])
823 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
824 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000825 argType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000826 ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i],
827 argType, VarDecl::None, 0);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 Param->setObjCDeclQualifier(
829 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
Chris Lattner4d391482007-12-12 07:09:47 +0000830 Params.push_back(Param);
831 }
832 QualType resultDeclType;
833
834 if (ReturnType)
835 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
836 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000837 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000838
839 Decl *CDecl = static_cast<Decl*>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000840 ObjCMethodDecl* ObjCMethod = new ObjCMethodDecl(MethodLoc, EndLoc, Sel,
Chris Lattner4d391482007-12-12 07:09:47 +0000841 resultDeclType,
842 CDecl,
843 0, -1, AttrList,
844 MethodType == tok::minus, isVariadic,
845 MethodDeclKind == tok::objc_optional ?
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000846 ObjCMethodDecl::Optional :
847 ObjCMethodDecl::Required);
848 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
849 ObjCMethod->setObjCDeclQualifier(
850 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
851 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000852
853 // For implementations (which can be very "coarse grain"), we add the
854 // method now. This allows the AST to implement lookup methods that work
855 // incrementally (without waiting until we parse the @end). It also allows
856 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000857 if (ObjCImplementationDecl *ImpDecl =
858 dyn_cast<ObjCImplementationDecl>(CDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000859 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000860 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000861 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000862 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000863 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000865 }
866 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000867 else if (ObjCCategoryImplDecl *CatImpDecl =
868 dyn_cast<ObjCCategoryImplDecl>(CDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000869 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000870 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000871 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000872 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000873 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000874 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000875 }
876 }
877 if (PrevMethod) {
878 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
880 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000881 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
882 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000883 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +0000884}
885
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
887 DeclTy **allProperties, unsigned NumProperties, ObjCDeclSpec &DS) {
888 ObjCPropertyDecl *PDecl = new ObjCPropertyDecl(AtLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000889
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000890 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
891 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +0000892
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000893 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
894 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +0000895 PDecl->setGetterName(DS.getGetterName());
896 }
897
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000898 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
899 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +0000900 PDecl->setSetterName(DS.getSetterName());
901 }
902
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000903 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
904 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +0000905
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000906 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
907 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +0000908
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
910 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +0000911
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000912 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
913 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +0000914
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000915 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
916 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +0000917
918 PDecl->setNumPropertyDecls(NumProperties);
919 if (NumProperties != 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000920 ObjCIvarDecl **properties = new ObjCIvarDecl*[NumProperties];
921 memcpy(properties, allProperties, NumProperties*sizeof(ObjCIvarDecl*));
Chris Lattner4d391482007-12-12 07:09:47 +0000922 PDecl->setPropertyDecls(properties);
923 }
924 return PDecl;
925}
926