blob: b02df056c82716340282a9530d22271b2cb00eff [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"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremenek42730c52008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff3ac43f92008-07-25 17:57:26 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000031
32 // Allow the rest of sema to find private method decl implementations.
33 if (MDecl->isInstance())
34 AddInstanceMethodToGlobalPool(MDecl);
35 else
36 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerf3874bc2008-04-06 04:47:34 +000039 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000040
41 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
43 struct DeclaratorChunk::ParamInfo PI;
44
45 // Insert the invisible arguments, self and _cmd!
46 PI.Ident = &Context.Idents.get("self");
47 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Steve Naroff1a5027c2008-06-05 14:49:39 +000048 QualType selfTy;
Chris Lattner855e51f2007-12-12 07:09:47 +000049 if (MDecl->isInstance()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +000050 selfTy = Context.getObjCIdType();
Gabor Greif9b2e0f92008-02-29 20:35:55 +000051 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
52 // There may be no interface context due to error in declaration of the
53 // interface (which has been reported). Recover gracefully
Chris Lattner3e254fb2008-04-08 04:40:51 +000054 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000055 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000056 }
Steve Naroff1a5027c2008-06-05 14:49:39 +000057 } else // we have a factory method.
58 selfTy = Context.getObjCClassType();
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000059 getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000060 PI.Ident, PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000061
62 PI.Ident = &Context.Idents.get("_cmd");
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000063 getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
Chris Lattner8c7c6a12008-06-17 18:05:57 +000064 PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
Chris Lattner3e254fb2008-04-08 04:40:51 +000065
Chris Lattner97316c02008-04-10 02:22:51 +000066 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000067 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000068 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000069 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000070 if (II)
71 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000072 }
73}
74
Chris Lattnere705e5e2008-07-21 22:17:28 +000075Sema::DeclTy *Sema::
76ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
77 IdentifierInfo *ClassName, SourceLocation ClassLoc,
78 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000079 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000080 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000081 assert(ClassName && "Missing class identifier");
82
83 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000084 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000085 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000086 Diag(ClassLoc, diag::err_redefinition_different_kind,
87 ClassName->getName());
88 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
89 }
90
Ted Kremenek42730c52008-01-07 19:49:32 +000091 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000092 if (IDecl) {
93 // Class already seen. Is it a forward declaration?
94 if (!IDecl->isForwardDecl())
95 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
96 else {
97 IDecl->setLocation(AtInterfaceLoc);
98 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000099 }
Chris Lattner5cece462008-07-21 07:06:49 +0000100 } else {
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000101 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +0000102 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000103 if (AttrList)
104 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000105
Steve Naroff15208162008-04-02 18:30:49 +0000106 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000107 // Remember that this needs to be removed when the scope is popped.
108 TUScope->AddDecl(IDecl);
109 }
110
111 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000114 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000115 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000116 Diag(SuperLoc, diag::err_redefinition_different_kind,
117 SuperName->getName());
118 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
119 }
120 else {
121 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000122 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000123
124 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000125 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000126 SuperClassEntry ? SuperClassEntry->getName()
127 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000128 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000129 }
130 }
131 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000132 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000133 IDecl->setLocEnd(SuperLoc);
134 } else { // we have a root class.
135 IDecl->setLocEnd(ClassLoc);
136 }
137
138 /// Check then save referenced protocols
Chris Lattnerae1ae492008-07-26 04:13:19 +0000139 if (NumProtoRefs) {
140 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000141 IDecl->setLocEnd(EndProtoLoc);
142 }
143 return IDecl;
144}
145
146/// ActOnCompatiblityAlias - this action is called after complete parsing of
147/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000148Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
149 IdentifierInfo *AliasName,
150 SourceLocation AliasLocation,
151 IdentifierInfo *ClassName,
152 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000153 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000154 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000155 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000156 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000157 Diag(AliasLocation, diag::warn_previous_alias_decl);
158 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
159 }
160 else {
161 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
162 AliasName->getName());
163 Diag(ADecl->getLocation(), diag::err_previous_declaration);
164 }
165 return 0;
166 }
167 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000168 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000169 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
170 if (CDecl == 0) {
171 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
172 if (CDeclU)
173 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000174 return 0;
175 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000176
177 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000178 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000179 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
180
181 ObjCAliasDecls[AliasName] = AliasDecl;
182 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000183 return AliasDecl;
184}
185
Chris Lattner2bdedd62008-07-26 04:03:38 +0000186Sema::DeclTy *
187Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
188 IdentifierInfo *ProtocolName,
189 SourceLocation ProtocolLoc,
190 DeclTy * const *ProtoRefs,
191 unsigned NumProtoRefs,
192 SourceLocation EndProtoLoc) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000193 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000195 if (PDecl) {
196 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000197 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000198 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
199 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000200 // Just return the protocol we already had.
201 // FIXME: don't leak the objects passed in!
202 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000203 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000204 // Make sure the cached decl gets a valid start location.
205 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000206 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000207 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000208 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000209 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000210 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000211 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000212
213 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000214 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000215 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000216 PDecl->setLocEnd(EndProtoLoc);
217 }
218 return PDecl;
219}
220
221/// FindProtocolDeclaration - This routine looks up protocols and
222/// issuer error if they are not declared. It returns list of protocol
223/// declarations in its 'Protocols' argument.
224void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000225Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000226 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000227 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000228 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000229 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000230 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
231 if (!PDecl) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000232 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
233 ProtocolId[i].first->getName());
Chris Lattner17d50a92008-07-26 03:47:43 +0000234 continue;
235 }
236
237 // If this is a forward declaration and we are supposed to warn in this
238 // case, do it.
239 if (WarnOnDeclarations && PDecl->isForwardDecl())
240 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
241 ProtocolId[i].first->getName());
242 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000243 }
244}
245
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000246/// DiagnosePropertyMismatch - Compares two properties for their
247/// attributes and types and warns on a variety of inconsistancies.
248///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000249void
250Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
251 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000252 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000253 ObjCPropertyDecl::PropertyAttributeKind CAttr =
254 Property->getPropertyAttributes();
255 ObjCPropertyDecl::PropertyAttributeKind SAttr =
256 SuperProperty->getPropertyAttributes();
257 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
258 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000259 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000260 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000261 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
262 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000263 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000264 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000265 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000266 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
267 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000268 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000269 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000270 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000271
272 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
273 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
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(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000276 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000277 if (Property->getSetterName() != SuperProperty->getSetterName())
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(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000280 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000281 if (Property->getGetterName() != SuperProperty->getGetterName())
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(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000284 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000285
Chris Lattnerc5cff302008-07-26 20:50:02 +0000286 if (Context.getCanonicalType(Property->getType()) !=
287 Context.getCanonicalType(SuperProperty->getType()))
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000288 Diag(Property->getLocation(), diag::warn_property_type,
289 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000290 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000291
292}
293
294/// ComparePropertiesInBaseAndSuper - This routine compares property
295/// declarations in base and its super class, if any, and issues
296/// diagnostics in a variety of inconsistant situations.
297///
298void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000299Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000300 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
301 if (!SDecl)
302 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000303 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
304 E = SDecl->classprop_end(); S != E; ++S) {
305 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000306 // Does property in super class has declaration in current class?
307 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
308 E = IDecl->classprop_end(); I != E; ++I) {
309 ObjCPropertyDecl *PDecl = (*I);
310 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000311 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000312 }
313 }
314}
315
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000316/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
317/// of properties declared in a protocol and adds them to the list
318/// of properties for current class if it is not there already.
319void
320Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
321 ObjCProtocolDecl *PDecl)
322{
323 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
324 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
325 E = PDecl->classprop_end(); P != E; ++P) {
326 ObjCPropertyDecl *Pr = (*P);
327 ObjCInterfaceDecl::classprop_iterator CP, CE;
328 // Is this property already in class's list of properties?
329 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
330 CP != CE; ++CP)
331 if ((*CP)->getIdentifier() == Pr->getIdentifier())
332 break;
333 if (CP == CE)
334 // Add this property to list of properties for thie class.
335 mergeProperties.push_back(Pr);
336 else
337 // Property protocol already exist in class. Diagnose any mismatch.
338 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
339 }
340 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
341}
342
343/// MergeProtocolPropertiesIntoClass - This routine merges properties
344/// declared in 'MergeItsProtocols' objects (which can be a class or an
345/// inherited protocol into the list of properties for class 'IDecl'
346///
347
348void
349Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
350 DeclTy *MergeItsProtocols) {
351 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000352 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000353 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
354 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000355 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000356 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
357
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000358 // Go thru the list of protocols for this class and recursively merge
359 // their properties into this class as well.
360 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
361 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000362 MergeProtocolPropertiesIntoClass(IDecl, *P);
363 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000364 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
365 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
366 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000367 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000368 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000369}
370
Chris Lattner855e51f2007-12-12 07:09:47 +0000371/// ActOnForwardProtocolDeclaration -
372Action::DeclTy *
373Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000374 const IdentifierLocPair *IdentList,
375 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000376 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000377
378 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000379 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000380 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000381 if (PDecl == 0) // Not already seen?
382 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000383
384 Protocols.push_back(PDecl);
385 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000386 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
387 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000388}
389
Chris Lattnere705e5e2008-07-21 22:17:28 +0000390Sema::DeclTy *Sema::
391ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
392 IdentifierInfo *ClassName, SourceLocation ClassLoc,
393 IdentifierInfo *CategoryName,
394 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000395 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000396 unsigned NumProtoRefs,
397 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000398 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000399
Chris Lattnere29dc832008-03-16 20:34:23 +0000400 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000401 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000402 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000403
404 /// Check that class of this category is already completely declared.
405 if (!IDecl || IDecl->isForwardDecl())
406 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000407 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000408 /// Check for duplicate interface declaration for this category
409 ObjCCategoryDecl *CDeclChain;
410 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
411 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000412 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000413 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000414 CategoryName->getName());
415 break;
416 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000417 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000418 if (!CDeclChain)
419 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000420 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000421
422 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000423 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
424 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000425 }
426 return CDecl;
427}
428
429/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000430/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000431/// object.
432Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
433 SourceLocation AtCatImplLoc,
434 IdentifierInfo *ClassName, SourceLocation ClassLoc,
435 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000436 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000437 ObjCCategoryImplDecl *CDecl =
438 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000439 /// Check that class of this category is already completely declared.
440 if (!IDecl || IDecl->isForwardDecl())
441 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
442
443 /// TODO: Check that CatName, category name, is not used in another
444 // implementation.
445 return CDecl;
446}
447
448Sema::DeclTy *Sema::ActOnStartClassImplementation(
449 SourceLocation AtClassImplLoc,
450 IdentifierInfo *ClassName, SourceLocation ClassLoc,
451 IdentifierInfo *SuperClassname,
452 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000454 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000455 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000456 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000457 Diag(ClassLoc, diag::err_redefinition_different_kind,
458 ClassName->getName());
459 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
460 }
461 else {
462 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000463 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000464 if (!IDecl)
465 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
466 }
467
468 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000469 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000470 if (SuperClassname) {
471 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000472 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000473 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000474 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
475 SuperClassname->getName());
476 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
477 }
478 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000479 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000480 if (!SDecl)
481 Diag(SuperClassLoc, diag::err_undef_superclass,
482 SuperClassname->getName(), ClassName->getName());
483 else if (IDecl && IDecl->getSuperClass() != SDecl) {
484 // This implementation and its interface do not have the same
485 // super class.
486 Diag(SuperClassLoc, diag::err_conflicting_super_class,
487 SDecl->getName());
488 Diag(SDecl->getLocation(), diag::err_previous_definition);
489 }
490 }
491 }
492
493 if (!IDecl) {
494 // Legacy case of @implementation with no corresponding @interface.
495 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000496
497 // FIXME: Do we support attributes on the @implementation? If so
498 // we should copy them over.
Chris Lattner5cece462008-07-21 07:06:49 +0000499 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000500 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000501 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000502 IDecl->setSuperClass(SDecl);
503 IDecl->setLocEnd(ClassLoc);
504
505 // Remember that this needs to be removed when the scope is popped.
506 TUScope->AddDecl(IDecl);
507 }
508
Ted Kremenek42730c52008-01-07 19:49:32 +0000509 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000510 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
511 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000512
513 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000514 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000515 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000516 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
517 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000518 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000519 return IMPDecl;
520}
521
Ted Kremenek42730c52008-01-07 19:49:32 +0000522void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
523 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000524 SourceLocation RBrace) {
525 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000526 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000527 if (!IDecl)
528 return;
529 /// Check case of non-existing @interface decl.
530 /// (legacy objective-c @implementation decl without an @interface decl).
531 /// Add implementations's ivar to the synthesize class's ivar list.
532 if (IDecl->ImplicitInterfaceDecl()) {
533 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
534 return;
535 }
536 // If implementation has empty ivar list, just return.
537 if (numIvars == 0)
538 return;
539
540 assert(ivars && "missing @implementation ivars");
541
542 // Check interface's Ivar list against those in the implementation.
543 // names and types must match.
544 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000545 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000546 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000547 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
548 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000549 ObjCIvarDecl* ImplIvar = ivars[j++];
550 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000551 assert (ImplIvar && "missing implementation ivar");
552 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000553 if (Context.getCanonicalType(ImplIvar->getType()) !=
554 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000555 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
556 ImplIvar->getIdentifier()->getName());
557 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
558 ClsIvar->getIdentifier()->getName());
559 }
560 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
561 // as error.
562 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
563 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
564 ImplIvar->getIdentifier()->getName());
565 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
566 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000567 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000568 }
569 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000570 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000571
572 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000573 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000574 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000575 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000576}
577
Steve Naroffb4f48512008-02-10 21:38:56 +0000578void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
579 bool &IncompleteImpl) {
580 if (!IncompleteImpl) {
581 Diag(ImpLoc, diag::warn_incomplete_impl);
582 IncompleteImpl = true;
583 }
584 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
585}
586
Steve Naroffb268d2a2008-02-08 22:06:17 +0000587/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000588/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000589void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
590 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000591 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000592 const llvm::DenseSet<Selector> &InsMap,
593 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000594 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000595 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000596 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000598 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000599 method->getImplementationControl() != ObjCMethodDecl::Optional)
600 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000601 }
602 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000603 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000604 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000605 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000606 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000607 method->getImplementationControl() != ObjCMethodDecl::Optional)
608 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000609 }
Chris Lattner0be08822008-07-21 21:32:27 +0000610 // Check on this protocols's referenced protocols, recursively.
611 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
612 E = PDecl->protocol_end(); PI != E; ++PI)
613 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000614}
615
Ted Kremenek42730c52008-01-07 19:49:32 +0000616void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
617 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000618 llvm::DenseSet<Selector> InsMap;
619 // Check and see if instance methods in class interface have been
620 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000621 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000622 E = IMPDecl->instmeth_end(); I != E; ++I)
623 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000624
625 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000626 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000627 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000628 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000629 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000630
Chris Lattner855e51f2007-12-12 07:09:47 +0000631 llvm::DenseSet<Selector> ClsMap;
632 // Check and see if class methods in class interface have been
633 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000634 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000635 E = IMPDecl->classmeth_end(); I != E; ++I)
636 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000637
Ted Kremenek42730c52008-01-07 19:49:32 +0000638 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000639 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000640 if (!ClsMap.count((*I)->getSelector()))
641 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000642
643 // Check the protocol list for unimplemented methods in the @implementation
644 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000645 const ObjCList<ObjCProtocolDecl> &Protocols =
646 IDecl->getReferencedProtocols();
647 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
648 E = Protocols.end(); I != E; ++I)
649 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000650 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000651}
652
653/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
654/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000655void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
656 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000657 llvm::DenseSet<Selector> InsMap;
658 // Check and see if instance methods in category interface have been
659 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000660 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000661 E = CatImplDecl->instmeth_end(); I != E; ++I)
662 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000663
664 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000665 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000666 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000667 if (!InsMap.count((*I)->getSelector()))
668 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
669
Chris Lattner855e51f2007-12-12 07:09:47 +0000670 llvm::DenseSet<Selector> ClsMap;
671 // Check and see if class methods in category interface have been
672 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000673 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000674 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
675 I != E; ++I)
676 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000677
Ted Kremenek42730c52008-01-07 19:49:32 +0000678 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000679 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000680 if (!ClsMap.count((*I)->getSelector()))
681 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000682
683 // Check the protocol list for unimplemented methods in the @implementation
684 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000685 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
686 E = CatClassDecl->protocol_end(); PI != E; ++PI)
687 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000688 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000689}
690
691/// ActOnForwardClassDeclaration -
692Action::DeclTy *
693Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
694 IdentifierInfo **IdentList, unsigned NumElts)
695{
Ted Kremenek42730c52008-01-07 19:49:32 +0000696 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000697
698 for (unsigned i = 0; i != NumElts; ++i) {
699 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000700 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000701 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000702 // GCC apparently allows the following idiom:
703 //
704 // typedef NSObject < XCElementTogglerP > XCElementToggler;
705 // @class XCElementToggler;
706 //
707 // FIXME: Make an extension?
708 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
709 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
710 Diag(AtClassLoc, diag::err_redefinition_different_kind,
711 IdentList[i]->getName());
712 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
713 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000714 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000715 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000716 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000717 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000718 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000719 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000720
721 // Remember that this needs to be removed when the scope is popped.
722 TUScope->AddDecl(IDecl);
723 }
724
725 Interfaces.push_back(IDecl);
726 }
727
Chris Lattnere29dc832008-03-16 20:34:23 +0000728 return ObjCClassDecl::Create(Context, AtClassLoc,
729 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000730}
731
732
733/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
734/// returns true, or false, accordingly.
735/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000736bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
737 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000738 if (Context.getCanonicalType(Method->getResultType()) !=
739 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000740 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000741 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000742 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
743 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000744 if (Context.getCanonicalType(ParamDecl->getType()) !=
745 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000746 return false;
747 }
748 return true;
749}
750
Ted Kremenek42730c52008-01-07 19:49:32 +0000751void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
752 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000753 if (!FirstMethod.Method) {
754 // Haven't seen a method with this selector name yet - add it.
755 FirstMethod.Method = Method;
756 FirstMethod.Next = 0;
757 } else {
758 // We've seen a method with this name, now check the type signature(s).
759 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
760
Ted Kremenek42730c52008-01-07 19:49:32 +0000761 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000762 Next = Next->Next)
763 match = MatchTwoMethodDeclarations(Method, Next->Method);
764
765 if (!match) {
766 // We have a new signature for an existing method - add it.
767 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000768 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000769 FirstMethod.Next = OMI;
770 }
771 }
772}
773
Ted Kremenek42730c52008-01-07 19:49:32 +0000774void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
775 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000776 if (!FirstMethod.Method) {
777 // Haven't seen a method with this selector name yet - add it.
778 FirstMethod.Method = Method;
779 FirstMethod.Next = 0;
780 } else {
781 // We've seen a method with this name, now check the type signature(s).
782 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
783
Ted Kremenek42730c52008-01-07 19:49:32 +0000784 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000785 Next = Next->Next)
786 match = MatchTwoMethodDeclarations(Method, Next->Method);
787
788 if (!match) {
789 // We have a new signature for an existing method - add it.
790 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000791 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000792 FirstMethod.Next = OMI;
793 }
794 }
795}
796
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000797// Note: For class/category implemenations, allMethods/allProperties is
798// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000799void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
800 DeclTy **allMethods, unsigned allNum,
801 DeclTy **allProperties, unsigned pNum) {
802 Decl *ClassDecl = static_cast<Decl *>(classDecl);
803
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000804 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
805 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000806 // should be true.
807 if (!ClassDecl)
808 return;
809
Ted Kremenek42730c52008-01-07 19:49:32 +0000810 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
811 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000812
Ted Kremenek42730c52008-01-07 19:49:32 +0000813 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
814 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000815
816 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000817 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
818 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000819 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000820
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000821 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000822 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
823 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000824 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
825 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
826 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
827 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000828 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000829 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000830 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000831
832 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000833 ObjCMethodDecl *Method =
834 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000835
836 if (!Method) continue; // Already issued a diagnostic.
837 if (Method->isInstance()) {
838 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000839 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000840 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
841 : false;
842 if (isInterfaceDeclKind && PrevMethod && !match
843 || checkIdenticalMethods && match) {
844 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
845 Method->getSelector().getName());
846 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
847 } else {
848 insMethods.push_back(Method);
849 InsMap[Method->getSelector()] = Method;
850 /// The following allows us to typecheck messages to "id".
851 AddInstanceMethodToGlobalPool(Method);
852 }
853 }
854 else {
855 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000856 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000857 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
858 : false;
859 if (isInterfaceDeclKind && PrevMethod && !match
860 || checkIdenticalMethods && match) {
861 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
862 Method->getSelector().getName());
863 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
864 } else {
865 clsMethods.push_back(Method);
866 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000867 /// The following allows us to typecheck messages to "Class".
868 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000869 }
870 }
871 }
872
Ted Kremenek42730c52008-01-07 19:49:32 +0000873 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000874 // Compares properties declaraed in this class to those of its
875 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000876 ComparePropertiesInBaseAndSuper(I);
877 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000878 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
879 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000880 // FIXME: It would be really nice if we could avoid this. Injecting
881 // methods into the interface makes it hard to distinguish "real" methods
882 // from synthesized "property" methods (that aren't in the source).
883 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000884 I->addPropertyMethods(Context, *P, insMethods);
885 }
886 I->addMethods(&insMethods[0], insMethods.size(),
887 &clsMethods[0], clsMethods.size(), AtEndLoc);
888
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000890 P->addMethods(&insMethods[0], insMethods.size(),
891 &clsMethods[0], clsMethods.size(), AtEndLoc);
892 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000893 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000894 C->addMethods(&insMethods[0], insMethods.size(),
895 &clsMethods[0], clsMethods.size(), AtEndLoc);
896 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000897 else if (ObjCImplementationDecl *IC =
898 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000899 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000900 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000901 ImplMethodsVsClassMethods(IC, IDecl);
902 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000904 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000905 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000906 // Find category interface decl and then check that all methods declared
907 // in this interface is implemented in the category @implementation.
908 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000909 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000910 Categories; Categories = Categories->getNextClassCategory()) {
911 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
912 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
913 break;
914 }
915 }
916 }
917 }
918}
919
920
921/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
922/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000923static Decl::ObjCDeclQualifier
924CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
925 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
926 if (PQTVal & ObjCDeclSpec::DQ_In)
927 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
928 if (PQTVal & ObjCDeclSpec::DQ_Inout)
929 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
930 if (PQTVal & ObjCDeclSpec::DQ_Out)
931 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
932 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
933 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
934 if (PQTVal & ObjCDeclSpec::DQ_Byref)
935 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
936 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
937 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000938
939 return ret;
940}
941
942Sema::DeclTy *Sema::ActOnMethodDeclaration(
943 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000944 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000945 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000946 Selector Sel,
947 // optional arguments. The number of types/arguments is obtained
948 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000949 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000950 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
951 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000952 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000953
954 // Make sure we can establish a context for the method.
955 if (!ClassDecl) {
956 Diag(MethodLoc, diag::error_missing_method_context);
957 return 0;
958 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000959 QualType resultDeclType;
960
961 if (ReturnType)
962 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
963 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000964 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000965
Chris Lattner114add62008-03-16 00:49:28 +0000966 ObjCMethodDecl* ObjCMethod =
967 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000968 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +0000969 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000970 false,
Chris Lattner114add62008-03-16 00:49:28 +0000971 MethodDeclKind == tok::objc_optional ?
972 ObjCMethodDecl::Optional :
973 ObjCMethodDecl::Required);
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000974 if (AttrList)
975 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner114add62008-03-16 00:49:28 +0000976
Chris Lattnereee57c02008-04-04 06:12:32 +0000977 llvm::SmallVector<ParmVarDecl*, 16> Params;
978
979 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
980 // FIXME: arg->AttrList must be stored too!
981 QualType argType;
982
983 if (ArgTypes[i])
984 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
985 else
986 argType = Context.getObjCIdType();
987 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
988 SourceLocation(/*FIXME*/),
989 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000990 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000991 Param->setObjCDeclQualifier(
992 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
993 Params.push_back(Param);
994 }
995
Ted Kremenek42730c52008-01-07 19:49:32 +0000996 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
997 ObjCMethod->setObjCDeclQualifier(
998 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
999 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001000
1001 // For implementations (which can be very "coarse grain"), we add the
1002 // method now. This allows the AST to implement lookup methods that work
1003 // incrementally (without waiting until we parse the @end). It also allows
1004 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001005 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001006 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001007 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001008 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001009 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001010 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001011 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001012 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001013 }
1014 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001015 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001016 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001017 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001018 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001019 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001020 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001021 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001022 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001023 }
1024 }
1025 if (PrevMethod) {
1026 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001027 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1028 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001029 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1030 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001031 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001032}
1033
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001034Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1035 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001036 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001037 Selector GetterSel,
1038 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001039 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001040 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001041 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1042 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001043 // Regardless of setter/getter attribute, we save the default getter/setter
1044 // selector names in anticipation of declaration of setter/getter methods.
1045 PDecl->setGetterName(GetterSel);
1046 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001047
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001048 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001049 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001050
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001051 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001052 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001053
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001054 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001055 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001056
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001057 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001058 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001059
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001060 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001061 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001062
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001063 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001064 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001065
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001066 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001067 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001068
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001069 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001070 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001071
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001072 if (MethodImplKind == tok::objc_required)
1073 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1074 else if (MethodImplKind == tok::objc_optional)
1075 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1076
Chris Lattner855e51f2007-12-12 07:09:47 +00001077 return PDecl;
1078}
1079
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001080/// ActOnPropertyImplDecl - This routine performs semantic checks and
1081/// builds the AST node for a property implementation declaration; declared
1082/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001083///
1084Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1085 SourceLocation PropertyLoc,
1086 bool Synthesize,
1087 DeclTy *ClassCatImpDecl,
1088 IdentifierInfo *PropertyId,
1089 IdentifierInfo *PropertyIvar) {
1090 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1091 // Make sure we have a context for the property implementation declaration.
1092 if (!ClassImpDecl) {
1093 Diag(AtLoc, diag::error_missing_property_context);
1094 return 0;
1095 }
1096 ObjCPropertyDecl *property = 0;
1097 ObjCInterfaceDecl* IDecl = 0;
1098 // Find the class or category class where this property must have
1099 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001100 ObjCImplementationDecl *IC = 0;
1101 ObjCCategoryImplDecl* CatImplClass = 0;
1102 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001103 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001104 // We always synthesize an interface for an implementation
1105 // without an interface decl. So, IDecl is always non-zero.
1106 assert(IDecl &&
1107 "ActOnPropertyImplDecl - @implementation without @interface");
1108
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001109 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001110 property = IDecl->FindPropertyDeclaration(PropertyId);
1111 if (!property) {
1112 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001113 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001114 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001115 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001116 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001117 if (Synthesize) {
1118 Diag(AtLoc, diag::error_synthesize_category_decl);
1119 return 0;
1120 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001121 IDecl = CatImplClass->getClassInterface();
1122 if (!IDecl) {
1123 Diag(AtLoc, diag::error_missing_property_interface);
1124 return 0;
1125 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001126 ObjCCategoryDecl *Category =
1127 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1128
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001129 // If category for this implementation not found, it is an error which
1130 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001131 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 return 0;
1133 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001134 property = Category->FindPropertyDeclaration(PropertyId);
1135 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001136 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001137 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001138 return 0;
1139 }
1140 }
1141 else {
1142 Diag(AtLoc, diag::error_bad_property_context);
1143 return 0;
1144 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001145 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001146 // Check that we have a valid, previously declared ivar for @synthesize
1147 if (Synthesize) {
1148 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001149 if (!PropertyIvar)
1150 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001151 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001152 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001153 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001154 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1155 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001156 return 0;
1157 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001158 // Check that type of property and its ivar match.
Chris Lattnerc5cff302008-07-26 20:50:02 +00001159 if (Context.getCanonicalType(Ivar->getType()) !=
1160 Context.getCanonicalType(property->getType())) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001161 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1162 Ivar->getName());
1163 return 0;
1164 }
1165
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001166 } else if (PropertyIvar) {
1167 // @dynamic
1168 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1169 return 0;
1170 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001171 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001172 ObjCPropertyImplDecl *PIDecl =
1173 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1174 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001175 ObjCPropertyImplDecl::Synthesize
1176 : ObjCPropertyImplDecl::Dynamic),
1177 Ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001178 if (IC)
1179 IC->addPropertyImplementation(PIDecl);
1180 else
1181 CatImplClass->addPropertyImplementation(PIDecl);
1182
1183 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001184}