blob: 6880eec9af6d89c45ffa8d92a971658d680fe17f [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) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000024 assert(getCurMethodDecl() == 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.
Chris Lattnerf3874bc2008-04-06 04:47:34 +000035 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000036
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.
Steve Naroff1a5027c2008-06-05 14:49:39 +000044 QualType selfTy;
Chris Lattner855e51f2007-12-12 07:09:47 +000045 if (MDecl->isInstance()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +000046 selfTy = Context.getObjCIdType();
Gabor Greif9b2e0f92008-02-29 20:35:55 +000047 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
48 // There may be no interface context due to error in declaration of the
49 // interface (which has been reported). Recover gracefully
Chris Lattner3e254fb2008-04-08 04:40:51 +000050 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000051 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000052 }
Steve Naroff1a5027c2008-06-05 14:49:39 +000053 } else // we have a factory method.
54 selfTy = Context.getObjCClassType();
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000055 getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000056 PI.Ident, PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000057
58 PI.Ident = &Context.Idents.get("_cmd");
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000059 getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000060 PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
Chris Lattner3e254fb2008-04-08 04:40:51 +000061
Chris Lattner97316c02008-04-10 02:22:51 +000062 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000063 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000064 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000065 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000066 if (II)
67 PushOnScopeChains(PDecl, 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.
Steve Naroff6384a012008-04-02 14:35:35 +000080 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
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);
Chris Lattner855e51f2007-12-12 07:09:47 +000095 }
Chris Lattner5cece462008-07-21 07:06:49 +000096 } else {
97 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000098 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +000099
Steve Naroff15208162008-04-02 18:30:49 +0000100 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000101 // Remember that this needs to be removed when the scope is popped.
102 TUScope->AddDecl(IDecl);
103 }
104
105 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000106 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000107 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000108 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 Diag(SuperLoc, diag::err_redefinition_different_kind,
111 SuperName->getName());
112 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
113 }
114 else {
115 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000116 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000117
118 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000119 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000120 SuperClassEntry ? SuperClassEntry->getName()
121 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000122 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000123 }
124 }
125 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000126 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000127 IDecl->setLocEnd(SuperLoc);
128 } else { // we have a root class.
129 IDecl->setLocEnd(ClassLoc);
130 }
131
132 /// Check then save referenced protocols
133 if (NumProtocols) {
Chris Lattner5cece462008-07-21 07:06:49 +0000134 llvm::SmallVector<ObjCProtocolDecl*, 8> RefProtos;
Chris Lattner855e51f2007-12-12 07:09:47 +0000135 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000136 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner8779c632008-07-21 18:34:02 +0000137 if (!RefPDecl)
138 Diag(EndProtoLoc, diag::err_undef_protocolref,
139 ProtocolNames[i]->getName(), ClassName->getName());
140 else if (RefPDecl->isForwardDecl())
Chris Lattner5cece462008-07-21 07:06:49 +0000141 Diag(EndProtoLoc, diag::warn_undef_protocolref,
Chris Lattner8779c632008-07-21 18:34:02 +0000142 ProtocolNames[i]->getName(), ClassName->getName());
Chris Lattner5cece462008-07-21 07:06:49 +0000143 else
144 RefProtos.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000145 }
Chris Lattner5cece462008-07-21 07:06:49 +0000146 if (!RefProtos.empty())
147 IDecl->addReferencedProtocols(&RefProtos[0], RefProtos.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000148 IDecl->setLocEnd(EndProtoLoc);
149 }
150 return IDecl;
151}
152
153/// ActOnCompatiblityAlias - this action is called after complete parsing of
154/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000155Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
156 IdentifierInfo *AliasName,
157 SourceLocation AliasLocation,
158 IdentifierInfo *ClassName,
159 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000161 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000162 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000163 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000164 Diag(AliasLocation, diag::warn_previous_alias_decl);
165 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
166 }
167 else {
168 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
169 AliasName->getName());
170 Diag(ADecl->getLocation(), diag::err_previous_declaration);
171 }
172 return 0;
173 }
174 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000175 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000176 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
177 if (CDecl == 0) {
178 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
179 if (CDeclU)
180 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000181 return 0;
182 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000183
184 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000186 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
187
188 ObjCAliasDecls[AliasName] = AliasDecl;
189 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000190 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
Chris Lattnerc1881852008-03-16 01:25:17 +0000202 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000203 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
204 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000205 // Just return the protocol we already had.
206 // FIXME: don't leak the objects passed in!
207 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000208 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000209
210 PDecl->setForwardDecl(false);
211 PDecl->AllocReferencedProtocols(NumProtoRefs);
212 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000213 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000214 ProtocolName);
215 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000217 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000218
219 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000220 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000221 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000222 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner8779c632008-07-21 18:34:02 +0000223 if (!RefPDecl)
224 Diag(ProtocolLoc, diag::err_undef_protocolref,
225 ProtoRefNames[i]->getName(), ProtocolName->getName());
226 else if (RefPDecl->isForwardDecl())
Chris Lattner855e51f2007-12-12 07:09:47 +0000227 Diag(ProtocolLoc, diag::warn_undef_protocolref,
Chris Lattner8779c632008-07-21 18:34:02 +0000228 ProtoRefNames[i]->getName(), ProtocolName->getName());
229
Fariborz Jahanian87829072007-12-20 19:24:10 +0000230 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000231 }
232 PDecl->setLocEnd(EndProtoLoc);
233 }
234 return PDecl;
235}
236
237/// FindProtocolDeclaration - This routine looks up protocols and
238/// issuer error if they are not declared. It returns list of protocol
239/// declarations in its 'Protocols' argument.
240void
241Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
242 IdentifierInfo **ProtocolId,
243 unsigned NumProtocols,
244 llvm::SmallVector<DeclTy *,8> &Protocols) {
245 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000246 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000247 if (!PDecl)
248 Diag(TypeLoc, diag::err_undeclared_protocol,
249 ProtocolId[i]->getName());
250 else
251 Protocols.push_back(PDecl);
252 }
253}
254
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000255/// DiagnosePropertyMismatch - Compares two properties for their
256/// attributes and types and warns on a variety of inconsistancies.
257///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000258void
259Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
260 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000261 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000262 ObjCPropertyDecl::PropertyAttributeKind CAttr =
263 Property->getPropertyAttributes();
264 ObjCPropertyDecl::PropertyAttributeKind SAttr =
265 SuperProperty->getPropertyAttributes();
266 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
267 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000268 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000269 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000270 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000272 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000273 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000274 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000275 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
276 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000277 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000278 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000279 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000280
281 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
282 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000283 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000284 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000285 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000286 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000287 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000288 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000289 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000290 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000291 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000292 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000293 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000294
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000295 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
296 Diag(Property->getLocation(), diag::warn_property_type,
297 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000298 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000299
300}
301
302/// ComparePropertiesInBaseAndSuper - This routine compares property
303/// declarations in base and its super class, if any, and issues
304/// diagnostics in a variety of inconsistant situations.
305///
306void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000307Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000308 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
309 if (!SDecl)
310 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000311 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
312 E = SDecl->classprop_end(); S != E; ++S) {
313 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000314 // Does property in super class has declaration in current class?
315 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
316 E = IDecl->classprop_end(); I != E; ++I) {
317 ObjCPropertyDecl *PDecl = (*I);
318 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000319 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000320 }
321 }
322}
323
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000324/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
325/// of properties declared in a protocol and adds them to the list
326/// of properties for current class if it is not there already.
327void
328Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
329 ObjCProtocolDecl *PDecl)
330{
331 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
332 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
333 E = PDecl->classprop_end(); P != E; ++P) {
334 ObjCPropertyDecl *Pr = (*P);
335 ObjCInterfaceDecl::classprop_iterator CP, CE;
336 // Is this property already in class's list of properties?
337 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
338 CP != CE; ++CP)
339 if ((*CP)->getIdentifier() == Pr->getIdentifier())
340 break;
341 if (CP == CE)
342 // Add this property to list of properties for thie class.
343 mergeProperties.push_back(Pr);
344 else
345 // Property protocol already exist in class. Diagnose any mismatch.
346 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
347 }
348 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
349}
350
351/// MergeProtocolPropertiesIntoClass - This routine merges properties
352/// declared in 'MergeItsProtocols' objects (which can be a class or an
353/// inherited protocol into the list of properties for class 'IDecl'
354///
355
356void
357Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
358 DeclTy *MergeItsProtocols) {
359 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000360 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000361 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
362 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000363 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000364 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
365
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000366 // Go thru the list of protocols for this class and recursively merge
367 // their properties into this class as well.
368 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
369 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000370 MergeProtocolPropertiesIntoClass(IDecl, *P);
371 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000372 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
373 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
374 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000375 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000376 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000377}
378
Chris Lattner855e51f2007-12-12 07:09:47 +0000379/// ActOnForwardProtocolDeclaration -
380Action::DeclTy *
381Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
382 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000383 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000384
385 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000386 IdentifierInfo *Ident = IdentList[i];
387 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
388 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000389 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000390 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000391 }
392
393 Protocols.push_back(PDecl);
394 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000395 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
396 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000397}
398
399Sema::DeclTy *Sema::ActOnStartCategoryInterface(
400 SourceLocation AtInterfaceLoc,
401 IdentifierInfo *ClassName, SourceLocation ClassLoc,
402 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
403 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
404 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000405 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000406
Chris Lattnere29dc832008-03-16 20:34:23 +0000407 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000408 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000409 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000410
411 /// Check that class of this category is already completely declared.
412 if (!IDecl || IDecl->isForwardDecl())
413 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000414 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000415 /// Check for duplicate interface declaration for this category
416 ObjCCategoryDecl *CDeclChain;
417 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
418 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000419 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000420 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000421 CategoryName->getName());
422 break;
423 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000424 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000425 if (!CDeclChain)
426 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000427 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000428
429 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000430 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
431 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000432 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000433 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner8779c632008-07-21 18:34:02 +0000434 if (!RefPDecl)
435 Diag(CategoryLoc, diag::err_undef_protocolref,
436 ProtoRefNames[i]->getName(), CategoryName->getName());
437 else if (RefPDecl->isForwardDecl())
Chris Lattner855e51f2007-12-12 07:09:47 +0000438 Diag(CategoryLoc, diag::warn_undef_protocolref,
Chris Lattner8779c632008-07-21 18:34:02 +0000439 ProtoRefNames[i]->getName(), CategoryName->getName());
Chris Lattner321b5d12008-03-16 20:47:45 +0000440 if (RefPDecl)
441 RefProtocols.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000442 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000443 if (!RefProtocols.empty())
Chris Lattnerc72eaaf2008-07-21 17:23:15 +0000444 CDecl->addReferencedProtocols(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000445 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000446 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000447 return CDecl;
448}
449
450/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000451/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000452/// object.
453Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
454 SourceLocation AtCatImplLoc,
455 IdentifierInfo *ClassName, SourceLocation ClassLoc,
456 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000457 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000458 ObjCCategoryImplDecl *CDecl =
459 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000460 /// Check that class of this category is already completely declared.
461 if (!IDecl || IDecl->isForwardDecl())
462 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
463
464 /// TODO: Check that CatName, category name, is not used in another
465 // implementation.
466 return CDecl;
467}
468
469Sema::DeclTy *Sema::ActOnStartClassImplementation(
470 SourceLocation AtClassImplLoc,
471 IdentifierInfo *ClassName, SourceLocation ClassLoc,
472 IdentifierInfo *SuperClassname,
473 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000474 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000475 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000476 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000477 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000478 Diag(ClassLoc, diag::err_redefinition_different_kind,
479 ClassName->getName());
480 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
481 }
482 else {
483 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000484 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000485 if (!IDecl)
486 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
487 }
488
489 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000490 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000491 if (SuperClassname) {
492 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000493 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000494 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000495 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
496 SuperClassname->getName());
497 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
498 }
499 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000500 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000501 if (!SDecl)
502 Diag(SuperClassLoc, diag::err_undef_superclass,
503 SuperClassname->getName(), ClassName->getName());
504 else if (IDecl && IDecl->getSuperClass() != SDecl) {
505 // This implementation and its interface do not have the same
506 // super class.
507 Diag(SuperClassLoc, diag::err_conflicting_super_class,
508 SDecl->getName());
509 Diag(SDecl->getLocation(), diag::err_previous_definition);
510 }
511 }
512 }
513
514 if (!IDecl) {
515 // Legacy case of @implementation with no corresponding @interface.
516 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000517 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000518 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000519 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000520 IDecl->setSuperClass(SDecl);
521 IDecl->setLocEnd(ClassLoc);
522
523 // Remember that this needs to be removed when the scope is popped.
524 TUScope->AddDecl(IDecl);
525 }
526
Ted Kremenek42730c52008-01-07 19:49:32 +0000527 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000528 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
529 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000530
531 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000532 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000533 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000534 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
535 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000536 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000537 return IMPDecl;
538}
539
Ted Kremenek42730c52008-01-07 19:49:32 +0000540void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
541 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000542 SourceLocation RBrace) {
543 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000544 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000545 if (!IDecl)
546 return;
547 /// Check case of non-existing @interface decl.
548 /// (legacy objective-c @implementation decl without an @interface decl).
549 /// Add implementations's ivar to the synthesize class's ivar list.
550 if (IDecl->ImplicitInterfaceDecl()) {
551 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
552 return;
553 }
554 // If implementation has empty ivar list, just return.
555 if (numIvars == 0)
556 return;
557
558 assert(ivars && "missing @implementation ivars");
559
560 // Check interface's Ivar list against those in the implementation.
561 // names and types must match.
562 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000563 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000564 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000565 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
566 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000567 ObjCIvarDecl* ImplIvar = ivars[j++];
568 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000569 assert (ImplIvar && "missing implementation ivar");
570 assert (ClsIvar && "missing class ivar");
571 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
572 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
573 ImplIvar->getIdentifier()->getName());
574 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
575 ClsIvar->getIdentifier()->getName());
576 }
577 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
578 // as error.
579 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
580 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
581 ImplIvar->getIdentifier()->getName());
582 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
583 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000584 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 }
586 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000588
589 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000590 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000591 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000592 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000593}
594
Steve Naroffb4f48512008-02-10 21:38:56 +0000595void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
596 bool &IncompleteImpl) {
597 if (!IncompleteImpl) {
598 Diag(ImpLoc, diag::warn_incomplete_impl);
599 IncompleteImpl = true;
600 }
601 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
602}
603
Steve Naroffb268d2a2008-02-08 22:06:17 +0000604/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000605/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000606void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
607 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000608 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000609 const llvm::DenseSet<Selector> &InsMap,
610 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000611 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000612 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000613 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000614 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000615 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000616 method->getImplementationControl() != ObjCMethodDecl::Optional)
617 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000618 }
619 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000620 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000621 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000623 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000624 method->getImplementationControl() != ObjCMethodDecl::Optional)
625 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000626 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000627 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000629 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000630 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000631}
632
Ted Kremenek42730c52008-01-07 19:49:32 +0000633void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
634 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000635 llvm::DenseSet<Selector> InsMap;
636 // Check and see if instance methods in class interface have been
637 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000638 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000639 E = IMPDecl->instmeth_end(); I != E; ++I)
640 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000641
642 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000643 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000644 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000645 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000646 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000647
Chris Lattner855e51f2007-12-12 07:09:47 +0000648 llvm::DenseSet<Selector> ClsMap;
649 // Check and see if class methods in class interface have been
650 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000651 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000652 E = IMPDecl->classmeth_end(); I != E; ++I)
653 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000654
Ted Kremenek42730c52008-01-07 19:49:32 +0000655 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000656 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000657 if (!ClsMap.count((*I)->getSelector()))
658 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000659
660 // Check the protocol list for unimplemented methods in the @implementation
661 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000662 const ObjCList<ObjCProtocolDecl> &Protocols =
663 IDecl->getReferencedProtocols();
664 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
665 E = Protocols.end(); I != E; ++I)
666 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000667 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000668}
669
670/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
671/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000672void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
673 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000674 llvm::DenseSet<Selector> InsMap;
675 // Check and see if instance methods in category interface have been
676 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000677 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000678 E = CatImplDecl->instmeth_end(); I != E; ++I)
679 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000680
681 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000682 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000683 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000684 if (!InsMap.count((*I)->getSelector()))
685 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
686
Chris Lattner855e51f2007-12-12 07:09:47 +0000687 llvm::DenseSet<Selector> ClsMap;
688 // Check and see if class methods in category interface have been
689 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000690 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000691 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
692 I != E; ++I)
693 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000694
Ted Kremenek42730c52008-01-07 19:49:32 +0000695 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000696 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000697 if (!ClsMap.count((*I)->getSelector()))
698 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000699
700 // Check the protocol list for unimplemented methods in the @implementation
701 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000702 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000703 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000704 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000705 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
706 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000707 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000708}
709
710/// ActOnForwardClassDeclaration -
711Action::DeclTy *
712Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
713 IdentifierInfo **IdentList, unsigned NumElts)
714{
Ted Kremenek42730c52008-01-07 19:49:32 +0000715 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000716
717 for (unsigned i = 0; i != NumElts; ++i) {
718 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000719 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000720 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000721 // GCC apparently allows the following idiom:
722 //
723 // typedef NSObject < XCElementTogglerP > XCElementToggler;
724 // @class XCElementToggler;
725 //
726 // FIXME: Make an extension?
727 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
728 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
729 Diag(AtClassLoc, diag::err_redefinition_different_kind,
730 IdentList[i]->getName());
731 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
732 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000733 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000734 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000735 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000736 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000737 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000738 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000739
740 // Remember that this needs to be removed when the scope is popped.
741 TUScope->AddDecl(IDecl);
742 }
743
744 Interfaces.push_back(IDecl);
745 }
746
Chris Lattnere29dc832008-03-16 20:34:23 +0000747 return ObjCClassDecl::Create(Context, AtClassLoc,
748 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000749}
750
751
752/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
753/// returns true, or false, accordingly.
754/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000755bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
756 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000757 if (Method->getResultType().getCanonicalType() !=
758 PrevMethod->getResultType().getCanonicalType())
759 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000760 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
762 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000763 if (Context.getCanonicalType(ParamDecl->getType()) !=
764 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000765 return false;
766 }
767 return true;
768}
769
Ted Kremenek42730c52008-01-07 19:49:32 +0000770void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
771 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000772 if (!FirstMethod.Method) {
773 // Haven't seen a method with this selector name yet - add it.
774 FirstMethod.Method = Method;
775 FirstMethod.Next = 0;
776 } else {
777 // We've seen a method with this name, now check the type signature(s).
778 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
779
Ted Kremenek42730c52008-01-07 19:49:32 +0000780 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000781 Next = Next->Next)
782 match = MatchTwoMethodDeclarations(Method, Next->Method);
783
784 if (!match) {
785 // We have a new signature for an existing method - add it.
786 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000787 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000788 FirstMethod.Next = OMI;
789 }
790 }
791}
792
Ted Kremenek42730c52008-01-07 19:49:32 +0000793void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
794 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000795 if (!FirstMethod.Method) {
796 // Haven't seen a method with this selector name yet - add it.
797 FirstMethod.Method = Method;
798 FirstMethod.Next = 0;
799 } else {
800 // We've seen a method with this name, now check the type signature(s).
801 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
802
Ted Kremenek42730c52008-01-07 19:49:32 +0000803 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000804 Next = Next->Next)
805 match = MatchTwoMethodDeclarations(Method, Next->Method);
806
807 if (!match) {
808 // We have a new signature for an existing method - add it.
809 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000810 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000811 FirstMethod.Next = OMI;
812 }
813 }
814}
815
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000816// Note: For class/category implemenations, allMethods/allProperties is
817// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000818void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
819 DeclTy **allMethods, unsigned allNum,
820 DeclTy **allProperties, unsigned pNum) {
821 Decl *ClassDecl = static_cast<Decl *>(classDecl);
822
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000823 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
824 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000825 // should be true.
826 if (!ClassDecl)
827 return;
828
Ted Kremenek42730c52008-01-07 19:49:32 +0000829 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
830 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000831
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
833 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000834
835 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000836 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
837 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000838 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000839
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000840 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000841 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
842 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000843 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
844 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
845 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
846 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000847 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000848 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000849 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000850
851 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000852 ObjCMethodDecl *Method =
853 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000854
855 if (!Method) continue; // Already issued a diagnostic.
856 if (Method->isInstance()) {
857 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000858 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000859 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
860 : false;
861 if (isInterfaceDeclKind && PrevMethod && !match
862 || checkIdenticalMethods && match) {
863 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
864 Method->getSelector().getName());
865 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
866 } else {
867 insMethods.push_back(Method);
868 InsMap[Method->getSelector()] = Method;
869 /// The following allows us to typecheck messages to "id".
870 AddInstanceMethodToGlobalPool(Method);
871 }
872 }
873 else {
874 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000875 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000876 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
877 : false;
878 if (isInterfaceDeclKind && PrevMethod && !match
879 || checkIdenticalMethods && match) {
880 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
881 Method->getSelector().getName());
882 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
883 } else {
884 clsMethods.push_back(Method);
885 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000886 /// The following allows us to typecheck messages to "Class".
887 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000888 }
889 }
890 }
891
Ted Kremenek42730c52008-01-07 19:49:32 +0000892 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000893 // Compares properties declaraed in this class to those of its
894 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000895 ComparePropertiesInBaseAndSuper(I);
896 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000897 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
898 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000899 // FIXME: It would be really nice if we could avoid this. Injecting
900 // methods into the interface makes it hard to distinguish "real" methods
901 // from synthesized "property" methods (that aren't in the source).
902 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000903 I->addPropertyMethods(Context, *P, insMethods);
904 }
905 I->addMethods(&insMethods[0], insMethods.size(),
906 &clsMethods[0], clsMethods.size(), AtEndLoc);
907
Ted Kremenek42730c52008-01-07 19:49:32 +0000908 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000909 P->addMethods(&insMethods[0], insMethods.size(),
910 &clsMethods[0], clsMethods.size(), AtEndLoc);
911 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000912 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000913 C->addMethods(&insMethods[0], insMethods.size(),
914 &clsMethods[0], clsMethods.size(), AtEndLoc);
915 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000916 else if (ObjCImplementationDecl *IC =
917 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000918 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000919 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000920 ImplMethodsVsClassMethods(IC, IDecl);
921 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000923 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000924 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000925 // Find category interface decl and then check that all methods declared
926 // in this interface is implemented in the category @implementation.
927 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000928 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000929 Categories; Categories = Categories->getNextClassCategory()) {
930 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
931 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
932 break;
933 }
934 }
935 }
936 }
937}
938
939
940/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
941/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000942static Decl::ObjCDeclQualifier
943CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
944 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
945 if (PQTVal & ObjCDeclSpec::DQ_In)
946 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
947 if (PQTVal & ObjCDeclSpec::DQ_Inout)
948 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
949 if (PQTVal & ObjCDeclSpec::DQ_Out)
950 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
951 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
952 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
953 if (PQTVal & ObjCDeclSpec::DQ_Byref)
954 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
955 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
956 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000957
958 return ret;
959}
960
961Sema::DeclTy *Sema::ActOnMethodDeclaration(
962 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000963 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000964 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000965 Selector Sel,
966 // optional arguments. The number of types/arguments is obtained
967 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000968 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000969 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
970 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000971 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000972
973 // Make sure we can establish a context for the method.
974 if (!ClassDecl) {
975 Diag(MethodLoc, diag::error_missing_method_context);
976 return 0;
977 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000978 QualType resultDeclType;
979
980 if (ReturnType)
981 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
982 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000983 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000984
Chris Lattner114add62008-03-16 00:49:28 +0000985 ObjCMethodDecl* ObjCMethod =
986 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000987 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000988 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000989 false,
Chris Lattner114add62008-03-16 00:49:28 +0000990 MethodDeclKind == tok::objc_optional ?
991 ObjCMethodDecl::Optional :
992 ObjCMethodDecl::Required);
993
Chris Lattnereee57c02008-04-04 06:12:32 +0000994 llvm::SmallVector<ParmVarDecl*, 16> Params;
995
996 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
997 // FIXME: arg->AttrList must be stored too!
998 QualType argType;
999
1000 if (ArgTypes[i])
1001 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1002 else
1003 argType = Context.getObjCIdType();
1004 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1005 SourceLocation(/*FIXME*/),
1006 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001007 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001008 Param->setObjCDeclQualifier(
1009 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1010 Params.push_back(Param);
1011 }
1012
Ted Kremenek42730c52008-01-07 19:49:32 +00001013 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1014 ObjCMethod->setObjCDeclQualifier(
1015 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1016 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001017
1018 // For implementations (which can be very "coarse grain"), we add the
1019 // method now. This allows the AST to implement lookup methods that work
1020 // incrementally (without waiting until we parse the @end). It also allows
1021 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001022 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001023 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001024 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001025 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001026 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001027 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001028 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001029 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001030 }
1031 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001032 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001033 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001034 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001035 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001036 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001037 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001038 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001039 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001040 }
1041 }
1042 if (PrevMethod) {
1043 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001044 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1045 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001046 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1047 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001048 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001049}
1050
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001051Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1052 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001053 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001054 Selector GetterSel,
1055 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001056 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001057 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001058 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1059 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001060 // Regardless of setter/getter attribute, we save the default getter/setter
1061 // selector names in anticipation of declaration of setter/getter methods.
1062 PDecl->setGetterName(GetterSel);
1063 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001064
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001065 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001066 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001067
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001068 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001069 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001070
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001071 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001072 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001073
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001074 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001075 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001076
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001077 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001079
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001080 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001082
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001083 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001084 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001085
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001086 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001087 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001088
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001089 if (MethodImplKind == tok::objc_required)
1090 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1091 else if (MethodImplKind == tok::objc_optional)
1092 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1093
Chris Lattner855e51f2007-12-12 07:09:47 +00001094 return PDecl;
1095}
1096
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001097/// ActOnPropertyImplDecl - This routine performs semantic checks and
1098/// builds the AST node for a property implementation declaration; declared
1099/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001100///
1101Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1102 SourceLocation PropertyLoc,
1103 bool Synthesize,
1104 DeclTy *ClassCatImpDecl,
1105 IdentifierInfo *PropertyId,
1106 IdentifierInfo *PropertyIvar) {
1107 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1108 // Make sure we have a context for the property implementation declaration.
1109 if (!ClassImpDecl) {
1110 Diag(AtLoc, diag::error_missing_property_context);
1111 return 0;
1112 }
1113 ObjCPropertyDecl *property = 0;
1114 ObjCInterfaceDecl* IDecl = 0;
1115 // Find the class or category class where this property must have
1116 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001117 ObjCImplementationDecl *IC = 0;
1118 ObjCCategoryImplDecl* CatImplClass = 0;
1119 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001120 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001121 // We always synthesize an interface for an implementation
1122 // without an interface decl. So, IDecl is always non-zero.
1123 assert(IDecl &&
1124 "ActOnPropertyImplDecl - @implementation without @interface");
1125
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001126 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001127 property = IDecl->FindPropertyDeclaration(PropertyId);
1128 if (!property) {
1129 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001130 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001131 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001133 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001134 if (Synthesize) {
1135 Diag(AtLoc, diag::error_synthesize_category_decl);
1136 return 0;
1137 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001138 IDecl = CatImplClass->getClassInterface();
1139 if (!IDecl) {
1140 Diag(AtLoc, diag::error_missing_property_interface);
1141 return 0;
1142 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001143 ObjCCategoryDecl *Category =
1144 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1145
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001146 // If category for this implementation not found, it is an error which
1147 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001148 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001149 return 0;
1150 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001151 property = Category->FindPropertyDeclaration(PropertyId);
1152 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001153 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001154 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001155 return 0;
1156 }
1157 }
1158 else {
1159 Diag(AtLoc, diag::error_bad_property_context);
1160 return 0;
1161 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001162 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001163 // Check that we have a valid, previously declared ivar for @synthesize
1164 if (Synthesize) {
1165 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001166 if (!PropertyIvar)
1167 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001168 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001169 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001170 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001171 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1172 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001173 return 0;
1174 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001175 // Check that type of property and its ivar match.
1176 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1177 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1178 Ivar->getName());
1179 return 0;
1180 }
1181
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001182 } else if (PropertyIvar) {
1183 // @dynamic
1184 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1185 return 0;
1186 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001187 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001188 ObjCPropertyImplDecl *PIDecl =
1189 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1190 (Synthesize ?
1191 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1192 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1193 Ivar);
1194 if (IC)
1195 IC->addPropertyImplementation(PIDecl);
1196 else
1197 CatImplClass->addPropertyImplementation(PIDecl);
1198
1199 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001200}