blob: a5ffb7090565f3e47b1ae575c527ce18271cba22 [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.
Chris Lattner855e51f2007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Daniel Dunbareaf91c32008-08-26 06:07:48 +000045 MDecl->createImplicitParams(Context);
Chris Lattner855e51f2007-12-12 07:09:47 +000046
Daniel Dunbareaf91c32008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000049
Chris Lattner97316c02008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000056 }
57}
58
Chris Lattnere705e5e2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Douglas Gregordd861062008-12-05 18:15:24 +000069 if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
70 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremenek42730c52008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +000079 }
80
Ted Kremenek42730c52008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattner271d4c22008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattner5b250652008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroff1422a622008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
90 return IDecl;
91 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000094 }
Chris Lattner5cece462008-07-21 07:06:49 +000095 } else {
Daniel Dunbard8bd6822008-08-20 18:02:42 +000096 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000100
Steve Naroff15208162008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
103 TUScope->AddDecl(IDecl);
104 }
105
106 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000107 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000109 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000111 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000112 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 }
114 else {
115 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000116 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner65cae292008-11-19 08:23:25 +0000117
118 if (!SuperClassEntry)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000119 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000120 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
121 else if (SuperClassEntry->isForwardDecl())
122 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattnerb1753422008-11-23 21:45:46 +0000123 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000124 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000125 }
126 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000127 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000128 IDecl->setLocEnd(SuperLoc);
129 } else { // we have a root class.
130 IDecl->setLocEnd(ClassLoc);
131 }
132
Steve Naroff1422a622008-11-18 19:15:30 +0000133 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000134 if (NumProtoRefs) {
135 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000136 IDecl->setLocEnd(EndProtoLoc);
137 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000138
139 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000140 return IDecl;
141}
142
143/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000144/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000145Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
146 IdentifierInfo *AliasName,
147 SourceLocation AliasLocation,
148 IdentifierInfo *ClassName,
149 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000150 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000151 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000152 if (ADecl) {
Chris Lattnerb13cb562008-11-23 23:20:13 +0000153 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner855e51f2007-12-12 07:09:47 +0000154 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattnerb13cb562008-11-23 23:20:13 +0000155 else
Chris Lattner65cae292008-11-19 08:23:25 +0000156 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerb13cb562008-11-23 23:20:13 +0000157 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000158 return 0;
159 }
160 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000161 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000162 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
163 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000164 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000165 if (CDeclU)
Chris Lattnerb13cb562008-11-23 23:20:13 +0000166 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000167 return 0;
168 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000169
170 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000171 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000172 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
173
174 ObjCAliasDecls[AliasName] = AliasDecl;
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000175
176 if (!CheckObjCDeclScope(AliasDecl))
177 TUScope->AddDecl(AliasDecl);
178
Chris Lattner855e51f2007-12-12 07:09:47 +0000179 return AliasDecl;
180}
181
Chris Lattner2bdedd62008-07-26 04:03:38 +0000182Sema::DeclTy *
183Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
184 IdentifierInfo *ProtocolName,
185 SourceLocation ProtocolLoc,
186 DeclTy * const *ProtoRefs,
187 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000188 SourceLocation EndProtoLoc,
189 AttributeList *AttrList) {
190 // FIXME: Deal with AttrList.
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 Lattner65cae292008-11-19 08:23:25 +0000196 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000197 Diag(PDecl->getLocation(), diag::note_previous_definition);
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 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000202 // Make sure the cached decl gets a valid start location.
203 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000204 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000205 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000206 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000207 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000208 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000209 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000210
211 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000212 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000213 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000214 PDecl->setLocEnd(EndProtoLoc);
215 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000216
217 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000218 return PDecl;
219}
220
221/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000222/// issues an error if they are not declared. It returns list of
223/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000224void
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 Lattner8ba580c2008-11-19 05:08:23 +0000232 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000233 << ProtocolId[i].first;
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())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000240 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000241 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000242 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
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000247/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000248///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000249void
250Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
251 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000252 const IdentifierInfo *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))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000259 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000260 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000261 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
262 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000263 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000264 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000265 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
266 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000267 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000268 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000269
270 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000272 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000273 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000274 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000275 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000276 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000277 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000278 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000279 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000280
Chris Lattnerc5cff302008-07-26 20:50:02 +0000281 if (Context.getCanonicalType(Property->getType()) !=
282 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000283 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000284 << Property->getType() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000285
286}
287
288/// ComparePropertiesInBaseAndSuper - This routine compares property
289/// declarations in base and its super class, if any, and issues
290/// diagnostics in a variety of inconsistant situations.
291///
292void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000293Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000294 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
295 if (!SDecl)
296 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000297 // FIXME: O(N^2)
Fariborz Jahanianed986602008-05-01 00:03:38 +0000298 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
299 E = SDecl->classprop_end(); S != E; ++S) {
300 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000301 // Does property in super class has declaration in current class?
302 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
303 E = IDecl->classprop_end(); I != E; ++I) {
304 ObjCPropertyDecl *PDecl = (*I);
305 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000306 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000307 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000308 }
309 }
310}
311
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000312/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
313/// of properties declared in a protocol and adds them to the list
314/// of properties for current class if it is not there already.
315void
316Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000317 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000318 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
319 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
320 E = PDecl->classprop_end(); P != E; ++P) {
321 ObjCPropertyDecl *Pr = (*P);
322 ObjCInterfaceDecl::classprop_iterator CP, CE;
323 // Is this property already in class's list of properties?
324 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
325 CP != CE; ++CP)
326 if ((*CP)->getIdentifier() == Pr->getIdentifier())
327 break;
328 if (CP == CE)
329 // Add this property to list of properties for thie class.
330 mergeProperties.push_back(Pr);
331 else
332 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000333 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000334 }
335 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
336}
337
338/// MergeProtocolPropertiesIntoClass - This routine merges properties
339/// declared in 'MergeItsProtocols' objects (which can be a class or an
340/// inherited protocol into the list of properties for class 'IDecl'
341///
342
343void
344Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
345 DeclTy *MergeItsProtocols) {
346 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000347 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000348 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
349 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000350 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000351 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
352
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000353 // Go thru the list of protocols for this class and recursively merge
354 // their properties into this class as well.
355 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
356 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000357 MergeProtocolPropertiesIntoClass(IDecl, *P);
358 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000359 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
360 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
361 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000362 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000363 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000364}
365
Chris Lattner855e51f2007-12-12 07:09:47 +0000366/// ActOnForwardProtocolDeclaration -
367Action::DeclTy *
368Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000369 const IdentifierLocPair *IdentList,
370 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000371 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000372
373 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000374 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000375 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000376 if (PDecl == 0) // Not already seen?
377 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000378
379 Protocols.push_back(PDecl);
380 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000381
382 ObjCForwardProtocolDecl *PDecl =
383 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
384 &Protocols[0], Protocols.size());
385
386 CheckObjCDeclScope(PDecl);
387 return PDecl;
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())
Chris Lattner65cae292008-11-19 08:23:25 +0000406 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
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) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000413 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner65cae292008-11-19 08:23:25 +0000414 << ClassName << CategoryName;
Chris Lattnerde1bd982008-11-23 22:38:38 +0000415 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000416 break;
417 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000418 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000419 if (!CDeclChain)
420 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000421 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000422
423 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000424 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
425 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000426 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000427
428 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000429 return CDecl;
430}
431
432/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000433/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000434/// object.
435Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
436 SourceLocation AtCatImplLoc,
437 IdentifierInfo *ClassName, SourceLocation ClassLoc,
438 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000439 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000440 ObjCCategoryImplDecl *CDecl =
441 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000442 /// Check that class of this category is already completely declared.
443 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000444 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000445
446 /// TODO: Check that CatName, category name, is not used in another
447 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000448 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000449
450 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000451 return CDecl;
452}
453
454Sema::DeclTy *Sema::ActOnStartClassImplementation(
455 SourceLocation AtClassImplLoc,
456 IdentifierInfo *ClassName, SourceLocation ClassLoc,
457 IdentifierInfo *SuperClassname,
458 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000459 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000460 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000461 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000462 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000463 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000464 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000465 }
466 else {
467 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000468 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000469 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000470 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000471 }
472
473 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000474 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000475 if (SuperClassname) {
476 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000477 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000478 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000479 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
480 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000481 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000482 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000483 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000484 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000485 Diag(SuperClassLoc, diag::err_undef_superclass)
486 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000487 else if (IDecl && IDecl->getSuperClass() != SDecl) {
488 // This implementation and its interface do not have the same
489 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000490 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000491 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000492 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000493 }
494 }
495 }
496
497 if (!IDecl) {
498 // Legacy case of @implementation with no corresponding @interface.
499 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000500
501 // FIXME: Do we support attributes on the @implementation? If so
502 // we should copy them over.
Chris Lattner5cece462008-07-21 07:06:49 +0000503 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000504 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000505 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000506 IDecl->setSuperClass(SDecl);
507 IDecl->setLocEnd(ClassLoc);
508
509 // Remember that this needs to be removed when the scope is popped.
510 TUScope->AddDecl(IDecl);
511 }
512
Ted Kremenek42730c52008-01-07 19:49:32 +0000513 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000514 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
515 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000516
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000517 if (CheckObjCDeclScope(IMPDecl))
518 return IMPDecl;
519
Chris Lattner855e51f2007-12-12 07:09:47 +0000520 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000521 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000522 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000523 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000524 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000525 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000526 return IMPDecl;
527}
528
Ted Kremenek42730c52008-01-07 19:49:32 +0000529void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
530 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000531 SourceLocation RBrace) {
532 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000533 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000534 if (!IDecl)
535 return;
536 /// Check case of non-existing @interface decl.
537 /// (legacy objective-c @implementation decl without an @interface decl).
538 /// Add implementations's ivar to the synthesize class's ivar list.
539 if (IDecl->ImplicitInterfaceDecl()) {
540 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
541 return;
542 }
543 // If implementation has empty ivar list, just return.
544 if (numIvars == 0)
545 return;
546
547 assert(ivars && "missing @implementation ivars");
548
549 // Check interface's Ivar list against those in the implementation.
550 // names and types must match.
551 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000552 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000553 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000554 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
555 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000556 ObjCIvarDecl* ImplIvar = ivars[j++];
557 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000558 assert (ImplIvar && "missing implementation ivar");
559 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000560 if (Context.getCanonicalType(ImplIvar->getType()) !=
561 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000562 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000563 << ImplIvar->getIdentifier()
564 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000565 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000566 }
567 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
568 // as error.
569 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000570 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000571 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000572 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000573 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000574 }
575 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000576 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000577
578 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000579 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000580 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000581 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000582}
583
Steve Naroffb4f48512008-02-10 21:38:56 +0000584void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
585 bool &IncompleteImpl) {
586 if (!IncompleteImpl) {
587 Diag(ImpLoc, diag::warn_incomplete_impl);
588 IncompleteImpl = true;
589 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000590 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000591}
592
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000593void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
594 ObjCMethodDecl *IntfMethodDecl) {
595 bool err = false;
596 QualType ImpMethodQType =
597 Context.getCanonicalType(ImpMethodDecl->getResultType());
598 QualType IntfMethodQType =
599 Context.getCanonicalType(IntfMethodDecl->getResultType());
600 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
601 err = true;
602 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
603 IF=IntfMethodDecl->param_begin(),
604 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
605 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
606 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
607 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
608 err = true;
609 break;
610 }
611 }
612 if (err) {
613 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
614 << ImpMethodDecl->getDeclName();
615 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
616 }
617}
618
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000619/// FIXME: Type hierarchies in Objective-C can be deep. We could most
620/// likely improve the efficiency of selector lookups and type
621/// checking by associating with each protocol / interface / category
622/// the flattened instance tables. If we used an immutable set to keep
623/// the table then it wouldn't add significant memory cost and it
624/// would be handy for lookups.
625
Steve Naroffb268d2a2008-02-08 22:06:17 +0000626/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000627/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000628void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
629 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000630 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000631 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000632 const llvm::DenseSet<Selector> &ClsMap,
633 ObjCInterfaceDecl *IDecl) {
634 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
635
636 // If a method lookup fails locally we still need to look and see if
637 // the method was implemented by a base class or an inherited
638 // protocol. This lookup is slow, but occurs rarely in correct code
639 // and otherwise would terminate in a warning.
640
Chris Lattner855e51f2007-12-12 07:09:47 +0000641 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000642 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000643 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000644 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000645 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000646 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000647 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000648 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000649 }
650 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000651 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000652 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000653 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000654 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
655 !ClsMap.count(method->getSelector()) &&
656 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000657 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000658 }
Chris Lattner0be08822008-07-21 21:32:27 +0000659 // Check on this protocols's referenced protocols, recursively.
660 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
661 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000662 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000663}
664
Ted Kremenek42730c52008-01-07 19:49:32 +0000665void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
666 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000667 llvm::DenseSet<Selector> InsMap;
668 // Check and see if instance methods in class interface have been
669 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000670 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000671 E = IMPDecl->instmeth_end(); I != E; ++I)
672 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000673
674 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000675 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000676 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000677 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000678 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000679 else if (!(*I)->isSynthesized()){
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000680 ObjCMethodDecl *ImpMethodDecl =
681 IMPDecl->getInstanceMethod((*I)->getSelector());
682 ObjCMethodDecl *IntfMethodDecl =
683 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000684 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
685
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000686 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000687
Chris Lattner855e51f2007-12-12 07:09:47 +0000688 llvm::DenseSet<Selector> ClsMap;
689 // Check and see if class methods in class interface have been
690 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000691 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000692 E = IMPDecl->classmeth_end(); I != E; ++I)
693 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000694
Ted Kremenek42730c52008-01-07 19:49:32 +0000695 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000696 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000697 if (!ClsMap.count((*I)->getSelector()))
698 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000699 else {
700 ObjCMethodDecl *ImpMethodDecl =
701 IMPDecl->getClassMethod((*I)->getSelector());
702 ObjCMethodDecl *IntfMethodDecl =
703 IDecl->getClassMethod((*I)->getSelector());
704 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
705 }
706
Chris Lattner855e51f2007-12-12 07:09:47 +0000707
708 // Check the protocol list for unimplemented methods in the @implementation
709 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000710 const ObjCList<ObjCProtocolDecl> &Protocols =
711 IDecl->getReferencedProtocols();
712 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
713 E = Protocols.end(); I != E; ++I)
714 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000715 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000716}
717
718/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000719/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000720void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
721 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000722 llvm::DenseSet<Selector> InsMap;
723 // Check and see if instance methods in category interface have been
724 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000725 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000726 E = CatImplDecl->instmeth_end(); I != E; ++I)
727 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000728
729 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000730 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000731 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000732 if (!InsMap.count((*I)->getSelector()))
733 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000734 else {
735 ObjCMethodDecl *ImpMethodDecl =
736 CatImplDecl->getInstanceMethod((*I)->getSelector());
737 ObjCMethodDecl *IntfMethodDecl =
738 CatClassDecl->getInstanceMethod((*I)->getSelector());
739 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
740 }
Steve Naroffb4f48512008-02-10 21:38:56 +0000741
Chris Lattner855e51f2007-12-12 07:09:47 +0000742 llvm::DenseSet<Selector> ClsMap;
743 // Check and see if class methods in category interface have been
744 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000745 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000746 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
747 I != E; ++I)
748 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000749
Ted Kremenek42730c52008-01-07 19:49:32 +0000750 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000751 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000752 if (!ClsMap.count((*I)->getSelector()))
753 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000754 else {
755 ObjCMethodDecl *ImpMethodDecl =
756 CatImplDecl->getClassMethod((*I)->getSelector());
757 ObjCMethodDecl *IntfMethodDecl =
758 CatClassDecl->getClassMethod((*I)->getSelector());
759 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
760 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000761 // Check the protocol list for unimplemented methods in the @implementation
762 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000763 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
764 E = CatClassDecl->protocol_end(); PI != E; ++PI)
765 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000766 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000767}
768
769/// ActOnForwardClassDeclaration -
770Action::DeclTy *
771Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
772 IdentifierInfo **IdentList, unsigned NumElts)
773{
Ted Kremenek42730c52008-01-07 19:49:32 +0000774 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000775
776 for (unsigned i = 0; i != NumElts; ++i) {
777 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000778 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregordd861062008-12-05 18:15:24 +0000779 if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
780 // Maybe we will complain about the shadowed template parameter.
781 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
782 // Just pretend that we didn't see the previous declaration.
783 PrevDecl = 0;
784 }
785
Ted Kremenek42730c52008-01-07 19:49:32 +0000786 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000787 // GCC apparently allows the following idiom:
788 //
789 // typedef NSObject < XCElementTogglerP > XCElementToggler;
790 // @class XCElementToggler;
791 //
792 // FIXME: Make an extension?
793 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
794 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000795 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000796 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000797 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000798 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000799 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000800 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000801 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000802 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000803 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000804
805 // Remember that this needs to be removed when the scope is popped.
806 TUScope->AddDecl(IDecl);
807 }
808
809 Interfaces.push_back(IDecl);
810 }
811
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000812 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
813 &Interfaces[0],
814 Interfaces.size());
815
816 CheckObjCDeclScope(CDecl);
817 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000818}
819
820
821/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
822/// returns true, or false, accordingly.
823/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000824bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000825 const ObjCMethodDecl *PrevMethod,
826 bool matchBasedOnSizeAndAlignment) {
827 QualType T1 = Context.getCanonicalType(Method->getResultType());
828 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
829
830 if (T1 != T2) {
831 // The result types are different.
832 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000833 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000834 // Incomplete types don't have a size and alignment.
835 if (T1->isIncompleteType() || T2->isIncompleteType())
836 return false;
837 // Check is based on size and alignment.
838 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
839 return false;
840 }
841 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
842 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
843 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
844 if (T1 != T2) {
845 // The result types are different.
846 if (!matchBasedOnSizeAndAlignment)
847 return false;
848 // Incomplete types don't have a size and alignment.
849 if (T1->isIncompleteType() || T2->isIncompleteType())
850 return false;
851 // Check is based on size and alignment.
852 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
853 return false;
854 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000855 }
856 return true;
857}
858
Ted Kremenek42730c52008-01-07 19:49:32 +0000859void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
860 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000861 if (!FirstMethod.Method) {
862 // Haven't seen a method with this selector name yet - add it.
863 FirstMethod.Method = Method;
864 FirstMethod.Next = 0;
865 } else {
866 // We've seen a method with this name, now check the type signature(s).
867 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
868
Ted Kremenek42730c52008-01-07 19:49:32 +0000869 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000870 Next = Next->Next)
871 match = MatchTwoMethodDeclarations(Method, Next->Method);
872
873 if (!match) {
874 // We have a new signature for an existing method - add it.
875 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +0000876 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +0000877 }
878 }
879}
880
Steve Naroffec7e0882008-10-21 10:50:19 +0000881// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +0000882ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
883 SourceRange R) {
884 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +0000885 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +0000886
887 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +0000888 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
889 // This checks if the methods differ by size & alignment.
890 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
891 issueWarning = true;
892 }
893 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +0000894 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +0000895 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000896 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000897 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +0000898 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000899 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000900 }
901 return MethList.Method;
902}
903
Ted Kremenek42730c52008-01-07 19:49:32 +0000904void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
905 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000906 if (!FirstMethod.Method) {
907 // Haven't seen a method with this selector name yet - add it.
908 FirstMethod.Method = Method;
909 FirstMethod.Next = 0;
910 } else {
911 // We've seen a method with this name, now check the type signature(s).
912 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
913
Ted Kremenek42730c52008-01-07 19:49:32 +0000914 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000915 Next = Next->Next)
916 match = MatchTwoMethodDeclarations(Method, Next->Method);
917
918 if (!match) {
919 // We have a new signature for an existing method - add it.
920 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000921 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000922 FirstMethod.Next = OMI;
923 }
924 }
925}
926
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000927/// diagnosePropertySetterGetterMismatch - Make sure that use-defined
928/// setter/getter methods have the property type and issue diagnostics
929/// if they don't.
930///
931void
932Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property,
933 const ObjCMethodDecl *GetterMethod,
934 const ObjCMethodDecl *SetterMethod) {
935 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +0000936 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000937 Diag(property->getLocation(),
938 diag::err_accessor_property_type_mismatch)
939 << property->getDeclName()
940 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +0000941 Diag(GetterMethod->getLocation(), diag::note_declared_at);
942 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000943
944 if (SetterMethod) {
Fariborz Jahaniana9307bb2008-12-06 21:11:37 +0000945 if (SetterMethod->getResultType() != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000946 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
947 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian88a62932008-12-06 21:48:16 +0000948 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000949 Diag(property->getLocation(),
950 diag::err_accessor_property_type_mismatch)
951 << property->getDeclName()
952 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +0000953 Diag(SetterMethod->getLocation(), diag::note_declared_at);
954 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000955 }
956}
957
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000958// Note: For class/category implemenations, allMethods/allProperties is
959// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000960void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
961 DeclTy **allMethods, unsigned allNum,
962 DeclTy **allProperties, unsigned pNum) {
963 Decl *ClassDecl = static_cast<Decl *>(classDecl);
964
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000965 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
966 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000967 // should be true.
968 if (!ClassDecl)
969 return;
970
Ted Kremenek42730c52008-01-07 19:49:32 +0000971 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
972 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000973
Ted Kremenek42730c52008-01-07 19:49:32 +0000974 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
975 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000976
977 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000978 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
979 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000980 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000981
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000982 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000983 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
984 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000985 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
986 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
987 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000988 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000989 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000990 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000991 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000992
993 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000994 ObjCMethodDecl *Method =
995 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000996
997 if (!Method) continue; // Already issued a diagnostic.
998 if (Method->isInstance()) {
999 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001000 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001001 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1002 : false;
1003 if (isInterfaceDeclKind && PrevMethod && !match
1004 || checkIdenticalMethods && match) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001005 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001006 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001007 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001008 } else {
1009 insMethods.push_back(Method);
1010 InsMap[Method->getSelector()] = Method;
1011 /// The following allows us to typecheck messages to "id".
1012 AddInstanceMethodToGlobalPool(Method);
1013 }
1014 }
1015 else {
1016 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001017 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001018 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1019 : false;
1020 if (isInterfaceDeclKind && PrevMethod && !match
1021 || checkIdenticalMethods && match) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001022 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001023 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001024 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001025 } else {
1026 clsMethods.push_back(Method);
1027 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001028 /// The following allows us to typecheck messages to "Class".
1029 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001030 }
1031 }
1032 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001033 // Save the size so we can detect if we've added any property methods.
1034 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1035 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner855e51f2007-12-12 07:09:47 +00001036
Ted Kremenek42730c52008-01-07 19:49:32 +00001037 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001038 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001039 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001040 ComparePropertiesInBaseAndSuper(I);
1041 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001042 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001043 e = I->classprop_end(); i != e; ++i) {
1044 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1045 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001046 I->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001047 }
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001048 I->addMethods(&insMethods[0], insMethods.size(),
1049 &clsMethods[0], clsMethods.size(), AtEndLoc);
1050
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001052 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001053 e = P->classprop_end(); i != e; ++i) {
1054 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1055 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001056 P->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001057 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001058 P->addMethods(&insMethods[0], insMethods.size(),
1059 &clsMethods[0], clsMethods.size(), AtEndLoc);
1060 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001061 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001062 // Categories are used to extend the class by declaring new methods.
1063 // By the same token, they are also used to add new properties. No
1064 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001065
1066 // FIXME: If we merge properties into class we should probably
1067 // merge them into category as well?
1068 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001069 e = C->classprop_end(); i != e; ++i) {
1070 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1071 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001072 C->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001073 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001074 C->addMethods(&insMethods[0], insMethods.size(),
1075 &clsMethods[0], clsMethods.size(), AtEndLoc);
1076 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001077 else if (ObjCImplementationDecl *IC =
1078 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001079 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001080 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +00001081 ImplMethodsVsClassMethods(IC, IDecl);
1082 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +00001083 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001084 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001085 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +00001086 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001087 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +00001088 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001089 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001090 Categories; Categories = Categories->getNextClassCategory()) {
1091 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1092 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1093 break;
1094 }
1095 }
1096 }
1097 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001098 // Add any synthesized methods to the global pool. This allows us to
1099 // handle the following, which is supported by GCC (and part of the design).
1100 //
1101 // @interface Foo
1102 // @property double bar;
1103 // @end
1104 //
1105 // void thisIsUnfortunate() {
1106 // id foo;
1107 // double bar = [foo bar];
1108 // }
1109 //
1110 if (insMethodsSizePriorToPropAdds < insMethods.size())
1111 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1112 AddInstanceMethodToGlobalPool(insMethods[i]);
1113 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1114 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1115 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner855e51f2007-12-12 07:09:47 +00001116}
1117
1118
1119/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1120/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001121static Decl::ObjCDeclQualifier
1122CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1123 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1124 if (PQTVal & ObjCDeclSpec::DQ_In)
1125 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1126 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1127 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1128 if (PQTVal & ObjCDeclSpec::DQ_Out)
1129 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1130 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1131 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1132 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1133 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1134 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1135 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001136
1137 return ret;
1138}
1139
1140Sema::DeclTy *Sema::ActOnMethodDeclaration(
1141 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001142 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001143 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001144 Selector Sel,
1145 // optional arguments. The number of types/arguments is obtained
1146 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001147 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +00001148 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1149 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001150 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001151
1152 // Make sure we can establish a context for the method.
1153 if (!ClassDecl) {
1154 Diag(MethodLoc, diag::error_missing_method_context);
1155 return 0;
1156 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001157 QualType resultDeclType;
1158
1159 if (ReturnType)
1160 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1161 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001162 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001163
Chris Lattner114add62008-03-16 00:49:28 +00001164 ObjCMethodDecl* ObjCMethod =
1165 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +00001166 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +00001167 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001168 false,
Chris Lattner114add62008-03-16 00:49:28 +00001169 MethodDeclKind == tok::objc_optional ?
1170 ObjCMethodDecl::Optional :
1171 ObjCMethodDecl::Required);
1172
Chris Lattnereee57c02008-04-04 06:12:32 +00001173 llvm::SmallVector<ParmVarDecl*, 16> Params;
1174
1175 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1176 // FIXME: arg->AttrList must be stored too!
1177 QualType argType;
1178
1179 if (ArgTypes[i])
1180 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1181 else
1182 argType = Context.getObjCIdType();
1183 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1184 SourceLocation(/*FIXME*/),
1185 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001186 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001187 Param->setObjCDeclQualifier(
1188 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1189 Params.push_back(Param);
1190 }
1191
Ted Kremenek42730c52008-01-07 19:49:32 +00001192 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1193 ObjCMethod->setObjCDeclQualifier(
1194 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1195 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001196
1197 if (AttrList)
1198 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001199
1200 // For implementations (which can be very "coarse grain"), we add the
1201 // method now. This allows the AST to implement lookup methods that work
1202 // incrementally (without waiting until we parse the @end). It also allows
1203 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001204 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001205 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001206 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001207 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001208 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001209 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001210 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001211 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001212 }
1213 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001214 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001215 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001216 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001217 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001218 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001219 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001220 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001221 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001222 }
1223 }
1224 if (PrevMethod) {
1225 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001226 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001227 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001228 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001229 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001230 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001231}
1232
Daniel Dunbar540ff472008-09-23 21:53:23 +00001233void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1234 SourceLocation Loc,
1235 unsigned &Attributes) {
1236 // FIXME: Improve the reported location.
1237
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001238 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001239 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001240 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1241 ObjCDeclSpec::DQ_PR_assign |
1242 ObjCDeclSpec::DQ_PR_copy |
1243 ObjCDeclSpec::DQ_PR_retain))) {
1244 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1245 "readwrite" :
1246 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1247 "assign" :
1248 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1249 "copy" : "retain";
1250
Chris Lattner8d756812008-11-20 06:13:02 +00001251 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001252 << "readonly" << which;
1253 return;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001254 }
1255
1256 // Check for copy or retain on non-object types.
1257 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1258 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001259 Diag(Loc, diag::err_objc_property_requires_object)
1260 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001261 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1262 }
1263
1264 // Check for more than one of { assign, copy, retain }.
1265 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1266 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001267 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1268 << "assign" << "copy";
1269 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001270 }
1271 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001272 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1273 << "assign" << "retain";
1274 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001275 }
1276 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1277 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001278 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1279 << "copy" << "retain";
1280 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001281 }
1282 }
1283
1284 // Warn if user supplied no assignment attribute, property is
1285 // readwrite, and this is an object type.
1286 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1287 ObjCDeclSpec::DQ_PR_retain)) &&
1288 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1289 Context.isObjCObjectPointerType(PropertyTy)) {
1290 // Skip this warning in gc-only mode.
1291 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1292 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1293
1294 // If non-gc code warn that this is likely inappropriate.
1295 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1296 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1297
1298 // FIXME: Implement warning dependent on NSCopying being
1299 // implemented. See also:
1300 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1301 // (please trim this list while you are at it).
1302 }
1303}
1304
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001305Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1306 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001307 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001308 Selector GetterSel,
1309 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001310 DeclTy *ClassCategory,
1311 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001312 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001313 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001314 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1315 // default is readwrite!
1316 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1317 // property is defaulted to 'assign' if it is readwrite and is
1318 // not retain or copy
1319 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1320 (isReadWrite &&
1321 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1322 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1323 QualType T = GetTypeForDeclarator(FD.D, S);
1324 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001325
1326 // May modify Attributes.
1327 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001328
1329 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1330 if (!CDecl->getIdentifier()) {
1331 // This is an anonymous category. property requires special
1332 // handling.
1333 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1334 if (ObjCPropertyDecl *PIDecl =
1335 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1336 // property 'PIDecl's readonly attribute will be over-ridden
1337 // with anonymous category's readwrite property attribute!
1338 unsigned PIkind = PIDecl->getPropertyAttributes();
1339 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
1340 if ((Attributes & ObjCPropertyDecl::OBJC_PR_retain) !=
1341 (PIkind & ObjCPropertyDecl::OBJC_PR_retain) ||
1342 (Attributes & ObjCPropertyDecl::OBJC_PR_copy) !=
1343 (PIkind & ObjCPropertyDecl::OBJC_PR_copy) ||
1344 (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
1345 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1346 Diag(AtLoc, diag::warn_property_attr_mismatch);
1347 PIDecl->makeitReadWriteAttribute();
1348 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1349 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1350 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1351 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1352 PIDecl->setSetterName(SetterSel);
1353 // FIXME: use a common routine with addPropertyMethods.
1354 ObjCMethodDecl *SetterDecl =
1355 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1356 Context.VoidTy,
1357 ICDecl,
1358 true, false, true,
1359 ObjCMethodDecl::Required);
1360 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1361 SetterDecl,
1362 SourceLocation(),
1363 FD.D.getIdentifier(),
1364 T,
1365 VarDecl::None,
1366 0, 0);
1367 SetterDecl->setMethodParams(&Argument, 1);
1368 PIDecl->setSetterMethodDecl(SetterDecl);
1369 }
1370 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001371 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001372 *isOverridingProperty = true;
1373 return 0;
1374 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001375 // No matching property found in the main class. Just fall thru
1376 // and add property to the anonymous category. It looks like
1377 // it works as is. This category becomes just like a category
1378 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001379 } else {
1380 Diag(CDecl->getLocation(), diag::err_continuation_class);
1381 *isOverridingProperty = true;
1382 return 0;
1383 }
1384 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001385
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001386 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1387 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001388 // Regardless of setter/getter attribute, we save the default getter/setter
1389 // selector names in anticipation of declaration of setter/getter methods.
1390 PDecl->setGetterName(GetterSel);
1391 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001392
Daniel Dunbar540ff472008-09-23 21:53:23 +00001393 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001394 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001395
Daniel Dunbar540ff472008-09-23 21:53:23 +00001396 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001397 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001398
Daniel Dunbar540ff472008-09-23 21:53:23 +00001399 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001400 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001401
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001402 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001403 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001404
Daniel Dunbar540ff472008-09-23 21:53:23 +00001405 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001406 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001407
Daniel Dunbar540ff472008-09-23 21:53:23 +00001408 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001409 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001410
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001411 if (isAssign)
1412 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1413
Daniel Dunbar540ff472008-09-23 21:53:23 +00001414 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001415 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001416
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001417 if (MethodImplKind == tok::objc_required)
1418 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1419 else if (MethodImplKind == tok::objc_optional)
1420 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1421
Chris Lattner855e51f2007-12-12 07:09:47 +00001422 return PDecl;
1423}
1424
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001425/// ActOnPropertyImplDecl - This routine performs semantic checks and
1426/// builds the AST node for a property implementation declaration; declared
1427/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001428///
1429Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1430 SourceLocation PropertyLoc,
1431 bool Synthesize,
1432 DeclTy *ClassCatImpDecl,
1433 IdentifierInfo *PropertyId,
1434 IdentifierInfo *PropertyIvar) {
1435 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1436 // Make sure we have a context for the property implementation declaration.
1437 if (!ClassImpDecl) {
1438 Diag(AtLoc, diag::error_missing_property_context);
1439 return 0;
1440 }
1441 ObjCPropertyDecl *property = 0;
1442 ObjCInterfaceDecl* IDecl = 0;
1443 // Find the class or category class where this property must have
1444 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001445 ObjCImplementationDecl *IC = 0;
1446 ObjCCategoryImplDecl* CatImplClass = 0;
1447 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001448 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001449 // We always synthesize an interface for an implementation
1450 // without an interface decl. So, IDecl is always non-zero.
1451 assert(IDecl &&
1452 "ActOnPropertyImplDecl - @implementation without @interface");
1453
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001454 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001455 property = IDecl->FindPropertyDeclaration(PropertyId);
1456 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001457 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001458 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001459 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001460 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001461 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001462 if (Synthesize) {
1463 Diag(AtLoc, diag::error_synthesize_category_decl);
1464 return 0;
1465 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001466 IDecl = CatImplClass->getClassInterface();
1467 if (!IDecl) {
1468 Diag(AtLoc, diag::error_missing_property_interface);
1469 return 0;
1470 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001471 ObjCCategoryDecl *Category =
1472 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1473
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001474 // If category for this implementation not found, it is an error which
1475 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001476 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001477 return 0;
1478 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001479 property = Category->FindPropertyDeclaration(PropertyId);
1480 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001481 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001482 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001483 return 0;
1484 }
1485 }
1486 else {
1487 Diag(AtLoc, diag::error_bad_property_context);
1488 return 0;
1489 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001490 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001491 // Check that we have a valid, previously declared ivar for @synthesize
1492 if (Synthesize) {
1493 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001494 if (!PropertyIvar)
1495 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001496 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001497 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001498 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001499 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001500 return 0;
1501 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001502 QualType PropType = Context.getCanonicalType(property->getType());
1503 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1504
Steve Naroff631e3922008-09-30 00:24:17 +00001505 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001506 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001507 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001508 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001509 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001510 return 0;
1511 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001512 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001513 } else if (PropertyIvar) {
1514 // @dynamic
1515 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1516 return 0;
1517 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001518 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001519 ObjCPropertyImplDecl *PIDecl =
1520 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1521 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001522 ObjCPropertyImplDecl::Synthesize
1523 : ObjCPropertyImplDecl::Dynamic),
1524 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001525 if (IC) {
1526 if (Synthesize)
1527 if (ObjCPropertyImplDecl *PPIDecl =
1528 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1529 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1530 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1531 << PropertyIvar;
1532 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1533 }
1534
1535 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1536 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1537 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1538 return 0;
1539 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001540 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001541 }
1542 else {
1543 if (Synthesize)
1544 if (ObjCPropertyImplDecl *PPIDecl =
1545 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1546 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1547 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1548 << PropertyIvar;
1549 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1550 }
1551
1552 if (ObjCPropertyImplDecl *PPIDecl =
1553 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1554 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1555 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1556 return 0;
1557 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001558 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001559 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001560
1561 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001562}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001563
1564bool Sema::CheckObjCDeclScope(Decl *D)
1565{
1566 if (isa<TranslationUnitDecl>(CurContext))
1567 return false;
1568
1569 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1570 D->setInvalidDecl();
1571
1572 return true;
1573}