blob: 89b7611d865add7dbe88362204d380576b3d34db [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner855e51f2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremenek42730c52008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Chris Lattner855e51f2007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremenek42730c52008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner855e51f2007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Narofffe9eb6a2007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
36
37 // Create Decl objects for each parameter, entrring them in the scope for
38 // binding to their use.
39 struct DeclaratorChunk::ParamInfo PI;
40
41 // Insert the invisible arguments, self and _cmd!
42 PI.Ident = &Context.Idents.get("self");
43 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
44 PI.InvalidType = false;
45 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +000046 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +000047 selfTy = Context.getPointerType(selfTy);
48 PI.TypeInfo = selfTy.getAsOpaquePtr();
49 } else
Ted Kremenek42730c52008-01-07 19:49:32 +000050 PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr();
Chris Lattner855e51f2007-12-12 07:09:47 +000051 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
52
53 PI.Ident = &Context.Idents.get("_cmd");
Ted Kremenek42730c52008-01-07 19:49:32 +000054 PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr();
Chris Lattner855e51f2007-12-12 07:09:47 +000055 ActOnParamDeclarator(PI, FnBodyScope);
56
57 for (int i = 0; i < MDecl->getNumParams(); i++) {
58 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
59 PI.Ident = PDecl->getIdentifier();
60 PI.IdentLoc = PDecl->getLocation(); // user vars have a real location.
61 PI.TypeInfo = PDecl->getType().getAsOpaquePtr();
62 ActOnParamDeclarator(PI, FnBodyScope);
63 }
64}
65
66Sema::DeclTy *Sema::ActOnStartClassInterface(
67 SourceLocation AtInterfaceLoc,
68 IdentifierInfo *ClassName, SourceLocation ClassLoc,
69 IdentifierInfo *SuperName, SourceLocation SuperLoc,
70 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
71 SourceLocation EndProtoLoc, AttributeList *AttrList) {
72 assert(ClassName && "Missing class identifier");
73
74 // Check for another declaration kind with the same name.
75 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremenek42730c52008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind,
78 ClassName->getName());
79 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
80 }
81
Ted Kremenek42730c52008-01-07 19:49:32 +000082 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000083 if (IDecl) {
84 // Class already seen. Is it a forward declaration?
85 if (!IDecl->isForwardDecl())
86 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
87 else {
88 IDecl->setLocation(AtInterfaceLoc);
89 IDecl->setForwardDecl(false);
90 IDecl->AllocIntfRefProtocols(NumProtocols);
91 }
92 }
93 else {
Ted Kremenek42730c52008-01-07 19:49:32 +000094 IDecl = new ObjCInterfaceDecl(AtInterfaceLoc, NumProtocols, ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +000095
96 // Chain & install the interface decl into the identifier.
97 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
98 ClassName->setFETokenInfo(IDecl);
99
100 // Remember that this needs to be removed when the scope is popped.
101 TUScope->AddDecl(IDecl);
102 }
103
104 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000105 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000106 // Check if a different kind of symbol declared in this scope.
107 PrevDecl = LookupInterfaceDecl(SuperName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000108 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000109 Diag(SuperLoc, diag::err_redefinition_different_kind,
110 SuperName->getName());
111 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
112 }
113 else {
114 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000115 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000116
117 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
118 Diag(AtInterfaceLoc, diag::err_undef_superclass,
119 SuperClassEntry ? SuperClassEntry->getName()
120 : SuperName->getName(),
121 ClassName->getName());
122 }
123 }
124 IDecl->setSuperClass(SuperClassEntry);
125 IDecl->setLocEnd(SuperLoc);
126 } else { // we have a root class.
127 IDecl->setLocEnd(ClassLoc);
128 }
129
130 /// Check then save referenced protocols
131 if (NumProtocols) {
132 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000133 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000134 if (!RefPDecl || RefPDecl->isForwardDecl())
135 Diag(ClassLoc, diag::warn_undef_protocolref,
136 ProtocolNames[i]->getName(),
137 ClassName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000138 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 }
140 IDecl->setLocEnd(EndProtoLoc);
141 }
142 return IDecl;
143}
144
145/// ActOnCompatiblityAlias - this action is called after complete parsing of
146/// @compaatibility_alias declaration. It sets up the alias relationships.
147Sema::DeclTy *Sema::ActOnCompatiblityAlias(
148 SourceLocation AtCompatibilityAliasLoc,
149 IdentifierInfo *AliasName, SourceLocation AliasLocation,
150 IdentifierInfo *ClassName, SourceLocation ClassLocation) {
151 // Look for previous declaration of alias name
152 ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary,
153 AliasLocation, TUScope);
154 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000155 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000156 Diag(AliasLocation, diag::warn_previous_alias_decl);
157 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
158 }
159 else {
160 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
161 AliasName->getName());
162 Diag(ADecl->getLocation(), diag::err_previous_declaration);
163 }
164 return 0;
165 }
166 // Check for class declaration
167 ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary,
168 ClassLocation, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000169 if (!CDecl || !isa<ObjCInterfaceDecl>(CDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000170 Diag(ClassLocation, diag::warn_undef_interface,
171 ClassName->getName());
172 if (CDecl)
173 Diag(CDecl->getLocation(), diag::warn_previous_declaration);
174 return 0;
175 }
176 // Everything checked out, instantiate a new alias declaration ast
Ted Kremenek42730c52008-01-07 19:49:32 +0000177 ObjCCompatibleAliasDecl *AliasDecl =
178 new ObjCCompatibleAliasDecl(AtCompatibilityAliasLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000179 AliasName,
Ted Kremenek42730c52008-01-07 19:49:32 +0000180 dyn_cast<ObjCInterfaceDecl>(CDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000181
182 // Chain & install the interface decl into the identifier.
183 AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>());
184 AliasName->setFETokenInfo(AliasDecl);
185 return AliasDecl;
186}
187
188Sema::DeclTy *Sema::ActOnStartProtocolInterface(
189 SourceLocation AtProtoInterfaceLoc,
190 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
191 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
192 SourceLocation EndProtoLoc) {
193 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000195 if (PDecl) {
196 // Protocol already seen. Better be a forward protocol declaration
197 if (!PDecl->isForwardDecl())
198 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
199 ProtocolName->getName());
200 else {
201 PDecl->setForwardDecl(false);
202 PDecl->AllocReferencedProtocols(NumProtoRefs);
203 }
204 }
205 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000206 PDecl = new ObjCProtocolDecl(AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner855e51f2007-12-12 07:09:47 +0000207 ProtocolName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000208 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000209 }
210
211 if (NumProtoRefs) {
212 /// Check then save referenced protocols
213 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000214 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000215 if (!RefPDecl || RefPDecl->isForwardDecl())
216 Diag(ProtocolLoc, diag::warn_undef_protocolref,
217 ProtoRefNames[i]->getName(),
218 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000219 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000220 }
221 PDecl->setLocEnd(EndProtoLoc);
222 }
223 return PDecl;
224}
225
226/// FindProtocolDeclaration - This routine looks up protocols and
227/// issuer error if they are not declared. It returns list of protocol
228/// declarations in its 'Protocols' argument.
229void
230Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
231 IdentifierInfo **ProtocolId,
232 unsigned NumProtocols,
233 llvm::SmallVector<DeclTy *,8> &Protocols) {
234 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000236 if (!PDecl)
237 Diag(TypeLoc, diag::err_undeclared_protocol,
238 ProtocolId[i]->getName());
239 else
240 Protocols.push_back(PDecl);
241 }
242}
243
244/// ActOnForwardProtocolDeclaration -
245Action::DeclTy *
246Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
247 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000248 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000249
250 for (unsigned i = 0; i != NumElts; ++i) {
251 IdentifierInfo *P = IdentList[i];
Ted Kremenek42730c52008-01-07 19:49:32 +0000252 ObjCProtocolDecl *PDecl = ObjCProtocols[P];
Chris Lattner855e51f2007-12-12 07:09:47 +0000253 if (!PDecl) { // Not already seen?
254 // FIXME: Pass in the location of the identifier!
Ted Kremenek42730c52008-01-07 19:49:32 +0000255 PDecl = new ObjCProtocolDecl(AtProtocolLoc, 0, P, true);
256 ObjCProtocols[P] = PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000257 }
258
259 Protocols.push_back(PDecl);
260 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000261 return new ObjCForwardProtocolDecl(AtProtocolLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000262 &Protocols[0], Protocols.size());
263}
264
265Sema::DeclTy *Sema::ActOnStartCategoryInterface(
266 SourceLocation AtInterfaceLoc,
267 IdentifierInfo *ClassName, SourceLocation ClassLoc,
268 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
269 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
270 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000271 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000272
273 /// Check that class of this category is already completely declared.
274 if (!IDecl || IDecl->isForwardDecl()) {
275 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
276 return 0;
277 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000278 ObjCCategoryDecl *CDecl = new ObjCCategoryDecl(AtInterfaceLoc, NumProtoRefs,
Chris Lattner855e51f2007-12-12 07:09:47 +0000279 CategoryName);
280 CDecl->setClassInterface(IDecl);
281 /// Check for duplicate interface declaration for this category
Ted Kremenek42730c52008-01-07 19:49:32 +0000282 ObjCCategoryDecl *CDeclChain;
Chris Lattner855e51f2007-12-12 07:09:47 +0000283 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
284 CDeclChain = CDeclChain->getNextClassCategory()) {
285 if (CDeclChain->getIdentifier() == CategoryName) {
286 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
287 CategoryName->getName());
288 break;
289 }
290 }
291 if (!CDeclChain)
292 CDecl->insertNextClassCategory();
293
294 if (NumProtoRefs) {
295 /// Check then save referenced protocols
296 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000297 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000298 if (!RefPDecl || RefPDecl->isForwardDecl()) {
299 Diag(CategoryLoc, diag::warn_undef_protocolref,
300 ProtoRefNames[i]->getName(),
301 CategoryName->getName());
302 }
Fariborz Jahanian87829072007-12-20 19:24:10 +0000303 CDecl->setCatReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000304 }
305 CDecl->setLocEnd(EndProtoLoc);
306 }
307 return CDecl;
308}
309
310/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000311/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000312/// object.
313Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
314 SourceLocation AtCatImplLoc,
315 IdentifierInfo *ClassName, SourceLocation ClassLoc,
316 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000317 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
318 ObjCCategoryImplDecl *CDecl = new ObjCCategoryImplDecl(AtCatImplLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000319 CatName, IDecl);
320 /// Check that class of this category is already completely declared.
321 if (!IDecl || IDecl->isForwardDecl())
322 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
323
324 /// TODO: Check that CatName, category name, is not used in another
325 // implementation.
326 return CDecl;
327}
328
329Sema::DeclTy *Sema::ActOnStartClassImplementation(
330 SourceLocation AtClassImplLoc,
331 IdentifierInfo *ClassName, SourceLocation ClassLoc,
332 IdentifierInfo *SuperClassname,
333 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000334 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000335 // Check for another declaration kind with the same name.
336 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000337 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000338 Diag(ClassLoc, diag::err_redefinition_different_kind,
339 ClassName->getName());
340 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
341 }
342 else {
343 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000344 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000345 if (!IDecl)
346 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
347 }
348
349 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000350 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000351 if (SuperClassname) {
352 // Check if a different kind of symbol declared in this scope.
353 PrevDecl = LookupInterfaceDecl(SuperClassname);
Ted Kremenek42730c52008-01-07 19:49:32 +0000354 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000355 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
356 SuperClassname->getName());
357 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
358 }
359 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000360 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000361 if (!SDecl)
362 Diag(SuperClassLoc, diag::err_undef_superclass,
363 SuperClassname->getName(), ClassName->getName());
364 else if (IDecl && IDecl->getSuperClass() != SDecl) {
365 // This implementation and its interface do not have the same
366 // super class.
367 Diag(SuperClassLoc, diag::err_conflicting_super_class,
368 SDecl->getName());
369 Diag(SDecl->getLocation(), diag::err_previous_definition);
370 }
371 }
372 }
373
374 if (!IDecl) {
375 // Legacy case of @implementation with no corresponding @interface.
376 // Build, chain & install the interface decl into the identifier.
Ted Kremenek42730c52008-01-07 19:49:32 +0000377 IDecl = new ObjCInterfaceDecl(AtClassImplLoc, 0, ClassName,
Chris Lattner855e51f2007-12-12 07:09:47 +0000378 false, true);
379 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
380 ClassName->setFETokenInfo(IDecl);
381 IDecl->setSuperClass(SDecl);
382 IDecl->setLocEnd(ClassLoc);
383
384 // Remember that this needs to be removed when the scope is popped.
385 TUScope->AddDecl(IDecl);
386 }
387
Ted Kremenek42730c52008-01-07 19:49:32 +0000388 ObjCImplementationDecl* IMPDecl =
389 new ObjCImplementationDecl(AtClassImplLoc, ClassName, IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000390
391 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000392 if (ObjCImplementations[ClassName])
Chris Lattner855e51f2007-12-12 07:09:47 +0000393 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
394 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000395 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000396 return IMPDecl;
397}
398
Ted Kremenek42730c52008-01-07 19:49:32 +0000399void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
400 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000401 SourceLocation RBrace) {
402 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000403 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000404 if (!IDecl)
405 return;
406 /// Check case of non-existing @interface decl.
407 /// (legacy objective-c @implementation decl without an @interface decl).
408 /// Add implementations's ivar to the synthesize class's ivar list.
409 if (IDecl->ImplicitInterfaceDecl()) {
410 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
411 return;
412 }
413 // If implementation has empty ivar list, just return.
414 if (numIvars == 0)
415 return;
416
417 assert(ivars && "missing @implementation ivars");
418
419 // Check interface's Ivar list against those in the implementation.
420 // names and types must match.
421 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000422 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000423 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000424 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
425 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000426 ObjCIvarDecl* ImplIvar = ivars[j++];
427 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000428 assert (ImplIvar && "missing implementation ivar");
429 assert (ClsIvar && "missing class ivar");
430 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
431 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
432 ImplIvar->getIdentifier()->getName());
433 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
434 ClsIvar->getIdentifier()->getName());
435 }
436 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
437 // as error.
438 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
439 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
440 ImplIvar->getIdentifier()->getName());
441 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
442 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000443 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000444 }
445 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000446 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000447
448 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000449 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000450 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000451 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000452}
453
454/// CheckProtocolMethodDefs - This routine checks unimpletented methods
455/// Declared in protocol, and those referenced by it.
Ted Kremenek42730c52008-01-07 19:49:32 +0000456void Sema::CheckProtocolMethodDefs(ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000457 bool& IncompleteImpl,
458 const llvm::DenseSet<Selector> &InsMap,
459 const llvm::DenseSet<Selector> &ClsMap) {
460 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000461 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000462 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000463 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000464 if (!InsMap.count(method->getSelector()) &&
Ted Kremenek42730c52008-01-07 19:49:32 +0000465 method->getImplementationControl() != ObjCMethodDecl::Optional) {
Steve Naroff2ce399a2007-12-14 23:37:57 +0000466 Diag(method->getLocation(), diag::warn_undef_method_impl,
467 method->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000468 IncompleteImpl = true;
469 }
470 }
471 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000472 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000473 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000474 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000475 if (!ClsMap.count(method->getSelector()) &&
Ted Kremenek42730c52008-01-07 19:49:32 +0000476 method->getImplementationControl() != ObjCMethodDecl::Optional) {
Steve Naroff2ce399a2007-12-14 23:37:57 +0000477 Diag(method->getLocation(), diag::warn_undef_method_impl,
478 method->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000479 IncompleteImpl = true;
480 }
Steve Naroff2ce399a2007-12-14 23:37:57 +0000481 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000482 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000483 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000484 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Chris Lattner855e51f2007-12-12 07:09:47 +0000485 CheckProtocolMethodDefs(RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
486}
487
Ted Kremenek42730c52008-01-07 19:49:32 +0000488void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
489 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000490 llvm::DenseSet<Selector> InsMap;
491 // Check and see if instance methods in class interface have been
492 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000493 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000494 E = IMPDecl->instmeth_end(); I != E; ++I)
495 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000496
497 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000498 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000499 E = IDecl->instmeth_end(); I != E; ++I)
500 if (!InsMap.count((*I)->getSelector())) {
501 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
502 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 IncompleteImpl = true;
504 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000505
Chris Lattner855e51f2007-12-12 07:09:47 +0000506 llvm::DenseSet<Selector> ClsMap;
507 // Check and see if class methods in class interface have been
508 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000509 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000510 E = IMPDecl->classmeth_end(); I != E; ++I)
511 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000512
Ted Kremenek42730c52008-01-07 19:49:32 +0000513 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000514 E = IDecl->classmeth_end(); I != E; ++I)
515 if (!ClsMap.count((*I)->getSelector())) {
516 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
517 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000518 IncompleteImpl = true;
519 }
520
521 // Check the protocol list for unimplemented methods in the @implementation
522 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000523 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000524 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Chris Lattner855e51f2007-12-12 07:09:47 +0000525 CheckProtocolMethodDefs(protocols[i], IncompleteImpl, InsMap, ClsMap);
526
527 if (IncompleteImpl)
528 Diag(IMPDecl->getLocation(), diag::warn_incomplete_impl_class,
529 IMPDecl->getName());
530}
531
532/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
533/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000534void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
535 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000536 llvm::DenseSet<Selector> InsMap;
537 // Check and see if instance methods in category interface have been
538 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000539 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000540 E = CatImplDecl->instmeth_end(); I != E; ++I)
541 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000542
543 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000544 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000545 E = CatClassDecl->instmeth_end(); I != E; ++I)
546 if (!InsMap.count((*I)->getSelector())) {
547 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
548 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000549 IncompleteImpl = true;
550 }
551 llvm::DenseSet<Selector> ClsMap;
552 // Check and see if class methods in category interface have been
553 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000554 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000555 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
556 I != E; ++I)
557 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000558
Ted Kremenek42730c52008-01-07 19:49:32 +0000559 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000560 E = CatClassDecl->classmeth_end(); I != E; ++I)
561 if (!ClsMap.count((*I)->getSelector())) {
562 Diag((*I)->getLocation(), diag::warn_undef_method_impl,
563 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000564 IncompleteImpl = true;
565 }
566
567 // Check the protocol list for unimplemented methods in the @implementation
568 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000569 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000570 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000571 ObjCProtocolDecl* PDecl = protocols[i];
Chris Lattner855e51f2007-12-12 07:09:47 +0000572 CheckProtocolMethodDefs(PDecl, IncompleteImpl, InsMap, ClsMap);
573 }
574 if (IncompleteImpl)
575 Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl_category,
576 CatClassDecl->getName());
577}
578
579/// ActOnForwardClassDeclaration -
580Action::DeclTy *
581Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
582 IdentifierInfo **IdentList, unsigned NumElts)
583{
Ted Kremenek42730c52008-01-07 19:49:32 +0000584 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000585
586 for (unsigned i = 0; i != NumElts; ++i) {
587 // Check for another declaration kind with the same name.
588 ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]);
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000590 Diag(AtClassLoc, diag::err_redefinition_different_kind,
591 IdentList[i]->getName());
592 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
593 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000594 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000595 if (!IDecl) { // Not already seen? Make a forward decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000596 IDecl = new ObjCInterfaceDecl(AtClassLoc, 0, IdentList[i], true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000597 // Chain & install the interface decl into the identifier.
598 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
599 IdentList[i]->setFETokenInfo(IDecl);
600
601 // Remember that this needs to be removed when the scope is popped.
602 TUScope->AddDecl(IDecl);
603 }
604
605 Interfaces.push_back(IDecl);
606 }
607
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 return new ObjCClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000609}
610
611
612/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
613/// returns true, or false, accordingly.
614/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000615bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
616 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000617 if (Method->getResultType().getCanonicalType() !=
618 PrevMethod->getResultType().getCanonicalType())
619 return false;
620 for (int i = 0; i < Method->getNumParams(); i++) {
621 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
622 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
623 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
624 return false;
625 }
626 return true;
627}
628
Ted Kremenek42730c52008-01-07 19:49:32 +0000629void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
630 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000631 if (!FirstMethod.Method) {
632 // Haven't seen a method with this selector name yet - add it.
633 FirstMethod.Method = Method;
634 FirstMethod.Next = 0;
635 } else {
636 // We've seen a method with this name, now check the type signature(s).
637 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
638
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000640 Next = Next->Next)
641 match = MatchTwoMethodDeclarations(Method, Next->Method);
642
643 if (!match) {
644 // We have a new signature for an existing method - add it.
645 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000646 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000647 FirstMethod.Next = OMI;
648 }
649 }
650}
651
Ted Kremenek42730c52008-01-07 19:49:32 +0000652void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
653 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000654 if (!FirstMethod.Method) {
655 // Haven't seen a method with this selector name yet - add it.
656 FirstMethod.Method = Method;
657 FirstMethod.Next = 0;
658 } else {
659 // We've seen a method with this name, now check the type signature(s).
660 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
661
Ted Kremenek42730c52008-01-07 19:49:32 +0000662 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000663 Next = Next->Next)
664 match = MatchTwoMethodDeclarations(Method, Next->Method);
665
666 if (!match) {
667 // We have a new signature for an existing method - add it.
668 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000669 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000670 FirstMethod.Next = OMI;
671 }
672 }
673}
674
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000675// Note: For class/category implemenations, allMethods/allProperties is
676// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000677void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
678 DeclTy **allMethods, unsigned allNum,
679 DeclTy **allProperties, unsigned pNum) {
680 Decl *ClassDecl = static_cast<Decl *>(classDecl);
681
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000682 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
683 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000684 // should be true.
685 if (!ClassDecl)
686 return;
687
Ted Kremenek42730c52008-01-07 19:49:32 +0000688 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
689 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000690
Ted Kremenek42730c52008-01-07 19:49:32 +0000691 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
692 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000693
694 bool isInterfaceDeclKind =
Ted Kremenek42730c52008-01-07 19:49:32 +0000695 (isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
696 || isa<ObjCProtocolDecl>(ClassDecl));
697 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000698
699 // TODO: property declaration in category and protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000700 if (pNum != 0 && isa<ObjCInterfaceDecl>(ClassDecl)) {
701 ObjCPropertyDecl **properties = new ObjCPropertyDecl*[pNum];
702 memcpy(properties, allProperties, pNum*sizeof(ObjCPropertyDecl*));
703 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setPropertyDecls(properties);
704 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum);
Chris Lattner855e51f2007-12-12 07:09:47 +0000705 }
706
707 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000708 ObjCMethodDecl *Method =
709 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000710
711 if (!Method) continue; // Already issued a diagnostic.
712 if (Method->isInstance()) {
713 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000714 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000715 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
716 : false;
717 if (isInterfaceDeclKind && PrevMethod && !match
718 || checkIdenticalMethods && match) {
719 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
720 Method->getSelector().getName());
721 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
722 } else {
723 insMethods.push_back(Method);
724 InsMap[Method->getSelector()] = Method;
725 /// The following allows us to typecheck messages to "id".
726 AddInstanceMethodToGlobalPool(Method);
727 }
728 }
729 else {
730 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000731 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000732 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
733 : false;
734 if (isInterfaceDeclKind && PrevMethod && !match
735 || checkIdenticalMethods && match) {
736 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
737 Method->getSelector().getName());
738 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
739 } else {
740 clsMethods.push_back(Method);
741 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000742 /// The following allows us to typecheck messages to "Class".
743 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000744 }
745 }
746 }
747
Ted Kremenek42730c52008-01-07 19:49:32 +0000748 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000749 I->addMethods(&insMethods[0], insMethods.size(),
750 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000751 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000752 P->addMethods(&insMethods[0], insMethods.size(),
753 &clsMethods[0], clsMethods.size(), AtEndLoc);
754 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000755 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000756 C->addMethods(&insMethods[0], insMethods.size(),
757 &clsMethods[0], clsMethods.size(), AtEndLoc);
758 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000759 else if (ObjCImplementationDecl *IC =
760 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000762 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000763 ImplMethodsVsClassMethods(IC, IDecl);
764 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000765 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000766 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000767 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000768 // Find category interface decl and then check that all methods declared
769 // in this interface is implemented in the category @implementation.
770 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000771 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000772 Categories; Categories = Categories->getNextClassCategory()) {
773 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
774 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
775 break;
776 }
777 }
778 }
779 }
780}
781
782
783/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
784/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000785static Decl::ObjCDeclQualifier
786CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
787 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
788 if (PQTVal & ObjCDeclSpec::DQ_In)
789 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
790 if (PQTVal & ObjCDeclSpec::DQ_Inout)
791 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
792 if (PQTVal & ObjCDeclSpec::DQ_Out)
793 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
794 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
795 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
796 if (PQTVal & ObjCDeclSpec::DQ_Byref)
797 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
798 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
799 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000800
801 return ret;
802}
803
804Sema::DeclTy *Sema::ActOnMethodDeclaration(
805 SourceLocation MethodLoc, SourceLocation EndLoc,
806 tok::TokenKind MethodType, DeclTy *ClassDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000807 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000808 Selector Sel,
809 // optional arguments. The number of types/arguments is obtained
810 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000811 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000812 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
813 bool isVariadic) {
814 llvm::SmallVector<ParmVarDecl*, 16> Params;
815
816 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
817 // FIXME: arg->AttrList must be stored too!
818 QualType argType;
819
820 if (ArgTypes[i])
821 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
822 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000823 argType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000824 ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i],
825 argType, VarDecl::None, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000826 Param->setObjCDeclQualifier(
827 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
Chris Lattner855e51f2007-12-12 07:09:47 +0000828 Params.push_back(Param);
829 }
830 QualType resultDeclType;
831
832 if (ReturnType)
833 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
834 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000836
837 Decl *CDecl = static_cast<Decl*>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000838 ObjCMethodDecl* ObjCMethod = new ObjCMethodDecl(MethodLoc, EndLoc, Sel,
Chris Lattner855e51f2007-12-12 07:09:47 +0000839 resultDeclType,
840 CDecl,
841 0, -1, AttrList,
842 MethodType == tok::minus, isVariadic,
843 MethodDeclKind == tok::objc_optional ?
Ted Kremenek42730c52008-01-07 19:49:32 +0000844 ObjCMethodDecl::Optional :
845 ObjCMethodDecl::Required);
846 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
847 ObjCMethod->setObjCDeclQualifier(
848 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
849 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000850
851 // For implementations (which can be very "coarse grain"), we add the
852 // method now. This allows the AST to implement lookup methods that work
853 // incrementally (without waiting until we parse the @end). It also allows
854 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000855 if (ObjCImplementationDecl *ImpDecl =
856 dyn_cast<ObjCImplementationDecl>(CDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000857 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000858 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000859 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000860 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000861 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000862 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000863 }
864 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000865 else if (ObjCCategoryImplDecl *CatImpDecl =
866 dyn_cast<ObjCCategoryImplDecl>(CDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000867 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000868 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000869 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000870 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000871 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000872 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000873 }
874 }
875 if (PrevMethod) {
876 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +0000877 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
878 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000879 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
880 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +0000882}
883
Ted Kremenek42730c52008-01-07 19:49:32 +0000884Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
885 DeclTy **allProperties, unsigned NumProperties, ObjCDeclSpec &DS) {
886 ObjCPropertyDecl *PDecl = new ObjCPropertyDecl(AtLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000887
Ted Kremenek42730c52008-01-07 19:49:32 +0000888 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
889 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +0000890
Ted Kremenek42730c52008-01-07 19:49:32 +0000891 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
892 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000893 PDecl->setGetterName(DS.getGetterName());
894 }
895
Ted Kremenek42730c52008-01-07 19:49:32 +0000896 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
897 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000898 PDecl->setSetterName(DS.getSetterName());
899 }
900
Ted Kremenek42730c52008-01-07 19:49:32 +0000901 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
902 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +0000903
Ted Kremenek42730c52008-01-07 19:49:32 +0000904 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
905 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +0000906
Ted Kremenek42730c52008-01-07 19:49:32 +0000907 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
908 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +0000909
Ted Kremenek42730c52008-01-07 19:49:32 +0000910 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
911 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +0000912
Ted Kremenek42730c52008-01-07 19:49:32 +0000913 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
914 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +0000915
916 PDecl->setNumPropertyDecls(NumProperties);
917 if (NumProperties != 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000918 ObjCIvarDecl **properties = new ObjCIvarDecl*[NumProperties];
919 memcpy(properties, allProperties, NumProperties*sizeof(ObjCIvarDecl*));
Chris Lattner855e51f2007-12-12 07:09:47 +0000920 PDecl->setPropertyDecls(properties);
921 }
922 return PDecl;
923}
924