blob: 824557f4c585c9ff2376e4e52a6867674817b47a [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()) {
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000046 ObjCInterfaceDecl *OID = MDecl->getClassInterface();
47 // There may be no interface context due to error in declaration of the
48 // interface (which has been reported). Recover gracefully
49 if (OID) {
50 QualType selfTy = Context.getObjCInterfaceType(OID);
51 selfTy = Context.getPointerType(selfTy);
52 PI.TypeInfo = selfTy.getAsOpaquePtr();
53 }
Chris Lattner855e51f2007-12-12 07:09:47 +000054 } else
Ted Kremenek42730c52008-01-07 19:49:32 +000055 PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr();
Chris Lattner855e51f2007-12-12 07:09:47 +000056 CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope));
57
58 PI.Ident = &Context.Idents.get("_cmd");
Ted Kremenek42730c52008-01-07 19:49:32 +000059 PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr();
Chris Lattner855e51f2007-12-12 07:09:47 +000060 ActOnParamDeclarator(PI, FnBodyScope);
61
62 for (int i = 0; i < MDecl->getNumParams(); i++) {
63 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
64 PI.Ident = PDecl->getIdentifier();
65 PI.IdentLoc = PDecl->getLocation(); // user vars have a real location.
66 PI.TypeInfo = PDecl->getType().getAsOpaquePtr();
Fariborz Jahanian00a313b2008-01-21 22:59:53 +000067 MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope));
Chris Lattner855e51f2007-12-12 07:09:47 +000068 }
69}
70
71Sema::DeclTy *Sema::ActOnStartClassInterface(
72 SourceLocation AtInterfaceLoc,
73 IdentifierInfo *ClassName, SourceLocation ClassLoc,
74 IdentifierInfo *SuperName, SourceLocation SuperLoc,
75 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
76 SourceLocation EndProtoLoc, AttributeList *AttrList) {
77 assert(ClassName && "Missing class identifier");
78
79 // Check for another declaration kind with the same name.
80 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremenek42730c52008-01-07 19:49:32 +000081 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000082 Diag(ClassLoc, diag::err_redefinition_different_kind,
83 ClassName->getName());
84 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
85 }
86
Ted Kremenek42730c52008-01-07 19:49:32 +000087 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000088 if (IDecl) {
89 // Class already seen. Is it a forward declaration?
90 if (!IDecl->isForwardDecl())
91 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
92 else {
93 IDecl->setLocation(AtInterfaceLoc);
94 IDecl->setForwardDecl(false);
95 IDecl->AllocIntfRefProtocols(NumProtocols);
96 }
97 }
98 else {
Ted Kremenek42730c52008-01-07 19:49:32 +000099 IDecl = new ObjCInterfaceDecl(AtInterfaceLoc, NumProtocols, ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
101 // Chain & install the interface decl into the identifier.
102 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
103 ClassName->setFETokenInfo(IDecl);
104
105 // Remember that this needs to be removed when the scope is popped.
106 TUScope->AddDecl(IDecl);
107 }
108
109 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000111 // Check if a different kind of symbol declared in this scope.
112 PrevDecl = LookupInterfaceDecl(SuperName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000114 Diag(SuperLoc, diag::err_redefinition_different_kind,
115 SuperName->getName());
116 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
117 }
118 else {
119 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000120 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000121
122 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
123 Diag(AtInterfaceLoc, diag::err_undef_superclass,
124 SuperClassEntry ? SuperClassEntry->getName()
125 : SuperName->getName(),
126 ClassName->getName());
127 }
128 }
129 IDecl->setSuperClass(SuperClassEntry);
130 IDecl->setLocEnd(SuperLoc);
131 } else { // we have a root class.
132 IDecl->setLocEnd(ClassLoc);
133 }
134
135 /// Check then save referenced protocols
136 if (NumProtocols) {
137 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000138 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 if (!RefPDecl || RefPDecl->isForwardDecl())
140 Diag(ClassLoc, diag::warn_undef_protocolref,
141 ProtocolNames[i]->getName(),
142 ClassName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000143 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000144 }
145 IDecl->setLocEnd(EndProtoLoc);
146 }
147 return IDecl;
148}
149
150/// ActOnCompatiblityAlias - this action is called after complete parsing of
151/// @compaatibility_alias declaration. It sets up the alias relationships.
152Sema::DeclTy *Sema::ActOnCompatiblityAlias(
153 SourceLocation AtCompatibilityAliasLoc,
154 IdentifierInfo *AliasName, SourceLocation AliasLocation,
155 IdentifierInfo *ClassName, SourceLocation ClassLocation) {
156 // Look for previous declaration of alias name
157 ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary,
158 AliasLocation, TUScope);
159 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000160 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000161 Diag(AliasLocation, diag::warn_previous_alias_decl);
162 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
163 }
164 else {
165 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
166 AliasName->getName());
167 Diag(ADecl->getLocation(), diag::err_previous_declaration);
168 }
169 return 0;
170 }
171 // Check for class declaration
172 ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary,
173 ClassLocation, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000174 if (!CDecl || !isa<ObjCInterfaceDecl>(CDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 Diag(ClassLocation, diag::warn_undef_interface,
176 ClassName->getName());
177 if (CDecl)
178 Diag(CDecl->getLocation(), diag::warn_previous_declaration);
179 return 0;
180 }
181 // Everything checked out, instantiate a new alias declaration ast
Ted Kremenek42730c52008-01-07 19:49:32 +0000182 ObjCCompatibleAliasDecl *AliasDecl =
183 new ObjCCompatibleAliasDecl(AtCompatibilityAliasLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000184 AliasName,
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 dyn_cast<ObjCInterfaceDecl>(CDecl));
Chris Lattner855e51f2007-12-12 07:09:47 +0000186
187 // Chain & install the interface decl into the identifier.
188 AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>());
189 AliasName->setFETokenInfo(AliasDecl);
190 return AliasDecl;
191}
192
193Sema::DeclTy *Sema::ActOnStartProtocolInterface(
194 SourceLocation AtProtoInterfaceLoc,
195 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
196 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
197 SourceLocation EndProtoLoc) {
198 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000199 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 if (PDecl) {
201 // Protocol already seen. Better be a forward protocol declaration
202 if (!PDecl->isForwardDecl())
203 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
204 ProtocolName->getName());
205 else {
206 PDecl->setForwardDecl(false);
207 PDecl->AllocReferencedProtocols(NumProtoRefs);
208 }
209 }
210 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000211 PDecl = new ObjCProtocolDecl(AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner855e51f2007-12-12 07:09:47 +0000212 ProtocolName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000213 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000214 }
215
216 if (NumProtoRefs) {
217 /// Check then save referenced protocols
218 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000219 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000220 if (!RefPDecl || RefPDecl->isForwardDecl())
221 Diag(ProtocolLoc, diag::warn_undef_protocolref,
222 ProtoRefNames[i]->getName(),
223 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000224 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 }
226 PDecl->setLocEnd(EndProtoLoc);
227 }
228 return PDecl;
229}
230
231/// FindProtocolDeclaration - This routine looks up protocols and
232/// issuer error if they are not declared. It returns list of protocol
233/// declarations in its 'Protocols' argument.
234void
235Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
236 IdentifierInfo **ProtocolId,
237 unsigned NumProtocols,
238 llvm::SmallVector<DeclTy *,8> &Protocols) {
239 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000241 if (!PDecl)
242 Diag(TypeLoc, diag::err_undeclared_protocol,
243 ProtocolId[i]->getName());
244 else
245 Protocols.push_back(PDecl);
246 }
247}
248
249/// ActOnForwardProtocolDeclaration -
250Action::DeclTy *
251Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
252 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000253 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000254
255 for (unsigned i = 0; i != NumElts; ++i) {
256 IdentifierInfo *P = IdentList[i];
Ted Kremenek42730c52008-01-07 19:49:32 +0000257 ObjCProtocolDecl *PDecl = ObjCProtocols[P];
Chris Lattner855e51f2007-12-12 07:09:47 +0000258 if (!PDecl) { // Not already seen?
259 // FIXME: Pass in the location of the identifier!
Ted Kremenek42730c52008-01-07 19:49:32 +0000260 PDecl = new ObjCProtocolDecl(AtProtocolLoc, 0, P, true);
261 ObjCProtocols[P] = PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000262 }
263
264 Protocols.push_back(PDecl);
265 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000266 return new ObjCForwardProtocolDecl(AtProtocolLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000267 &Protocols[0], Protocols.size());
268}
269
270Sema::DeclTy *Sema::ActOnStartCategoryInterface(
271 SourceLocation AtInterfaceLoc,
272 IdentifierInfo *ClassName, SourceLocation ClassLoc,
273 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
274 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
275 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000276 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000277
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);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000281
282 /// Check that class of this category is already completely declared.
283 if (!IDecl || IDecl->isForwardDecl())
284 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
285 else {
286 /// Check for duplicate interface declaration for this category
287 ObjCCategoryDecl *CDeclChain;
288 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
289 CDeclChain = CDeclChain->getNextClassCategory()) {
290 if (CDeclChain->getIdentifier() == CategoryName) {
291 Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(),
292 CategoryName->getName());
293 break;
294 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000295 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000296 if (!CDeclChain)
297 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000298 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000299
300 if (NumProtoRefs) {
301 /// Check then save referenced protocols
302 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000303 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000304 if (!RefPDecl || RefPDecl->isForwardDecl()) {
305 Diag(CategoryLoc, diag::warn_undef_protocolref,
306 ProtoRefNames[i]->getName(),
307 CategoryName->getName());
308 }
Fariborz Jahanian87829072007-12-20 19:24:10 +0000309 CDecl->setCatReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000310 }
311 CDecl->setLocEnd(EndProtoLoc);
312 }
313 return CDecl;
314}
315
316/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000317/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000318/// object.
319Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
320 SourceLocation AtCatImplLoc,
321 IdentifierInfo *ClassName, SourceLocation ClassLoc,
322 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000323 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
324 ObjCCategoryImplDecl *CDecl = new ObjCCategoryImplDecl(AtCatImplLoc,
Chris Lattner855e51f2007-12-12 07:09:47 +0000325 CatName, IDecl);
326 /// Check that class of this category is already completely declared.
327 if (!IDecl || IDecl->isForwardDecl())
328 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
329
330 /// TODO: Check that CatName, category name, is not used in another
331 // implementation.
332 return CDecl;
333}
334
335Sema::DeclTy *Sema::ActOnStartClassImplementation(
336 SourceLocation AtClassImplLoc,
337 IdentifierInfo *ClassName, SourceLocation ClassLoc,
338 IdentifierInfo *SuperClassname,
339 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000340 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000341 // Check for another declaration kind with the same name.
342 ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName);
Ted Kremenek42730c52008-01-07 19:49:32 +0000343 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000344 Diag(ClassLoc, diag::err_redefinition_different_kind,
345 ClassName->getName());
346 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
347 }
348 else {
349 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000350 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000351 if (!IDecl)
352 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
353 }
354
355 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000356 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000357 if (SuperClassname) {
358 // Check if a different kind of symbol declared in this scope.
359 PrevDecl = LookupInterfaceDecl(SuperClassname);
Ted Kremenek42730c52008-01-07 19:49:32 +0000360 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000361 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
362 SuperClassname->getName());
363 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
364 }
365 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000366 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000367 if (!SDecl)
368 Diag(SuperClassLoc, diag::err_undef_superclass,
369 SuperClassname->getName(), ClassName->getName());
370 else if (IDecl && IDecl->getSuperClass() != SDecl) {
371 // This implementation and its interface do not have the same
372 // super class.
373 Diag(SuperClassLoc, diag::err_conflicting_super_class,
374 SDecl->getName());
375 Diag(SDecl->getLocation(), diag::err_previous_definition);
376 }
377 }
378 }
379
380 if (!IDecl) {
381 // Legacy case of @implementation with no corresponding @interface.
382 // Build, chain & install the interface decl into the identifier.
Ted Kremenek42730c52008-01-07 19:49:32 +0000383 IDecl = new ObjCInterfaceDecl(AtClassImplLoc, 0, ClassName,
Chris Lattner855e51f2007-12-12 07:09:47 +0000384 false, true);
385 IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>());
386 ClassName->setFETokenInfo(IDecl);
387 IDecl->setSuperClass(SDecl);
388 IDecl->setLocEnd(ClassLoc);
389
390 // Remember that this needs to be removed when the scope is popped.
391 TUScope->AddDecl(IDecl);
392 }
393
Ted Kremenek42730c52008-01-07 19:49:32 +0000394 ObjCImplementationDecl* IMPDecl =
395 new ObjCImplementationDecl(AtClassImplLoc, ClassName, IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000396
397 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000398 if (ObjCImplementations[ClassName])
Chris Lattner855e51f2007-12-12 07:09:47 +0000399 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
400 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000401 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000402 return IMPDecl;
403}
404
Ted Kremenek42730c52008-01-07 19:49:32 +0000405void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
406 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000407 SourceLocation RBrace) {
408 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000409 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000410 if (!IDecl)
411 return;
412 /// Check case of non-existing @interface decl.
413 /// (legacy objective-c @implementation decl without an @interface decl).
414 /// Add implementations's ivar to the synthesize class's ivar list.
415 if (IDecl->ImplicitInterfaceDecl()) {
416 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
417 return;
418 }
419 // If implementation has empty ivar list, just return.
420 if (numIvars == 0)
421 return;
422
423 assert(ivars && "missing @implementation ivars");
424
425 // Check interface's Ivar list against those in the implementation.
426 // names and types must match.
427 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000428 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000429 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000430 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
431 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000432 ObjCIvarDecl* ImplIvar = ivars[j++];
433 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000434 assert (ImplIvar && "missing implementation ivar");
435 assert (ClsIvar && "missing class ivar");
436 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
437 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
438 ImplIvar->getIdentifier()->getName());
439 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
440 ClsIvar->getIdentifier()->getName());
441 }
442 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
443 // as error.
444 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
445 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
446 ImplIvar->getIdentifier()->getName());
447 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
448 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000449 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000450 }
451 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000452 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000453
454 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000455 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000456 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000457 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000458}
459
Steve Naroffb268d2a2008-02-08 22:06:17 +0000460/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000461/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000462void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
463 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000464 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000465 const llvm::DenseSet<Selector> &InsMap,
466 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000467 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000468 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000469 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000470 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000471 if (!InsMap.count(method->getSelector()) &&
Ted Kremenek42730c52008-01-07 19:49:32 +0000472 method->getImplementationControl() != ObjCMethodDecl::Optional) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000473 if (!IncompleteImpl) {
474 Diag(ImpLoc, diag::warn_incomplete_impl);
475 IncompleteImpl = true;
476 }
477 Diag(ImpLoc, diag::warn_undef_method_impl,
Steve Naroff2ce399a2007-12-14 23:37:57 +0000478 method->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000479 }
480 }
481 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000482 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000483 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000484 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000485 if (!ClsMap.count(method->getSelector()) &&
Ted Kremenek42730c52008-01-07 19:49:32 +0000486 method->getImplementationControl() != ObjCMethodDecl::Optional) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000487 if (!IncompleteImpl) {
488 Diag(ImpLoc, diag::warn_incomplete_impl);
489 IncompleteImpl = true;
490 }
491 Diag(ImpLoc, diag::warn_undef_method_impl,
Steve Naroff2ce399a2007-12-14 23:37:57 +0000492 method->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000493 }
Steve Naroff2ce399a2007-12-14 23:37:57 +0000494 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000495 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000496 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000497 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000498 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000499}
500
Ted Kremenek42730c52008-01-07 19:49:32 +0000501void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
502 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 llvm::DenseSet<Selector> InsMap;
504 // Check and see if instance methods in class interface have been
505 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000506 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000507 E = IMPDecl->instmeth_end(); I != E; ++I)
508 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000509
510 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000511 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000512 E = IDecl->instmeth_end(); I != E; ++I)
513 if (!InsMap.count((*I)->getSelector())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000514 if (!IncompleteImpl) {
515 Diag(IMPDecl->getLocation(), diag::warn_incomplete_impl);
516 IncompleteImpl = true;
517 }
518 Diag(IMPDecl->getLocation(), diag::warn_undef_method_impl,
Chris Lattner9d76c722007-12-12 17:58:05 +0000519 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000520 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000521
Chris Lattner855e51f2007-12-12 07:09:47 +0000522 llvm::DenseSet<Selector> ClsMap;
523 // Check and see if class methods in class interface have been
524 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000525 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000526 E = IMPDecl->classmeth_end(); I != E; ++I)
527 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000528
Ted Kremenek42730c52008-01-07 19:49:32 +0000529 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000530 E = IDecl->classmeth_end(); I != E; ++I)
531 if (!ClsMap.count((*I)->getSelector())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000532 if (!IncompleteImpl) {
533 Diag(IMPDecl->getLocation(), diag::warn_incomplete_impl);
534 IncompleteImpl = true;
535 }
536 Diag(IMPDecl->getLocation(), diag::warn_undef_method_impl,
Chris Lattner9d76c722007-12-12 17:58:05 +0000537 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 }
539
540 // Check the protocol list for unimplemented methods in the @implementation
541 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000542 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000543 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000544 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
545 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000546}
547
548/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
549/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000550void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
551 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000552 llvm::DenseSet<Selector> InsMap;
553 // Check and see if instance methods in category interface have been
554 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000555 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000556 E = CatImplDecl->instmeth_end(); I != E; ++I)
557 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000558
559 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000560 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000561 E = CatClassDecl->instmeth_end(); I != E; ++I)
562 if (!InsMap.count((*I)->getSelector())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000563 if (!IncompleteImpl) {
564 Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl);
565 IncompleteImpl = true;
566 }
567 Diag(CatImplDecl->getLocation(), diag::warn_undef_method_impl,
Steve Naroff2ce399a2007-12-14 23:37:57 +0000568 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000569 }
570 llvm::DenseSet<Selector> ClsMap;
571 // Check and see if class methods in category interface have been
572 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000573 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000574 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
575 I != E; ++I)
576 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000577
Ted Kremenek42730c52008-01-07 19:49:32 +0000578 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000579 E = CatClassDecl->classmeth_end(); I != E; ++I)
580 if (!ClsMap.count((*I)->getSelector())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000581 if (!IncompleteImpl) {
582 Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl);
583 IncompleteImpl = true;
584 }
585 Diag(CatImplDecl->getLocation(), diag::warn_undef_method_impl,
Steve Naroff2ce399a2007-12-14 23:37:57 +0000586 (*I)->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 }
588
589 // Check the protocol list for unimplemented methods in the @implementation
590 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000591 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000592 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000593 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000594 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
595 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000596 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000597}
598
599/// ActOnForwardClassDeclaration -
600Action::DeclTy *
601Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
602 IdentifierInfo **IdentList, unsigned NumElts)
603{
Ted Kremenek42730c52008-01-07 19:49:32 +0000604 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000605
606 for (unsigned i = 0; i != NumElts; ++i) {
607 // Check for another declaration kind with the same name.
608 ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]);
Ted Kremenek42730c52008-01-07 19:49:32 +0000609 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000610 Diag(AtClassLoc, diag::err_redefinition_different_kind,
611 IdentList[i]->getName());
612 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
613 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000614 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000615 if (!IDecl) { // Not already seen? Make a forward decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 IDecl = new ObjCInterfaceDecl(AtClassLoc, 0, IdentList[i], true);
Chris Lattner855e51f2007-12-12 07:09:47 +0000617 // Chain & install the interface decl into the identifier.
618 IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>());
619 IdentList[i]->setFETokenInfo(IDecl);
620
621 // Remember that this needs to be removed when the scope is popped.
622 TUScope->AddDecl(IDecl);
623 }
624
625 Interfaces.push_back(IDecl);
626 }
627
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 return new ObjCClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000629}
630
631
632/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
633/// returns true, or false, accordingly.
634/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000635bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
636 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000637 if (Method->getResultType().getCanonicalType() !=
638 PrevMethod->getResultType().getCanonicalType())
639 return false;
640 for (int i = 0; i < Method->getNumParams(); i++) {
641 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
642 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
643 if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType())
644 return false;
645 }
646 return true;
647}
648
Ted Kremenek42730c52008-01-07 19:49:32 +0000649void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
650 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000651 if (!FirstMethod.Method) {
652 // Haven't seen a method with this selector name yet - add it.
653 FirstMethod.Method = Method;
654 FirstMethod.Next = 0;
655 } else {
656 // We've seen a method with this name, now check the type signature(s).
657 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
658
Ted Kremenek42730c52008-01-07 19:49:32 +0000659 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000660 Next = Next->Next)
661 match = MatchTwoMethodDeclarations(Method, Next->Method);
662
663 if (!match) {
664 // We have a new signature for an existing method - add it.
665 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000666 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000667 FirstMethod.Next = OMI;
668 }
669 }
670}
671
Ted Kremenek42730c52008-01-07 19:49:32 +0000672void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
673 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000674 if (!FirstMethod.Method) {
675 // Haven't seen a method with this selector name yet - add it.
676 FirstMethod.Method = Method;
677 FirstMethod.Next = 0;
678 } else {
679 // We've seen a method with this name, now check the type signature(s).
680 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
681
Ted Kremenek42730c52008-01-07 19:49:32 +0000682 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000683 Next = Next->Next)
684 match = MatchTwoMethodDeclarations(Method, Next->Method);
685
686 if (!match) {
687 // We have a new signature for an existing method - add it.
688 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000690 FirstMethod.Next = OMI;
691 }
692 }
693}
694
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000695// Note: For class/category implemenations, allMethods/allProperties is
696// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000697void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
698 DeclTy **allMethods, unsigned allNum,
699 DeclTy **allProperties, unsigned pNum) {
700 Decl *ClassDecl = static_cast<Decl *>(classDecl);
701
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000702 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
703 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000704 // should be true.
705 if (!ClassDecl)
706 return;
707
Ted Kremenek42730c52008-01-07 19:49:32 +0000708 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
709 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000710
Ted Kremenek42730c52008-01-07 19:49:32 +0000711 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
712 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000713
714 bool isInterfaceDeclKind =
Ted Kremenek42730c52008-01-07 19:49:32 +0000715 (isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
716 || isa<ObjCProtocolDecl>(ClassDecl));
717 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000718
719 // TODO: property declaration in category and protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000720 if (pNum != 0 && isa<ObjCInterfaceDecl>(ClassDecl)) {
721 ObjCPropertyDecl **properties = new ObjCPropertyDecl*[pNum];
722 memcpy(properties, allProperties, pNum*sizeof(ObjCPropertyDecl*));
723 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setPropertyDecls(properties);
724 dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum);
Chris Lattner855e51f2007-12-12 07:09:47 +0000725 }
726
727 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000728 ObjCMethodDecl *Method =
729 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000730
731 if (!Method) continue; // Already issued a diagnostic.
732 if (Method->isInstance()) {
733 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000734 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000735 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
736 : false;
737 if (isInterfaceDeclKind && PrevMethod && !match
738 || checkIdenticalMethods && match) {
739 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
740 Method->getSelector().getName());
741 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
742 } else {
743 insMethods.push_back(Method);
744 InsMap[Method->getSelector()] = Method;
745 /// The following allows us to typecheck messages to "id".
746 AddInstanceMethodToGlobalPool(Method);
747 }
748 }
749 else {
750 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000751 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000752 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
753 : false;
754 if (isInterfaceDeclKind && PrevMethod && !match
755 || checkIdenticalMethods && match) {
756 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
757 Method->getSelector().getName());
758 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
759 } else {
760 clsMethods.push_back(Method);
761 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000762 /// The following allows us to typecheck messages to "Class".
763 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000764 }
765 }
766 }
767
Ted Kremenek42730c52008-01-07 19:49:32 +0000768 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000769 I->addMethods(&insMethods[0], insMethods.size(),
770 &clsMethods[0], clsMethods.size(), AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000771 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000772 P->addMethods(&insMethods[0], insMethods.size(),
773 &clsMethods[0], clsMethods.size(), AtEndLoc);
774 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000775 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000776 C->addMethods(&insMethods[0], insMethods.size(),
777 &clsMethods[0], clsMethods.size(), AtEndLoc);
778 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000779 else if (ObjCImplementationDecl *IC =
780 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000781 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000782 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000783 ImplMethodsVsClassMethods(IC, IDecl);
784 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000785 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000786 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000787 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000788 // Find category interface decl and then check that all methods declared
789 // in this interface is implemented in the category @implementation.
790 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000791 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000792 Categories; Categories = Categories->getNextClassCategory()) {
793 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
794 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
795 break;
796 }
797 }
798 }
799 }
800}
801
802
803/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
804/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000805static Decl::ObjCDeclQualifier
806CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
807 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
808 if (PQTVal & ObjCDeclSpec::DQ_In)
809 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
810 if (PQTVal & ObjCDeclSpec::DQ_Inout)
811 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
812 if (PQTVal & ObjCDeclSpec::DQ_Out)
813 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
814 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
815 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
816 if (PQTVal & ObjCDeclSpec::DQ_Byref)
817 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
818 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
819 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000820
821 return ret;
822}
823
824Sema::DeclTy *Sema::ActOnMethodDeclaration(
825 SourceLocation MethodLoc, SourceLocation EndLoc,
826 tok::TokenKind MethodType, DeclTy *ClassDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000827 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000828 Selector Sel,
829 // optional arguments. The number of types/arguments is obtained
830 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000831 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000832 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
833 bool isVariadic) {
834 llvm::SmallVector<ParmVarDecl*, 16> Params;
835
836 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
837 // FIXME: arg->AttrList must be stored too!
838 QualType argType;
839
840 if (ArgTypes[i])
841 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
842 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000843 argType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000844 ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i],
845 argType, VarDecl::None, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000846 Param->setObjCDeclQualifier(
847 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
Chris Lattner855e51f2007-12-12 07:09:47 +0000848 Params.push_back(Param);
849 }
850 QualType resultDeclType;
851
852 if (ReturnType)
853 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
854 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000855 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000856
857 Decl *CDecl = static_cast<Decl*>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000858 ObjCMethodDecl* ObjCMethod = new ObjCMethodDecl(MethodLoc, EndLoc, Sel,
Chris Lattner855e51f2007-12-12 07:09:47 +0000859 resultDeclType,
860 CDecl,
861 0, -1, AttrList,
862 MethodType == tok::minus, isVariadic,
863 MethodDeclKind == tok::objc_optional ?
Ted Kremenek42730c52008-01-07 19:49:32 +0000864 ObjCMethodDecl::Optional :
865 ObjCMethodDecl::Required);
866 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
867 ObjCMethod->setObjCDeclQualifier(
868 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
869 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000870
871 // For implementations (which can be very "coarse grain"), we add the
872 // method now. This allows the AST to implement lookup methods that work
873 // incrementally (without waiting until we parse the @end). It also allows
874 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000875 if (ObjCImplementationDecl *ImpDecl =
876 dyn_cast<ObjCImplementationDecl>(CDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000877 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000878 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000879 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000880 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000881 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000882 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000883 }
884 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000885 else if (ObjCCategoryImplDecl *CatImpDecl =
886 dyn_cast<ObjCCategoryImplDecl>(CDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000887 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +0000888 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000890 } else {
Steve Naroff74273de2007-12-19 22:27:04 +0000891 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +0000892 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +0000893 }
894 }
895 if (PrevMethod) {
896 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +0000897 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
898 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +0000899 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
900 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000901 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +0000902}
903
Ted Kremenek42730c52008-01-07 19:49:32 +0000904Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc,
905 DeclTy **allProperties, unsigned NumProperties, ObjCDeclSpec &DS) {
906 ObjCPropertyDecl *PDecl = new ObjCPropertyDecl(AtLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000907
Ted Kremenek42730c52008-01-07 19:49:32 +0000908 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
909 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +0000910
Ted Kremenek42730c52008-01-07 19:49:32 +0000911 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) {
912 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000913 PDecl->setGetterName(DS.getGetterName());
914 }
915
Ted Kremenek42730c52008-01-07 19:49:32 +0000916 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) {
917 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +0000918 PDecl->setSetterName(DS.getSetterName());
919 }
920
Ted Kremenek42730c52008-01-07 19:49:32 +0000921 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
922 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +0000923
Ted Kremenek42730c52008-01-07 19:49:32 +0000924 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
925 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +0000926
Ted Kremenek42730c52008-01-07 19:49:32 +0000927 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
928 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +0000929
Ted Kremenek42730c52008-01-07 19:49:32 +0000930 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
931 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +0000932
Ted Kremenek42730c52008-01-07 19:49:32 +0000933 if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
934 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +0000935
936 PDecl->setNumPropertyDecls(NumProperties);
937 if (NumProperties != 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000938 ObjCIvarDecl **properties = new ObjCIvarDecl*[NumProperties];
939 memcpy(properties, allProperties, NumProperties*sizeof(ObjCIvarDecl*));
Chris Lattner855e51f2007-12-12 07:09:47 +0000940 PDecl->setPropertyDecls(properties);
941 }
942 return PDecl;
943}
944