blob: e2c58c22a4e67495ac861225d41c67ac5bf842f0 [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 {
101 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +0000102 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000103
Steve Naroff15208162008-04-02 18:30:49 +0000104 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000105 // Remember that this needs to be removed when the scope is popped.
106 TUScope->AddDecl(IDecl);
107 }
108
109 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000111 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000112 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000114 Diag(SuperLoc, diag::err_redefinition_different_kind,
115 SuperName->getName());
116 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
117 }
118 else {
119 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000120 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000121
122 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000123 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000124 SuperClassEntry ? SuperClassEntry->getName()
125 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000126 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000127 }
128 }
129 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000130 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000131 IDecl->setLocEnd(SuperLoc);
132 } else { // we have a root class.
133 IDecl->setLocEnd(ClassLoc);
134 }
135
136 /// Check then save referenced protocols
Chris Lattnerae1ae492008-07-26 04:13:19 +0000137 if (NumProtoRefs) {
138 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 IDecl->setLocEnd(EndProtoLoc);
140 }
141 return IDecl;
142}
143
144/// ActOnCompatiblityAlias - this action is called after complete parsing of
145/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000146Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
147 IdentifierInfo *AliasName,
148 SourceLocation AliasLocation,
149 IdentifierInfo *ClassName,
150 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000151 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000152 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000153 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000154 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000155 Diag(AliasLocation, diag::warn_previous_alias_decl);
156 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
157 }
158 else {
159 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
160 AliasName->getName());
161 Diag(ADecl->getLocation(), diag::err_previous_declaration);
162 }
163 return 0;
164 }
165 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000166 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000167 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
168 if (CDecl == 0) {
169 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
170 if (CDeclU)
171 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000172 return 0;
173 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000174
175 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000176 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000177 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
178
179 ObjCAliasDecls[AliasName] = AliasDecl;
180 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000181 return AliasDecl;
182}
183
Chris Lattner2bdedd62008-07-26 04:03:38 +0000184Sema::DeclTy *
185Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
186 IdentifierInfo *ProtocolName,
187 SourceLocation ProtocolLoc,
188 DeclTy * const *ProtoRefs,
189 unsigned NumProtoRefs,
190 SourceLocation EndProtoLoc) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000191 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000192 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000193 if (PDecl) {
194 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000195 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000196 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
197 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000198 // Just return the protocol we already had.
199 // FIXME: don't leak the objects passed in!
200 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000201 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000202
203 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000204 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000205 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000206 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000207 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000208 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000209
210 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000211 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000212 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000213 PDecl->setLocEnd(EndProtoLoc);
214 }
215 return PDecl;
216}
217
218/// FindProtocolDeclaration - This routine looks up protocols and
219/// issuer error if they are not declared. It returns list of protocol
220/// declarations in its 'Protocols' argument.
221void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000222Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000223 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000224 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000225 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000226 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000227 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
228 if (!PDecl) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000229 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
230 ProtocolId[i].first->getName());
Chris Lattner17d50a92008-07-26 03:47:43 +0000231 continue;
232 }
233
234 // If this is a forward declaration and we are supposed to warn in this
235 // case, do it.
236 if (WarnOnDeclarations && PDecl->isForwardDecl())
237 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
238 ProtocolId[i].first->getName());
239 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000240 }
241}
242
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000243/// DiagnosePropertyMismatch - Compares two properties for their
244/// attributes and types and warns on a variety of inconsistancies.
245///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000246void
247Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
248 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000249 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000250 ObjCPropertyDecl::PropertyAttributeKind CAttr =
251 Property->getPropertyAttributes();
252 ObjCPropertyDecl::PropertyAttributeKind SAttr =
253 SuperProperty->getPropertyAttributes();
254 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
255 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000256 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000257 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000258 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
259 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000260 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000261 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000262 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000263 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
264 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000265 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000266 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000267 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000268
269 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
270 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000271 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000272 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000273 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000274 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000275 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000276 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000277 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000278 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000279 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000280 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000281 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000282
Chris Lattnerc5cff302008-07-26 20:50:02 +0000283 if (Context.getCanonicalType(Property->getType()) !=
284 Context.getCanonicalType(SuperProperty->getType()))
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000285 Diag(Property->getLocation(), diag::warn_property_type,
286 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000287 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000288
289}
290
291/// ComparePropertiesInBaseAndSuper - This routine compares property
292/// declarations in base and its super class, if any, and issues
293/// diagnostics in a variety of inconsistant situations.
294///
295void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000296Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000297 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
298 if (!SDecl)
299 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000300 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
301 E = SDecl->classprop_end(); S != E; ++S) {
302 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000303 // Does property in super class has declaration in current class?
304 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
305 E = IDecl->classprop_end(); I != E; ++I) {
306 ObjCPropertyDecl *PDecl = (*I);
307 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000308 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000309 }
310 }
311}
312
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000313/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
314/// of properties declared in a protocol and adds them to the list
315/// of properties for current class if it is not there already.
316void
317Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
318 ObjCProtocolDecl *PDecl)
319{
320 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
321 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
322 E = PDecl->classprop_end(); P != E; ++P) {
323 ObjCPropertyDecl *Pr = (*P);
324 ObjCInterfaceDecl::classprop_iterator CP, CE;
325 // Is this property already in class's list of properties?
326 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
327 CP != CE; ++CP)
328 if ((*CP)->getIdentifier() == Pr->getIdentifier())
329 break;
330 if (CP == CE)
331 // Add this property to list of properties for thie class.
332 mergeProperties.push_back(Pr);
333 else
334 // Property protocol already exist in class. Diagnose any mismatch.
335 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
336 }
337 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
338}
339
340/// MergeProtocolPropertiesIntoClass - This routine merges properties
341/// declared in 'MergeItsProtocols' objects (which can be a class or an
342/// inherited protocol into the list of properties for class 'IDecl'
343///
344
345void
346Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
347 DeclTy *MergeItsProtocols) {
348 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000349 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000350 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
351 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000352 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000353 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
354
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000355 // Go thru the list of protocols for this class and recursively merge
356 // their properties into this class as well.
357 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
358 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000359 MergeProtocolPropertiesIntoClass(IDecl, *P);
360 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000361 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
362 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
363 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000364 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000365 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000366}
367
Chris Lattner855e51f2007-12-12 07:09:47 +0000368/// ActOnForwardProtocolDeclaration -
369Action::DeclTy *
370Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000371 const IdentifierLocPair *IdentList,
372 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000373 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000374
375 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000376 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000377 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000378 if (PDecl == 0) // Not already seen?
379 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000380
381 Protocols.push_back(PDecl);
382 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000383 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
384 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000385}
386
Chris Lattnere705e5e2008-07-21 22:17:28 +0000387Sema::DeclTy *Sema::
388ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
389 IdentifierInfo *ClassName, SourceLocation ClassLoc,
390 IdentifierInfo *CategoryName,
391 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000392 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000393 unsigned NumProtoRefs,
394 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000395 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000396
Chris Lattnere29dc832008-03-16 20:34:23 +0000397 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000398 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000399 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000400
401 /// Check that class of this category is already completely declared.
402 if (!IDecl || IDecl->isForwardDecl())
403 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000404 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000405 /// Check for duplicate interface declaration for this category
406 ObjCCategoryDecl *CDeclChain;
407 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
408 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000409 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000410 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000411 CategoryName->getName());
412 break;
413 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000414 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000415 if (!CDeclChain)
416 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000417 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000418
419 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000420 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
421 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000422 }
423 return CDecl;
424}
425
426/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000427/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000428/// object.
429Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
430 SourceLocation AtCatImplLoc,
431 IdentifierInfo *ClassName, SourceLocation ClassLoc,
432 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000433 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000434 ObjCCategoryImplDecl *CDecl =
435 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000436 /// Check that class of this category is already completely declared.
437 if (!IDecl || IDecl->isForwardDecl())
438 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
439
440 /// TODO: Check that CatName, category name, is not used in another
441 // implementation.
442 return CDecl;
443}
444
445Sema::DeclTy *Sema::ActOnStartClassImplementation(
446 SourceLocation AtClassImplLoc,
447 IdentifierInfo *ClassName, SourceLocation ClassLoc,
448 IdentifierInfo *SuperClassname,
449 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000450 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000451 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000452 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000454 Diag(ClassLoc, diag::err_redefinition_different_kind,
455 ClassName->getName());
456 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
457 }
458 else {
459 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000460 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000461 if (!IDecl)
462 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
463 }
464
465 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000466 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000467 if (SuperClassname) {
468 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000469 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000470 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000471 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
472 SuperClassname->getName());
473 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
474 }
475 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000476 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000477 if (!SDecl)
478 Diag(SuperClassLoc, diag::err_undef_superclass,
479 SuperClassname->getName(), ClassName->getName());
480 else if (IDecl && IDecl->getSuperClass() != SDecl) {
481 // This implementation and its interface do not have the same
482 // super class.
483 Diag(SuperClassLoc, diag::err_conflicting_super_class,
484 SDecl->getName());
485 Diag(SDecl->getLocation(), diag::err_previous_definition);
486 }
487 }
488 }
489
490 if (!IDecl) {
491 // Legacy case of @implementation with no corresponding @interface.
492 // Build, chain & install the interface decl into the identifier.
Chris Lattner5cece462008-07-21 07:06:49 +0000493 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000494 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000495 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000496 IDecl->setSuperClass(SDecl);
497 IDecl->setLocEnd(ClassLoc);
498
499 // Remember that this needs to be removed when the scope is popped.
500 TUScope->AddDecl(IDecl);
501 }
502
Ted Kremenek42730c52008-01-07 19:49:32 +0000503 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000504 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
505 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000506
507 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000508 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000509 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000510 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
511 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000512 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000513 return IMPDecl;
514}
515
Ted Kremenek42730c52008-01-07 19:49:32 +0000516void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
517 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000518 SourceLocation RBrace) {
519 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000520 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000521 if (!IDecl)
522 return;
523 /// Check case of non-existing @interface decl.
524 /// (legacy objective-c @implementation decl without an @interface decl).
525 /// Add implementations's ivar to the synthesize class's ivar list.
526 if (IDecl->ImplicitInterfaceDecl()) {
527 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
528 return;
529 }
530 // If implementation has empty ivar list, just return.
531 if (numIvars == 0)
532 return;
533
534 assert(ivars && "missing @implementation ivars");
535
536 // Check interface's Ivar list against those in the implementation.
537 // names and types must match.
538 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000539 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000540 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000541 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
542 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000543 ObjCIvarDecl* ImplIvar = ivars[j++];
544 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000545 assert (ImplIvar && "missing implementation ivar");
546 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000547 if (Context.getCanonicalType(ImplIvar->getType()) !=
548 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000549 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
550 ImplIvar->getIdentifier()->getName());
551 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
552 ClsIvar->getIdentifier()->getName());
553 }
554 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
555 // as error.
556 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
557 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
558 ImplIvar->getIdentifier()->getName());
559 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
560 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000561 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000562 }
563 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000564 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000565
566 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000567 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000568 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000569 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000570}
571
Steve Naroffb4f48512008-02-10 21:38:56 +0000572void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
573 bool &IncompleteImpl) {
574 if (!IncompleteImpl) {
575 Diag(ImpLoc, diag::warn_incomplete_impl);
576 IncompleteImpl = true;
577 }
578 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
579}
580
Steve Naroffb268d2a2008-02-08 22:06:17 +0000581/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000582/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000583void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
584 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000586 const llvm::DenseSet<Selector> &InsMap,
587 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000588 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000589 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000590 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000591 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000592 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000593 method->getImplementationControl() != ObjCMethodDecl::Optional)
594 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000595 }
596 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000598 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000599 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000600 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000601 method->getImplementationControl() != ObjCMethodDecl::Optional)
602 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000603 }
Chris Lattner0be08822008-07-21 21:32:27 +0000604 // Check on this protocols's referenced protocols, recursively.
605 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
606 E = PDecl->protocol_end(); PI != E; ++PI)
607 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000608}
609
Ted Kremenek42730c52008-01-07 19:49:32 +0000610void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
611 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000612 llvm::DenseSet<Selector> InsMap;
613 // Check and see if instance methods in class interface have been
614 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000615 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000616 E = IMPDecl->instmeth_end(); I != E; ++I)
617 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000618
619 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000620 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000621 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000622 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000623 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000624
Chris Lattner855e51f2007-12-12 07:09:47 +0000625 llvm::DenseSet<Selector> ClsMap;
626 // Check and see if class methods in class interface have been
627 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000628 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000629 E = IMPDecl->classmeth_end(); I != E; ++I)
630 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000631
Ted Kremenek42730c52008-01-07 19:49:32 +0000632 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000633 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000634 if (!ClsMap.count((*I)->getSelector()))
635 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000636
637 // Check the protocol list for unimplemented methods in the @implementation
638 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000639 const ObjCList<ObjCProtocolDecl> &Protocols =
640 IDecl->getReferencedProtocols();
641 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
642 E = Protocols.end(); I != E; ++I)
643 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000644 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000645}
646
647/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
648/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000649void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
650 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000651 llvm::DenseSet<Selector> InsMap;
652 // Check and see if instance methods in category interface have been
653 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000654 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000655 E = CatImplDecl->instmeth_end(); I != E; ++I)
656 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000657
658 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000659 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000660 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000661 if (!InsMap.count((*I)->getSelector()))
662 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
663
Chris Lattner855e51f2007-12-12 07:09:47 +0000664 llvm::DenseSet<Selector> ClsMap;
665 // Check and see if class methods in category interface have been
666 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000667 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000668 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
669 I != E; ++I)
670 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000671
Ted Kremenek42730c52008-01-07 19:49:32 +0000672 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000673 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000674 if (!ClsMap.count((*I)->getSelector()))
675 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000676
677 // Check the protocol list for unimplemented methods in the @implementation
678 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000679 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
680 E = CatClassDecl->protocol_end(); PI != E; ++PI)
681 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000682 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000683}
684
685/// ActOnForwardClassDeclaration -
686Action::DeclTy *
687Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
688 IdentifierInfo **IdentList, unsigned NumElts)
689{
Ted Kremenek42730c52008-01-07 19:49:32 +0000690 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000691
692 for (unsigned i = 0; i != NumElts; ++i) {
693 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000694 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000695 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000696 // GCC apparently allows the following idiom:
697 //
698 // typedef NSObject < XCElementTogglerP > XCElementToggler;
699 // @class XCElementToggler;
700 //
701 // FIXME: Make an extension?
702 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
703 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
704 Diag(AtClassLoc, diag::err_redefinition_different_kind,
705 IdentList[i]->getName());
706 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
707 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000708 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000709 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000710 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000711 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000712 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000713 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000714
715 // Remember that this needs to be removed when the scope is popped.
716 TUScope->AddDecl(IDecl);
717 }
718
719 Interfaces.push_back(IDecl);
720 }
721
Chris Lattnere29dc832008-03-16 20:34:23 +0000722 return ObjCClassDecl::Create(Context, AtClassLoc,
723 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000724}
725
726
727/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
728/// returns true, or false, accordingly.
729/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000730bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
731 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000732 if (Context.getCanonicalType(Method->getResultType()) !=
733 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000734 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000735 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000736 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
737 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000738 if (Context.getCanonicalType(ParamDecl->getType()) !=
739 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000740 return false;
741 }
742 return true;
743}
744
Ted Kremenek42730c52008-01-07 19:49:32 +0000745void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
746 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000747 if (!FirstMethod.Method) {
748 // Haven't seen a method with this selector name yet - add it.
749 FirstMethod.Method = Method;
750 FirstMethod.Next = 0;
751 } else {
752 // We've seen a method with this name, now check the type signature(s).
753 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
754
Ted Kremenek42730c52008-01-07 19:49:32 +0000755 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000756 Next = Next->Next)
757 match = MatchTwoMethodDeclarations(Method, Next->Method);
758
759 if (!match) {
760 // We have a new signature for an existing method - add it.
761 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000762 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000763 FirstMethod.Next = OMI;
764 }
765 }
766}
767
Ted Kremenek42730c52008-01-07 19:49:32 +0000768void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
769 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000770 if (!FirstMethod.Method) {
771 // Haven't seen a method with this selector name yet - add it.
772 FirstMethod.Method = Method;
773 FirstMethod.Next = 0;
774 } else {
775 // We've seen a method with this name, now check the type signature(s).
776 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
777
Ted Kremenek42730c52008-01-07 19:49:32 +0000778 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000779 Next = Next->Next)
780 match = MatchTwoMethodDeclarations(Method, Next->Method);
781
782 if (!match) {
783 // We have a new signature for an existing method - add it.
784 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000785 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000786 FirstMethod.Next = OMI;
787 }
788 }
789}
790
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000791// Note: For class/category implemenations, allMethods/allProperties is
792// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000793void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
794 DeclTy **allMethods, unsigned allNum,
795 DeclTy **allProperties, unsigned pNum) {
796 Decl *ClassDecl = static_cast<Decl *>(classDecl);
797
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000798 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
799 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000800 // should be true.
801 if (!ClassDecl)
802 return;
803
Ted Kremenek42730c52008-01-07 19:49:32 +0000804 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
805 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000806
Ted Kremenek42730c52008-01-07 19:49:32 +0000807 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
808 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000809
810 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000811 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
812 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000813 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000814
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000815 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000816 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
817 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000818 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
819 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
820 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
821 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000822 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000823 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000824 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000825
826 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000827 ObjCMethodDecl *Method =
828 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000829
830 if (!Method) continue; // Already issued a diagnostic.
831 if (Method->isInstance()) {
832 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000833 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000834 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
835 : false;
836 if (isInterfaceDeclKind && PrevMethod && !match
837 || checkIdenticalMethods && match) {
838 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
839 Method->getSelector().getName());
840 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
841 } else {
842 insMethods.push_back(Method);
843 InsMap[Method->getSelector()] = Method;
844 /// The following allows us to typecheck messages to "id".
845 AddInstanceMethodToGlobalPool(Method);
846 }
847 }
848 else {
849 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000850 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000851 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
852 : false;
853 if (isInterfaceDeclKind && PrevMethod && !match
854 || checkIdenticalMethods && match) {
855 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
856 Method->getSelector().getName());
857 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
858 } else {
859 clsMethods.push_back(Method);
860 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000861 /// The following allows us to typecheck messages to "Class".
862 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000863 }
864 }
865 }
866
Ted Kremenek42730c52008-01-07 19:49:32 +0000867 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000868 // Compares properties declaraed in this class to those of its
869 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000870 ComparePropertiesInBaseAndSuper(I);
871 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000872 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
873 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000874 // FIXME: It would be really nice if we could avoid this. Injecting
875 // methods into the interface makes it hard to distinguish "real" methods
876 // from synthesized "property" methods (that aren't in the source).
877 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000878 I->addPropertyMethods(Context, *P, insMethods);
879 }
880 I->addMethods(&insMethods[0], insMethods.size(),
881 &clsMethods[0], clsMethods.size(), AtEndLoc);
882
Ted Kremenek42730c52008-01-07 19:49:32 +0000883 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000884 P->addMethods(&insMethods[0], insMethods.size(),
885 &clsMethods[0], clsMethods.size(), AtEndLoc);
886 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000887 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000888 C->addMethods(&insMethods[0], insMethods.size(),
889 &clsMethods[0], clsMethods.size(), AtEndLoc);
890 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000891 else if (ObjCImplementationDecl *IC =
892 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000893 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000894 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000895 ImplMethodsVsClassMethods(IC, IDecl);
896 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000897 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000898 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000899 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000900 // Find category interface decl and then check that all methods declared
901 // in this interface is implemented in the category @implementation.
902 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000904 Categories; Categories = Categories->getNextClassCategory()) {
905 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
906 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
907 break;
908 }
909 }
910 }
911 }
912}
913
914
915/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
916/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000917static Decl::ObjCDeclQualifier
918CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
919 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
920 if (PQTVal & ObjCDeclSpec::DQ_In)
921 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
922 if (PQTVal & ObjCDeclSpec::DQ_Inout)
923 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
924 if (PQTVal & ObjCDeclSpec::DQ_Out)
925 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
926 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
927 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
928 if (PQTVal & ObjCDeclSpec::DQ_Byref)
929 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
930 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
931 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000932
933 return ret;
934}
935
936Sema::DeclTy *Sema::ActOnMethodDeclaration(
937 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000938 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000939 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000940 Selector Sel,
941 // optional arguments. The number of types/arguments is obtained
942 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000943 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000944 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
945 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000946 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000947
948 // Make sure we can establish a context for the method.
949 if (!ClassDecl) {
950 Diag(MethodLoc, diag::error_missing_method_context);
951 return 0;
952 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000953 QualType resultDeclType;
954
955 if (ReturnType)
956 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
957 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000958 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000959
Chris Lattner114add62008-03-16 00:49:28 +0000960 ObjCMethodDecl* ObjCMethod =
961 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000962 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000963 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000964 false,
Chris Lattner114add62008-03-16 00:49:28 +0000965 MethodDeclKind == tok::objc_optional ?
966 ObjCMethodDecl::Optional :
967 ObjCMethodDecl::Required);
968
Chris Lattnereee57c02008-04-04 06:12:32 +0000969 llvm::SmallVector<ParmVarDecl*, 16> Params;
970
971 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
972 // FIXME: arg->AttrList must be stored too!
973 QualType argType;
974
975 if (ArgTypes[i])
976 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
977 else
978 argType = Context.getObjCIdType();
979 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
980 SourceLocation(/*FIXME*/),
981 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000982 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000983 Param->setObjCDeclQualifier(
984 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
985 Params.push_back(Param);
986 }
987
Ted Kremenek42730c52008-01-07 19:49:32 +0000988 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
989 ObjCMethod->setObjCDeclQualifier(
990 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
991 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000992
993 // For implementations (which can be very "coarse grain"), we add the
994 // method now. This allows the AST to implement lookup methods that work
995 // incrementally (without waiting until we parse the @end). It also allows
996 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +0000997 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +0000998 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000999 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001000 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001001 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001002 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001003 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001004 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001005 }
1006 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001007 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001008 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001009 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001010 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001011 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001012 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001013 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001014 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001015 }
1016 }
1017 if (PrevMethod) {
1018 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001019 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1020 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001021 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1022 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001023 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001024}
1025
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001026Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1027 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001028 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001029 Selector GetterSel,
1030 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001031 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001032 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001033 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1034 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001035 // Regardless of setter/getter attribute, we save the default getter/setter
1036 // selector names in anticipation of declaration of setter/getter methods.
1037 PDecl->setGetterName(GetterSel);
1038 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001039
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001040 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001041 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001042
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001043 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001044 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001045
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001046 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001047 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001048
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001049 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001050 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001051
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001052 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001053 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001054
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001055 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001056 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001057
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001058 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001059 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001060
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001061 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001062 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001063
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001064 if (MethodImplKind == tok::objc_required)
1065 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1066 else if (MethodImplKind == tok::objc_optional)
1067 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1068
Chris Lattner855e51f2007-12-12 07:09:47 +00001069 return PDecl;
1070}
1071
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001072/// ActOnPropertyImplDecl - This routine performs semantic checks and
1073/// builds the AST node for a property implementation declaration; declared
1074/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001075///
1076Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1077 SourceLocation PropertyLoc,
1078 bool Synthesize,
1079 DeclTy *ClassCatImpDecl,
1080 IdentifierInfo *PropertyId,
1081 IdentifierInfo *PropertyIvar) {
1082 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1083 // Make sure we have a context for the property implementation declaration.
1084 if (!ClassImpDecl) {
1085 Diag(AtLoc, diag::error_missing_property_context);
1086 return 0;
1087 }
1088 ObjCPropertyDecl *property = 0;
1089 ObjCInterfaceDecl* IDecl = 0;
1090 // Find the class or category class where this property must have
1091 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001092 ObjCImplementationDecl *IC = 0;
1093 ObjCCategoryImplDecl* CatImplClass = 0;
1094 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001095 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001096 // We always synthesize an interface for an implementation
1097 // without an interface decl. So, IDecl is always non-zero.
1098 assert(IDecl &&
1099 "ActOnPropertyImplDecl - @implementation without @interface");
1100
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001101 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001102 property = IDecl->FindPropertyDeclaration(PropertyId);
1103 if (!property) {
1104 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001105 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001106 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001107 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001108 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001109 if (Synthesize) {
1110 Diag(AtLoc, diag::error_synthesize_category_decl);
1111 return 0;
1112 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001113 IDecl = CatImplClass->getClassInterface();
1114 if (!IDecl) {
1115 Diag(AtLoc, diag::error_missing_property_interface);
1116 return 0;
1117 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001118 ObjCCategoryDecl *Category =
1119 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1120
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001121 // If category for this implementation not found, it is an error which
1122 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001123 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001124 return 0;
1125 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001126 property = Category->FindPropertyDeclaration(PropertyId);
1127 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001128 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001129 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001130 return 0;
1131 }
1132 }
1133 else {
1134 Diag(AtLoc, diag::error_bad_property_context);
1135 return 0;
1136 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001137 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001138 // Check that we have a valid, previously declared ivar for @synthesize
1139 if (Synthesize) {
1140 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001141 if (!PropertyIvar)
1142 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001143 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001144 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001145 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001146 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1147 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001148 return 0;
1149 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001150 // Check that type of property and its ivar match.
Chris Lattnerc5cff302008-07-26 20:50:02 +00001151 if (Context.getCanonicalType(Ivar->getType()) !=
1152 Context.getCanonicalType(property->getType())) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001153 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1154 Ivar->getName());
1155 return 0;
1156 }
1157
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001158 } else if (PropertyIvar) {
1159 // @dynamic
1160 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1161 return 0;
1162 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001163 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001164 ObjCPropertyImplDecl *PIDecl =
1165 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1166 (Synthesize ?
1167 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1168 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1169 Ivar);
1170 if (IC)
1171 IC->addPropertyImplementation(PIDecl);
1172 else
1173 CatImplClass->addPropertyImplementation(PIDecl);
1174
1175 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001176}