blob: c3c2426bdc27a65789c9bda2b8ca30b9df5230a5 [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());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000140 else {
141 if (RefPDecl->isForwardDecl())
142 Diag(EndProtoLoc, diag::warn_undef_protocolref,
143 ProtocolNames[i]->getName(), ClassName->getName());
Chris Lattner5cece462008-07-21 07:06:49 +0000144 RefProtos.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000145 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000146 }
Chris Lattner5cece462008-07-21 07:06:49 +0000147 if (!RefProtos.empty())
148 IDecl->addReferencedProtocols(&RefProtos[0], RefProtos.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000149 IDecl->setLocEnd(EndProtoLoc);
150 }
151 return IDecl;
152}
153
154/// ActOnCompatiblityAlias - this action is called after complete parsing of
155/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000156Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
157 IdentifierInfo *AliasName,
158 SourceLocation AliasLocation,
159 IdentifierInfo *ClassName,
160 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000161 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000162 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000163 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000164 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000165 Diag(AliasLocation, diag::warn_previous_alias_decl);
166 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
167 }
168 else {
169 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
170 AliasName->getName());
171 Diag(ADecl->getLocation(), diag::err_previous_declaration);
172 }
173 return 0;
174 }
175 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000176 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000177 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
178 if (CDecl == 0) {
179 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
180 if (CDeclU)
181 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000182 return 0;
183 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000184
185 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000186 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000187 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
188
189 ObjCAliasDecls[AliasName] = AliasDecl;
190 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000191 return AliasDecl;
192}
193
194Sema::DeclTy *Sema::ActOnStartProtocolInterface(
195 SourceLocation AtProtoInterfaceLoc,
196 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
197 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
198 SourceLocation EndProtoLoc) {
199 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000200 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000201 if (PDecl) {
202 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000203 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000204 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
205 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000206 // Just return the protocol we already had.
207 // FIXME: don't leak the objects passed in!
208 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000209 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000210
211 PDecl->setForwardDecl(false);
212 PDecl->AllocReferencedProtocols(NumProtoRefs);
213 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000214 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000215 ProtocolName);
216 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000217 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000218 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000219
220 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000221 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000222 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000223 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner8779c632008-07-21 18:34:02 +0000224 if (!RefPDecl)
225 Diag(ProtocolLoc, diag::err_undef_protocolref,
226 ProtoRefNames[i]->getName(), ProtocolName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000227 else {
228 if (RefPDecl->isForwardDecl())
229 Diag(ProtocolLoc, diag::warn_undef_protocolref,
230 ProtoRefNames[i]->getName(), ProtocolName->getName());
231 PDecl->setReferencedProtocols(i, RefPDecl);
232 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000233 }
234 PDecl->setLocEnd(EndProtoLoc);
235 }
236 return PDecl;
237}
238
239/// FindProtocolDeclaration - This routine looks up protocols and
240/// issuer error if they are not declared. It returns list of protocol
241/// declarations in its 'Protocols' argument.
242void
243Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
244 IdentifierInfo **ProtocolId,
245 unsigned NumProtocols,
246 llvm::SmallVector<DeclTy *,8> &Protocols) {
247 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000248 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000249 if (!PDecl)
250 Diag(TypeLoc, diag::err_undeclared_protocol,
251 ProtocolId[i]->getName());
252 else
253 Protocols.push_back(PDecl);
254 }
255}
256
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000257/// DiagnosePropertyMismatch - Compares two properties for their
258/// attributes and types and warns on a variety of inconsistancies.
259///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000260void
261Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
262 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000263 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000264 ObjCPropertyDecl::PropertyAttributeKind CAttr =
265 Property->getPropertyAttributes();
266 ObjCPropertyDecl::PropertyAttributeKind SAttr =
267 SuperProperty->getPropertyAttributes();
268 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
269 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000270 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000271 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000272 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
273 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000274 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000275 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000276 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000277 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
278 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000279 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000280 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000281 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000282
283 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
284 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000285 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000286 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000287 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000288 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000289 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000290 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000291 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000292 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000293 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000294 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000295 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000296
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000297 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
298 Diag(Property->getLocation(), diag::warn_property_type,
299 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000300 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000301
302}
303
304/// ComparePropertiesInBaseAndSuper - This routine compares property
305/// declarations in base and its super class, if any, and issues
306/// diagnostics in a variety of inconsistant situations.
307///
308void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000309Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000310 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
311 if (!SDecl)
312 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000313 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
314 E = SDecl->classprop_end(); S != E; ++S) {
315 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000316 // Does property in super class has declaration in current class?
317 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
318 E = IDecl->classprop_end(); I != E; ++I) {
319 ObjCPropertyDecl *PDecl = (*I);
320 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000321 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000322 }
323 }
324}
325
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000326/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
327/// of properties declared in a protocol and adds them to the list
328/// of properties for current class if it is not there already.
329void
330Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
331 ObjCProtocolDecl *PDecl)
332{
333 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
334 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
335 E = PDecl->classprop_end(); P != E; ++P) {
336 ObjCPropertyDecl *Pr = (*P);
337 ObjCInterfaceDecl::classprop_iterator CP, CE;
338 // Is this property already in class's list of properties?
339 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
340 CP != CE; ++CP)
341 if ((*CP)->getIdentifier() == Pr->getIdentifier())
342 break;
343 if (CP == CE)
344 // Add this property to list of properties for thie class.
345 mergeProperties.push_back(Pr);
346 else
347 // Property protocol already exist in class. Diagnose any mismatch.
348 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
349 }
350 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
351}
352
353/// MergeProtocolPropertiesIntoClass - This routine merges properties
354/// declared in 'MergeItsProtocols' objects (which can be a class or an
355/// inherited protocol into the list of properties for class 'IDecl'
356///
357
358void
359Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
360 DeclTy *MergeItsProtocols) {
361 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000362 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000363 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
364 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000365 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000366 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
367
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000368 // Go thru the list of protocols for this class and recursively merge
369 // their properties into this class as well.
370 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
371 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000372 MergeProtocolPropertiesIntoClass(IDecl, *P);
373 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000374 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
375 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
376 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000377 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000378 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000379}
380
Chris Lattner855e51f2007-12-12 07:09:47 +0000381/// ActOnForwardProtocolDeclaration -
382Action::DeclTy *
383Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
384 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000385 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000386
387 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000388 IdentifierInfo *Ident = IdentList[i];
389 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
390 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000391 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000392 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000393 }
394
395 Protocols.push_back(PDecl);
396 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000397 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
398 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000399}
400
401Sema::DeclTy *Sema::ActOnStartCategoryInterface(
402 SourceLocation AtInterfaceLoc,
403 IdentifierInfo *ClassName, SourceLocation ClassLoc,
404 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
405 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
406 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000407 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000408
Chris Lattnere29dc832008-03-16 20:34:23 +0000409 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000410 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000411 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000412
413 /// Check that class of this category is already completely declared.
414 if (!IDecl || IDecl->isForwardDecl())
415 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000416 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000417 /// Check for duplicate interface declaration for this category
418 ObjCCategoryDecl *CDeclChain;
419 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
420 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000421 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000422 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000423 CategoryName->getName());
424 break;
425 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000426 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000427 if (!CDeclChain)
428 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000429 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000430
431 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000432 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
433 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000434 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000435 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner8779c632008-07-21 18:34:02 +0000436 if (!RefPDecl)
437 Diag(CategoryLoc, diag::err_undef_protocolref,
438 ProtoRefNames[i]->getName(), CategoryName->getName());
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000439 else {
440 if (RefPDecl->isForwardDecl())
441 Diag(CategoryLoc, diag::warn_undef_protocolref,
442 ProtoRefNames[i]->getName(), CategoryName->getName());
Chris Lattner321b5d12008-03-16 20:47:45 +0000443 RefProtocols.push_back(RefPDecl);
Chris Lattnere22cf7c2008-07-21 18:35:17 +0000444 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000445 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000446 if (!RefProtocols.empty())
Chris Lattnerc72eaaf2008-07-21 17:23:15 +0000447 CDecl->addReferencedProtocols(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000448 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000449 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000450 return CDecl;
451}
452
453/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000454/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000455/// object.
456Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
457 SourceLocation AtCatImplLoc,
458 IdentifierInfo *ClassName, SourceLocation ClassLoc,
459 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000460 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000461 ObjCCategoryImplDecl *CDecl =
462 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000463 /// Check that class of this category is already completely declared.
464 if (!IDecl || IDecl->isForwardDecl())
465 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
466
467 /// TODO: Check that CatName, category name, is not used in another
468 // implementation.
469 return CDecl;
470}
471
472Sema::DeclTy *Sema::ActOnStartClassImplementation(
473 SourceLocation AtClassImplLoc,
474 IdentifierInfo *ClassName, SourceLocation ClassLoc,
475 IdentifierInfo *SuperClassname,
476 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000477 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000478 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000479 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000480 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000481 Diag(ClassLoc, diag::err_redefinition_different_kind,
482 ClassName->getName());
483 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
484 }
485 else {
486 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000487 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000488 if (!IDecl)
489 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
490 }
491
492 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000493 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000494 if (SuperClassname) {
495 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000496 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000497 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000498 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
499 SuperClassname->getName());
500 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
501 }
502 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000503 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000504 if (!SDecl)
505 Diag(SuperClassLoc, diag::err_undef_superclass,
506 SuperClassname->getName(), ClassName->getName());
507 else if (IDecl && IDecl->getSuperClass() != SDecl) {
508 // This implementation and its interface do not have the same
509 // super class.
510 Diag(SuperClassLoc, diag::err_conflicting_super_class,
511 SDecl->getName());
512 Diag(SDecl->getLocation(), diag::err_previous_definition);
513 }
514 }
515 }
516
517 if (!IDecl) {
518 // Legacy case of @implementation with no corresponding @interface.
519 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000520 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000521 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000522 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000523 IDecl->setSuperClass(SDecl);
524 IDecl->setLocEnd(ClassLoc);
525
526 // Remember that this needs to be removed when the scope is popped.
527 TUScope->AddDecl(IDecl);
528 }
529
Ted Kremenek42730c52008-01-07 19:49:32 +0000530 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000531 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
532 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000533
534 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000535 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000536 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000537 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
538 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000539 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000540 return IMPDecl;
541}
542
Ted Kremenek42730c52008-01-07 19:49:32 +0000543void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
544 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000545 SourceLocation RBrace) {
546 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000547 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000548 if (!IDecl)
549 return;
550 /// Check case of non-existing @interface decl.
551 /// (legacy objective-c @implementation decl without an @interface decl).
552 /// Add implementations's ivar to the synthesize class's ivar list.
553 if (IDecl->ImplicitInterfaceDecl()) {
554 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
555 return;
556 }
557 // If implementation has empty ivar list, just return.
558 if (numIvars == 0)
559 return;
560
561 assert(ivars && "missing @implementation ivars");
562
563 // Check interface's Ivar list against those in the implementation.
564 // names and types must match.
565 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000566 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000567 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000568 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
569 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000570 ObjCIvarDecl* ImplIvar = ivars[j++];
571 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000572 assert (ImplIvar && "missing implementation ivar");
573 assert (ClsIvar && "missing class ivar");
574 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
575 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
576 ImplIvar->getIdentifier()->getName());
577 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
578 ClsIvar->getIdentifier()->getName());
579 }
580 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
581 // as error.
582 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
583 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
584 ImplIvar->getIdentifier()->getName());
585 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
586 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000587 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000588 }
589 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000590 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000591
592 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000593 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000594 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000595 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000596}
597
Steve Naroffb4f48512008-02-10 21:38:56 +0000598void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
599 bool &IncompleteImpl) {
600 if (!IncompleteImpl) {
601 Diag(ImpLoc, diag::warn_incomplete_impl);
602 IncompleteImpl = true;
603 }
604 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
605}
606
Steve Naroffb268d2a2008-02-08 22:06:17 +0000607/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000608/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000609void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
610 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000611 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000612 const llvm::DenseSet<Selector> &InsMap,
613 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000614 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000615 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000616 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000618 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000619 method->getImplementationControl() != ObjCMethodDecl::Optional)
620 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000621 }
622 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000623 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000624 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000625 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000626 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000627 method->getImplementationControl() != ObjCMethodDecl::Optional)
628 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000629 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000630 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000631 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000632 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000633 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000634}
635
Ted Kremenek42730c52008-01-07 19:49:32 +0000636void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
637 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000638 llvm::DenseSet<Selector> InsMap;
639 // Check and see if instance methods in class interface have been
640 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000641 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000642 E = IMPDecl->instmeth_end(); I != E; ++I)
643 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000644
645 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000646 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000647 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000648 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000649 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000650
Chris Lattner855e51f2007-12-12 07:09:47 +0000651 llvm::DenseSet<Selector> ClsMap;
652 // Check and see if class methods in class interface have been
653 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000654 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000655 E = IMPDecl->classmeth_end(); I != E; ++I)
656 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000657
Ted Kremenek42730c52008-01-07 19:49:32 +0000658 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000659 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000660 if (!ClsMap.count((*I)->getSelector()))
661 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000662
663 // Check the protocol list for unimplemented methods in the @implementation
664 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000665 const ObjCList<ObjCProtocolDecl> &Protocols =
666 IDecl->getReferencedProtocols();
667 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
668 E = Protocols.end(); I != E; ++I)
669 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000670 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000671}
672
673/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
674/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000675void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
676 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000677 llvm::DenseSet<Selector> InsMap;
678 // Check and see if instance methods in category interface have been
679 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000680 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000681 E = CatImplDecl->instmeth_end(); I != E; ++I)
682 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000683
684 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000686 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000687 if (!InsMap.count((*I)->getSelector()))
688 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
689
Chris Lattner855e51f2007-12-12 07:09:47 +0000690 llvm::DenseSet<Selector> ClsMap;
691 // Check and see if class methods in category interface have been
692 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000693 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000694 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
695 I != E; ++I)
696 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000697
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000699 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000700 if (!ClsMap.count((*I)->getSelector()))
701 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000702
703 // Check the protocol list for unimplemented methods in the @implementation
704 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000705 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000706 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000707 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000708 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
709 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000710 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000711}
712
713/// ActOnForwardClassDeclaration -
714Action::DeclTy *
715Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
716 IdentifierInfo **IdentList, unsigned NumElts)
717{
Ted Kremenek42730c52008-01-07 19:49:32 +0000718 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000719
720 for (unsigned i = 0; i != NumElts; ++i) {
721 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000722 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000723 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000724 // GCC apparently allows the following idiom:
725 //
726 // typedef NSObject < XCElementTogglerP > XCElementToggler;
727 // @class XCElementToggler;
728 //
729 // FIXME: Make an extension?
730 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
731 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
732 Diag(AtClassLoc, diag::err_redefinition_different_kind,
733 IdentList[i]->getName());
734 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
735 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000736 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000737 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000738 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000739 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000740 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000741 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000742
743 // Remember that this needs to be removed when the scope is popped.
744 TUScope->AddDecl(IDecl);
745 }
746
747 Interfaces.push_back(IDecl);
748 }
749
Chris Lattnere29dc832008-03-16 20:34:23 +0000750 return ObjCClassDecl::Create(Context, AtClassLoc,
751 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000752}
753
754
755/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
756/// returns true, or false, accordingly.
757/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000758bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
759 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000760 if (Method->getResultType().getCanonicalType() !=
761 PrevMethod->getResultType().getCanonicalType())
762 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000763 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000764 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
765 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000766 if (Context.getCanonicalType(ParamDecl->getType()) !=
767 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000768 return false;
769 }
770 return true;
771}
772
Ted Kremenek42730c52008-01-07 19:49:32 +0000773void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
774 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000775 if (!FirstMethod.Method) {
776 // Haven't seen a method with this selector name yet - add it.
777 FirstMethod.Method = Method;
778 FirstMethod.Next = 0;
779 } else {
780 // We've seen a method with this name, now check the type signature(s).
781 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
782
Ted Kremenek42730c52008-01-07 19:49:32 +0000783 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000784 Next = Next->Next)
785 match = MatchTwoMethodDeclarations(Method, Next->Method);
786
787 if (!match) {
788 // We have a new signature for an existing method - add it.
789 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000790 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000791 FirstMethod.Next = OMI;
792 }
793 }
794}
795
Ted Kremenek42730c52008-01-07 19:49:32 +0000796void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
797 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000798 if (!FirstMethod.Method) {
799 // Haven't seen a method with this selector name yet - add it.
800 FirstMethod.Method = Method;
801 FirstMethod.Next = 0;
802 } else {
803 // We've seen a method with this name, now check the type signature(s).
804 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
805
Ted Kremenek42730c52008-01-07 19:49:32 +0000806 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000807 Next = Next->Next)
808 match = MatchTwoMethodDeclarations(Method, Next->Method);
809
810 if (!match) {
811 // We have a new signature for an existing method - add it.
812 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000813 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000814 FirstMethod.Next = OMI;
815 }
816 }
817}
818
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000819// Note: For class/category implemenations, allMethods/allProperties is
820// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000821void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
822 DeclTy **allMethods, unsigned allNum,
823 DeclTy **allProperties, unsigned pNum) {
824 Decl *ClassDecl = static_cast<Decl *>(classDecl);
825
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000826 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
827 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000828 // should be true.
829 if (!ClassDecl)
830 return;
831
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
833 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000834
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
836 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000837
838 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000839 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
840 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000841 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000842
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000843 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000844 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
845 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000846 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
847 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
848 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
849 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000850 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000851 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000852 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000853
854 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000855 ObjCMethodDecl *Method =
856 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000857
858 if (!Method) continue; // Already issued a diagnostic.
859 if (Method->isInstance()) {
860 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000861 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000862 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
863 : false;
864 if (isInterfaceDeclKind && PrevMethod && !match
865 || checkIdenticalMethods && match) {
866 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
867 Method->getSelector().getName());
868 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
869 } else {
870 insMethods.push_back(Method);
871 InsMap[Method->getSelector()] = Method;
872 /// The following allows us to typecheck messages to "id".
873 AddInstanceMethodToGlobalPool(Method);
874 }
875 }
876 else {
877 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000878 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000879 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
880 : false;
881 if (isInterfaceDeclKind && PrevMethod && !match
882 || checkIdenticalMethods && match) {
883 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
884 Method->getSelector().getName());
885 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
886 } else {
887 clsMethods.push_back(Method);
888 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000889 /// The following allows us to typecheck messages to "Class".
890 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000891 }
892 }
893 }
894
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000896 // Compares properties declaraed in this class to those of its
897 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000898 ComparePropertiesInBaseAndSuper(I);
899 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000900 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
901 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000902 // FIXME: It would be really nice if we could avoid this. Injecting
903 // methods into the interface makes it hard to distinguish "real" methods
904 // from synthesized "property" methods (that aren't in the source).
905 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000906 I->addPropertyMethods(Context, *P, insMethods);
907 }
908 I->addMethods(&insMethods[0], insMethods.size(),
909 &clsMethods[0], clsMethods.size(), AtEndLoc);
910
Ted Kremenek42730c52008-01-07 19:49:32 +0000911 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000912 P->addMethods(&insMethods[0], insMethods.size(),
913 &clsMethods[0], clsMethods.size(), AtEndLoc);
914 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000915 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000916 C->addMethods(&insMethods[0], insMethods.size(),
917 &clsMethods[0], clsMethods.size(), AtEndLoc);
918 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000919 else if (ObjCImplementationDecl *IC =
920 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000921 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000923 ImplMethodsVsClassMethods(IC, IDecl);
924 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000925 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000926 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000927 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000928 // Find category interface decl and then check that all methods declared
929 // in this interface is implemented in the category @implementation.
930 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000931 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000932 Categories; Categories = Categories->getNextClassCategory()) {
933 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
934 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
935 break;
936 }
937 }
938 }
939 }
940}
941
942
943/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
944/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000945static Decl::ObjCDeclQualifier
946CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
947 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
948 if (PQTVal & ObjCDeclSpec::DQ_In)
949 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
950 if (PQTVal & ObjCDeclSpec::DQ_Inout)
951 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
952 if (PQTVal & ObjCDeclSpec::DQ_Out)
953 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
954 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
955 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
956 if (PQTVal & ObjCDeclSpec::DQ_Byref)
957 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
958 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
959 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000960
961 return ret;
962}
963
964Sema::DeclTy *Sema::ActOnMethodDeclaration(
965 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000966 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000967 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000968 Selector Sel,
969 // optional arguments. The number of types/arguments is obtained
970 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000971 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000972 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
973 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000974 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000975
976 // Make sure we can establish a context for the method.
977 if (!ClassDecl) {
978 Diag(MethodLoc, diag::error_missing_method_context);
979 return 0;
980 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000981 QualType resultDeclType;
982
983 if (ReturnType)
984 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
985 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000986 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000987
Chris Lattner114add62008-03-16 00:49:28 +0000988 ObjCMethodDecl* ObjCMethod =
989 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000990 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000991 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000992 false,
Chris Lattner114add62008-03-16 00:49:28 +0000993 MethodDeclKind == tok::objc_optional ?
994 ObjCMethodDecl::Optional :
995 ObjCMethodDecl::Required);
996
Chris Lattnereee57c02008-04-04 06:12:32 +0000997 llvm::SmallVector<ParmVarDecl*, 16> Params;
998
999 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1000 // FIXME: arg->AttrList must be stored too!
1001 QualType argType;
1002
1003 if (ArgTypes[i])
1004 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1005 else
1006 argType = Context.getObjCIdType();
1007 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1008 SourceLocation(/*FIXME*/),
1009 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001010 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001011 Param->setObjCDeclQualifier(
1012 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1013 Params.push_back(Param);
1014 }
1015
Ted Kremenek42730c52008-01-07 19:49:32 +00001016 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1017 ObjCMethod->setObjCDeclQualifier(
1018 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1019 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001020
1021 // For implementations (which can be very "coarse grain"), we add the
1022 // method now. This allows the AST to implement lookup methods that work
1023 // incrementally (without waiting until we parse the @end). It also allows
1024 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001025 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001026 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001027 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001028 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001029 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001030 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001031 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001032 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001033 }
1034 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001035 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001036 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001037 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001038 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001039 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001040 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001041 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001043 }
1044 }
1045 if (PrevMethod) {
1046 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001047 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1048 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001049 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1050 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001052}
1053
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001054Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1055 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001056 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001057 Selector GetterSel,
1058 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001059 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001060 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001061 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1062 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001063 // Regardless of setter/getter attribute, we save the default getter/setter
1064 // selector names in anticipation of declaration of setter/getter methods.
1065 PDecl->setGetterName(GetterSel);
1066 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001067
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001068 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001069 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001070
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001071 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001072 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001073
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001074 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001075 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001076
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001077 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001079
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001080 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001082
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001083 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001084 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001085
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001086 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001087 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001088
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001089 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001090 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001091
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001092 if (MethodImplKind == tok::objc_required)
1093 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1094 else if (MethodImplKind == tok::objc_optional)
1095 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1096
Chris Lattner855e51f2007-12-12 07:09:47 +00001097 return PDecl;
1098}
1099
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001100/// ActOnPropertyImplDecl - This routine performs semantic checks and
1101/// builds the AST node for a property implementation declaration; declared
1102/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001103///
1104Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1105 SourceLocation PropertyLoc,
1106 bool Synthesize,
1107 DeclTy *ClassCatImpDecl,
1108 IdentifierInfo *PropertyId,
1109 IdentifierInfo *PropertyIvar) {
1110 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1111 // Make sure we have a context for the property implementation declaration.
1112 if (!ClassImpDecl) {
1113 Diag(AtLoc, diag::error_missing_property_context);
1114 return 0;
1115 }
1116 ObjCPropertyDecl *property = 0;
1117 ObjCInterfaceDecl* IDecl = 0;
1118 // Find the class or category class where this property must have
1119 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001120 ObjCImplementationDecl *IC = 0;
1121 ObjCCategoryImplDecl* CatImplClass = 0;
1122 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001123 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001124 // We always synthesize an interface for an implementation
1125 // without an interface decl. So, IDecl is always non-zero.
1126 assert(IDecl &&
1127 "ActOnPropertyImplDecl - @implementation without @interface");
1128
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001129 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001130 property = IDecl->FindPropertyDeclaration(PropertyId);
1131 if (!property) {
1132 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001133 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001134 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001135 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001136 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001137 if (Synthesize) {
1138 Diag(AtLoc, diag::error_synthesize_category_decl);
1139 return 0;
1140 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001141 IDecl = CatImplClass->getClassInterface();
1142 if (!IDecl) {
1143 Diag(AtLoc, diag::error_missing_property_interface);
1144 return 0;
1145 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001146 ObjCCategoryDecl *Category =
1147 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1148
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001149 // If category for this implementation not found, it is an error which
1150 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001151 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001152 return 0;
1153 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001154 property = Category->FindPropertyDeclaration(PropertyId);
1155 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001156 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001157 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001158 return 0;
1159 }
1160 }
1161 else {
1162 Diag(AtLoc, diag::error_bad_property_context);
1163 return 0;
1164 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001165 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001166 // Check that we have a valid, previously declared ivar for @synthesize
1167 if (Synthesize) {
1168 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001169 if (!PropertyIvar)
1170 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001171 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001172 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001173 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001174 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1175 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001176 return 0;
1177 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001178 // Check that type of property and its ivar match.
1179 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1180 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1181 Ivar->getName());
1182 return 0;
1183 }
1184
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001185 } else if (PropertyIvar) {
1186 // @dynamic
1187 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1188 return 0;
1189 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001190 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001191 ObjCPropertyImplDecl *PIDecl =
1192 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1193 (Synthesize ?
1194 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1195 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1196 Ivar);
1197 if (IC)
1198 IC->addPropertyImplementation(PIDecl);
1199 else
1200 CatImplClass->addPropertyImplementation(PIDecl);
1201
1202 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001203}