blob: 83013a7de3ba0c9650a5707afad1fbc3b47c4b9a [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 &&
936 GetterMethod->getResultType() != property->getType())
937 Diag(property->getLocation(),
938 diag::err_accessor_property_type_mismatch)
939 << property->getDeclName()
940 << GetterMethod->getSelector().getAsIdentifierInfo();
941
942 if (SetterMethod) {
Fariborz Jahaniana9307bb2008-12-06 21:11:37 +0000943 if (SetterMethod->getResultType() != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000944 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
945 if (SetterMethod->getNumParams() != 1 ||
946 (SetterMethod->getParamDecl(0)->getType() != property->getType()))
947 Diag(property->getLocation(),
948 diag::err_accessor_property_type_mismatch)
949 << property->getDeclName()
950 << SetterMethod->getSelector().getAsIdentifierInfo();
951 }
952}
953
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000954// Note: For class/category implemenations, allMethods/allProperties is
955// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000956void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
957 DeclTy **allMethods, unsigned allNum,
958 DeclTy **allProperties, unsigned pNum) {
959 Decl *ClassDecl = static_cast<Decl *>(classDecl);
960
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000961 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
962 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000963 // should be true.
964 if (!ClassDecl)
965 return;
966
Ted Kremenek42730c52008-01-07 19:49:32 +0000967 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
968 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000969
Ted Kremenek42730c52008-01-07 19:49:32 +0000970 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
971 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000972
973 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000974 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
975 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000976 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000977
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000978 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000979 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
980 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000981 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
982 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
983 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000984 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000985 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000986 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000987 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000988
989 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000990 ObjCMethodDecl *Method =
991 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000992
993 if (!Method) continue; // Already issued a diagnostic.
994 if (Method->isInstance()) {
995 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000996 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000997 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
998 : false;
999 if (isInterfaceDeclKind && PrevMethod && !match
1000 || checkIdenticalMethods && match) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001001 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001002 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001003 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001004 } else {
1005 insMethods.push_back(Method);
1006 InsMap[Method->getSelector()] = Method;
1007 /// The following allows us to typecheck messages to "id".
1008 AddInstanceMethodToGlobalPool(Method);
1009 }
1010 }
1011 else {
1012 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001013 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001014 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1015 : false;
1016 if (isInterfaceDeclKind && PrevMethod && !match
1017 || checkIdenticalMethods && match) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001018 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001019 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001020 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001021 } else {
1022 clsMethods.push_back(Method);
1023 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001024 /// The following allows us to typecheck messages to "Class".
1025 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001026 }
1027 }
1028 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001029 // Save the size so we can detect if we've added any property methods.
1030 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1031 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner855e51f2007-12-12 07:09:47 +00001032
Ted Kremenek42730c52008-01-07 19:49:32 +00001033 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001034 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001035 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001036 ComparePropertiesInBaseAndSuper(I);
1037 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001038 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001039 e = I->classprop_end(); i != e; ++i) {
1040 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1041 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001042 I->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001043 }
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001044 I->addMethods(&insMethods[0], insMethods.size(),
1045 &clsMethods[0], clsMethods.size(), AtEndLoc);
1046
Ted Kremenek42730c52008-01-07 19:49:32 +00001047 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001048 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001049 e = P->classprop_end(); i != e; ++i) {
1050 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1051 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001052 P->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001053 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001054 P->addMethods(&insMethods[0], insMethods.size(),
1055 &clsMethods[0], clsMethods.size(), AtEndLoc);
1056 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001057 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001058 // Categories are used to extend the class by declaring new methods.
1059 // By the same token, they are also used to add new properties. No
1060 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001061
1062 // FIXME: If we merge properties into class we should probably
1063 // merge them into category as well?
1064 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001065 e = C->classprop_end(); i != e; ++i) {
1066 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1067 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001068 C->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001069 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001070 C->addMethods(&insMethods[0], insMethods.size(),
1071 &clsMethods[0], clsMethods.size(), AtEndLoc);
1072 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001073 else if (ObjCImplementationDecl *IC =
1074 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001075 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001076 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +00001077 ImplMethodsVsClassMethods(IC, IDecl);
1078 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +00001079 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001080 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +00001082 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001083 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +00001084 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001085 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001086 Categories; Categories = Categories->getNextClassCategory()) {
1087 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1088 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1089 break;
1090 }
1091 }
1092 }
1093 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001094 // Add any synthesized methods to the global pool. This allows us to
1095 // handle the following, which is supported by GCC (and part of the design).
1096 //
1097 // @interface Foo
1098 // @property double bar;
1099 // @end
1100 //
1101 // void thisIsUnfortunate() {
1102 // id foo;
1103 // double bar = [foo bar];
1104 // }
1105 //
1106 if (insMethodsSizePriorToPropAdds < insMethods.size())
1107 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1108 AddInstanceMethodToGlobalPool(insMethods[i]);
1109 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1110 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1111 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner855e51f2007-12-12 07:09:47 +00001112}
1113
1114
1115/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1116/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001117static Decl::ObjCDeclQualifier
1118CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1119 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1120 if (PQTVal & ObjCDeclSpec::DQ_In)
1121 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1122 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1123 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1124 if (PQTVal & ObjCDeclSpec::DQ_Out)
1125 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1126 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1127 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1128 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1129 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1130 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1131 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001132
1133 return ret;
1134}
1135
1136Sema::DeclTy *Sema::ActOnMethodDeclaration(
1137 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001138 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001139 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001140 Selector Sel,
1141 // optional arguments. The number of types/arguments is obtained
1142 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001143 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +00001144 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1145 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001146 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001147
1148 // Make sure we can establish a context for the method.
1149 if (!ClassDecl) {
1150 Diag(MethodLoc, diag::error_missing_method_context);
1151 return 0;
1152 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001153 QualType resultDeclType;
1154
1155 if (ReturnType)
1156 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1157 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001158 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001159
Chris Lattner114add62008-03-16 00:49:28 +00001160 ObjCMethodDecl* ObjCMethod =
1161 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +00001162 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +00001163 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001164 false,
Chris Lattner114add62008-03-16 00:49:28 +00001165 MethodDeclKind == tok::objc_optional ?
1166 ObjCMethodDecl::Optional :
1167 ObjCMethodDecl::Required);
1168
Chris Lattnereee57c02008-04-04 06:12:32 +00001169 llvm::SmallVector<ParmVarDecl*, 16> Params;
1170
1171 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1172 // FIXME: arg->AttrList must be stored too!
1173 QualType argType;
1174
1175 if (ArgTypes[i])
1176 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1177 else
1178 argType = Context.getObjCIdType();
1179 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1180 SourceLocation(/*FIXME*/),
1181 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001182 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001183 Param->setObjCDeclQualifier(
1184 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1185 Params.push_back(Param);
1186 }
1187
Ted Kremenek42730c52008-01-07 19:49:32 +00001188 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1189 ObjCMethod->setObjCDeclQualifier(
1190 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1191 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001192
1193 if (AttrList)
1194 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001195
1196 // For implementations (which can be very "coarse grain"), we add the
1197 // method now. This allows the AST to implement lookup methods that work
1198 // incrementally (without waiting until we parse the @end). It also allows
1199 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001200 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001201 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001202 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001203 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001204 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001205 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001206 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001207 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001208 }
1209 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001210 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001211 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001212 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001213 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001214 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001215 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001216 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001217 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001218 }
1219 }
1220 if (PrevMethod) {
1221 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001222 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001223 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001224 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001225 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001226 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001227}
1228
Daniel Dunbar540ff472008-09-23 21:53:23 +00001229void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1230 SourceLocation Loc,
1231 unsigned &Attributes) {
1232 // FIXME: Improve the reported location.
1233
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001234 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001235 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001236 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1237 ObjCDeclSpec::DQ_PR_assign |
1238 ObjCDeclSpec::DQ_PR_copy |
1239 ObjCDeclSpec::DQ_PR_retain))) {
1240 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1241 "readwrite" :
1242 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1243 "assign" :
1244 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1245 "copy" : "retain";
1246
Chris Lattner8d756812008-11-20 06:13:02 +00001247 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001248 << "readonly" << which;
1249 return;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001250 }
1251
1252 // Check for copy or retain on non-object types.
1253 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1254 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001255 Diag(Loc, diag::err_objc_property_requires_object)
1256 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001257 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1258 }
1259
1260 // Check for more than one of { assign, copy, retain }.
1261 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1262 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001263 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1264 << "assign" << "copy";
1265 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001266 }
1267 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001268 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1269 << "assign" << "retain";
1270 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001271 }
1272 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1273 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001274 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1275 << "copy" << "retain";
1276 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001277 }
1278 }
1279
1280 // Warn if user supplied no assignment attribute, property is
1281 // readwrite, and this is an object type.
1282 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1283 ObjCDeclSpec::DQ_PR_retain)) &&
1284 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1285 Context.isObjCObjectPointerType(PropertyTy)) {
1286 // Skip this warning in gc-only mode.
1287 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1288 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1289
1290 // If non-gc code warn that this is likely inappropriate.
1291 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1292 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1293
1294 // FIXME: Implement warning dependent on NSCopying being
1295 // implemented. See also:
1296 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1297 // (please trim this list while you are at it).
1298 }
1299}
1300
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001301Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1302 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001303 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001304 Selector GetterSel,
1305 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001306 DeclTy *ClassCategory,
1307 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001308 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001309 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001310 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1311 // default is readwrite!
1312 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1313 // property is defaulted to 'assign' if it is readwrite and is
1314 // not retain or copy
1315 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1316 (isReadWrite &&
1317 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1318 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1319 QualType T = GetTypeForDeclarator(FD.D, S);
1320 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001321
1322 // May modify Attributes.
1323 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001324
1325 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1326 if (!CDecl->getIdentifier()) {
1327 // This is an anonymous category. property requires special
1328 // handling.
1329 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1330 if (ObjCPropertyDecl *PIDecl =
1331 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1332 // property 'PIDecl's readonly attribute will be over-ridden
1333 // with anonymous category's readwrite property attribute!
1334 unsigned PIkind = PIDecl->getPropertyAttributes();
1335 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
1336 if ((Attributes & ObjCPropertyDecl::OBJC_PR_retain) !=
1337 (PIkind & ObjCPropertyDecl::OBJC_PR_retain) ||
1338 (Attributes & ObjCPropertyDecl::OBJC_PR_copy) !=
1339 (PIkind & ObjCPropertyDecl::OBJC_PR_copy) ||
1340 (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
1341 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1342 Diag(AtLoc, diag::warn_property_attr_mismatch);
1343 PIDecl->makeitReadWriteAttribute();
1344 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1345 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1346 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1347 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1348 PIDecl->setSetterName(SetterSel);
1349 // FIXME: use a common routine with addPropertyMethods.
1350 ObjCMethodDecl *SetterDecl =
1351 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1352 Context.VoidTy,
1353 ICDecl,
1354 true, false, true,
1355 ObjCMethodDecl::Required);
1356 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1357 SetterDecl,
1358 SourceLocation(),
1359 FD.D.getIdentifier(),
1360 T,
1361 VarDecl::None,
1362 0, 0);
1363 SetterDecl->setMethodParams(&Argument, 1);
1364 PIDecl->setSetterMethodDecl(SetterDecl);
1365 }
1366 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001367 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001368 *isOverridingProperty = true;
1369 return 0;
1370 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001371 // No matching property found in the main class. Just fall thru
1372 // and add property to the anonymous category. It looks like
1373 // it works as is. This category becomes just like a category
1374 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001375 } else {
1376 Diag(CDecl->getLocation(), diag::err_continuation_class);
1377 *isOverridingProperty = true;
1378 return 0;
1379 }
1380 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001381
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001382 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1383 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001384 // Regardless of setter/getter attribute, we save the default getter/setter
1385 // selector names in anticipation of declaration of setter/getter methods.
1386 PDecl->setGetterName(GetterSel);
1387 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001388
Daniel Dunbar540ff472008-09-23 21:53:23 +00001389 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001390 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001391
Daniel Dunbar540ff472008-09-23 21:53:23 +00001392 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001393 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001394
Daniel Dunbar540ff472008-09-23 21:53:23 +00001395 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001396 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001397
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001398 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001399 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001400
Daniel Dunbar540ff472008-09-23 21:53:23 +00001401 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001402 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001403
Daniel Dunbar540ff472008-09-23 21:53:23 +00001404 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001405 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001406
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001407 if (isAssign)
1408 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1409
Daniel Dunbar540ff472008-09-23 21:53:23 +00001410 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001411 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001412
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001413 if (MethodImplKind == tok::objc_required)
1414 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1415 else if (MethodImplKind == tok::objc_optional)
1416 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1417
Chris Lattner855e51f2007-12-12 07:09:47 +00001418 return PDecl;
1419}
1420
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001421/// ActOnPropertyImplDecl - This routine performs semantic checks and
1422/// builds the AST node for a property implementation declaration; declared
1423/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001424///
1425Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1426 SourceLocation PropertyLoc,
1427 bool Synthesize,
1428 DeclTy *ClassCatImpDecl,
1429 IdentifierInfo *PropertyId,
1430 IdentifierInfo *PropertyIvar) {
1431 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1432 // Make sure we have a context for the property implementation declaration.
1433 if (!ClassImpDecl) {
1434 Diag(AtLoc, diag::error_missing_property_context);
1435 return 0;
1436 }
1437 ObjCPropertyDecl *property = 0;
1438 ObjCInterfaceDecl* IDecl = 0;
1439 // Find the class or category class where this property must have
1440 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001441 ObjCImplementationDecl *IC = 0;
1442 ObjCCategoryImplDecl* CatImplClass = 0;
1443 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001444 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001445 // We always synthesize an interface for an implementation
1446 // without an interface decl. So, IDecl is always non-zero.
1447 assert(IDecl &&
1448 "ActOnPropertyImplDecl - @implementation without @interface");
1449
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001450 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001451 property = IDecl->FindPropertyDeclaration(PropertyId);
1452 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001453 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001454 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001455 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001456 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001457 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001458 if (Synthesize) {
1459 Diag(AtLoc, diag::error_synthesize_category_decl);
1460 return 0;
1461 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001462 IDecl = CatImplClass->getClassInterface();
1463 if (!IDecl) {
1464 Diag(AtLoc, diag::error_missing_property_interface);
1465 return 0;
1466 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001467 ObjCCategoryDecl *Category =
1468 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1469
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001470 // If category for this implementation not found, it is an error which
1471 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001472 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001473 return 0;
1474 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001475 property = Category->FindPropertyDeclaration(PropertyId);
1476 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001477 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001478 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001479 return 0;
1480 }
1481 }
1482 else {
1483 Diag(AtLoc, diag::error_bad_property_context);
1484 return 0;
1485 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001486 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001487 // Check that we have a valid, previously declared ivar for @synthesize
1488 if (Synthesize) {
1489 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001490 if (!PropertyIvar)
1491 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001492 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001493 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001494 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001495 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001496 return 0;
1497 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001498 QualType PropType = Context.getCanonicalType(property->getType());
1499 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1500
Steve Naroff631e3922008-09-30 00:24:17 +00001501 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001502 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001503 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001504 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001505 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001506 return 0;
1507 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001508 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001509 } else if (PropertyIvar) {
1510 // @dynamic
1511 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1512 return 0;
1513 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001514 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001515 ObjCPropertyImplDecl *PIDecl =
1516 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1517 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001518 ObjCPropertyImplDecl::Synthesize
1519 : ObjCPropertyImplDecl::Dynamic),
1520 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001521 if (IC) {
1522 if (Synthesize)
1523 if (ObjCPropertyImplDecl *PPIDecl =
1524 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1525 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1526 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1527 << PropertyIvar;
1528 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1529 }
1530
1531 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1532 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1533 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1534 return 0;
1535 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001536 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001537 }
1538 else {
1539 if (Synthesize)
1540 if (ObjCPropertyImplDecl *PPIDecl =
1541 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1542 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1543 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1544 << PropertyIvar;
1545 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1546 }
1547
1548 if (ObjCPropertyImplDecl *PPIDecl =
1549 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1550 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1551 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1552 return 0;
1553 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001554 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001555 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001556
1557 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001558}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001559
1560bool Sema::CheckObjCDeclScope(Decl *D)
1561{
1562 if (isa<TranslationUnitDecl>(CurContext))
1563 return false;
1564
1565 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1566 D->setInvalidDecl();
1567
1568 return true;
1569}