blob: c030a370e40de5fd18ddc5dd3e676b678de93c8e [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner855e51f2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner855e51f2007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremenek42730c52008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff3ac43f92008-07-25 17:57:26 +000026 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28 // If we don't have a valid method decl, simply return.
29 if (!MDecl)
30 return;
Steve Narofffe9eb6a2007-12-18 01:30:32 +000031
32 // Allow the rest of sema to find private method decl implementations.
33 if (MDecl->isInstance())
34 AddInstanceMethodToGlobalPool(MDecl);
35 else
36 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerf3874bc2008-04-06 04:47:34 +000039 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000040
41 // Create Decl objects for each parameter, entrring them in the scope for
42 // binding to their use.
Chris Lattner855e51f2007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Daniel Dunbareaf91c32008-08-26 06:07:48 +000045 MDecl->createImplicitParams(Context);
Chris Lattner855e51f2007-12-12 07:09:47 +000046
Daniel Dunbareaf91c32008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +000049
Chris Lattner97316c02008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000056 }
57}
58
Chris Lattnere705e5e2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattnerae1ae492008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000069 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +000070 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +000071 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
72 }
73
Ted Kremenek42730c52008-01-07 19:49:32 +000074 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000075 if (IDecl) {
76 // Class already seen. Is it a forward declaration?
Steve Naroff1422a622008-11-18 19:15:30 +000077 if (!IDecl->isForwardDecl()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +000078 Diag(AtInterfaceLoc, diag::err_duplicate_class_def) << IDecl->getName();
Chris Lattner5b250652008-11-23 22:46:27 +000079 Diag(IDecl->getLocation(), diag::note_previous_definition);
80
Steve Naroff1422a622008-11-18 19:15:30 +000081 // Return the previous class interface.
82 // FIXME: don't leak the objects passed in!
83 return IDecl;
84 } else {
Chris Lattner855e51f2007-12-12 07:09:47 +000085 IDecl->setLocation(AtInterfaceLoc);
86 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000087 }
Chris Lattner5cece462008-07-21 07:06:49 +000088 } else {
Daniel Dunbard8bd6822008-08-20 18:02:42 +000089 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000090 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000091 if (AttrList)
92 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000093
Steve Naroff15208162008-04-02 18:30:49 +000094 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +000095 // Remember that this needs to be removed when the scope is popped.
96 TUScope->AddDecl(IDecl);
97 }
98
99 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000100 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000101 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000102 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000103 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000104 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000105 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
106 }
107 else {
108 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner65cae292008-11-19 08:23:25 +0000110
111 if (!SuperClassEntry)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000112 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner65cae292008-11-19 08:23:25 +0000113 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
114 else if (SuperClassEntry->isForwardDecl())
115 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattnerb1753422008-11-23 21:45:46 +0000116 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner65cae292008-11-19 08:23:25 +0000117 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000118 }
119 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000120 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000121 IDecl->setLocEnd(SuperLoc);
122 } else { // we have a root class.
123 IDecl->setLocEnd(ClassLoc);
124 }
125
Steve Naroff1422a622008-11-18 19:15:30 +0000126 /// Check then save referenced protocols.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000127 if (NumProtoRefs) {
128 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000129 IDecl->setLocEnd(EndProtoLoc);
130 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000131
132 CheckObjCDeclScope(IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000133 return IDecl;
134}
135
136/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000137/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000138Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
139 IdentifierInfo *AliasName,
140 SourceLocation AliasLocation,
141 IdentifierInfo *ClassName,
142 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000143 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000144 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000145 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000146 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000147 Diag(AliasLocation, diag::warn_previous_alias_decl);
148 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
149 }
150 else {
Chris Lattner65cae292008-11-19 08:23:25 +0000151 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000152 Diag(ADecl->getLocation(), diag::err_previous_declaration);
153 }
154 return 0;
155 }
156 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000157 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000158 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
159 if (CDecl == 0) {
Chris Lattner65cae292008-11-19 08:23:25 +0000160 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner2d1c4312008-03-16 21:17:37 +0000161 if (CDeclU)
162 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000163 return 0;
164 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000165
166 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000168 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
169
170 ObjCAliasDecls[AliasName] = AliasDecl;
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000171
172 if (!CheckObjCDeclScope(AliasDecl))
173 TUScope->AddDecl(AliasDecl);
174
Chris Lattner855e51f2007-12-12 07:09:47 +0000175 return AliasDecl;
176}
177
Chris Lattner2bdedd62008-07-26 04:03:38 +0000178Sema::DeclTy *
179Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
180 IdentifierInfo *ProtocolName,
181 SourceLocation ProtocolLoc,
182 DeclTy * const *ProtoRefs,
183 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000184 SourceLocation EndProtoLoc,
185 AttributeList *AttrList) {
186 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000187 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000188 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000189 if (PDecl) {
190 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000191 if (!PDecl->isForwardDecl()) {
Chris Lattner65cae292008-11-19 08:23:25 +0000192 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattner5b250652008-11-23 22:46:27 +0000193 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerc1881852008-03-16 01:25:17 +0000194 // Just return the protocol we already had.
195 // FIXME: don't leak the objects passed in!
196 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000197 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000198 // Make sure the cached decl gets a valid start location.
199 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000200 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000201 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000202 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000203 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000204 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000205 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000206
207 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000208 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000209 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000210 PDecl->setLocEnd(EndProtoLoc);
211 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000212
213 CheckObjCDeclScope(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000214 return PDecl;
215}
216
217/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000218/// issues an error if they are not declared. It returns list of
219/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000220void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000221Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000222 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000223 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000224 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000226 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
227 if (!PDecl) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000228 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner65cae292008-11-19 08:23:25 +0000229 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000230 continue;
231 }
232
233 // If this is a forward declaration and we are supposed to warn in this
234 // case, do it.
235 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000236 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner65cae292008-11-19 08:23:25 +0000237 << ProtocolId[i].first;
Chris Lattner17d50a92008-07-26 03:47:43 +0000238 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000239 }
240}
241
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000242/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000243/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000244///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000245void
246Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
247 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000248 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000249 ObjCPropertyDecl::PropertyAttributeKind CAttr =
250 Property->getPropertyAttributes();
251 ObjCPropertyDecl::PropertyAttributeKind SAttr =
252 SuperProperty->getPropertyAttributes();
253 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
254 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattner8ba580c2008-11-19 05:08:23 +0000255 Diag(Property->getLocation(), diag::warn_readonly_property)
256 << Property->getName() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000257 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
258 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattner70b93d82008-11-18 22:52:51 +0000259 Diag(Property->getLocation(), diag::warn_property_attribute)
260 << Property->getName() << "copy" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000261 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
262 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattner70b93d82008-11-18 22:52:51 +0000263 Diag(Property->getLocation(), diag::warn_property_attribute)
264 << Property->getName() << "retain" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000265
266 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
267 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattner70b93d82008-11-18 22:52:51 +0000268 Diag(Property->getLocation(), diag::warn_property_attribute)
269 << Property->getName() << "atomic" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000270 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000271 Diag(Property->getLocation(), diag::warn_property_attribute)
272 << Property->getName() << "setter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000273 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattner70b93d82008-11-18 22:52:51 +0000274 Diag(Property->getLocation(), diag::warn_property_attribute)
275 << Property->getName() << "getter" << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000276
Chris Lattnerc5cff302008-07-26 20:50:02 +0000277 if (Context.getCanonicalType(Property->getType()) !=
278 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattner70b93d82008-11-18 22:52:51 +0000279 Diag(Property->getLocation(), diag::warn_property_type)
280 << Property->getType().getAsString() << inheritedName;
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000281
282}
283
284/// ComparePropertiesInBaseAndSuper - This routine compares property
285/// declarations in base and its super class, if any, and issues
286/// diagnostics in a variety of inconsistant situations.
287///
288void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000289Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000290 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
291 if (!SDecl)
292 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000293 // FIXME: O(N^2)
Fariborz Jahanianed986602008-05-01 00:03:38 +0000294 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
295 E = SDecl->classprop_end(); S != E; ++S) {
296 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000297 // Does property in super class has declaration in current class?
298 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
299 E = IDecl->classprop_end(); I != E; ++I) {
300 ObjCPropertyDecl *PDecl = (*I);
301 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000302 DiagnosePropertyMismatch(PDecl, SuperPDecl,
303 SDecl->getIdentifierName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000304 }
305 }
306}
307
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000308/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
309/// of properties declared in a protocol and adds them to the list
310/// of properties for current class if it is not there already.
311void
312Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
313 ObjCProtocolDecl *PDecl)
314{
315 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
316 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
317 E = PDecl->classprop_end(); P != E; ++P) {
318 ObjCPropertyDecl *Pr = (*P);
319 ObjCInterfaceDecl::classprop_iterator CP, CE;
320 // Is this property already in class's list of properties?
321 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
322 CP != CE; ++CP)
323 if ((*CP)->getIdentifier() == Pr->getIdentifier())
324 break;
325 if (CP == CE)
326 // Add this property to list of properties for thie class.
327 mergeProperties.push_back(Pr);
328 else
329 // Property protocol already exist in class. Diagnose any mismatch.
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000330 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifierName());
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000331 }
332 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
333}
334
335/// MergeProtocolPropertiesIntoClass - This routine merges properties
336/// declared in 'MergeItsProtocols' objects (which can be a class or an
337/// inherited protocol into the list of properties for class 'IDecl'
338///
339
340void
341Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
342 DeclTy *MergeItsProtocols) {
343 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000344 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000345 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
346 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000347 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000348 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
349
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000350 // Go thru the list of protocols for this class and recursively merge
351 // their properties into this class as well.
352 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
353 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000354 MergeProtocolPropertiesIntoClass(IDecl, *P);
355 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000356 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
357 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
358 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000359 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000360 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000361}
362
Chris Lattner855e51f2007-12-12 07:09:47 +0000363/// ActOnForwardProtocolDeclaration -
364Action::DeclTy *
365Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000366 const IdentifierLocPair *IdentList,
367 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000368 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000369
370 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000371 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000372 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000373 if (PDecl == 0) // Not already seen?
374 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000375
376 Protocols.push_back(PDecl);
377 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000378
379 ObjCForwardProtocolDecl *PDecl =
380 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
381 &Protocols[0], Protocols.size());
382
383 CheckObjCDeclScope(PDecl);
384 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000385}
386
Chris Lattnere705e5e2008-07-21 22:17:28 +0000387Sema::DeclTy *Sema::
388ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
389 IdentifierInfo *ClassName, SourceLocation ClassLoc,
390 IdentifierInfo *CategoryName,
391 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000392 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000393 unsigned NumProtoRefs,
394 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000395 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000396
Chris Lattnere29dc832008-03-16 20:34:23 +0000397 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000398 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000399 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000400
401 /// Check that class of this category is already completely declared.
402 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000403 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffac0580b2008-06-05 15:03:27 +0000404 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000405 /// Check for duplicate interface declaration for this category
406 ObjCCategoryDecl *CDeclChain;
407 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
408 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000409 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000410 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner65cae292008-11-19 08:23:25 +0000411 << ClassName << CategoryName;
Chris Lattnerde1bd982008-11-23 22:38:38 +0000412 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000413 break;
414 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000415 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000416 if (!CDeclChain)
417 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000418 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000419
420 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000421 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
422 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000423 }
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000424
425 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000426 return CDecl;
427}
428
429/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000430/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000431/// object.
432Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
433 SourceLocation AtCatImplLoc,
434 IdentifierInfo *ClassName, SourceLocation ClassLoc,
435 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000436 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000437 ObjCCategoryImplDecl *CDecl =
438 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000439 /// Check that class of this category is already completely declared.
440 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner65cae292008-11-19 08:23:25 +0000441 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000442
443 /// TODO: Check that CatName, category name, is not used in another
444 // implementation.
Steve Naroff4b9846d2008-09-28 14:55:53 +0000445 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000446
447 CheckObjCDeclScope(CDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000448 return CDecl;
449}
450
451Sema::DeclTy *Sema::ActOnStartClassImplementation(
452 SourceLocation AtClassImplLoc,
453 IdentifierInfo *ClassName, SourceLocation ClassLoc,
454 IdentifierInfo *SuperClassname,
455 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000456 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000457 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000458 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000459 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000460 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000461 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
462 }
463 else {
464 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000465 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000466 if (!IDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000467 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000468 }
469
470 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000471 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000472 if (SuperClassname) {
473 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000474 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000475 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner65cae292008-11-19 08:23:25 +0000476 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
477 << SuperClassname;
Chris Lattner855e51f2007-12-12 07:09:47 +0000478 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
Chris Lattner65cae292008-11-19 08:23:25 +0000479 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000480 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000481 if (!SDecl)
Chris Lattner65cae292008-11-19 08:23:25 +0000482 Diag(SuperClassLoc, diag::err_undef_superclass)
483 << SuperClassname << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000484 else if (IDecl && IDecl->getSuperClass() != SDecl) {
485 // This implementation and its interface do not have the same
486 // super class.
Chris Lattner65cae292008-11-19 08:23:25 +0000487 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnerb1753422008-11-23 21:45:46 +0000488 << SDecl->getDeclName();
Chris Lattner855e51f2007-12-12 07:09:47 +0000489 Diag(SDecl->getLocation(), diag::err_previous_definition);
490 }
491 }
492 }
493
494 if (!IDecl) {
495 // Legacy case of @implementation with no corresponding @interface.
496 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000497
498 // FIXME: Do we support attributes on the @implementation? If so
499 // we should copy them over.
Chris Lattner5cece462008-07-21 07:06:49 +0000500 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000501 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000502 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 IDecl->setSuperClass(SDecl);
504 IDecl->setLocEnd(ClassLoc);
505
506 // Remember that this needs to be removed when the scope is popped.
507 TUScope->AddDecl(IDecl);
508 }
509
Ted Kremenek42730c52008-01-07 19:49:32 +0000510 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000511 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
512 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000513
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000514 if (CheckObjCDeclScope(IMPDecl))
515 return IMPDecl;
516
Chris Lattner855e51f2007-12-12 07:09:47 +0000517 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000518 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000519 // FIXME: Don't leak everything!
Chris Lattner65cae292008-11-19 08:23:25 +0000520 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner855e51f2007-12-12 07:09:47 +0000521 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000522 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000523 return IMPDecl;
524}
525
Ted Kremenek42730c52008-01-07 19:49:32 +0000526void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
527 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000528 SourceLocation RBrace) {
529 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000530 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000531 if (!IDecl)
532 return;
533 /// Check case of non-existing @interface decl.
534 /// (legacy objective-c @implementation decl without an @interface decl).
535 /// Add implementations's ivar to the synthesize class's ivar list.
536 if (IDecl->ImplicitInterfaceDecl()) {
537 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
538 return;
539 }
540 // If implementation has empty ivar list, just return.
541 if (numIvars == 0)
542 return;
543
544 assert(ivars && "missing @implementation ivars");
545
546 // Check interface's Ivar list against those in the implementation.
547 // names and types must match.
548 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000549 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000550 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000551 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
552 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000553 ObjCIvarDecl* ImplIvar = ivars[j++];
554 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000555 assert (ImplIvar && "missing implementation ivar");
556 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000557 if (Context.getCanonicalType(ImplIvar->getType()) !=
558 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000559 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnerb1753422008-11-23 21:45:46 +0000560 << ImplIvar->getIdentifier()
561 << ImplIvar->getType() << ClsIvar->getType();
562 Diag(ClsIvar->getLocation(), diag::err_previous_definition);
Chris Lattner855e51f2007-12-12 07:09:47 +0000563 }
564 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
565 // as error.
566 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000567 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnerb1753422008-11-23 21:45:46 +0000568 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
569 Diag(ClsIvar->getLocation(), diag::err_previous_definition);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000570 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000571 }
572 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000573 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000574
575 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000576 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000577 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000578 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000579}
580
Steve Naroffb4f48512008-02-10 21:38:56 +0000581void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
582 bool &IncompleteImpl) {
583 if (!IncompleteImpl) {
584 Diag(ImpLoc, diag::warn_incomplete_impl);
585 IncompleteImpl = true;
586 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000587 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroffb4f48512008-02-10 21:38:56 +0000588}
589
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000590/// FIXME: Type hierarchies in Objective-C can be deep. We could most
591/// likely improve the efficiency of selector lookups and type
592/// checking by associating with each protocol / interface / category
593/// the flattened instance tables. If we used an immutable set to keep
594/// the table then it wouldn't add significant memory cost and it
595/// would be handy for lookups.
596
Steve Naroffb268d2a2008-02-08 22:06:17 +0000597/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000598/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000599void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
600 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000601 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000602 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000603 const llvm::DenseSet<Selector> &ClsMap,
604 ObjCInterfaceDecl *IDecl) {
605 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
606
607 // If a method lookup fails locally we still need to look and see if
608 // the method was implemented by a base class or an inherited
609 // protocol. This lookup is slow, but occurs rarely in correct code
610 // and otherwise would terminate in a warning.
611
Chris Lattner855e51f2007-12-12 07:09:47 +0000612 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000613 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000614 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000615 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000616 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
617 !InsMap.count(method->getSelector()) &&
618 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000619 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000620 }
621 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000622 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000623 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000624 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000625 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
626 !ClsMap.count(method->getSelector()) &&
627 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000628 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000629 }
Chris Lattner0be08822008-07-21 21:32:27 +0000630 // Check on this protocols's referenced protocols, recursively.
631 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
632 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000633 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000634}
635
Ted Kremenek42730c52008-01-07 19:49:32 +0000636void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
637 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000638 llvm::DenseSet<Selector> InsMap;
639 // Check and see if instance methods in class interface have been
640 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000641 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000642 E = IMPDecl->instmeth_end(); I != E; ++I)
643 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000644
645 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000646 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000647 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000648 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000649 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000650
Chris Lattner855e51f2007-12-12 07:09:47 +0000651 llvm::DenseSet<Selector> ClsMap;
652 // Check and see if class methods in class interface have been
653 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000654 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000655 E = IMPDecl->classmeth_end(); I != E; ++I)
656 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000657
Ted Kremenek42730c52008-01-07 19:49:32 +0000658 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000659 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000660 if (!ClsMap.count((*I)->getSelector()))
661 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000662
663 // Check the protocol list for unimplemented methods in the @implementation
664 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000665 const ObjCList<ObjCProtocolDecl> &Protocols =
666 IDecl->getReferencedProtocols();
667 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
668 E = Protocols.end(); I != E; ++I)
669 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000670 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000671}
672
673/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000674/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000675void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
676 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000677 llvm::DenseSet<Selector> InsMap;
678 // Check and see if instance methods in category interface have been
679 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000680 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000681 E = CatImplDecl->instmeth_end(); I != E; ++I)
682 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000683
684 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000686 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000687 if (!InsMap.count((*I)->getSelector()))
688 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
689
Chris Lattner855e51f2007-12-12 07:09:47 +0000690 llvm::DenseSet<Selector> ClsMap;
691 // Check and see if class methods in category interface have been
692 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000693 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000694 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
695 I != E; ++I)
696 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000697
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000699 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000700 if (!ClsMap.count((*I)->getSelector()))
701 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000702
703 // Check the protocol list for unimplemented methods in the @implementation
704 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000705 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
706 E = CatClassDecl->protocol_end(); PI != E; ++PI)
707 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000708 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000709}
710
711/// ActOnForwardClassDeclaration -
712Action::DeclTy *
713Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
714 IdentifierInfo **IdentList, unsigned NumElts)
715{
Ted Kremenek42730c52008-01-07 19:49:32 +0000716 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000717
718 for (unsigned i = 0; i != NumElts; ++i) {
719 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000720 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000721 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000722 // GCC apparently allows the following idiom:
723 //
724 // typedef NSObject < XCElementTogglerP > XCElementToggler;
725 // @class XCElementToggler;
726 //
727 // FIXME: Make an extension?
728 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
729 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner65cae292008-11-19 08:23:25 +0000730 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Steve Naroffd2549972008-06-05 22:57:10 +0000731 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
732 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000733 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000734 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000735 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000736 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000737 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000738 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000739
740 // Remember that this needs to be removed when the scope is popped.
741 TUScope->AddDecl(IDecl);
742 }
743
744 Interfaces.push_back(IDecl);
745 }
746
Anders Carlsson0a6ab172008-11-04 16:57:32 +0000747 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
748 &Interfaces[0],
749 Interfaces.size());
750
751 CheckObjCDeclScope(CDecl);
752 return CDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000753}
754
755
756/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
757/// returns true, or false, accordingly.
758/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000759bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Naroffb91afca2008-10-21 10:37:50 +0000760 const ObjCMethodDecl *PrevMethod,
761 bool matchBasedOnSizeAndAlignment) {
762 QualType T1 = Context.getCanonicalType(Method->getResultType());
763 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
764
765 if (T1 != T2) {
766 // The result types are different.
767 if (!matchBasedOnSizeAndAlignment)
Chris Lattner855e51f2007-12-12 07:09:47 +0000768 return false;
Steve Naroffb91afca2008-10-21 10:37:50 +0000769 // Incomplete types don't have a size and alignment.
770 if (T1->isIncompleteType() || T2->isIncompleteType())
771 return false;
772 // Check is based on size and alignment.
773 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
774 return false;
775 }
776 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
777 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
778 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
779 if (T1 != T2) {
780 // The result types are different.
781 if (!matchBasedOnSizeAndAlignment)
782 return false;
783 // Incomplete types don't have a size and alignment.
784 if (T1->isIncompleteType() || T2->isIncompleteType())
785 return false;
786 // Check is based on size and alignment.
787 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
788 return false;
789 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000790 }
791 return true;
792}
793
Ted Kremenek42730c52008-01-07 19:49:32 +0000794void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
795 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000796 if (!FirstMethod.Method) {
797 // Haven't seen a method with this selector name yet - add it.
798 FirstMethod.Method = Method;
799 FirstMethod.Next = 0;
800 } else {
801 // We've seen a method with this name, now check the type signature(s).
802 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
803
Ted Kremenek42730c52008-01-07 19:49:32 +0000804 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000805 Next = Next->Next)
806 match = MatchTwoMethodDeclarations(Method, Next->Method);
807
808 if (!match) {
809 // We have a new signature for an existing method - add it.
810 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000811 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000812 FirstMethod.Next = OMI;
813 }
814 }
815}
816
Steve Naroffec7e0882008-10-21 10:50:19 +0000817// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff68354f32008-09-30 14:38:43 +0000818ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
819 SourceRange R) {
820 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Naroffb91afca2008-10-21 10:37:50 +0000821 bool issueWarning = false;
Steve Naroff68354f32008-09-30 14:38:43 +0000822
823 if (MethList.Method && MethList.Next) {
Steve Naroffb91afca2008-10-21 10:37:50 +0000824 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
825 // This checks if the methods differ by size & alignment.
826 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
827 issueWarning = true;
828 }
829 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000830 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel.getName() << R;
Chris Lattner9d2cf082008-11-19 05:27:50 +0000831 Diag(MethList.Method->getLocStart(), diag::warn_using_decl)
832 << MethList.Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000833 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner9d2cf082008-11-19 05:27:50 +0000834 Diag(Next->Method->getLocStart(), diag::warn_also_found_decl)
835 << Next->Method->getSourceRange();
Steve Naroff68354f32008-09-30 14:38:43 +0000836 }
837 return MethList.Method;
838}
839
Ted Kremenek42730c52008-01-07 19:49:32 +0000840void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
841 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000842 if (!FirstMethod.Method) {
843 // Haven't seen a method with this selector name yet - add it.
844 FirstMethod.Method = Method;
845 FirstMethod.Next = 0;
846 } else {
847 // We've seen a method with this name, now check the type signature(s).
848 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
849
Ted Kremenek42730c52008-01-07 19:49:32 +0000850 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000851 Next = Next->Next)
852 match = MatchTwoMethodDeclarations(Method, Next->Method);
853
854 if (!match) {
855 // We have a new signature for an existing method - add it.
856 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000857 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000858 FirstMethod.Next = OMI;
859 }
860 }
861}
862
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000863// Note: For class/category implemenations, allMethods/allProperties is
864// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000865void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
866 DeclTy **allMethods, unsigned allNum,
867 DeclTy **allProperties, unsigned pNum) {
868 Decl *ClassDecl = static_cast<Decl *>(classDecl);
869
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000870 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
871 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000872 // should be true.
873 if (!ClassDecl)
874 return;
875
Ted Kremenek42730c52008-01-07 19:49:32 +0000876 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
877 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000878
Ted Kremenek42730c52008-01-07 19:49:32 +0000879 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
880 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000881
882 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000883 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
884 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000885 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000886
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000887 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000888 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
889 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000890 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
891 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
892 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000893 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000894 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000895 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000896 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000897
898 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000899 ObjCMethodDecl *Method =
900 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000901
902 if (!Method) continue; // Already issued a diagnostic.
903 if (Method->isInstance()) {
904 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000905 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000906 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
907 : false;
908 if (isInterfaceDeclKind && PrevMethod && !match
909 || checkIdenticalMethods && match) {
Chris Lattner8d756812008-11-20 06:13:02 +0000910 Diag(Method->getLocation(), diag::error_duplicate_method_decl)
911 << Method->getSelector().getName();
Chris Lattner855e51f2007-12-12 07:09:47 +0000912 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
913 } else {
914 insMethods.push_back(Method);
915 InsMap[Method->getSelector()] = Method;
916 /// The following allows us to typecheck messages to "id".
917 AddInstanceMethodToGlobalPool(Method);
918 }
919 }
920 else {
921 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000922 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000923 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
924 : false;
925 if (isInterfaceDeclKind && PrevMethod && !match
926 || checkIdenticalMethods && match) {
Chris Lattner8d756812008-11-20 06:13:02 +0000927 Diag(Method->getLocation(), diag::error_duplicate_method_decl)
928 << Method->getSelector().getName();
Chris Lattner855e51f2007-12-12 07:09:47 +0000929 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
930 } else {
931 clsMethods.push_back(Method);
932 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000933 /// The following allows us to typecheck messages to "Class".
934 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000935 }
936 }
937 }
Steve Naroffe2e61e72008-09-29 14:20:56 +0000938 // Save the size so we can detect if we've added any property methods.
939 unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
940 unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
Chris Lattner855e51f2007-12-12 07:09:47 +0000941
Ted Kremenek42730c52008-01-07 19:49:32 +0000942 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000943 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +0000944 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000945 ComparePropertiesInBaseAndSuper(I);
946 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000947 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
948 e = I->classprop_end(); i != e; ++i)
949 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000950 I->addMethods(&insMethods[0], insMethods.size(),
951 &clsMethods[0], clsMethods.size(), AtEndLoc);
952
Ted Kremenek42730c52008-01-07 19:49:32 +0000953 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000954 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
955 e = P->classprop_end(); i != e; ++i)
956 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner855e51f2007-12-12 07:09:47 +0000957 P->addMethods(&insMethods[0], insMethods.size(),
958 &clsMethods[0], clsMethods.size(), AtEndLoc);
959 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000960 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000961 // FIXME: Need to compare properties to those in interface?
962
963 // FIXME: If we merge properties into class we should probably
964 // merge them into category as well?
965 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
966 e = C->classprop_end(); i != e; ++i)
967 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner855e51f2007-12-12 07:09:47 +0000968 C->addMethods(&insMethods[0], insMethods.size(),
969 &clsMethods[0], clsMethods.size(), AtEndLoc);
970 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000971 else if (ObjCImplementationDecl *IC =
972 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000973 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000974 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000975 ImplMethodsVsClassMethods(IC, IDecl);
976 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000977 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000978 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000979 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000980 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000981 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +0000982 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000983 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000984 Categories; Categories = Categories->getNextClassCategory()) {
985 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
986 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
987 break;
988 }
989 }
990 }
991 }
Steve Naroffe2e61e72008-09-29 14:20:56 +0000992 // Add any synthesized methods to the global pool. This allows us to
993 // handle the following, which is supported by GCC (and part of the design).
994 //
995 // @interface Foo
996 // @property double bar;
997 // @end
998 //
999 // void thisIsUnfortunate() {
1000 // id foo;
1001 // double bar = [foo bar];
1002 // }
1003 //
1004 if (insMethodsSizePriorToPropAdds < insMethods.size())
1005 for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1006 AddInstanceMethodToGlobalPool(insMethods[i]);
1007 if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1008 for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1009 AddFactoryMethodToGlobalPool(clsMethods[i]);
Chris Lattner855e51f2007-12-12 07:09:47 +00001010}
1011
1012
1013/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1014/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +00001015static Decl::ObjCDeclQualifier
1016CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1017 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1018 if (PQTVal & ObjCDeclSpec::DQ_In)
1019 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1020 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1021 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1022 if (PQTVal & ObjCDeclSpec::DQ_Out)
1023 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1024 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1025 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1026 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1027 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1028 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1029 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +00001030
1031 return ret;
1032}
1033
1034Sema::DeclTy *Sema::ActOnMethodDeclaration(
1035 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +00001036 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001037 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +00001038 Selector Sel,
1039 // optional arguments. The number of types/arguments is obtained
1040 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +00001041 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +00001042 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1043 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +00001044 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +00001045
1046 // Make sure we can establish a context for the method.
1047 if (!ClassDecl) {
1048 Diag(MethodLoc, diag::error_missing_method_context);
1049 return 0;
1050 }
Chris Lattner855e51f2007-12-12 07:09:47 +00001051 QualType resultDeclType;
1052
1053 if (ReturnType)
1054 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1055 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +00001056 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +00001057
Chris Lattner114add62008-03-16 00:49:28 +00001058 ObjCMethodDecl* ObjCMethod =
1059 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +00001060 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +00001061 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +00001062 false,
Chris Lattner114add62008-03-16 00:49:28 +00001063 MethodDeclKind == tok::objc_optional ?
1064 ObjCMethodDecl::Optional :
1065 ObjCMethodDecl::Required);
1066
Chris Lattnereee57c02008-04-04 06:12:32 +00001067 llvm::SmallVector<ParmVarDecl*, 16> Params;
1068
1069 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1070 // FIXME: arg->AttrList must be stored too!
1071 QualType argType;
1072
1073 if (ArgTypes[i])
1074 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1075 else
1076 argType = Context.getObjCIdType();
1077 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1078 SourceLocation(/*FIXME*/),
1079 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001080 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +00001081 Param->setObjCDeclQualifier(
1082 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1083 Params.push_back(Param);
1084 }
1085
Ted Kremenek42730c52008-01-07 19:49:32 +00001086 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1087 ObjCMethod->setObjCDeclQualifier(
1088 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1089 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001090
1091 if (AttrList)
1092 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001093
1094 // For implementations (which can be very "coarse grain"), we add the
1095 // method now. This allows the AST to implement lookup methods that work
1096 // incrementally (without waiting until we parse the @end). It also allows
1097 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001098 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001099 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001100 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001101 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001102 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001103 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001104 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001105 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001106 }
1107 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001108 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001109 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001110 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001111 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001112 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001113 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001114 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001115 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001116 }
1117 }
1118 if (PrevMethod) {
1119 // You can never have two method definitions with the same name.
Chris Lattner8d756812008-11-20 06:13:02 +00001120 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl)
1121 << ObjCMethod->getSelector().getName();
Chris Lattner855e51f2007-12-12 07:09:47 +00001122 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1123 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001124 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001125}
1126
Daniel Dunbar540ff472008-09-23 21:53:23 +00001127void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1128 SourceLocation Loc,
1129 unsigned &Attributes) {
1130 // FIXME: Improve the reported location.
1131
1132 // readonly and readwrite conflict.
1133 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1134 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001135 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1136 << "readonly" << "readwrite";
1137 Attributes &= ~ObjCDeclSpec::DQ_PR_readonly;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001138 }
1139
1140 // Check for copy or retain on non-object types.
1141 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1142 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner8d756812008-11-20 06:13:02 +00001143 Diag(Loc, diag::err_objc_property_requires_object)
1144 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar540ff472008-09-23 21:53:23 +00001145 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1146 }
1147
1148 // Check for more than one of { assign, copy, retain }.
1149 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1150 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner8d756812008-11-20 06:13:02 +00001151 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1152 << "assign" << "copy";
1153 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001154 }
1155 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001156 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1157 << "assign" << "retain";
1158 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001159 }
1160 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1161 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner8d756812008-11-20 06:13:02 +00001162 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1163 << "copy" << "retain";
1164 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar540ff472008-09-23 21:53:23 +00001165 }
1166 }
1167
1168 // Warn if user supplied no assignment attribute, property is
1169 // readwrite, and this is an object type.
1170 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1171 ObjCDeclSpec::DQ_PR_retain)) &&
1172 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1173 Context.isObjCObjectPointerType(PropertyTy)) {
1174 // Skip this warning in gc-only mode.
1175 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1176 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1177
1178 // If non-gc code warn that this is likely inappropriate.
1179 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1180 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1181
1182 // FIXME: Implement warning dependent on NSCopying being
1183 // implemented. See also:
1184 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1185 // (please trim this list while you are at it).
1186 }
1187}
1188
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001189Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1190 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001191 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001192 Selector GetterSel,
1193 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001194 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001195 QualType T = GetTypeForDeclarator(FD.D, S);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001196 unsigned Attributes = ODS.getPropertyAttributes();
1197
1198 // May modify Attributes.
1199 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1200
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001201 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1202 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001203 // Regardless of setter/getter attribute, we save the default getter/setter
1204 // selector names in anticipation of declaration of setter/getter methods.
1205 PDecl->setGetterName(GetterSel);
1206 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001207
Daniel Dunbar540ff472008-09-23 21:53:23 +00001208 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001209 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001210
Daniel Dunbar540ff472008-09-23 21:53:23 +00001211 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001212 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001213
Daniel Dunbar540ff472008-09-23 21:53:23 +00001214 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001215 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001216
Daniel Dunbar540ff472008-09-23 21:53:23 +00001217 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001218 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001219
Daniel Dunbar540ff472008-09-23 21:53:23 +00001220 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001221 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001222
Daniel Dunbar540ff472008-09-23 21:53:23 +00001223 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001224 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001225
Daniel Dunbar540ff472008-09-23 21:53:23 +00001226 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001227 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001228
Daniel Dunbar540ff472008-09-23 21:53:23 +00001229 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001230 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001231
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001232 if (MethodImplKind == tok::objc_required)
1233 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1234 else if (MethodImplKind == tok::objc_optional)
1235 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1236
Chris Lattner855e51f2007-12-12 07:09:47 +00001237 return PDecl;
1238}
1239
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001240/// ActOnPropertyImplDecl - This routine performs semantic checks and
1241/// builds the AST node for a property implementation declaration; declared
1242/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001243///
1244Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1245 SourceLocation PropertyLoc,
1246 bool Synthesize,
1247 DeclTy *ClassCatImpDecl,
1248 IdentifierInfo *PropertyId,
1249 IdentifierInfo *PropertyIvar) {
1250 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1251 // Make sure we have a context for the property implementation declaration.
1252 if (!ClassImpDecl) {
1253 Diag(AtLoc, diag::error_missing_property_context);
1254 return 0;
1255 }
1256 ObjCPropertyDecl *property = 0;
1257 ObjCInterfaceDecl* IDecl = 0;
1258 // Find the class or category class where this property must have
1259 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001260 ObjCImplementationDecl *IC = 0;
1261 ObjCCategoryImplDecl* CatImplClass = 0;
1262 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001263 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001264 // We always synthesize an interface for an implementation
1265 // without an interface decl. So, IDecl is always non-zero.
1266 assert(IDecl &&
1267 "ActOnPropertyImplDecl - @implementation without @interface");
1268
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001269 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001270 property = IDecl->FindPropertyDeclaration(PropertyId);
1271 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001272 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001273 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001274 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001275 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001276 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001277 if (Synthesize) {
1278 Diag(AtLoc, diag::error_synthesize_category_decl);
1279 return 0;
1280 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001281 IDecl = CatImplClass->getClassInterface();
1282 if (!IDecl) {
1283 Diag(AtLoc, diag::error_missing_property_interface);
1284 return 0;
1285 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001286 ObjCCategoryDecl *Category =
1287 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1288
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001289 // If category for this implementation not found, it is an error which
1290 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001291 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001292 return 0;
1293 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001294 property = Category->FindPropertyDeclaration(PropertyId);
1295 if (!property) {
Chris Lattner8d756812008-11-20 06:13:02 +00001296 Diag(PropertyLoc, diag::error_bad_category_property_decl)
1297 << Category->getName();
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001298 return 0;
1299 }
1300 }
1301 else {
1302 Diag(AtLoc, diag::error_bad_property_context);
1303 return 0;
1304 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001305 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001306 // Check that we have a valid, previously declared ivar for @synthesize
1307 if (Synthesize) {
1308 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001309 if (!PropertyIvar)
1310 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001311 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001312 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001313 if (!Ivar) {
Chris Lattner65cae292008-11-19 08:23:25 +00001314 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001315 return 0;
1316 }
Steve Naroffc32e45c2008-09-30 10:07:56 +00001317 QualType PropType = Context.getCanonicalType(property->getType());
1318 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1319
Steve Naroff631e3922008-09-30 00:24:17 +00001320 // Check that type of property and its ivar are type compatible.
Steve Naroffc32e45c2008-09-30 10:07:56 +00001321 if (PropType != IvarType) {
Steve Naroff3557a342008-10-16 14:59:30 +00001322 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner8d756812008-11-20 06:13:02 +00001323 Diag(PropertyLoc, diag::error_property_ivar_type)
1324 << property->getName() << Ivar->getName();
Steve Naroffc32e45c2008-09-30 10:07:56 +00001325 return 0;
1326 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001327 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001328 } else if (PropertyIvar) {
1329 // @dynamic
1330 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1331 return 0;
1332 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001333 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001334 ObjCPropertyImplDecl *PIDecl =
1335 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1336 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001337 ObjCPropertyImplDecl::Synthesize
1338 : ObjCPropertyImplDecl::Dynamic),
1339 Ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001340 if (IC)
1341 IC->addPropertyImplementation(PIDecl);
1342 else
1343 CatImplClass->addPropertyImplementation(PIDecl);
1344
1345 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001346}
Anders Carlsson0a6ab172008-11-04 16:57:32 +00001347
1348bool Sema::CheckObjCDeclScope(Decl *D)
1349{
1350 if (isa<TranslationUnitDecl>(CurContext))
1351 return false;
1352
1353 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1354 D->setInvalidDecl();
1355
1356 return true;
1357}