blob: 7759bac681fed1ba18278a9f56b4fdca742144eb [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.
Douglas Gregor8acb7272008-12-11 16:49:14 +000039 PushDeclContext(FnBodyScope, 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!
Fariborz Jahanian91dd9d32008-12-09 20:23:04 +000045 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
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 Gregor2715a1f2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +000070 // 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 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000210 if (AttrList)
211 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000212 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000213 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000214 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000215 PDecl->setLocEnd(EndProtoLoc);
216 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000217
218 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000219 return PDecl;
220}
221
222/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000223/// issues an error if they are not declared. It returns list of
224/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000225void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000226Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000227 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000228 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000229 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000230 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000231 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
232 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000233 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000234 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000235 continue;
236 }
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000237 for (const Attr *attr = PDecl->getAttrs(); attr; attr = attr->getNext()) {
238 if (attr->hasKind(Attr::Unavailable))
239 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
240 PDecl->getDeclName();
241 if (attr->hasKind(Attr::Deprecated))
242 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
243 PDecl->getDeclName();
244 }
Chris Lattner17d50a92008-07-26 03:47:43 +0000245
246 // If this is a forward declaration and we are supposed to warn in this
247 // case, do it.
248 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000249 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000250 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000251 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000252 }
253}
254
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000255/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000256/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000257///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000258void
259Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
260 ObjCPropertyDecl *SuperProperty,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000261 const IdentifierInfo *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000262 ObjCPropertyDecl::PropertyAttributeKind CAttr =
263 Property->getPropertyAttributes();
264 ObjCPropertyDecl::PropertyAttributeKind SAttr =
265 SuperProperty->getPropertyAttributes();
266 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
267 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000268 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000269 << Property->getDeclName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000270 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
271 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
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() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000274 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
275 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000276 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000277 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000278
279 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
280 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000281 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000282 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000283 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000284 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000285 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000286 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000287 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000288 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000289
Chris Lattnerc5cff302008-07-26 20:50:02 +0000290 if (Context.getCanonicalType(Property->getType()) !=
291 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000292 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000293 << Property->getType() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000294
295}
296
297/// ComparePropertiesInBaseAndSuper - This routine compares property
298/// declarations in base and its super class, if any, and issues
299/// diagnostics in a variety of inconsistant situations.
300///
301void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000302Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000303 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
304 if (!SDecl)
305 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000306 // FIXME: O(N^2)
Fariborz Jahanianed986602008-05-01 00:03:38 +0000307 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
308 E = SDecl->classprop_end(); S != E; ++S) {
309 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000310 // Does property in super class has declaration in current class?
311 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
312 E = IDecl->classprop_end(); I != E; ++I) {
313 ObjCPropertyDecl *PDecl = (*I);
314 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000315 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000316 SDecl->getIdentifier());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000317 }
318 }
319}
320
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000321/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
322/// of properties declared in a protocol and adds them to the list
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000323/// of properties for current class/category if it is not there already.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000324void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000325Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000326 ObjCProtocolDecl *PDecl) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000327 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000328 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
329 if (!IDecl) {
330 // Category
331 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
332 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
333 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
334 E = PDecl->classprop_end(); P != E; ++P) {
335 ObjCPropertyDecl *Pr = (*P);
336 ObjCCategoryDecl::classprop_iterator CP, CE;
337 // Is this property already in category's list of properties?
338 for (CP = CatDecl->classprop_begin(), CE = CatDecl->classprop_end();
339 CP != CE; ++CP)
340 if ((*CP)->getIdentifier() == Pr->getIdentifier())
341 break;
342 if (CP == CE)
343 // Add this property to list of properties for thie class.
344 mergeProperties.push_back(Pr);
345 else
346 // Property protocol already exist in class. Diagnose any mismatch.
347 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
348 }
349 CatDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
350 return;
351 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000352 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
353 E = PDecl->classprop_end(); P != E; ++P) {
354 ObjCPropertyDecl *Pr = (*P);
355 ObjCInterfaceDecl::classprop_iterator CP, CE;
356 // Is this property already in class's list of properties?
357 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
358 CP != CE; ++CP)
359 if ((*CP)->getIdentifier() == Pr->getIdentifier())
360 break;
361 if (CP == CE)
362 // Add this property to list of properties for thie class.
363 mergeProperties.push_back(Pr);
364 else
365 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000366 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000367 }
368 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
369}
370
371/// MergeProtocolPropertiesIntoClass - This routine merges properties
372/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000373/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000374///
375
376void
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000377Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000378 DeclTy *MergeItsProtocols) {
379 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanianacad6d12008-12-06 23:03:39 +0000380 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
381
382 if (!IDecl) {
383 // Category
384 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
385 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
386 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
387 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
388 E = MDecl->protocol_end(); P != E; ++P)
389 // Merge properties of category (*P) into IDECL's
390 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
391
392 // Go thru the list of protocols for this category and recursively merge
393 // their properties into this class as well.
394 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
395 E = CatDecl->protocol_end(); P != E; ++P)
396 MergeProtocolPropertiesIntoClass(CatDecl, *P);
397 } else {
398 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
399 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
400 E = MD->protocol_end(); P != E; ++P)
401 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
402 }
403 return;
404 }
405
Chris Lattner5cece462008-07-21 07:06:49 +0000406 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000407 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
408 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000409 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000410 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
411
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000412 // Go thru the list of protocols for this class and recursively merge
413 // their properties into this class as well.
414 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
415 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000416 MergeProtocolPropertiesIntoClass(IDecl, *P);
417 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000418 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
419 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
420 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000421 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000422 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000423}
424
Chris Lattner855e51f2007-12-12 07:09:47 +0000425/// ActOnForwardProtocolDeclaration -
426Action::DeclTy *
427Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000428 const IdentifierLocPair *IdentList,
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000429 unsigned NumElts,
430 AttributeList *attrList) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000431 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000432
433 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000434 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000435 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000436 if (PDecl == 0) // Not already seen?
437 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Fariborz Jahanian8f9b1b22008-12-17 01:07:27 +0000438 if (attrList)
439 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner855e51f2007-12-12 07:09:47 +0000440 Protocols.push_back(PDecl);
441 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000442
443 ObjCForwardProtocolDecl *PDecl =
444 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
445 &Protocols[0], Protocols.size());
446
447 CheckObjCDeclScope(PDecl);
448 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000449}
450
Chris Lattnere705e5e2008-07-21 22:17:28 +0000451Sema::DeclTy *Sema::
452ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
453 IdentifierInfo *ClassName, SourceLocation ClassLoc,
454 IdentifierInfo *CategoryName,
455 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000456 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000457 unsigned NumProtoRefs,
458 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000459 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000460
Chris Lattnere29dc832008-03-16 20:34:23 +0000461 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000462 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000463 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000464
465 /// Check that class of this category is already completely declared.
466 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000467 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffac0580b2008-06-05 15:03:27 +0000468 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000469 /// Check for duplicate interface declaration for this category
470 ObjCCategoryDecl *CDeclChain;
471 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
472 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000473 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000474 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner65cae292008-11-19 08:23:25 +0000475 << ClassName << CategoryName;
Chris Lattnerde1bd982008-11-23 22:38:38 +0000476 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000477 break;
478 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000479 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000480 if (!CDeclChain)
481 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000482 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000483
484 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000485 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
486 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000487 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000488
489 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000490 return CDecl;
491}
492
493/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000494/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000495/// object.
496Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
497 SourceLocation AtCatImplLoc,
498 IdentifierInfo *ClassName, SourceLocation ClassLoc,
499 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000500 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000501 ObjCCategoryImplDecl *CDecl =
502 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 /// Check that class of this category is already completely declared.
504 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000505 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000506
507 /// TODO: Check that CatName, category name, is not used in another
508 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000509 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000510
511 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000512 return CDecl;
513}
514
515Sema::DeclTy *Sema::ActOnStartClassImplementation(
516 SourceLocation AtClassImplLoc,
517 IdentifierInfo *ClassName, SourceLocation ClassLoc,
518 IdentifierInfo *SuperClassname,
519 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000520 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000521 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000522 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000523 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000524 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner1336cab2008-11-23 23:12:31 +0000525 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000526 }
527 else {
528 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000529 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000530 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000531 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000532 }
533
534 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000535 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000536 if (SuperClassname) {
537 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000538 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000539 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000540 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
541 << SuperClassname;
Chris Lattner1336cab2008-11-23 23:12:31 +0000542 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000543 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000544 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000545 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000546 Diag(SuperClassLoc, diag::err_undef_superclass)
547 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000548 else if (IDecl && IDecl->getSuperClass() != SDecl) {
549 // This implementation and its interface do not have the same
550 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000551 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000552 << SDecl->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +0000553 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000554 }
555 }
556 }
557
558 if (!IDecl) {
559 // Legacy case of @implementation with no corresponding @interface.
560 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000561
562 // FIXME: Do we support attributes on the @implementation? If so
563 // we should copy them over.
Chris Lattner5cece462008-07-21 07:06:49 +0000564 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000565 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000566 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000567 IDecl->setSuperClass(SDecl);
568 IDecl->setLocEnd(ClassLoc);
569
570 // Remember that this needs to be removed when the scope is popped.
571 TUScope->AddDecl(IDecl);
572 }
573
Ted Kremenek42730c52008-01-07 19:49:32 +0000574 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000575 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
576 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000577
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000578 if (CheckObjCDeclScope(IMPDecl))
579 return IMPDecl;
580
Chris Lattner855e51f2007-12-12 07:09:47 +0000581 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000582 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000583 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000584 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000586 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000587 return IMPDecl;
588}
589
Ted Kremenek42730c52008-01-07 19:49:32 +0000590void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
591 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000592 SourceLocation RBrace) {
593 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000594 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000595 if (!IDecl)
596 return;
597 /// Check case of non-existing @interface decl.
598 /// (legacy objective-c @implementation decl without an @interface decl).
599 /// Add implementations's ivar to the synthesize class's ivar list.
600 if (IDecl->ImplicitInterfaceDecl()) {
601 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
602 return;
603 }
604 // If implementation has empty ivar list, just return.
605 if (numIvars == 0)
606 return;
607
608 assert(ivars && "missing @implementation ivars");
609
610 // Check interface's Ivar list against those in the implementation.
611 // names and types must match.
612 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000613 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000614 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000615 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
616 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 ObjCIvarDecl* ImplIvar = ivars[j++];
618 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000619 assert (ImplIvar && "missing implementation ivar");
620 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000621 if (Context.getCanonicalType(ImplIvar->getType()) !=
622 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000623 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000624 << ImplIvar->getIdentifier()
625 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner1336cab2008-11-23 23:12:31 +0000626 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000627 }
628 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
629 // as error.
630 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000631 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000632 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner1336cab2008-11-23 23:12:31 +0000633 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000634 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000635 }
636 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000637 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000638
639 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000640 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000641 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000642 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000643}
644
Steve Naroffb4f48512008-02-10 21:38:56 +0000645void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
646 bool &IncompleteImpl) {
647 if (!IncompleteImpl) {
648 Diag(ImpLoc, diag::warn_incomplete_impl);
649 IncompleteImpl = true;
650 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000651 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000652}
653
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000654void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
655 ObjCMethodDecl *IntfMethodDecl) {
656 bool err = false;
657 QualType ImpMethodQType =
658 Context.getCanonicalType(ImpMethodDecl->getResultType());
659 QualType IntfMethodQType =
660 Context.getCanonicalType(IntfMethodDecl->getResultType());
661 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
662 err = true;
663 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
664 IF=IntfMethodDecl->param_begin(),
665 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
666 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
667 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
668 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
669 err = true;
670 break;
671 }
672 }
673 if (err) {
674 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
675 << ImpMethodDecl->getDeclName();
676 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
677 }
678}
679
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000680/// FIXME: Type hierarchies in Objective-C can be deep. We could most
681/// likely improve the efficiency of selector lookups and type
682/// checking by associating with each protocol / interface / category
683/// the flattened instance tables. If we used an immutable set to keep
684/// the table then it wouldn't add significant memory cost and it
685/// would be handy for lookups.
686
Steve Naroffb268d2a2008-02-08 22:06:17 +0000687/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000688/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000689void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
690 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000691 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000692 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000693 const llvm::DenseSet<Selector> &ClsMap,
694 ObjCInterfaceDecl *IDecl) {
695 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
696
697 // If a method lookup fails locally we still need to look and see if
698 // the method was implemented by a base class or an inherited
699 // protocol. This lookup is slow, but occurs rarely in correct code
700 // and otherwise would terminate in a warning.
701
Chris Lattner855e51f2007-12-12 07:09:47 +0000702 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000703 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000704 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000705 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000706 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahanian4b871b32008-11-24 22:16:00 +0000707 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000708 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000709 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000710 }
711 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000712 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000713 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000714 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000715 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
716 !ClsMap.count(method->getSelector()) &&
717 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000718 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000719 }
Chris Lattner0be08822008-07-21 21:32:27 +0000720 // Check on this protocols's referenced protocols, recursively.
721 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
722 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000723 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000724}
725
Ted Kremenek42730c52008-01-07 19:49:32 +0000726void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
727 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000728 llvm::DenseSet<Selector> InsMap;
729 // Check and see if instance methods in class interface have been
730 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000731 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000732 E = IMPDecl->instmeth_end(); I != E; ++I)
733 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000734
735 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000736 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000737 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000738 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000739 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000740 else if (!(*I)->isSynthesized()){
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000741 ObjCMethodDecl *ImpMethodDecl =
742 IMPDecl->getInstanceMethod((*I)->getSelector());
743 ObjCMethodDecl *IntfMethodDecl =
744 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000745 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
746
Fariborz Jahanian64c46312008-12-05 01:35:25 +0000747 }
Chris Lattner9d76c722007-12-12 17:58:05 +0000748
Chris Lattner855e51f2007-12-12 07:09:47 +0000749 llvm::DenseSet<Selector> ClsMap;
750 // Check and see if class methods in class interface have been
751 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000752 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000753 E = IMPDecl->classmeth_end(); I != E; ++I)
754 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000755
Ted Kremenek42730c52008-01-07 19:49:32 +0000756 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000757 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000758 if (!ClsMap.count((*I)->getSelector()))
759 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000760 else {
761 ObjCMethodDecl *ImpMethodDecl =
762 IMPDecl->getClassMethod((*I)->getSelector());
763 ObjCMethodDecl *IntfMethodDecl =
764 IDecl->getClassMethod((*I)->getSelector());
765 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
766 }
767
Chris Lattner855e51f2007-12-12 07:09:47 +0000768
769 // Check the protocol list for unimplemented methods in the @implementation
770 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000771 const ObjCList<ObjCProtocolDecl> &Protocols =
772 IDecl->getReferencedProtocols();
773 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
774 E = Protocols.end(); I != E; ++I)
775 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000776 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000777}
778
779/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000780/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000781void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
782 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000783 llvm::DenseSet<Selector> InsMap;
784 // Check and see if instance methods in category interface have been
785 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000786 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000787 E = CatImplDecl->instmeth_end(); I != E; ++I)
788 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000789
790 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000791 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000792 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000793 if (!InsMap.count((*I)->getSelector()))
794 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000795 else {
796 ObjCMethodDecl *ImpMethodDecl =
797 CatImplDecl->getInstanceMethod((*I)->getSelector());
798 ObjCMethodDecl *IntfMethodDecl =
799 CatClassDecl->getInstanceMethod((*I)->getSelector());
800 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
801 }
Steve Naroffb4f48512008-02-10 21:38:56 +0000802
Chris Lattner855e51f2007-12-12 07:09:47 +0000803 llvm::DenseSet<Selector> ClsMap;
804 // Check and see if class methods in category interface have been
805 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000806 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000807 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
808 I != E; ++I)
809 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000810
Ted Kremenek42730c52008-01-07 19:49:32 +0000811 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000812 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000813 if (!ClsMap.count((*I)->getSelector()))
814 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian4e0f6452008-12-05 18:18:52 +0000815 else {
816 ObjCMethodDecl *ImpMethodDecl =
817 CatImplDecl->getClassMethod((*I)->getSelector());
818 ObjCMethodDecl *IntfMethodDecl =
819 CatClassDecl->getClassMethod((*I)->getSelector());
820 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
821 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000822 // Check the protocol list for unimplemented methods in the @implementation
823 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000824 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
825 E = CatClassDecl->protocol_end(); PI != E; ++PI)
826 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000827 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000828}
829
830/// ActOnForwardClassDeclaration -
831Action::DeclTy *
832Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
833 IdentifierInfo **IdentList, unsigned NumElts)
834{
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000836
837 for (unsigned i = 0; i != NumElts; ++i) {
838 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000839 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000840 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregordd861062008-12-05 18:15:24 +0000841 // Maybe we will complain about the shadowed template parameter.
842 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
843 // Just pretend that we didn't see the previous declaration.
844 PrevDecl = 0;
845 }
846
Ted Kremenek42730c52008-01-07 19:49:32 +0000847 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000848 // GCC apparently allows the following idiom:
849 //
850 // typedef NSObject < XCElementTogglerP > XCElementToggler;
851 // @class XCElementToggler;
852 //
853 // FIXME: Make an extension?
854 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
855 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000856 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner1336cab2008-11-23 23:12:31 +0000857 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffd2549972008-06-05 22:57:10 +0000858 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000859 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000860 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000861 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000862 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000863 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000864 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000865
866 // Remember that this needs to be removed when the scope is popped.
867 TUScope->AddDecl(IDecl);
868 }
869
870 Interfaces.push_back(IDecl);
871 }
872
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000873 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
874 &Interfaces[0],
875 Interfaces.size());
876
877 CheckObjCDeclScope(CDecl);
878 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000879}
880
881
882/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
883/// returns true, or false, accordingly.
884/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000885bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000886 const ObjCMethodDecl *PrevMethod,
887 bool matchBasedOnSizeAndAlignment) {
888 QualType T1 = Context.getCanonicalType(Method->getResultType());
889 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
890
891 if (T1 != T2) {
892 // The result types are different.
893 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000894 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000895 // Incomplete types don't have a size and alignment.
896 if (T1->isIncompleteType() || T2->isIncompleteType())
897 return false;
898 // Check is based on size and alignment.
899 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
900 return false;
901 }
902 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
903 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
904 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
905 if (T1 != T2) {
906 // The result types are different.
907 if (!matchBasedOnSizeAndAlignment)
908 return false;
909 // Incomplete types don't have a size and alignment.
910 if (T1->isIncompleteType() || T2->isIncompleteType())
911 return false;
912 // Check is based on size and alignment.
913 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
914 return false;
915 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000916 }
917 return true;
918}
919
Ted Kremenek42730c52008-01-07 19:49:32 +0000920void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
921 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000922 if (!FirstMethod.Method) {
923 // Haven't seen a method with this selector name yet - add it.
924 FirstMethod.Method = Method;
925 FirstMethod.Next = 0;
926 } else {
927 // We've seen a method with this name, now check the type signature(s).
928 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
929
Ted Kremenek42730c52008-01-07 19:49:32 +0000930 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000931 Next = Next->Next)
932 match = MatchTwoMethodDeclarations(Method, Next->Method);
933
934 if (!match) {
935 // We have a new signature for an existing method - add it.
936 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner3a8f2942008-11-24 03:33:13 +0000937 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner855e51f2007-12-12 07:09:47 +0000938 }
939 }
940}
941
Steve Naroffec7e0882008-10-21 10:50:19 +0000942// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +0000943ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
944 SourceRange R) {
945 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +0000946 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +0000947
948 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +0000949 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
950 // This checks if the methods differ by size & alignment.
951 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
952 issueWarning = true;
953 }
954 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +0000955 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattneref2a3c62008-11-23 23:26:13 +0000956 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000957 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000958 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattneref2a3c62008-11-23 23:26:13 +0000959 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000960 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000961 }
962 return MethList.Method;
963}
964
Ted Kremenek42730c52008-01-07 19:49:32 +0000965void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
966 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000967 if (!FirstMethod.Method) {
968 // Haven't seen a method with this selector name yet - add it.
969 FirstMethod.Method = Method;
970 FirstMethod.Next = 0;
971 } else {
972 // We've seen a method with this name, now check the type signature(s).
973 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
974
Ted Kremenek42730c52008-01-07 19:49:32 +0000975 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000976 Next = Next->Next)
977 match = MatchTwoMethodDeclarations(Method, Next->Method);
978
979 if (!match) {
980 // We have a new signature for an existing method - add it.
981 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000982 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000983 FirstMethod.Next = OMI;
984 }
985 }
986}
987
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000988/// diagnosePropertySetterGetterMismatch - Make sure that use-defined
989/// setter/getter methods have the property type and issue diagnostics
990/// if they don't.
991///
992void
993Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property,
994 const ObjCMethodDecl *GetterMethod,
995 const ObjCMethodDecl *SetterMethod) {
996 if (GetterMethod &&
Fariborz Jahanian88a62932008-12-06 21:48:16 +0000997 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +0000998 Diag(property->getLocation(),
999 diag::err_accessor_property_type_mismatch)
1000 << property->getDeclName()
1001 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001002 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1003 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001004
1005 if (SetterMethod) {
Fariborz Jahanian5c150542008-12-06 23:12:49 +00001006 if (Context.getCanonicalType(SetterMethod->getResultType())
1007 != Context.VoidTy)
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001008 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1009 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001010 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001011 Diag(property->getLocation(),
1012 diag::err_accessor_property_type_mismatch)
1013 << property->getDeclName()
1014 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian88a62932008-12-06 21:48:16 +00001015 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1016 }
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001017 }
1018}
1019
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001020// Note: For class/category implemenations, allMethods/allProperties is
1021// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +00001022void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1023 DeclTy **allMethods, unsigned allNum,
1024 DeclTy **allProperties, unsigned pNum) {
1025 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1026
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001027 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1028 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +00001029 // should be true.
1030 if (!ClassDecl)
1031 return;
1032
Ted Kremenek42730c52008-01-07 19:49:32 +00001033 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
1034 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +00001035
Ted Kremenek42730c52008-01-07 19:49:32 +00001036 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1037 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +00001038
1039 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +00001040 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1041 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001043
Seo Sanghyeon0473be42008-07-05 02:01:25 +00001044 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +00001045 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
1046 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +00001047 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1048 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
1049 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001050 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +00001051 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +00001052 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +00001053 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001054
1055 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001056 ObjCMethodDecl *Method =
1057 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +00001058
1059 if (!Method) continue; // Already issued a diagnostic.
1060 if (Method->isInstance()) {
1061 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001062 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001063 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1064 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001065 if ((isInterfaceDeclKind && PrevMethod && !match)
1066 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001067 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001068 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001069 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001070 } else {
1071 insMethods.push_back(Method);
1072 InsMap[Method->getSelector()] = Method;
1073 /// The following allows us to typecheck messages to "id".
1074 AddInstanceMethodToGlobalPool(Method);
1075 }
1076 }
1077 else {
1078 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +00001079 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +00001080 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1081 : false;
Eli Friedman70c167d2008-12-16 20:15:50 +00001082 if ((isInterfaceDeclKind && PrevMethod && !match)
1083 || (checkIdenticalMethods && match)) {
Chris Lattner1336cab2008-11-23 23:12:31 +00001084 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001085 << Method->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001086 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001087 } else {
1088 clsMethods.push_back(Method);
1089 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +00001090 /// The following allows us to typecheck messages to "Class".
1091 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +00001092 }
1093 }
1094 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001095 // Save the size so we can detect if we've added any property methods.
1096 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1097 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner855e51f2007-12-12 07:09:47 +00001098
Ted Kremenek42730c52008-01-07 19:49:32 +00001099 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001100 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +00001101 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +00001102 ComparePropertiesInBaseAndSuper(I);
1103 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001104 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001105 e = I->classprop_end(); i != e; ++i) {
1106 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1107 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001108 I->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001109 }
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001110 I->addMethods(&insMethods[0], insMethods.size(),
1111 &clsMethods[0], clsMethods.size(), AtEndLoc);
1112
Ted Kremenek42730c52008-01-07 19:49:32 +00001113 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001114 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001115 e = P->classprop_end(); i != e; ++i) {
1116 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1117 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001118 P->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001119 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001120 P->addMethods(&insMethods[0], insMethods.size(),
1121 &clsMethods[0], clsMethods.size(), AtEndLoc);
1122 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001123 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian37aaf4f2008-12-06 19:59:02 +00001124 // Categories are used to extend the class by declaring new methods.
1125 // By the same token, they are also used to add new properties. No
1126 // need to compare the added property to those in the class.
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001127
Fariborz Jahanianacad6d12008-12-06 23:03:39 +00001128 // Merge protocol properties into category
1129 MergeProtocolPropertiesIntoClass(C, C);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001130 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001131 e = C->classprop_end(); i != e; ++i) {
1132 diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1133 InsMap[(*i)->getSetterName()]);
Fariborz Jahanianecfbb492008-12-02 00:19:12 +00001134 C->addPropertyMethods(Context, *i, insMethods, InsMap);
Fariborz Jahanianfaca7972008-12-02 18:39:49 +00001135 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001136 C->addMethods(&insMethods[0], insMethods.size(),
1137 &clsMethods[0], clsMethods.size(), AtEndLoc);
1138 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001139 else if (ObjCImplementationDecl *IC =
1140 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001141 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001142 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +00001143 ImplMethodsVsClassMethods(IC, IDecl);
1144 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +00001145 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +00001146 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001147 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +00001148 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +00001149 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +00001150 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001151 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +00001152 Categories; Categories = Categories->getNextClassCategory()) {
1153 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1154 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1155 break;
1156 }
1157 }
1158 }
1159 }
Steve Naroffe2e61e72008-09-29 14:20:56 +00001160 // Add any synthesized methods to the global pool. This allows us to
1161 // handle the following, which is supported by GCC (and part of the design).
1162 //
1163 // @interface Foo
1164 // @property double bar;
1165 // @end
1166 //
1167 // void thisIsUnfortunate() {
1168 // id foo;
1169 // double bar = [foo bar];
1170 // }
1171 //
1172 if (insMethodsSizePriorToPropAdds < insMethods.size())
1173 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1174 AddInstanceMethodToGlobalPool(insMethods[i]);
1175 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1176 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1177 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner855e51f2007-12-12 07:09:47 +00001178}
1179
1180
1181/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1182/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001183static Decl::ObjCDeclQualifier
1184CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1185 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1186 if (PQTVal & ObjCDeclSpec::DQ_In)
1187 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1188 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1189 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1190 if (PQTVal & ObjCDeclSpec::DQ_Out)
1191 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1192 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1193 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1194 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1195 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1196 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1197 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001198
1199 return ret;
1200}
1201
1202Sema::DeclTy *Sema::ActOnMethodDeclaration(
1203 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001204 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001205 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001206 Selector Sel,
1207 // optional arguments. The number of types/arguments is obtained
1208 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001209 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +00001210 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1211 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001212 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001213
1214 // Make sure we can establish a context for the method.
1215 if (!ClassDecl) {
1216 Diag(MethodLoc, diag::error_missing_method_context);
1217 return 0;
1218 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001219 QualType resultDeclType;
1220
1221 if (ReturnType)
1222 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1223 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001224 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001225
Chris Lattner114add62008-03-16 00:49:28 +00001226 ObjCMethodDecl* ObjCMethod =
1227 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +00001228 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +00001229 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001230 false,
Chris Lattner114add62008-03-16 00:49:28 +00001231 MethodDeclKind == tok::objc_optional ?
1232 ObjCMethodDecl::Optional :
1233 ObjCMethodDecl::Required);
1234
Chris Lattnereee57c02008-04-04 06:12:32 +00001235 llvm::SmallVector<ParmVarDecl*, 16> Params;
1236
1237 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1238 // FIXME: arg->AttrList must be stored too!
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001239 QualType argType, originalArgType;
Chris Lattnereee57c02008-04-04 06:12:32 +00001240
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001241 if (ArgTypes[i]) {
Chris Lattnereee57c02008-04-04 06:12:32 +00001242 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001243 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001244 if (argType->isArrayType()) { // (char *[]) -> (char **)
1245 originalArgType = argType;
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001246 argType = Context.getArrayDecayedType(argType);
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001247 }
Steve Naroff46c8c7e2008-12-09 19:36:17 +00001248 else if (argType->isFunctionType())
1249 argType = Context.getPointerType(argType);
1250 } else
Chris Lattnereee57c02008-04-04 06:12:32 +00001251 argType = Context.getObjCIdType();
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001252 ParmVarDecl* Param;
1253 if (originalArgType.isNull())
1254 Param = ParmVarDecl::Create(Context, ObjCMethod,
1255 SourceLocation(/*FIXME*/),
1256 ArgNames[i], argType,
1257 VarDecl::None, 0, 0);
1258 else
1259 Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
1260 SourceLocation(/*FIXME*/),
1261 ArgNames[i], argType, originalArgType,
1262 VarDecl::None, 0, 0);
1263
Chris Lattnereee57c02008-04-04 06:12:32 +00001264 Param->setObjCDeclQualifier(
1265 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1266 Params.push_back(Param);
1267 }
1268
Ted Kremenek42730c52008-01-07 19:49:32 +00001269 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1270 ObjCMethod->setObjCDeclQualifier(
1271 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1272 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001273
1274 if (AttrList)
1275 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001276
1277 // For implementations (which can be very "coarse grain"), we add the
1278 // method now. This allows the AST to implement lookup methods that work
1279 // incrementally (without waiting until we parse the @end). It also allows
1280 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001281 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001282 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001283 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001284 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001285 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001286 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001287 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001288 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001289 }
1290 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001291 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001292 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001293 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001294 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001295 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001296 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001297 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001298 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001299 }
1300 }
1301 if (PrevMethod) {
1302 // You can never have two method definitions with the same name.
Chris Lattner1336cab2008-11-23 23:12:31 +00001303 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner3a8f2942008-11-24 03:33:13 +00001304 << ObjCMethod->getDeclName();
Chris Lattner1336cab2008-11-23 23:12:31 +00001305 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +00001306 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001307 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001308}
1309
Daniel Dunbar540ff472008-09-23 21:53:23 +00001310void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1311 SourceLocation Loc,
1312 unsigned &Attributes) {
1313 // FIXME: Improve the reported location.
1314
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001315 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar540ff472008-09-23 21:53:23 +00001316 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001317 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1318 ObjCDeclSpec::DQ_PR_assign |
1319 ObjCDeclSpec::DQ_PR_copy |
1320 ObjCDeclSpec::DQ_PR_retain))) {
1321 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1322 "readwrite" :
1323 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1324 "assign" :
1325 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1326 "copy" : "retain";
1327
Fariborz Jahanianf1892902008-12-08 19:28:10 +00001328 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1329 diag::err_objc_property_attr_mutually_exclusive :
1330 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian7ad911c2008-12-06 01:12:43 +00001331 << "readonly" << which;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001332 }
1333
1334 // Check for copy or retain on non-object types.
1335 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1336 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001337 Diag(Loc, diag::err_objc_property_requires_object)
1338 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001339 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1340 }
1341
1342 // Check for more than one of { assign, copy, retain }.
1343 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1344 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001345 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1346 << "assign" << "copy";
1347 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001348 }
1349 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001350 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1351 << "assign" << "retain";
1352 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001353 }
1354 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1355 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001356 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1357 << "copy" << "retain";
1358 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001359 }
1360 }
1361
1362 // Warn if user supplied no assignment attribute, property is
1363 // readwrite, and this is an object type.
1364 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1365 ObjCDeclSpec::DQ_PR_retain)) &&
1366 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1367 Context.isObjCObjectPointerType(PropertyTy)) {
1368 // Skip this warning in gc-only mode.
1369 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1370 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1371
1372 // If non-gc code warn that this is likely inappropriate.
1373 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1374 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1375
1376 // FIXME: Implement warning dependent on NSCopying being
1377 // implemented. See also:
1378 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1379 // (please trim this list while you are at it).
1380 }
1381}
1382
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001383Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1384 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001385 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001386 Selector GetterSel,
1387 Selector SetterSel,
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001388 DeclTy *ClassCategory,
1389 bool *isOverridingProperty,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001390 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001391 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001392 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1393 // default is readwrite!
1394 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1395 // property is defaulted to 'assign' if it is readwrite and is
1396 // not retain or copy
1397 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1398 (isReadWrite &&
1399 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1400 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1401 QualType T = GetTypeForDeclarator(FD.D, S);
1402 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001403
1404 // May modify Attributes.
1405 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001406
1407 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1408 if (!CDecl->getIdentifier()) {
1409 // This is an anonymous category. property requires special
1410 // handling.
1411 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1412 if (ObjCPropertyDecl *PIDecl =
1413 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1414 // property 'PIDecl's readonly attribute will be over-ridden
1415 // with anonymous category's readwrite property attribute!
1416 unsigned PIkind = PIDecl->getPropertyAttributes();
1417 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian965242e2008-12-08 18:47:29 +00001418 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001419 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1420 Diag(AtLoc, diag::warn_property_attr_mismatch);
1421 PIDecl->makeitReadWriteAttribute();
1422 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1423 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1424 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1425 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1426 PIDecl->setSetterName(SetterSel);
1427 // FIXME: use a common routine with addPropertyMethods.
1428 ObjCMethodDecl *SetterDecl =
1429 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1430 Context.VoidTy,
1431 ICDecl,
1432 true, false, true,
1433 ObjCMethodDecl::Required);
1434 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1435 SetterDecl,
1436 SourceLocation(),
1437 FD.D.getIdentifier(),
1438 T,
1439 VarDecl::None,
1440 0, 0);
1441 SetterDecl->setMethodParams(&Argument, 1);
1442 PIDecl->setSetterMethodDecl(SetterDecl);
1443 }
1444 else
Fariborz Jahanian3d7aa252008-12-04 22:56:16 +00001445 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001446 *isOverridingProperty = true;
1447 return 0;
1448 }
Fariborz Jahanianc1235662008-11-26 20:33:54 +00001449 // No matching property found in the main class. Just fall thru
1450 // and add property to the anonymous category. It looks like
1451 // it works as is. This category becomes just like a category
1452 // for its primary class.
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001453 } else {
1454 Diag(CDecl->getLocation(), diag::err_continuation_class);
1455 *isOverridingProperty = true;
1456 return 0;
1457 }
1458 }
Daniel Dunbar540ff472008-09-23 21:53:23 +00001459
Fariborz Jahanianf8bfa0c2008-12-16 17:51:01 +00001460 Type *t = T.getTypePtr();
1461 if (t->isArrayType() || t->isFunctionType())
1462 Diag(AtLoc, diag::err_property_type) << T;
1463
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001464 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1465 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001466 // Regardless of setter/getter attribute, we save the default getter/setter
1467 // selector names in anticipation of declaration of setter/getter methods.
1468 PDecl->setGetterName(GetterSel);
1469 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001470
Daniel Dunbar540ff472008-09-23 21:53:23 +00001471 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001472 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001473
Daniel Dunbar540ff472008-09-23 21:53:23 +00001474 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001475 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001476
Daniel Dunbar540ff472008-09-23 21:53:23 +00001477 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001478 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001479
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001480 if (isReadWrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001481 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001482
Daniel Dunbar540ff472008-09-23 21:53:23 +00001483 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001484 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001485
Daniel Dunbar540ff472008-09-23 21:53:23 +00001486 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001487 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001488
Fariborz Jahanianbcda0b42008-11-26 20:01:34 +00001489 if (isAssign)
1490 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1491
Daniel Dunbar540ff472008-09-23 21:53:23 +00001492 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001493 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001494
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001495 if (MethodImplKind == tok::objc_required)
1496 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1497 else if (MethodImplKind == tok::objc_optional)
1498 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1499
Chris Lattner855e51f2007-12-12 07:09:47 +00001500 return PDecl;
1501}
1502
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001503/// ActOnPropertyImplDecl - This routine performs semantic checks and
1504/// builds the AST node for a property implementation declaration; declared
1505/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001506///
1507Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1508 SourceLocation PropertyLoc,
1509 bool Synthesize,
1510 DeclTy *ClassCatImpDecl,
1511 IdentifierInfo *PropertyId,
1512 IdentifierInfo *PropertyIvar) {
1513 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1514 // Make sure we have a context for the property implementation declaration.
1515 if (!ClassImpDecl) {
1516 Diag(AtLoc, diag::error_missing_property_context);
1517 return 0;
1518 }
1519 ObjCPropertyDecl *property = 0;
1520 ObjCInterfaceDecl* IDecl = 0;
1521 // Find the class or category class where this property must have
1522 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001523 ObjCImplementationDecl *IC = 0;
1524 ObjCCategoryImplDecl* CatImplClass = 0;
1525 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001526 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001527 // We always synthesize an interface for an implementation
1528 // without an interface decl. So, IDecl is always non-zero.
1529 assert(IDecl &&
1530 "ActOnPropertyImplDecl - @implementation without @interface");
1531
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001532 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001533 property = IDecl->FindPropertyDeclaration(PropertyId);
1534 if (!property) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001535 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001536 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001537 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001538 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001539 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001540 if (Synthesize) {
1541 Diag(AtLoc, diag::error_synthesize_category_decl);
1542 return 0;
1543 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001544 IDecl = CatImplClass->getClassInterface();
1545 if (!IDecl) {
1546 Diag(AtLoc, diag::error_missing_property_interface);
1547 return 0;
1548 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001549 ObjCCategoryDecl *Category =
1550 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1551
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001552 // If category for this implementation not found, it is an error which
1553 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001554 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001555 return 0;
1556 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001557 property = Category->FindPropertyDeclaration(PropertyId);
1558 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001559 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattner271d4c22008-11-24 05:29:24 +00001560 << Category->getDeclName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001561 return 0;
1562 }
1563 }
1564 else {
1565 Diag(AtLoc, diag::error_bad_property_context);
1566 return 0;
1567 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001568 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001569 // Check that we have a valid, previously declared ivar for @synthesize
1570 if (Synthesize) {
1571 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001572 if (!PropertyIvar)
1573 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001574 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001575 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001576 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001577 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001578 return 0;
1579 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001580 QualType PropType = Context.getCanonicalType(property->getType());
1581 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1582
Steve Naroff631e3922008-09-30 00:24:17 +00001583 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001584 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001585 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001586 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattner271d4c22008-11-24 05:29:24 +00001587 << property->getDeclName() << Ivar->getDeclName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001588 return 0;
1589 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001590 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001591 } else if (PropertyIvar) {
1592 // @dynamic
1593 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1594 return 0;
1595 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001596 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001597 ObjCPropertyImplDecl *PIDecl =
1598 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1599 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001600 ObjCPropertyImplDecl::Synthesize
1601 : ObjCPropertyImplDecl::Dynamic),
1602 Ivar);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001603 if (IC) {
1604 if (Synthesize)
1605 if (ObjCPropertyImplDecl *PPIDecl =
1606 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1607 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1608 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1609 << PropertyIvar;
1610 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1611 }
1612
1613 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1614 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1615 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1616 return 0;
1617 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001618 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001619 }
1620 else {
1621 if (Synthesize)
1622 if (ObjCPropertyImplDecl *PPIDecl =
1623 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1624 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1625 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1626 << PropertyIvar;
1627 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1628 }
1629
1630 if (ObjCPropertyImplDecl *PPIDecl =
1631 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1632 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1633 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1634 return 0;
1635 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001636 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanian68342282008-12-05 22:32:48 +00001637 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001638
1639 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001640}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001641
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001642bool Sema::CheckObjCDeclScope(Decl *D) {
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001643 if (isa<TranslationUnitDecl>(CurContext))
1644 return false;
1645
1646 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1647 D->setInvalidDecl();
1648
1649 return true;
1650}
Chris Lattner9bb9abf2008-12-17 07:13:27 +00001651
1652/// Collect the instance variables declared in an Objective-C object. Used in
1653/// the creation of structures from objects using the @defs directive.
1654/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1655/// part of the AST generation logic of @defs.
1656static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1657 ASTContext& Ctx,
1658 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1659 if (Class->getSuperClass())
1660 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1661
1662 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1663 for (ObjCInterfaceDecl::ivar_iterator
1664 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1665
1666 ObjCIvarDecl* ID = *I;
1667 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1668 ID->getLocation(),
1669 ID->getIdentifier(),
1670 ID->getType(),
1671 ID->getBitWidth()));
1672 }
1673}
1674
1675/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1676/// instance variables of ClassName into Decls.
1677void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1678 IdentifierInfo *ClassName,
1679 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1680 // Check that ClassName is a valid class
1681 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1682 if (!Class) {
1683 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1684 return;
1685 }
1686 // Collect the instance variables
1687 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1688
1689 // Introduce all of these fields into the appropriate scope.
1690 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1691 D != Decls.end(); ++D) {
1692 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1693 if (getLangOptions().CPlusPlus)
1694 PushOnScopeChains(cast<FieldDecl>(FD), S);
1695 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
1696 Record->addDecl(Context, FD);
1697 }
1698}
1699