blob: 5188740af62ab2c2592e9a2e74062bfea82a5141 [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 Lattner855e51f2007-12-12 07:09:47 +0000137 if (!RefPDecl || RefPDecl->isForwardDecl())
Chris Lattner5cece462008-07-21 07:06:49 +0000138 Diag(EndProtoLoc, diag::warn_undef_protocolref,
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 ProtocolNames[i]->getName(),
140 ClassName->getName());
Chris Lattner5cece462008-07-21 07:06:49 +0000141 else
142 RefProtos.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000143 }
Chris Lattner5cece462008-07-21 07:06:49 +0000144 if (!RefProtos.empty())
145 IDecl->addReferencedProtocols(&RefProtos[0], RefProtos.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000146 IDecl->setLocEnd(EndProtoLoc);
147 }
148 return IDecl;
149}
150
151/// ActOnCompatiblityAlias - this action is called after complete parsing of
152/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000153Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
154 IdentifierInfo *AliasName,
155 SourceLocation AliasLocation,
156 IdentifierInfo *ClassName,
157 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000158 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000159 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000160 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000161 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000162 Diag(AliasLocation, diag::warn_previous_alias_decl);
163 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
164 }
165 else {
166 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
167 AliasName->getName());
168 Diag(ADecl->getLocation(), diag::err_previous_declaration);
169 }
170 return 0;
171 }
172 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000173 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000174 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
175 if (CDecl == 0) {
176 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
177 if (CDeclU)
178 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000179 return 0;
180 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000181
182 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000183 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000184 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
185
186 ObjCAliasDecls[AliasName] = AliasDecl;
187 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000188 return AliasDecl;
189}
190
191Sema::DeclTy *Sema::ActOnStartProtocolInterface(
192 SourceLocation AtProtoInterfaceLoc,
193 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
194 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
195 SourceLocation EndProtoLoc) {
196 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000197 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000198 if (PDecl) {
199 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000200 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000201 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
202 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000203 // Just return the protocol we already had.
204 // FIXME: don't leak the objects passed in!
205 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000206 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000207
208 PDecl->setForwardDecl(false);
209 PDecl->AllocReferencedProtocols(NumProtoRefs);
210 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000211 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000212 ProtocolName);
213 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000214 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000215 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000216
217 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000218 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000219 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000220 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000221 if (!RefPDecl || RefPDecl->isForwardDecl())
222 Diag(ProtocolLoc, diag::warn_undef_protocolref,
223 ProtoRefNames[i]->getName(),
224 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000225 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000226 }
227 PDecl->setLocEnd(EndProtoLoc);
228 }
229 return PDecl;
230}
231
232/// FindProtocolDeclaration - This routine looks up protocols and
233/// issuer error if they are not declared. It returns list of protocol
234/// declarations in its 'Protocols' argument.
235void
236Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
237 IdentifierInfo **ProtocolId,
238 unsigned NumProtocols,
239 llvm::SmallVector<DeclTy *,8> &Protocols) {
240 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000241 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000242 if (!PDecl)
243 Diag(TypeLoc, diag::err_undeclared_protocol,
244 ProtocolId[i]->getName());
245 else
246 Protocols.push_back(PDecl);
247 }
248}
249
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000250/// DiagnosePropertyMismatch - Compares two properties for their
251/// attributes and types and warns on a variety of inconsistancies.
252///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000253void
254Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
255 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000256 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000257 ObjCPropertyDecl::PropertyAttributeKind CAttr =
258 Property->getPropertyAttributes();
259 ObjCPropertyDecl::PropertyAttributeKind SAttr =
260 SuperProperty->getPropertyAttributes();
261 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
262 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000263 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000264 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000265 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
266 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000267 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000268 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000269 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000270 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
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(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000274 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000275
276 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
277 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000278 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000279 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000280 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000281 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000282 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000283 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000284 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000285 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000286 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000287 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000288 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000289
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000290 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
291 Diag(Property->getLocation(), diag::warn_property_type,
292 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000293 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000294
295}
296
297/// ComparePropertiesInBaseAndSuper - This routine compares property
298/// declarations in base and its super class, if any, and issues
299/// diagnostics in a variety of inconsistant situations.
300///
301void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000302Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000303 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
304 if (!SDecl)
305 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000306 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
307 E = SDecl->classprop_end(); S != E; ++S) {
308 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000309 // Does property in super class has declaration in current class?
310 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
311 E = IDecl->classprop_end(); I != E; ++I) {
312 ObjCPropertyDecl *PDecl = (*I);
313 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000314 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000315 }
316 }
317}
318
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000319/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
320/// of properties declared in a protocol and adds them to the list
321/// of properties for current class if it is not there already.
322void
323Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
324 ObjCProtocolDecl *PDecl)
325{
326 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
327 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
328 E = PDecl->classprop_end(); P != E; ++P) {
329 ObjCPropertyDecl *Pr = (*P);
330 ObjCInterfaceDecl::classprop_iterator CP, CE;
331 // Is this property already in class's list of properties?
332 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
333 CP != CE; ++CP)
334 if ((*CP)->getIdentifier() == Pr->getIdentifier())
335 break;
336 if (CP == CE)
337 // Add this property to list of properties for thie class.
338 mergeProperties.push_back(Pr);
339 else
340 // Property protocol already exist in class. Diagnose any mismatch.
341 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
342 }
343 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
344}
345
346/// MergeProtocolPropertiesIntoClass - This routine merges properties
347/// declared in 'MergeItsProtocols' objects (which can be a class or an
348/// inherited protocol into the list of properties for class 'IDecl'
349///
350
351void
352Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
353 DeclTy *MergeItsProtocols) {
354 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000355 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000356 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
357 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000358 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000359 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
360
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000361 // Go thru the list of protocols for this class and recursively merge
362 // their properties into this class as well.
363 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
364 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000365 MergeProtocolPropertiesIntoClass(IDecl, *P);
366 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000367 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
368 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
369 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000370 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000371 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000372}
373
Chris Lattner855e51f2007-12-12 07:09:47 +0000374/// ActOnForwardProtocolDeclaration -
375Action::DeclTy *
376Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
377 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000378 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000379
380 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000381 IdentifierInfo *Ident = IdentList[i];
382 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
383 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000384 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000385 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000386 }
387
388 Protocols.push_back(PDecl);
389 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000390 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
391 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000392}
393
394Sema::DeclTy *Sema::ActOnStartCategoryInterface(
395 SourceLocation AtInterfaceLoc,
396 IdentifierInfo *ClassName, SourceLocation ClassLoc,
397 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
398 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
399 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000400 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000401
Chris Lattnere29dc832008-03-16 20:34:23 +0000402 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000403 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000404 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000405
406 /// Check that class of this category is already completely declared.
407 if (!IDecl || IDecl->isForwardDecl())
408 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000409 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000410 /// Check for duplicate interface declaration for this category
411 ObjCCategoryDecl *CDeclChain;
412 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
413 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000414 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000415 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000416 CategoryName->getName());
417 break;
418 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000419 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000420 if (!CDeclChain)
421 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000422 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000423
424 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000425 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
426 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000427 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000428 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000429 if (!RefPDecl || RefPDecl->isForwardDecl()) {
430 Diag(CategoryLoc, diag::warn_undef_protocolref,
431 ProtoRefNames[i]->getName(),
432 CategoryName->getName());
433 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000434 if (RefPDecl)
435 RefProtocols.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000436 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000437 if (!RefProtocols.empty())
Chris Lattnerc72eaaf2008-07-21 17:23:15 +0000438 CDecl->addReferencedProtocols(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000439 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000440 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000441 return CDecl;
442}
443
444/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000445/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000446/// object.
447Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
448 SourceLocation AtCatImplLoc,
449 IdentifierInfo *ClassName, SourceLocation ClassLoc,
450 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000451 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000452 ObjCCategoryImplDecl *CDecl =
453 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000454 /// Check that class of this category is already completely declared.
455 if (!IDecl || IDecl->isForwardDecl())
456 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
457
458 /// TODO: Check that CatName, category name, is not used in another
459 // implementation.
460 return CDecl;
461}
462
463Sema::DeclTy *Sema::ActOnStartClassImplementation(
464 SourceLocation AtClassImplLoc,
465 IdentifierInfo *ClassName, SourceLocation ClassLoc,
466 IdentifierInfo *SuperClassname,
467 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000468 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000469 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000470 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000471 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000472 Diag(ClassLoc, diag::err_redefinition_different_kind,
473 ClassName->getName());
474 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
475 }
476 else {
477 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000478 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000479 if (!IDecl)
480 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
481 }
482
483 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000484 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000485 if (SuperClassname) {
486 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000487 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000488 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000489 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
490 SuperClassname->getName());
491 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
492 }
493 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000494 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000495 if (!SDecl)
496 Diag(SuperClassLoc, diag::err_undef_superclass,
497 SuperClassname->getName(), ClassName->getName());
498 else if (IDecl && IDecl->getSuperClass() != SDecl) {
499 // This implementation and its interface do not have the same
500 // super class.
501 Diag(SuperClassLoc, diag::err_conflicting_super_class,
502 SDecl->getName());
503 Diag(SDecl->getLocation(), diag::err_previous_definition);
504 }
505 }
506 }
507
508 if (!IDecl) {
509 // Legacy case of @implementation with no corresponding @interface.
510 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000511 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000512 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000513 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000514 IDecl->setSuperClass(SDecl);
515 IDecl->setLocEnd(ClassLoc);
516
517 // Remember that this needs to be removed when the scope is popped.
518 TUScope->AddDecl(IDecl);
519 }
520
Ted Kremenek42730c52008-01-07 19:49:32 +0000521 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000522 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
523 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000524
525 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000526 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000527 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000528 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
529 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000530 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000531 return IMPDecl;
532}
533
Ted Kremenek42730c52008-01-07 19:49:32 +0000534void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
535 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000536 SourceLocation RBrace) {
537 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000538 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000539 if (!IDecl)
540 return;
541 /// Check case of non-existing @interface decl.
542 /// (legacy objective-c @implementation decl without an @interface decl).
543 /// Add implementations's ivar to the synthesize class's ivar list.
544 if (IDecl->ImplicitInterfaceDecl()) {
545 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
546 return;
547 }
548 // If implementation has empty ivar list, just return.
549 if (numIvars == 0)
550 return;
551
552 assert(ivars && "missing @implementation ivars");
553
554 // Check interface's Ivar list against those in the implementation.
555 // names and types must match.
556 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000557 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000558 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000559 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
560 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 ObjCIvarDecl* ImplIvar = ivars[j++];
562 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000563 assert (ImplIvar && "missing implementation ivar");
564 assert (ClsIvar && "missing class ivar");
565 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
566 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
567 ImplIvar->getIdentifier()->getName());
568 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
569 ClsIvar->getIdentifier()->getName());
570 }
571 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
572 // as error.
573 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
574 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
575 ImplIvar->getIdentifier()->getName());
576 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
577 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000578 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000579 }
580 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000581 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000582
583 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000584 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000585 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000586 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000587}
588
Steve Naroffb4f48512008-02-10 21:38:56 +0000589void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
590 bool &IncompleteImpl) {
591 if (!IncompleteImpl) {
592 Diag(ImpLoc, diag::warn_incomplete_impl);
593 IncompleteImpl = true;
594 }
595 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
596}
597
Steve Naroffb268d2a2008-02-08 22:06:17 +0000598/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000599/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000600void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
601 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000602 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000603 const llvm::DenseSet<Selector> &InsMap,
604 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000605 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000606 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000607 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000609 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000610 method->getImplementationControl() != ObjCMethodDecl::Optional)
611 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000612 }
613 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000614 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000615 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000617 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000618 method->getImplementationControl() != ObjCMethodDecl::Optional)
619 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000620 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000621 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000623 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000624 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000625}
626
Ted Kremenek42730c52008-01-07 19:49:32 +0000627void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
628 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000629 llvm::DenseSet<Selector> InsMap;
630 // Check and see if instance methods in class interface have been
631 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000632 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000633 E = IMPDecl->instmeth_end(); I != E; ++I)
634 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000635
636 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000637 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000638 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000639 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000640 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000641
Chris Lattner855e51f2007-12-12 07:09:47 +0000642 llvm::DenseSet<Selector> ClsMap;
643 // Check and see if class methods in class interface have been
644 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000645 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000646 E = IMPDecl->classmeth_end(); I != E; ++I)
647 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000648
Ted Kremenek42730c52008-01-07 19:49:32 +0000649 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000650 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000651 if (!ClsMap.count((*I)->getSelector()))
652 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000653
654 // Check the protocol list for unimplemented methods in the @implementation
655 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000656 const ObjCList<ObjCProtocolDecl> &Protocols =
657 IDecl->getReferencedProtocols();
658 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
659 E = Protocols.end(); I != E; ++I)
660 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000661 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000662}
663
664/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
665/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000666void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
667 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000668 llvm::DenseSet<Selector> InsMap;
669 // Check and see if instance methods in category interface have been
670 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000671 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000672 E = CatImplDecl->instmeth_end(); I != E; ++I)
673 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000674
675 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000676 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000677 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000678 if (!InsMap.count((*I)->getSelector()))
679 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
680
Chris Lattner855e51f2007-12-12 07:09:47 +0000681 llvm::DenseSet<Selector> ClsMap;
682 // Check and see if class methods in category interface have been
683 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000684 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000685 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
686 I != E; ++I)
687 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000688
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000690 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000691 if (!ClsMap.count((*I)->getSelector()))
692 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000693
694 // Check the protocol list for unimplemented methods in the @implementation
695 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000696 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000697 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000699 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
700 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000701 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000702}
703
704/// ActOnForwardClassDeclaration -
705Action::DeclTy *
706Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
707 IdentifierInfo **IdentList, unsigned NumElts)
708{
Ted Kremenek42730c52008-01-07 19:49:32 +0000709 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000710
711 for (unsigned i = 0; i != NumElts; ++i) {
712 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000713 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000714 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000715 // GCC apparently allows the following idiom:
716 //
717 // typedef NSObject < XCElementTogglerP > XCElementToggler;
718 // @class XCElementToggler;
719 //
720 // FIXME: Make an extension?
721 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
722 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
723 Diag(AtClassLoc, diag::err_redefinition_different_kind,
724 IdentList[i]->getName());
725 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
726 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000727 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000728 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000729 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000730 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000731 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000732 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000733
734 // Remember that this needs to be removed when the scope is popped.
735 TUScope->AddDecl(IDecl);
736 }
737
738 Interfaces.push_back(IDecl);
739 }
740
Chris Lattnere29dc832008-03-16 20:34:23 +0000741 return ObjCClassDecl::Create(Context, AtClassLoc,
742 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000743}
744
745
746/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
747/// returns true, or false, accordingly.
748/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000749bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
750 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000751 if (Method->getResultType().getCanonicalType() !=
752 PrevMethod->getResultType().getCanonicalType())
753 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000754 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000755 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
756 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000757 if (Context.getCanonicalType(ParamDecl->getType()) !=
758 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000759 return false;
760 }
761 return true;
762}
763
Ted Kremenek42730c52008-01-07 19:49:32 +0000764void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
765 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000766 if (!FirstMethod.Method) {
767 // Haven't seen a method with this selector name yet - add it.
768 FirstMethod.Method = Method;
769 FirstMethod.Next = 0;
770 } else {
771 // We've seen a method with this name, now check the type signature(s).
772 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
773
Ted Kremenek42730c52008-01-07 19:49:32 +0000774 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000775 Next = Next->Next)
776 match = MatchTwoMethodDeclarations(Method, Next->Method);
777
778 if (!match) {
779 // We have a new signature for an existing method - add it.
780 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000781 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000782 FirstMethod.Next = OMI;
783 }
784 }
785}
786
Ted Kremenek42730c52008-01-07 19:49:32 +0000787void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
788 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000789 if (!FirstMethod.Method) {
790 // Haven't seen a method with this selector name yet - add it.
791 FirstMethod.Method = Method;
792 FirstMethod.Next = 0;
793 } else {
794 // We've seen a method with this name, now check the type signature(s).
795 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
796
Ted Kremenek42730c52008-01-07 19:49:32 +0000797 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000798 Next = Next->Next)
799 match = MatchTwoMethodDeclarations(Method, Next->Method);
800
801 if (!match) {
802 // We have a new signature for an existing method - add it.
803 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000804 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000805 FirstMethod.Next = OMI;
806 }
807 }
808}
809
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000810// Note: For class/category implemenations, allMethods/allProperties is
811// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000812void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
813 DeclTy **allMethods, unsigned allNum,
814 DeclTy **allProperties, unsigned pNum) {
815 Decl *ClassDecl = static_cast<Decl *>(classDecl);
816
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000817 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
818 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000819 // should be true.
820 if (!ClassDecl)
821 return;
822
Ted Kremenek42730c52008-01-07 19:49:32 +0000823 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
824 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000825
Ted Kremenek42730c52008-01-07 19:49:32 +0000826 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
827 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000828
829 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000830 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
831 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000833
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000834 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000835 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
836 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000837 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
838 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
839 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
840 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000841 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000842 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000843 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000844
845 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000846 ObjCMethodDecl *Method =
847 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000848
849 if (!Method) continue; // Already issued a diagnostic.
850 if (Method->isInstance()) {
851 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000852 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000853 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
854 : false;
855 if (isInterfaceDeclKind && PrevMethod && !match
856 || checkIdenticalMethods && match) {
857 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
858 Method->getSelector().getName());
859 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
860 } else {
861 insMethods.push_back(Method);
862 InsMap[Method->getSelector()] = Method;
863 /// The following allows us to typecheck messages to "id".
864 AddInstanceMethodToGlobalPool(Method);
865 }
866 }
867 else {
868 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000869 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000870 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
871 : false;
872 if (isInterfaceDeclKind && PrevMethod && !match
873 || checkIdenticalMethods && match) {
874 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
875 Method->getSelector().getName());
876 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
877 } else {
878 clsMethods.push_back(Method);
879 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000880 /// The following allows us to typecheck messages to "Class".
881 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000882 }
883 }
884 }
885
Ted Kremenek42730c52008-01-07 19:49:32 +0000886 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000887 // Compares properties declaraed in this class to those of its
888 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000889 ComparePropertiesInBaseAndSuper(I);
890 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000891 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
892 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000893 // FIXME: It would be really nice if we could avoid this. Injecting
894 // methods into the interface makes it hard to distinguish "real" methods
895 // from synthesized "property" methods (that aren't in the source).
896 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000897 I->addPropertyMethods(Context, *P, insMethods);
898 }
899 I->addMethods(&insMethods[0], insMethods.size(),
900 &clsMethods[0], clsMethods.size(), AtEndLoc);
901
Ted Kremenek42730c52008-01-07 19:49:32 +0000902 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000903 P->addMethods(&insMethods[0], insMethods.size(),
904 &clsMethods[0], clsMethods.size(), AtEndLoc);
905 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000907 C->addMethods(&insMethods[0], insMethods.size(),
908 &clsMethods[0], clsMethods.size(), AtEndLoc);
909 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000910 else if (ObjCImplementationDecl *IC =
911 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000912 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000913 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000914 ImplMethodsVsClassMethods(IC, IDecl);
915 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000916 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000917 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000918 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000919 // Find category interface decl and then check that all methods declared
920 // in this interface is implemented in the category @implementation.
921 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000923 Categories; Categories = Categories->getNextClassCategory()) {
924 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
925 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
926 break;
927 }
928 }
929 }
930 }
931}
932
933
934/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
935/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000936static Decl::ObjCDeclQualifier
937CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
938 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
939 if (PQTVal & ObjCDeclSpec::DQ_In)
940 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
941 if (PQTVal & ObjCDeclSpec::DQ_Inout)
942 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
943 if (PQTVal & ObjCDeclSpec::DQ_Out)
944 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
945 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
946 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
947 if (PQTVal & ObjCDeclSpec::DQ_Byref)
948 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
949 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
950 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000951
952 return ret;
953}
954
955Sema::DeclTy *Sema::ActOnMethodDeclaration(
956 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000957 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000958 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000959 Selector Sel,
960 // optional arguments. The number of types/arguments is obtained
961 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000962 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000963 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
964 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000965 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000966
967 // Make sure we can establish a context for the method.
968 if (!ClassDecl) {
969 Diag(MethodLoc, diag::error_missing_method_context);
970 return 0;
971 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000972 QualType resultDeclType;
973
974 if (ReturnType)
975 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
976 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000977 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000978
Chris Lattner114add62008-03-16 00:49:28 +0000979 ObjCMethodDecl* ObjCMethod =
980 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000981 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000982 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000983 false,
Chris Lattner114add62008-03-16 00:49:28 +0000984 MethodDeclKind == tok::objc_optional ?
985 ObjCMethodDecl::Optional :
986 ObjCMethodDecl::Required);
987
Chris Lattnereee57c02008-04-04 06:12:32 +0000988 llvm::SmallVector<ParmVarDecl*, 16> Params;
989
990 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
991 // FIXME: arg->AttrList must be stored too!
992 QualType argType;
993
994 if (ArgTypes[i])
995 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
996 else
997 argType = Context.getObjCIdType();
998 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
999 SourceLocation(/*FIXME*/),
1000 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001001 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001002 Param->setObjCDeclQualifier(
1003 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1004 Params.push_back(Param);
1005 }
1006
Ted Kremenek42730c52008-01-07 19:49:32 +00001007 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1008 ObjCMethod->setObjCDeclQualifier(
1009 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1010 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001011
1012 // For implementations (which can be very "coarse grain"), we add the
1013 // method now. This allows the AST to implement lookup methods that work
1014 // incrementally (without waiting until we parse the @end). It also allows
1015 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001016 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001017 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001018 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001019 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001020 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001021 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001022 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001023 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001024 }
1025 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001026 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001027 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001028 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001029 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001030 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001031 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001032 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001033 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001034 }
1035 }
1036 if (PrevMethod) {
1037 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001038 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1039 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001040 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1041 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001043}
1044
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001045Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1046 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001047 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001048 Selector GetterSel,
1049 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001050 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001051 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001052 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1053 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001054 // Regardless of setter/getter attribute, we save the default getter/setter
1055 // selector names in anticipation of declaration of setter/getter methods.
1056 PDecl->setGetterName(GetterSel);
1057 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001058
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001059 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001060 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001061
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001062 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001063 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001064
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001065 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001066 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001067
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001068 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001069 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001070
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001071 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001072 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001073
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001074 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001075 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001076
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001077 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001079
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001080 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001082
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001083 if (MethodImplKind == tok::objc_required)
1084 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1085 else if (MethodImplKind == tok::objc_optional)
1086 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1087
Chris Lattner855e51f2007-12-12 07:09:47 +00001088 return PDecl;
1089}
1090
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001091/// ActOnPropertyImplDecl - This routine performs semantic checks and
1092/// builds the AST node for a property implementation declaration; declared
1093/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001094///
1095Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1096 SourceLocation PropertyLoc,
1097 bool Synthesize,
1098 DeclTy *ClassCatImpDecl,
1099 IdentifierInfo *PropertyId,
1100 IdentifierInfo *PropertyIvar) {
1101 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1102 // Make sure we have a context for the property implementation declaration.
1103 if (!ClassImpDecl) {
1104 Diag(AtLoc, diag::error_missing_property_context);
1105 return 0;
1106 }
1107 ObjCPropertyDecl *property = 0;
1108 ObjCInterfaceDecl* IDecl = 0;
1109 // Find the class or category class where this property must have
1110 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001111 ObjCImplementationDecl *IC = 0;
1112 ObjCCategoryImplDecl* CatImplClass = 0;
1113 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001114 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001115 // We always synthesize an interface for an implementation
1116 // without an interface decl. So, IDecl is always non-zero.
1117 assert(IDecl &&
1118 "ActOnPropertyImplDecl - @implementation without @interface");
1119
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001120 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001121 property = IDecl->FindPropertyDeclaration(PropertyId);
1122 if (!property) {
1123 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001124 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001125 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001126 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001127 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001128 if (Synthesize) {
1129 Diag(AtLoc, diag::error_synthesize_category_decl);
1130 return 0;
1131 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 IDecl = CatImplClass->getClassInterface();
1133 if (!IDecl) {
1134 Diag(AtLoc, diag::error_missing_property_interface);
1135 return 0;
1136 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001137 ObjCCategoryDecl *Category =
1138 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1139
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001140 // If category for this implementation not found, it is an error which
1141 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001142 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001143 return 0;
1144 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001145 property = Category->FindPropertyDeclaration(PropertyId);
1146 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001147 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001148 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001149 return 0;
1150 }
1151 }
1152 else {
1153 Diag(AtLoc, diag::error_bad_property_context);
1154 return 0;
1155 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001156 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001157 // Check that we have a valid, previously declared ivar for @synthesize
1158 if (Synthesize) {
1159 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001160 if (!PropertyIvar)
1161 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001162 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001163 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001164 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001165 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1166 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001167 return 0;
1168 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001169 // Check that type of property and its ivar match.
1170 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1171 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1172 Ivar->getName());
1173 return 0;
1174 }
1175
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001176 } else if (PropertyIvar) {
1177 // @dynamic
1178 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1179 return 0;
1180 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001181 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001182 ObjCPropertyImplDecl *PIDecl =
1183 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1184 (Synthesize ?
1185 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1186 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1187 Ivar);
1188 if (IC)
1189 IC->addPropertyImplementation(PIDecl);
1190 else
1191 CatImplClass->addPropertyImplementation(PIDecl);
1192
1193 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001194}