blob: 01a453d3be67026a2fd49392b1f6d408ecde66e4 [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
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000593/// FIXME: Type hierarchies in Objective-C can be deep. We could most
594/// likely improve the efficiency of selector lookups and type
595/// checking by associating with each protocol / interface / category
596/// the flattened instance tables. If we used an immutable set to keep
597/// the table then it wouldn't add significant memory cost and it
598/// would be handy for lookups.
599
Steve Naroffb268d2a2008-02-08 22:06:17 +0000600/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000601/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000602void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
603 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000604 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000605 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000606 const llvm::DenseSet<Selector> &ClsMap,
607 ObjCInterfaceDecl *IDecl) {
608 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
609
610 // If a method lookup fails locally we still need to look and see if
611 // the method was implemented by a base class or an inherited
612 // protocol. This lookup is slow, but occurs rarely in correct code
613 // and otherwise would terminate in a warning.
614
Chris Lattner855e51f2007-12-12 07:09:47 +0000615 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000617 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000618 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000619 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000620 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000621 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000622 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 }
624 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000625 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000626 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000627 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000628 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
629 !ClsMap.count(method->getSelector()) &&
630 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000631 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000632 }
Chris Lattner0be08822008-07-21 21:32:27 +0000633 // Check on this protocols's referenced protocols, recursively.
634 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
635 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000636 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000637}
638
Ted Kremenek42730c52008-01-07 19:49:32 +0000639void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
640 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000641 llvm::DenseSet<Selector> InsMap;
642 // Check and see if instance methods in class interface have been
643 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000644 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000645 E = IMPDecl->instmeth_end(); I != E; ++I)
646 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000647
648 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000649 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000650 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000651 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000652 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000653 else if (!(*I)->isSynthesized()){
654 bool err = false;
655 ObjCMethodDecl *ImpMethodDecl =
656 IMPDecl->getInstanceMethod((*I)->getSelector());
657 ObjCMethodDecl *IntfMethodDecl =
658 IDecl->getInstanceMethod((*I)->getSelector());
659 QualType ImpMethodQType =
660 Context.getCanonicalType(ImpMethodDecl->getResultType());
661 QualType IntfMethodQType =
662 Context.getCanonicalType(IntfMethodDecl->getResultType());
663 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
664 err = true;
665 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
666 IF=IntfMethodDecl->param_begin(),
667 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
668 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
669 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
670 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
671 err = true;
672 break;
673 }
674 }
675 if (err) {
676 Diag(ImpMethodDecl->getLocation(), diag::err_conflicting_types)
677 << ImpMethodDecl->getDeclName();
678 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
679 }
680 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000681
Chris Lattner855e51f2007-12-12 07:09:47 +0000682 llvm::DenseSet<Selector> ClsMap;
683 // Check and see if class methods in class interface have been
684 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000686 E = IMPDecl->classmeth_end(); I != E; ++I)
687 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000688
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000690 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000691 if (!ClsMap.count((*I)->getSelector()))
692 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000693
694 // Check the protocol list for unimplemented methods in the @implementation
695 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000696 const ObjCList<ObjCProtocolDecl> &Protocols =
697 IDecl->getReferencedProtocols();
698 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
699 E = Protocols.end(); I != E; ++I)
700 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000701 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000702}
703
704/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000705/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000706void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
707 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000708 llvm::DenseSet<Selector> InsMap;
709 // Check and see if instance methods in category interface have been
710 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000711 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000712 E = CatImplDecl->instmeth_end(); I != E; ++I)
713 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000714
715 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000716 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000717 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000718 if (!InsMap.count((*I)->getSelector()))
719 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
720
Chris Lattner855e51f2007-12-12 07:09:47 +0000721 llvm::DenseSet<Selector> ClsMap;
722 // Check and see if class methods in category interface have been
723 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000724 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000725 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
726 I != E; ++I)
727 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000728
Ted Kremenek42730c52008-01-07 19:49:32 +0000729 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000730 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000731 if (!ClsMap.count((*I)->getSelector()))
732 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000733
734 // Check the protocol list for unimplemented methods in the @implementation
735 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000736 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
737 E = CatClassDecl->protocol_end(); PI != E; ++PI)
738 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000739 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000740}
741
742/// ActOnForwardClassDeclaration -
743Action::DeclTy *
744Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
745 IdentifierInfo **IdentList, unsigned NumElts)
746{
Ted Kremenek42730c52008-01-07 19:49:32 +0000747 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000748
749 for (unsigned i = 0; i != NumElts; ++i) {
750 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000751 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregordd861062008-12-05 18:15:24 +0000752 if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
753 // Maybe we will complain about the shadowed template parameter.
754 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
755 // Just pretend that we didn't see the previous declaration.
756 PrevDecl = 0;
757 }
758
Ted Kremenek42730c52008-01-07 19:49:32 +0000759 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000760 // GCC apparently allows the following idiom:
761 //
762 // typedef NSObject < XCElementTogglerP > XCElementToggler;
763 // @class XCElementToggler;
764 //
765 // FIXME: Make an extension?
766 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
767 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000768 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000769 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000770 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000771 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000772 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000773 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000774 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000775 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000776 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000777
778 // Remember that this needs to be removed when the scope is popped.
779 TUScope->AddDecl(IDecl);
780 }
781
782 Interfaces.push_back(IDecl);
783 }
784
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000785 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
786 &Interfaces[0],
787 Interfaces.size());
788
789 CheckObjCDeclScope(CDecl);
790 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000791}
792
793
794/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
795/// returns true, or false, accordingly.
796/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000797bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000798 const ObjCMethodDecl *PrevMethod,
799 bool matchBasedOnSizeAndAlignment) {
800 QualType T1 = Context.getCanonicalType(Method->getResultType());
801 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
802
803 if (T1 != T2) {
804 // The result types are different.
805 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000806 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000807 // Incomplete types don't have a size and alignment.
808 if (T1->isIncompleteType() || T2->isIncompleteType())
809 return false;
810 // Check is based on size and alignment.
811 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
812 return false;
813 }
814 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
815 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
816 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
817 if (T1 != T2) {
818 // The result types are different.
819 if (!matchBasedOnSizeAndAlignment)
820 return false;
821 // Incomplete types don't have a size and alignment.
822 if (T1->isIncompleteType() || T2->isIncompleteType())
823 return false;
824 // Check is based on size and alignment.
825 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
826 return false;
827 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000828 }
829 return true;
830}
831
Ted Kremenek42730c52008-01-07 19:49:32 +0000832void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
833 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000834 if (!FirstMethod.Method) {
835 // Haven't seen a method with this selector name yet - add it.
836 FirstMethod.Method = Method;
837 FirstMethod.Next = 0;
838 } else {
839 // We've seen a method with this name, now check the type signature(s).
840 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
841
Ted Kremenek42730c52008-01-07 19:49:32 +0000842 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000843 Next = Next->Next)
844 match = MatchTwoMethodDeclarations(Method, Next->Method);
845
846 if (!match) {
847 // We have a new signature for an existing method - add it.
848 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +0000849 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +0000850 }
851 }
852}
853
Steve Naroffec7e0882008-10-21 10:50:19 +0000854// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +0000855ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
856 SourceRange R) {
857 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +0000858 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +0000859
860 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +0000861 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
862 // This checks if the methods differ by size & alignment.
863 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
864 issueWarning = true;
865 }
866 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +0000867 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +0000868 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000869 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000870 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +0000871 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000872 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000873 }
874 return MethList.Method;
875}
876
Ted Kremenek42730c52008-01-07 19:49:32 +0000877void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
878 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000879 if (!FirstMethod.Method) {
880 // Haven't seen a method with this selector name yet - add it.
881 FirstMethod.Method = Method;
882 FirstMethod.Next = 0;
883 } else {
884 // We've seen a method with this name, now check the type signature(s).
885 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
886
Ted Kremenek42730c52008-01-07 19:49:32 +0000887 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000888 Next = Next->Next)
889 match = MatchTwoMethodDeclarations(Method, Next->Method);
890
891 if (!match) {
892 // We have a new signature for an existing method - add it.
893 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000894 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000895 FirstMethod.Next = OMI;
896 }
897 }
898}
899
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000900/// diagnosePropertySetterGetterMismatch - Make sure that use-defined
901/// setter/getter methods have the property type and issue diagnostics
902/// if they don't.
903///
904void
905Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property,
906 const ObjCMethodDecl *GetterMethod,
907 const ObjCMethodDecl *SetterMethod) {
908 if (GetterMethod &&
909 GetterMethod->getResultType() != property->getType())
910 Diag(property->getLocation(),
911 diag::err_accessor_property_type_mismatch)
912 << property->getDeclName()
913 << GetterMethod->getSelector().getAsIdentifierInfo();
914
915 if (SetterMethod) {
916 if (SetterMethod->getResultType() != Context.VoidPtrTy)
917 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
918 if (SetterMethod->getNumParams() != 1 ||
919 (SetterMethod->getParamDecl(0)->getType() != property->getType()))
920 Diag(property->getLocation(),
921 diag::err_accessor_property_type_mismatch)
922 << property->getDeclName()
923 << SetterMethod->getSelector().getAsIdentifierInfo();
924 }
925}
926
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000927// Note: For class/category implemenations, allMethods/allProperties is
928// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000929void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
930 DeclTy **allMethods, unsigned allNum,
931 DeclTy **allProperties, unsigned pNum) {
932 Decl *ClassDecl = static_cast<Decl *>(classDecl);
933
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000934 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
935 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000936 // should be true.
937 if (!ClassDecl)
938 return;
939
Ted Kremenek42730c52008-01-07 19:49:32 +0000940 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
941 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000942
Ted Kremenek42730c52008-01-07 19:49:32 +0000943 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
944 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000945
946 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000947 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
948 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000949 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000950
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000951 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000952 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
953 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000954 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
955 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
956 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000957 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000958 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000959 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000960 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000961
962 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000963 ObjCMethodDecl *Method =
964 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000965
966 if (!Method) continue; // Already issued a diagnostic.
967 if (Method->isInstance()) {
968 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000969 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000970 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
971 : false;
972 if (isInterfaceDeclKind && PrevMethod && !match
973 || checkIdenticalMethods && match) {
Chris Lattner1336cab2008-11-23 23:12:31 +0000974 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +0000975 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000976 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000977 } else {
978 insMethods.push_back(Method);
979 InsMap[Method->getSelector()] = Method;
980 /// The following allows us to typecheck messages to "id".
981 AddInstanceMethodToGlobalPool(Method);
982 }
983 }
984 else {
985 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000986 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000987 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
988 : false;
989 if (isInterfaceDeclKind && PrevMethod && !match
990 || checkIdenticalMethods && match) {
Chris Lattner1336cab2008-11-23 23:12:31 +0000991 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +0000992 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000993 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000994 } else {
995 clsMethods.push_back(Method);
996 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000997 /// The following allows us to typecheck messages to "Class".
998 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000999 }
1000 }
1001 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001002 // Save the size so we can detect if we've added any property methods.
1003 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1004 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner855e51f2007-12-12 07:09:47 +00001005
Ted Kremenek42730c52008-01-07 19:49:32 +00001006 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001007 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001008 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001009 ComparePropertiesInBaseAndSuper(I);
1010 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001011 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001012 e = I->classprop_end(); i != e; ++i) {
1013 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1014 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001015 I->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001016 }
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001017 I->addMethods(&insMethods[0], insMethods.size(),
1018 &clsMethods[0], clsMethods.size(), AtEndLoc);
1019
Ted Kremenek42730c52008-01-07 19:49:32 +00001020 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001021 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001022 e = P->classprop_end(); i != e; ++i) {
1023 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1024 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001025 P->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001026 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001027 P->addMethods(&insMethods[0], insMethods.size(),
1028 &clsMethods[0], clsMethods.size(), AtEndLoc);
1029 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001030 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001031 // FIXME: Need to compare properties to those in interface?
1032
1033 // FIXME: If we merge properties into class we should probably
1034 // merge them into category as well?
1035 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001036 e = C->classprop_end(); i != e; ++i) {
1037 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1038 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001039 C->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001040 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001041 C->addMethods(&insMethods[0], insMethods.size(),
1042 &clsMethods[0], clsMethods.size(), AtEndLoc);
1043 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001044 else if (ObjCImplementationDecl *IC =
1045 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001046 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001047 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +00001048 ImplMethodsVsClassMethods(IC, IDecl);
1049 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +00001050 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001051 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001052 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +00001053 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001054 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +00001055 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001056 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001057 Categories; Categories = Categories->getNextClassCategory()) {
1058 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1059 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1060 break;
1061 }
1062 }
1063 }
1064 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001065 // Add any synthesized methods to the global pool. This allows us to
1066 // handle the following, which is supported by GCC (and part of the design).
1067 //
1068 // @interface Foo
1069 // @property double bar;
1070 // @end
1071 //
1072 // void thisIsUnfortunate() {
1073 // id foo;
1074 // double bar = [foo bar];
1075 // }
1076 //
1077 if (insMethodsSizePriorToPropAdds < insMethods.size())
1078 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1079 AddInstanceMethodToGlobalPool(insMethods[i]);
1080 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1081 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1082 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner855e51f2007-12-12 07:09:47 +00001083}
1084
1085
1086/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1087/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001088static Decl::ObjCDeclQualifier
1089CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1090 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1091 if (PQTVal & ObjCDeclSpec::DQ_In)
1092 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1093 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1094 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1095 if (PQTVal & ObjCDeclSpec::DQ_Out)
1096 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1097 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1098 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1099 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1100 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1101 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1102 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001103
1104 return ret;
1105}
1106
1107Sema::DeclTy *Sema::ActOnMethodDeclaration(
1108 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001109 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001110 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001111 Selector Sel,
1112 // optional arguments. The number of types/arguments is obtained
1113 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001114 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +00001115 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1116 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001117 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001118
1119 // Make sure we can establish a context for the method.
1120 if (!ClassDecl) {
1121 Diag(MethodLoc, diag::error_missing_method_context);
1122 return 0;
1123 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001124 QualType resultDeclType;
1125
1126 if (ReturnType)
1127 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1128 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001129 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001130
Chris Lattner114add62008-03-16 00:49:28 +00001131 ObjCMethodDecl* ObjCMethod =
1132 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +00001133 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +00001134 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001135 false,
Chris Lattner114add62008-03-16 00:49:28 +00001136 MethodDeclKind == tok::objc_optional ?
1137 ObjCMethodDecl::Optional :
1138 ObjCMethodDecl::Required);
1139
Chris Lattnereee57c02008-04-04 06:12:32 +00001140 llvm::SmallVector<ParmVarDecl*, 16> Params;
1141
1142 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1143 // FIXME: arg->AttrList must be stored too!
1144 QualType argType;
1145
1146 if (ArgTypes[i])
1147 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1148 else
1149 argType = Context.getObjCIdType();
1150 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1151 SourceLocation(/*FIXME*/),
1152 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001153 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001154 Param->setObjCDeclQualifier(
1155 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1156 Params.push_back(Param);
1157 }
1158
Ted Kremenek42730c52008-01-07 19:49:32 +00001159 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1160 ObjCMethod->setObjCDeclQualifier(
1161 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1162 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001163
1164 if (AttrList)
1165 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001166
1167 // For implementations (which can be very "coarse grain"), we add the
1168 // method now. This allows the AST to implement lookup methods that work
1169 // incrementally (without waiting until we parse the @end). It also allows
1170 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001171 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001172 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001173 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001174 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001175 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001176 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001177 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001178 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001179 }
1180 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001181 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001182 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001183 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001184 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001185 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001186 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001187 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001188 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001189 }
1190 }
1191 if (PrevMethod) {
1192 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001193 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001194 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001195 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001196 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001197 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001198}
1199
Daniel Dunbar540ff472008-09-23 21:53:23 +00001200void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1201 SourceLocation Loc,
1202 unsigned &Attributes) {
1203 // FIXME: Improve the reported location.
1204
1205 // readonly and readwrite conflict.
1206 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1207 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001208 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1209 << "readonly" << "readwrite";
1210 Attributes &= ~ObjCDeclSpec::DQ_PR_readonly;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001211 }
1212
1213 // Check for copy or retain on non-object types.
1214 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1215 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001216 Diag(Loc, diag::err_objc_property_requires_object)
1217 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001218 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1219 }
1220
1221 // Check for more than one of { assign, copy, retain }.
1222 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1223 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001224 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1225 << "assign" << "copy";
1226 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001227 }
1228 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001229 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1230 << "assign" << "retain";
1231 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001232 }
1233 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1234 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001235 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1236 << "copy" << "retain";
1237 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001238 }
1239 }
1240
1241 // Warn if user supplied no assignment attribute, property is
1242 // readwrite, and this is an object type.
1243 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1244 ObjCDeclSpec::DQ_PR_retain)) &&
1245 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1246 Context.isObjCObjectPointerType(PropertyTy)) {
1247 // Skip this warning in gc-only mode.
1248 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1249 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1250
1251 // If non-gc code warn that this is likely inappropriate.
1252 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1253 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1254
1255 // FIXME: Implement warning dependent on NSCopying being
1256 // implemented. See also:
1257 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1258 // (please trim this list while you are at it).
1259 }
1260}
1261
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001262Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1263 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001264 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001265 Selector GetterSel,
1266 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001267 DeclTy *ClassCategory,
1268 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001269 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001270 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001271 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1272 // default is readwrite!
1273 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1274 // property is defaulted to 'assign' if it is readwrite and is
1275 // not retain or copy
1276 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1277 (isReadWrite &&
1278 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1279 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1280 QualType T = GetTypeForDeclarator(FD.D, S);
1281 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001282
1283 // May modify Attributes.
1284 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001285
1286 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1287 if (!CDecl->getIdentifier()) {
1288 // This is an anonymous category. property requires special
1289 // handling.
1290 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1291 if (ObjCPropertyDecl *PIDecl =
1292 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1293 // property 'PIDecl's readonly attribute will be over-ridden
1294 // with anonymous category's readwrite property attribute!
1295 unsigned PIkind = PIDecl->getPropertyAttributes();
1296 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
1297 if ((Attributes & ObjCPropertyDecl::OBJC_PR_retain) !=
1298 (PIkind & ObjCPropertyDecl::OBJC_PR_retain) ||
1299 (Attributes & ObjCPropertyDecl::OBJC_PR_copy) !=
1300 (PIkind & ObjCPropertyDecl::OBJC_PR_copy) ||
1301 (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
1302 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1303 Diag(AtLoc, diag::warn_property_attr_mismatch);
1304 PIDecl->makeitReadWriteAttribute();
1305 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1306 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1307 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1308 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1309 PIDecl->setSetterName(SetterSel);
1310 // FIXME: use a common routine with addPropertyMethods.
1311 ObjCMethodDecl *SetterDecl =
1312 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1313 Context.VoidTy,
1314 ICDecl,
1315 true, false, true,
1316 ObjCMethodDecl::Required);
1317 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1318 SetterDecl,
1319 SourceLocation(),
1320 FD.D.getIdentifier(),
1321 T,
1322 VarDecl::None,
1323 0, 0);
1324 SetterDecl->setMethodParams(&Argument, 1);
1325 PIDecl->setSetterMethodDecl(SetterDecl);
1326 }
1327 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001328 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001329 *isOverridingProperty = true;
1330 return 0;
1331 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001332 // No matching property found in the main class. Just fall thru
1333 // and add property to the anonymous category. It looks like
1334 // it works as is. This category becomes just like a category
1335 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001336 } else {
1337 Diag(CDecl->getLocation(), diag::err_continuation_class);
1338 *isOverridingProperty = true;
1339 return 0;
1340 }
1341 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001342
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001343 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1344 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001345 // Regardless of setter/getter attribute, we save the default getter/setter
1346 // selector names in anticipation of declaration of setter/getter methods.
1347 PDecl->setGetterName(GetterSel);
1348 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001349
Daniel Dunbar540ff472008-09-23 21:53:23 +00001350 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001351 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001352
Daniel Dunbar540ff472008-09-23 21:53:23 +00001353 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001354 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001355
Daniel Dunbar540ff472008-09-23 21:53:23 +00001356 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001357 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001358
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001359 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001360 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001361
Daniel Dunbar540ff472008-09-23 21:53:23 +00001362 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001363 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001364
Daniel Dunbar540ff472008-09-23 21:53:23 +00001365 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001366 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001367
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001368 if (isAssign)
1369 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1370
Daniel Dunbar540ff472008-09-23 21:53:23 +00001371 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001373
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001374 if (MethodImplKind == tok::objc_required)
1375 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1376 else if (MethodImplKind == tok::objc_optional)
1377 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1378
Chris Lattner855e51f2007-12-12 07:09:47 +00001379 return PDecl;
1380}
1381
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001382/// ActOnPropertyImplDecl - This routine performs semantic checks and
1383/// builds the AST node for a property implementation declaration; declared
1384/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001385///
1386Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1387 SourceLocation PropertyLoc,
1388 bool Synthesize,
1389 DeclTy *ClassCatImpDecl,
1390 IdentifierInfo *PropertyId,
1391 IdentifierInfo *PropertyIvar) {
1392 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1393 // Make sure we have a context for the property implementation declaration.
1394 if (!ClassImpDecl) {
1395 Diag(AtLoc, diag::error_missing_property_context);
1396 return 0;
1397 }
1398 ObjCPropertyDecl *property = 0;
1399 ObjCInterfaceDecl* IDecl = 0;
1400 // Find the class or category class where this property must have
1401 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001402 ObjCImplementationDecl *IC = 0;
1403 ObjCCategoryImplDecl* CatImplClass = 0;
1404 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001405 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001406 // We always synthesize an interface for an implementation
1407 // without an interface decl. So, IDecl is always non-zero.
1408 assert(IDecl &&
1409 "ActOnPropertyImplDecl - @implementation without @interface");
1410
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001411 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001412 property = IDecl->FindPropertyDeclaration(PropertyId);
1413 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001414 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001415 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001416 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001417 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001418 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001419 if (Synthesize) {
1420 Diag(AtLoc, diag::error_synthesize_category_decl);
1421 return 0;
1422 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001423 IDecl = CatImplClass->getClassInterface();
1424 if (!IDecl) {
1425 Diag(AtLoc, diag::error_missing_property_interface);
1426 return 0;
1427 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001428 ObjCCategoryDecl *Category =
1429 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1430
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001431 // If category for this implementation not found, it is an error which
1432 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001433 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001434 return 0;
1435 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001436 property = Category->FindPropertyDeclaration(PropertyId);
1437 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001438 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001439 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001440 return 0;
1441 }
1442 }
1443 else {
1444 Diag(AtLoc, diag::error_bad_property_context);
1445 return 0;
1446 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001447 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001448 // Check that we have a valid, previously declared ivar for @synthesize
1449 if (Synthesize) {
1450 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001451 if (!PropertyIvar)
1452 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001453 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001454 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001455 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001456 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001457 return 0;
1458 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001459 QualType PropType = Context.getCanonicalType(property->getType());
1460 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1461
Steve Naroff631e3922008-09-30 00:24:17 +00001462 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001463 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001464 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001465 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001466 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001467 return 0;
1468 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001469 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001470 } else if (PropertyIvar) {
1471 // @dynamic
1472 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1473 return 0;
1474 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001475 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001476 ObjCPropertyImplDecl *PIDecl =
1477 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1478 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001479 ObjCPropertyImplDecl::Synthesize
1480 : ObjCPropertyImplDecl::Dynamic),
1481 Ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001482 if (IC)
1483 IC->addPropertyImplementation(PIDecl);
1484 else
1485 CatImplClass->addPropertyImplementation(PIDecl);
1486
1487 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001488}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001489
1490bool Sema::CheckObjCDeclScope(Decl *D)
1491{
1492 if (isa<TranslationUnitDecl>(CurContext))
1493 return false;
1494
1495 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1496 D->setInvalidDecl();
1497
1498 return true;
1499}