blob: 6b1d78bb776b5c60f5fee60e64db7f6ef1b81c21 [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;
Chris Lattner4d391482007-12-12 07:09:47 +000046 if (MDecl->isInstance()) {
Fariborz Jahanian20552d22008-01-10 20:33:58 +000047 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
50 if (OID) {
51 QualType selfTy = Context.getObjCInterfaceType(OID);
52 selfTy = Context.getPointerType(selfTy);
53 PI.TypeInfo = selfTy.getAsOpaquePtr();
54 }
Chris Lattner4d391482007-12-12 07:09:47 +000055 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +000056 PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr();
Chris Lattner4d391482007-12-12 07:09:47 +000057 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
58
59 PI.Ident = &Context.Idents.get("_cmd");
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr();
Chris Lattner4d391482007-12-12 07:09:47 +000061 ActOnParamDeclarator(PI, FnBodyScope);
62
63 for (int i = 0; i < MDecl->getNumParams(); i++) {
64 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
65 PI.Ident = PDecl->getIdentifier();
66 PI.IdentLoc = PDecl->getLocation(); // user vars have a real location.
67 PI.TypeInfo = PDecl->getType().getAsOpaquePtr();
Fariborz Jahanian2338d582008-01-21 22:59:53 +000068 MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope));
Chris Lattner4d391482007-12-12 07:09:47 +000069 }
70}
71
72Sema::DeclTy *Sema::ActOnStartClassInterface(
73 SourceLocation AtInterfaceLoc,
74 IdentifierInfo *ClassName, SourceLocation ClassLoc,
75 IdentifierInfo *SuperName, SourceLocation SuperLoc,
76 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
77 SourceLocation EndProtoLoc, AttributeList *AttrList) {
78 assert(ClassName && "Missing class identifier");
79
80 // Check for another declaration kind with the same name.
81 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000082 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +000083 Diag(ClassLoc, diag::err_redefinition_different_kind,
84 ClassName->getName());
85 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
86 }
87
Ted Kremeneka526c5c2008-01-07 19:49:32 +000088 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000089 if (IDecl) {
90 // Class already seen. Is it a forward declaration?
91 if (!IDecl->isForwardDecl())
92 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
93 else {
94 IDecl->setLocation(AtInterfaceLoc);
95 IDecl->setForwardDecl(false);
96 IDecl->AllocIntfRefProtocols(NumProtocols);
97 }
98 }
99 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 IDecl = new ObjCInterfaceDecl(AtInterfaceLoc, NumProtocols, ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000101
102 // Chain & install the interface decl into the identifier.
103 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
104 ClassName->setFETokenInfo(IDecl);
105
106 // Remember that this needs to be removed when the scope is popped.
107 TUScope->AddDecl(IDecl);
108 }
109
110 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000112 // Check if a different kind of symbol declared in this scope.
113 PrevDecl = LookupInterfaceDecl(SuperName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000114 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000115 Diag(SuperLoc, diag::err_redefinition_different_kind,
116 SuperName->getName());
117 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
118 }
119 else {
120 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000121 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000122
123 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
124 Diag(AtInterfaceLoc, diag::err_undef_superclass,
125 SuperClassEntry ? SuperClassEntry->getName()
126 : SuperName->getName(),
127 ClassName->getName());
128 }
129 }
130 IDecl->setSuperClass(SuperClassEntry);
131 IDecl->setLocEnd(SuperLoc);
132 } else { // we have a root class.
133 IDecl->setLocEnd(ClassLoc);
134 }
135
136 /// Check then save referenced protocols
137 if (NumProtocols) {
138 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000139 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000140 if (!RefPDecl || RefPDecl->isForwardDecl())
141 Diag(ClassLoc, diag::warn_undef_protocolref,
142 ProtocolNames[i]->getName(),
143 ClassName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000144 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000145 }
146 IDecl->setLocEnd(EndProtoLoc);
147 }
148 return IDecl;
149}
150
151/// ActOnCompatiblityAlias - this action is called after complete parsing of
152/// @compaatibility_alias declaration. It sets up the alias relationships.
153Sema::DeclTy *Sema::ActOnCompatiblityAlias(
154 SourceLocation AtCompatibilityAliasLoc,
155 IdentifierInfo *AliasName, SourceLocation AliasLocation,
156 IdentifierInfo *ClassName, SourceLocation ClassLocation) {
157 // Look for previous declaration of alias name
158 ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary,
159 AliasLocation, TUScope);
160 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000161 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000162 Diag(AliasLocation, diag::warn_previous_alias_decl);
163 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
164 }
165 else {
166 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
167 AliasName->getName());
168 Diag(ADecl->getLocation(), diag::err_previous_declaration);
169 }
170 return 0;
171 }
172 // Check for class declaration
173 ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary,
174 ClassLocation, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 if (!CDecl || !isa<ObjCInterfaceDecl>(CDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000176 Diag(ClassLocation, diag::warn_undef_interface,
177 ClassName->getName());
178 if (CDecl)
179 Diag(CDecl->getLocation(), diag::warn_previous_declaration);
180 return 0;
181 }
182 // Everything checked out, instantiate a new alias declaration ast
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 ObjCCompatibleAliasDecl *AliasDecl =
184 new ObjCCompatibleAliasDecl(AtCompatibilityAliasLoc,
Chris Lattner4d391482007-12-12 07:09:47 +0000185 AliasName,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000186 dyn_cast<ObjCInterfaceDecl>(CDecl));
Chris Lattner4d391482007-12-12 07:09:47 +0000187
188 // Chain & install the interface decl into the identifier.
189 AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>());
190 AliasName->setFETokenInfo(AliasDecl);
191 return AliasDecl;
192}
193
194Sema::DeclTy *Sema::ActOnStartProtocolInterface(
195 SourceLocation AtProtoInterfaceLoc,
196 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
197 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
198 SourceLocation EndProtoLoc) {
199 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000200 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000201 if (PDecl) {
202 // Protocol already seen. Better be a forward protocol declaration
203 if (!PDecl->isForwardDecl())
204 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
205 ProtocolName->getName());
206 else {
207 PDecl->setForwardDecl(false);
208 PDecl->AllocReferencedProtocols(NumProtoRefs);
209 }
210 }
211 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000212 PDecl = new ObjCProtocolDecl(AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner4d391482007-12-12 07:09:47 +0000213 ProtocolName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000214 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000215 }
216
217 if (NumProtoRefs) {
218 /// Check then save referenced protocols
219 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000220 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000221 if (!RefPDecl || RefPDecl->isForwardDecl())
222 Diag(ProtocolLoc, diag::warn_undef_protocolref,
223 ProtoRefNames[i]->getName(),
224 ProtocolName->getName());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000225 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000226 }
227 PDecl->setLocEnd(EndProtoLoc);
228 }
229 return PDecl;
230}
231
232/// FindProtocolDeclaration - This routine looks up protocols and
233/// issuer error if they are not declared. It returns list of protocol
234/// declarations in its 'Protocols' argument.
235void
236Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
237 IdentifierInfo **ProtocolId,
238 unsigned NumProtocols,
239 llvm::SmallVector<DeclTy *,8> &Protocols) {
240 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000241 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000242 if (!PDecl)
243 Diag(TypeLoc, diag::err_undeclared_protocol,
244 ProtocolId[i]->getName());
245 else
246 Protocols.push_back(PDecl);
247 }
248}
249
250/// ActOnForwardProtocolDeclaration -
251Action::DeclTy *
252Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
253 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000254 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000255
256 for (unsigned i = 0; i != NumElts; ++i) {
257 IdentifierInfo *P = IdentList[i];
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000258 ObjCProtocolDecl *PDecl = ObjCProtocols[P];
Chris Lattner4d391482007-12-12 07:09:47 +0000259 if (!PDecl) { // Not already seen?
260 // FIXME: Pass in the location of the identifier!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000261 PDecl = new ObjCProtocolDecl(AtProtocolLoc, 0, P, true);
262 ObjCProtocols[P] = PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000263 }
264
265 Protocols.push_back(PDecl);
266 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000267 return new ObjCForwardProtocolDecl(AtProtocolLoc,
Chris Lattner4d391482007-12-12 07:09:47 +0000268 &Protocols[0], Protocols.size());
269}
270
271Sema::DeclTy *Sema::ActOnStartCategoryInterface(
272 SourceLocation AtInterfaceLoc,
273 IdentifierInfo *ClassName, SourceLocation ClassLoc,
274 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
275 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
276 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000277 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000278
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000279 ObjCCategoryDecl *CDecl = new ObjCCategoryDecl(AtInterfaceLoc, NumProtoRefs,
Chris Lattner4d391482007-12-12 07:09:47 +0000280 CategoryName);
281 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000282
283 /// Check that class of this category is already completely declared.
284 if (!IDecl || IDecl->isForwardDecl())
285 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
286 else {
287 /// Check for duplicate interface declaration for this category
288 ObjCCategoryDecl *CDeclChain;
289 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
290 CDeclChain = CDeclChain->getNextClassCategory()) {
291 if (CDeclChain->getIdentifier() == CategoryName) {
292 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
293 CategoryName->getName());
294 break;
295 }
Chris Lattner4d391482007-12-12 07:09:47 +0000296 }
Chris Lattner4d391482007-12-12 07:09:47 +0000297 if (!CDeclChain)
298 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000299 }
Chris Lattner4d391482007-12-12 07:09:47 +0000300
301 if (NumProtoRefs) {
302 /// Check then save referenced protocols
303 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000304 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner4d391482007-12-12 07:09:47 +0000305 if (!RefPDecl || RefPDecl->isForwardDecl()) {
306 Diag(CategoryLoc, diag::warn_undef_protocolref,
307 ProtoRefNames[i]->getName(),
308 CategoryName->getName());
309 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000310 CDecl->setCatReferencedProtocols(i, RefPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000311 }
312 CDecl->setLocEnd(EndProtoLoc);
313 }
314 return CDecl;
315}
316
317/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000318/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000319/// object.
320Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
321 SourceLocation AtCatImplLoc,
322 IdentifierInfo *ClassName, SourceLocation ClassLoc,
323 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000324 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
325 ObjCCategoryImplDecl *CDecl = new ObjCCategoryImplDecl(AtCatImplLoc,
Chris Lattner4d391482007-12-12 07:09:47 +0000326 CatName, IDecl);
327 /// Check that class of this category is already completely declared.
328 if (!IDecl || IDecl->isForwardDecl())
329 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
330
331 /// TODO: Check that CatName, category name, is not used in another
332 // implementation.
333 return CDecl;
334}
335
336Sema::DeclTy *Sema::ActOnStartClassImplementation(
337 SourceLocation AtClassImplLoc,
338 IdentifierInfo *ClassName, SourceLocation ClassLoc,
339 IdentifierInfo *SuperClassname,
340 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000341 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000342 // Check for another declaration kind with the same name.
343 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000344 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000345 Diag(ClassLoc, diag::err_redefinition_different_kind,
346 ClassName->getName());
347 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
348 }
349 else {
350 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000351 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000352 if (!IDecl)
353 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
354 }
355
356 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000357 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000358 if (SuperClassname) {
359 // Check if a different kind of symbol declared in this scope.
360 PrevDecl = LookupInterfaceDecl(SuperClassname);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000362 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
363 SuperClassname->getName());
364 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
365 }
366 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000367 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000368 if (!SDecl)
369 Diag(SuperClassLoc, diag::err_undef_superclass,
370 SuperClassname->getName(), ClassName->getName());
371 else if (IDecl && IDecl->getSuperClass() != SDecl) {
372 // This implementation and its interface do not have the same
373 // super class.
374 Diag(SuperClassLoc, diag::err_conflicting_super_class,
375 SDecl->getName());
376 Diag(SDecl->getLocation(), diag::err_previous_definition);
377 }
378 }
379 }
380
381 if (!IDecl) {
382 // Legacy case of @implementation with no corresponding @interface.
383 // Build, chain & install the interface decl into the identifier.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000384 IDecl = new ObjCInterfaceDecl(AtClassImplLoc, 0, ClassName,
Chris Lattner4d391482007-12-12 07:09:47 +0000385 false, true);
386 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
387 ClassName->setFETokenInfo(IDecl);
388 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 Kremeneka526c5c2008-01-07 19:49:32 +0000395 ObjCImplementationDecl* IMPDecl =
396 new ObjCImplementationDecl(AtClassImplLoc, ClassName, IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000397
398 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000399 if (ObjCImplementations[ClassName])
Chris Lattner4d391482007-12-12 07:09:47 +0000400 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
401 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000402 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000403 return IMPDecl;
404}
405
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000406void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
407 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000408 SourceLocation RBrace) {
409 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000410 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000411 if (!IDecl)
412 return;
413 /// Check case of non-existing @interface decl.
414 /// (legacy objective-c @implementation decl without an @interface decl).
415 /// Add implementations's ivar to the synthesize class's ivar list.
416 if (IDecl->ImplicitInterfaceDecl()) {
417 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
418 return;
419 }
420 // If implementation has empty ivar list, just return.
421 if (numIvars == 0)
422 return;
423
424 assert(ivars && "missing @implementation ivars");
425
426 // Check interface's Ivar list against those in the implementation.
427 // names and types must match.
428 //
Chris Lattner4d391482007-12-12 07:09:47 +0000429 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000430 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000431 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
432 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000433 ObjCIvarDecl* ImplIvar = ivars[j++];
434 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000435 assert (ImplIvar && "missing implementation ivar");
436 assert (ClsIvar && "missing class ivar");
437 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
438 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
439 ImplIvar->getIdentifier()->getName());
440 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
441 ClsIvar->getIdentifier()->getName());
442 }
443 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
444 // as error.
445 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
446 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
447 ImplIvar->getIdentifier()->getName());
448 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
449 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000450 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000451 }
452 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000453 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000454
455 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000456 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000457 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000458 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000459}
460
Steve Naroff3c2eb662008-02-10 21:38:56 +0000461void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
462 bool &IncompleteImpl) {
463 if (!IncompleteImpl) {
464 Diag(ImpLoc, diag::warn_incomplete_impl);
465 IncompleteImpl = true;
466 }
467 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
468}
469
Steve Naroffefe7f362008-02-08 22:06:17 +0000470/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000471/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000472void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
473 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000474 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000475 const llvm::DenseSet<Selector> &InsMap,
476 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000477 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000478 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000479 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000480 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000481 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000482 method->getImplementationControl() != ObjCMethodDecl::Optional)
483 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000484 }
485 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000487 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000488 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000489 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000490 method->getImplementationControl() != ObjCMethodDecl::Optional)
491 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000492 }
Chris Lattner4d391482007-12-12 07:09:47 +0000493 // Check on this protocols's referenced protocols, recursively
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000494 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000495 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000496 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000497}
498
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000499void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
500 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000501 llvm::DenseSet<Selector> InsMap;
502 // Check and see if instance methods in class interface have been
503 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000504 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000505 E = IMPDecl->instmeth_end(); I != E; ++I)
506 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000507
508 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000509 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000510 E = IDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000511 if (!InsMap.count((*I)->getSelector()))
512 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000513
Chris Lattner4d391482007-12-12 07:09:47 +0000514 llvm::DenseSet<Selector> ClsMap;
515 // Check and see if class methods in class interface have been
516 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000517 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000518 E = IMPDecl->classmeth_end(); I != E; ++I)
519 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000520
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000521 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000522 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000523 if (!ClsMap.count((*I)->getSelector()))
524 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000525
526 // Check the protocol list for unimplemented methods in the @implementation
527 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000528 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000529 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffefe7f362008-02-08 22:06:17 +0000530 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
531 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000532}
533
534/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
535/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000536void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
537 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000538 llvm::DenseSet<Selector> InsMap;
539 // Check and see if instance methods in category interface have been
540 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000541 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000542 E = CatImplDecl->instmeth_end(); I != E; ++I)
543 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000544
545 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000546 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000547 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000548 if (!InsMap.count((*I)->getSelector()))
549 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
550
Chris Lattner4d391482007-12-12 07:09:47 +0000551 llvm::DenseSet<Selector> ClsMap;
552 // Check and see if class methods in category interface have been
553 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000554 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000555 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
556 I != E; ++I)
557 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000558
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000559 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000560 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000561 if (!ClsMap.count((*I)->getSelector()))
562 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000563
564 // Check the protocol list for unimplemented methods in the @implementation
565 // class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000566 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000567 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000568 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffefe7f362008-02-08 22:06:17 +0000569 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
570 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000571 }
Chris Lattner4d391482007-12-12 07:09:47 +0000572}
573
574/// ActOnForwardClassDeclaration -
575Action::DeclTy *
576Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
577 IdentifierInfo **IdentList, unsigned NumElts)
578{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000580
581 for (unsigned i = 0; i != NumElts; ++i) {
582 // Check for another declaration kind with the same name.
583 ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000585 Diag(AtClassLoc, diag::err_redefinition_different_kind,
586 IdentList[i]->getName());
587 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
588 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000590 if (!IDecl) { // Not already seen? Make a forward decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591 IDecl = new ObjCInterfaceDecl(AtClassLoc, 0, IdentList[i], true);
Chris Lattner4d391482007-12-12 07:09:47 +0000592 // Chain & install the interface decl into the identifier.
593 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
594 IdentList[i]->setFETokenInfo(IDecl);
595
596 // Remember that this needs to be removed when the scope is popped.
597 TUScope->AddDecl(IDecl);
598 }
599
600 Interfaces.push_back(IDecl);
601 }
602
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000603 return new ObjCClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000604}
605
606
607/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
608/// returns true, or false, accordingly.
609/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000610bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
611 const ObjCMethodDecl *PrevMethod) {
Chris Lattner4d391482007-12-12 07:09:47 +0000612 if (Method->getResultType().getCanonicalType() !=
613 PrevMethod->getResultType().getCanonicalType())
614 return false;
615 for (int i = 0; i < Method->getNumParams(); i++) {
616 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
617 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
618 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
619 return false;
620 }
621 return true;
622}
623
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
625 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000626 if (!FirstMethod.Method) {
627 // Haven't seen a method with this selector name yet - add it.
628 FirstMethod.Method = Method;
629 FirstMethod.Next = 0;
630 } else {
631 // We've seen a method with this name, now check the type signature(s).
632 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
633
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000634 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000635 Next = Next->Next)
636 match = MatchTwoMethodDeclarations(Method, Next->Method);
637
638 if (!match) {
639 // We have a new signature for an existing method - add it.
640 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000641 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000642 FirstMethod.Next = OMI;
643 }
644 }
645}
646
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
648 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000649 if (!FirstMethod.Method) {
650 // Haven't seen a method with this selector name yet - add it.
651 FirstMethod.Method = Method;
652 FirstMethod.Next = 0;
653 } else {
654 // We've seen a method with this name, now check the type signature(s).
655 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
656
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000657 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000658 Next = Next->Next)
659 match = MatchTwoMethodDeclarations(Method, Next->Method);
660
661 if (!match) {
662 // We have a new signature for an existing method - add it.
663 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000664 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000665 FirstMethod.Next = OMI;
666 }
667 }
668}
669
Steve Naroffa56f6162007-12-18 01:30:32 +0000670// Note: For class/category implemenations, allMethods/allProperties is
671// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000672void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
673 DeclTy **allMethods, unsigned allNum,
674 DeclTy **allProperties, unsigned pNum) {
675 Decl *ClassDecl = static_cast<Decl *>(classDecl);
676
Steve Naroffa56f6162007-12-18 01:30:32 +0000677 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
678 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000679 // should be true.
680 if (!ClassDecl)
681 return;
682
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
684 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000685
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000686 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
687 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000688
689 bool isInterfaceDeclKind =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690 (isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
691 || isa<ObjCProtocolDecl>(ClassDecl));
692 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000693
694 // TODO: property declaration in category and protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000695 if (pNum != 0 && isa<ObjCInterfaceDecl>(ClassDecl)) {
696 ObjCPropertyDecl **properties = new ObjCPropertyDecl*[pNum];
697 memcpy(properties, allProperties, pNum*sizeof(ObjCPropertyDecl*));
698 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setPropertyDecls(properties);
699 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum);
Chris Lattner4d391482007-12-12 07:09:47 +0000700 }
701
702 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000703 ObjCMethodDecl *Method =
704 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000709 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000726 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-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 Naroffa56f6162007-12-18 01:30:32 +0000737 /// The following allows us to typecheck messages to "Class".
738 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000739 }
740 }
741 }
742
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000743 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000744 I->addMethods(&insMethods[0], insMethods.size(),
745 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000746 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000747 P->addMethods(&insMethods[0], insMethods.size(),
748 &clsMethods[0], clsMethods.size(), AtEndLoc);
749 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000750 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000751 C->addMethods(&insMethods[0], insMethods.size(),
752 &clsMethods[0], clsMethods.size(), AtEndLoc);
753 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000754 else if (ObjCImplementationDecl *IC =
755 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000756 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000757 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000758 ImplMethodsVsClassMethods(IC, IDecl);
759 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000761 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000762 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000766 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-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 Kremeneka526c5c2008-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 Lattner4d391482007-12-12 07:09:47 +0000795
796 return ret;
797}
798
799Sema::DeclTy *Sema::ActOnMethodDeclaration(
800 SourceLocation MethodLoc, SourceLocation EndLoc,
801 tok::TokenKind MethodType, DeclTy *ClassDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000802 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000803 Selector Sel,
804 // optional arguments. The number of types/arguments is obtained
805 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000807 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
808 bool isVariadic) {
809 llvm::SmallVector<ParmVarDecl*, 16> Params;
810
811 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
812 // FIXME: arg->AttrList must be stored too!
813 QualType argType;
814
815 if (ArgTypes[i])
816 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
817 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818 argType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000819 ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i],
820 argType, VarDecl::None, 0);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000821 Param->setObjCDeclQualifier(
822 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
Chris Lattner4d391482007-12-12 07:09:47 +0000823 Params.push_back(Param);
824 }
825 QualType resultDeclType;
826
827 if (ReturnType)
828 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
829 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000830 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000831
832 Decl *CDecl = static_cast<Decl*>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000833 ObjCMethodDecl* ObjCMethod = new ObjCMethodDecl(MethodLoc, EndLoc, Sel,
Chris Lattner4d391482007-12-12 07:09:47 +0000834 resultDeclType,
835 CDecl,
836 0, -1, AttrList,
837 MethodType == tok::minus, isVariadic,
838 MethodDeclKind == tok::objc_optional ?
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000839 ObjCMethodDecl::Optional :
840 ObjCMethodDecl::Required);
841 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
842 ObjCMethod->setObjCDeclQualifier(
843 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
844 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000845
846 // For implementations (which can be very "coarse grain"), we add the
847 // method now. This allows the AST to implement lookup methods that work
848 // incrementally (without waiting until we parse the @end). It also allows
849 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850 if (ObjCImplementationDecl *ImpDecl =
851 dyn_cast<ObjCImplementationDecl>(CDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000852 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000853 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000854 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000855 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000856 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000857 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000858 }
859 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000860 else if (ObjCCategoryImplDecl *CatImpDecl =
861 dyn_cast<ObjCCategoryImplDecl>(CDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000862 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000863 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000865 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +0000866 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000867 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +0000868 }
869 }
870 if (PrevMethod) {
871 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000872 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
873 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +0000874 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
875 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000876 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +0000877}
878
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
880 DeclTy **allProperties, unsigned NumProperties, ObjCDeclSpec &DS) {
881 ObjCPropertyDecl *PDecl = new ObjCPropertyDecl(AtLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000882
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000883 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
884 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +0000885
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
887 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +0000888 PDecl->setGetterName(DS.getGetterName());
889 }
890
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000891 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
892 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +0000893 PDecl->setSetterName(DS.getSetterName());
894 }
895
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000896 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
897 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +0000898
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000899 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
900 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +0000901
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000902 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
903 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +0000904
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000905 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
906 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +0000907
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
909 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +0000910
911 PDecl->setNumPropertyDecls(NumProperties);
912 if (NumProperties != 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000913 ObjCIvarDecl **properties = new ObjCIvarDecl*[NumProperties];
914 memcpy(properties, allProperties, NumProperties*sizeof(ObjCIvarDecl*));
Chris Lattner4d391482007-12-12 07:09:47 +0000915 PDecl->setPropertyDecls(properties);
916 }
917 return PDecl;
918}
919