blob: a09daa0814b5ecf50b62aa179b2bd8a36e2825fe [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 Lattner855e51f2007-12-12 07:09:47 +000070 Diag(ClassLoc, diag::err_redefinition_different_kind,
71 ClassName->getName());
72 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
73 }
74
Ted Kremenek42730c52008-01-07 19:49:32 +000075 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000076 if (IDecl) {
77 // Class already seen. Is it a forward declaration?
78 if (!IDecl->isForwardDecl())
79 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
80 else {
81 IDecl->setLocation(AtInterfaceLoc);
82 IDecl->setForwardDecl(false);
Chris Lattner855e51f2007-12-12 07:09:47 +000083 }
Chris Lattner5cece462008-07-21 07:06:49 +000084 } else {
Daniel Dunbard8bd6822008-08-20 18:02:42 +000085 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroff7c371742008-04-11 19:35:35 +000086 ClassName, ClassLoc);
Daniel Dunbard8bd6822008-08-20 18:02:42 +000087 if (AttrList)
88 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +000089
Steve Naroff15208162008-04-02 18:30:49 +000090 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +000091 // Remember that this needs to be removed when the scope is popped.
92 TUScope->AddDecl(IDecl);
93 }
94
95 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +000096 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +000097 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +000098 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000099 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000100 Diag(SuperLoc, diag::err_redefinition_different_kind,
101 SuperName->getName());
102 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
103 }
104 else {
105 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000106 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000107
108 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattner5cece462008-07-21 07:06:49 +0000109 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 SuperClassEntry ? SuperClassEntry->getName()
111 : SuperName->getName(),
Chris Lattner5cece462008-07-21 07:06:49 +0000112 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 }
114 }
115 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000116 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000117 IDecl->setLocEnd(SuperLoc);
118 } else { // we have a root class.
119 IDecl->setLocEnd(ClassLoc);
120 }
121
122 /// Check then save referenced protocols
Chris Lattnerae1ae492008-07-26 04:13:19 +0000123 if (NumProtoRefs) {
124 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000125 IDecl->setLocEnd(EndProtoLoc);
126 }
127 return IDecl;
128}
129
130/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000131/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000132Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
133 IdentifierInfo *AliasName,
134 SourceLocation AliasLocation,
135 IdentifierInfo *ClassName,
136 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000137 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000138 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000140 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000141 Diag(AliasLocation, diag::warn_previous_alias_decl);
142 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
143 }
144 else {
145 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
146 AliasName->getName());
147 Diag(ADecl->getLocation(), diag::err_previous_declaration);
148 }
149 return 0;
150 }
151 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000152 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000153 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
154 if (CDecl == 0) {
155 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
156 if (CDeclU)
157 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000158 return 0;
159 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000160
161 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000162 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000163 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
164
165 ObjCAliasDecls[AliasName] = AliasDecl;
166 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000167 return AliasDecl;
168}
169
Chris Lattner2bdedd62008-07-26 04:03:38 +0000170Sema::DeclTy *
171Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
172 IdentifierInfo *ProtocolName,
173 SourceLocation ProtocolLoc,
174 DeclTy * const *ProtoRefs,
175 unsigned NumProtoRefs,
Daniel Dunbar28680d12008-09-26 04:48:09 +0000176 SourceLocation EndProtoLoc,
177 AttributeList *AttrList) {
178 // FIXME: Deal with AttrList.
Chris Lattner855e51f2007-12-12 07:09:47 +0000179 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000180 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000181 if (PDecl) {
182 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000183 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000184 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
185 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000186 // Just return the protocol we already had.
187 // FIXME: don't leak the objects passed in!
188 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000189 }
Steve Naroff9a9e5212008-08-13 16:39:22 +0000190 // Make sure the cached decl gets a valid start location.
191 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattnerc1881852008-03-16 01:25:17 +0000192 PDecl->setForwardDecl(false);
Chris Lattnerc1881852008-03-16 01:25:17 +0000193 } else {
Chris Lattner0be08822008-07-21 21:32:27 +0000194 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattner7afba9c2008-03-16 20:19:15 +0000195 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000196 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000197 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000198
199 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000200 /// Check then save referenced protocols.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000201 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner855e51f2007-12-12 07:09:47 +0000202 PDecl->setLocEnd(EndProtoLoc);
203 }
204 return PDecl;
205}
206
207/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000208/// issues an error if they are not declared. It returns list of
209/// protocol declarations in its 'Protocols' argument.
Chris Lattner855e51f2007-12-12 07:09:47 +0000210void
Chris Lattner2bdedd62008-07-26 04:03:38 +0000211Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000212 const IdentifierLocPair *ProtocolId,
Chris Lattner855e51f2007-12-12 07:09:47 +0000213 unsigned NumProtocols,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000214 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000215 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattner17d50a92008-07-26 03:47:43 +0000216 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
217 if (!PDecl) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000218 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
219 ProtocolId[i].first->getName());
Chris Lattner17d50a92008-07-26 03:47:43 +0000220 continue;
221 }
222
223 // If this is a forward declaration and we are supposed to warn in this
224 // case, do it.
225 if (WarnOnDeclarations && PDecl->isForwardDecl())
226 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
227 ProtocolId[i].first->getName());
228 Protocols.push_back(PDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000229 }
230}
231
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000232/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000233/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000234///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000235void
236Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
237 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000238 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000239 ObjCPropertyDecl::PropertyAttributeKind CAttr =
240 Property->getPropertyAttributes();
241 ObjCPropertyDecl::PropertyAttributeKind SAttr =
242 SuperProperty->getPropertyAttributes();
243 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
244 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000245 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000246 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000247 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
248 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000249 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000250 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000251 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000252 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
253 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000254 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000255 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000256 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000257
258 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
259 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000260 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000261 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000262 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000263 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000264 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000265 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000266 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000267 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000268 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000269 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000270 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000271
Chris Lattnerc5cff302008-07-26 20:50:02 +0000272 if (Context.getCanonicalType(Property->getType()) !=
273 Context.getCanonicalType(SuperProperty->getType()))
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000274 Diag(Property->getLocation(), diag::warn_property_type,
275 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000276 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000277
278}
279
280/// ComparePropertiesInBaseAndSuper - This routine compares property
281/// declarations in base and its super class, if any, and issues
282/// diagnostics in a variety of inconsistant situations.
283///
284void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000285Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000286 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
287 if (!SDecl)
288 return;
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000289 // FIXME: O(N^2)
Fariborz Jahanianed986602008-05-01 00:03:38 +0000290 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
291 E = SDecl->classprop_end(); S != E; ++S) {
292 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000293 // Does property in super class has declaration in current class?
294 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
295 E = IDecl->classprop_end(); I != E; ++I) {
296 ObjCPropertyDecl *PDecl = (*I);
297 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000298 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000299 }
300 }
301}
302
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000303/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
304/// of properties declared in a protocol and adds them to the list
305/// of properties for current class if it is not there already.
306void
307Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
308 ObjCProtocolDecl *PDecl)
309{
310 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
311 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
312 E = PDecl->classprop_end(); P != E; ++P) {
313 ObjCPropertyDecl *Pr = (*P);
314 ObjCInterfaceDecl::classprop_iterator CP, CE;
315 // Is this property already in class's list of properties?
316 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
317 CP != CE; ++CP)
318 if ((*CP)->getIdentifier() == Pr->getIdentifier())
319 break;
320 if (CP == CE)
321 // Add this property to list of properties for thie class.
322 mergeProperties.push_back(Pr);
323 else
324 // Property protocol already exist in class. Diagnose any mismatch.
325 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
326 }
327 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
328}
329
330/// MergeProtocolPropertiesIntoClass - This routine merges properties
331/// declared in 'MergeItsProtocols' objects (which can be a class or an
332/// inherited protocol into the list of properties for class 'IDecl'
333///
334
335void
336Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
337 DeclTy *MergeItsProtocols) {
338 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattner5cece462008-07-21 07:06:49 +0000339 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000340 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
341 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000342 // Merge properties of class (*P) into IDECL's
Chris Lattner5cece462008-07-21 07:06:49 +0000343 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
344
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000345 // Go thru the list of protocols for this class and recursively merge
346 // their properties into this class as well.
347 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
348 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattner5cece462008-07-21 07:06:49 +0000349 MergeProtocolPropertiesIntoClass(IDecl, *P);
350 } else {
Argiris Kirtzidisfc1ea132008-07-21 09:18:38 +0000351 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
352 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
353 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000354 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattner5cece462008-07-21 07:06:49 +0000355 }
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000356}
357
Chris Lattner855e51f2007-12-12 07:09:47 +0000358/// ActOnForwardProtocolDeclaration -
359Action::DeclTy *
360Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000361 const IdentifierLocPair *IdentList,
362 unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000363 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000364
365 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnere705e5e2008-07-21 22:17:28 +0000366 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattner7afba9c2008-03-16 20:19:15 +0000367 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattnere705e5e2008-07-21 22:17:28 +0000368 if (PDecl == 0) // Not already seen?
369 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000370
371 Protocols.push_back(PDecl);
372 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000373 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
374 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000375}
376
Chris Lattnere705e5e2008-07-21 22:17:28 +0000377Sema::DeclTy *Sema::
378ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
379 IdentifierInfo *ClassName, SourceLocation ClassLoc,
380 IdentifierInfo *CategoryName,
381 SourceLocation CategoryLoc,
Chris Lattner45142b92008-07-26 04:07:02 +0000382 DeclTy * const *ProtoRefs,
Chris Lattnere705e5e2008-07-21 22:17:28 +0000383 unsigned NumProtoRefs,
384 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000385 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000386
Chris Lattnere29dc832008-03-16 20:34:23 +0000387 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000388 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000389 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000390
391 /// Check that class of this category is already completely declared.
392 if (!IDecl || IDecl->isForwardDecl())
393 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000394 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000395 /// Check for duplicate interface declaration for this category
396 ObjCCategoryDecl *CDeclChain;
397 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
398 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000399 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000400 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000401 CategoryName->getName());
402 break;
403 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000404 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000405 if (!CDeclChain)
406 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000407 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000408
409 if (NumProtoRefs) {
Chris Lattner45142b92008-07-26 04:07:02 +0000410 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
411 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000412 }
413 return CDecl;
414}
415
416/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000417/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000418/// object.
419Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
420 SourceLocation AtCatImplLoc,
421 IdentifierInfo *ClassName, SourceLocation ClassLoc,
422 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000423 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000424 ObjCCategoryImplDecl *CDecl =
425 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000426 /// Check that class of this category is already completely declared.
427 if (!IDecl || IDecl->isForwardDecl())
428 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
429
430 /// TODO: Check that CatName, category name, is not used in another
431 // implementation.
432 return CDecl;
433}
434
435Sema::DeclTy *Sema::ActOnStartClassImplementation(
436 SourceLocation AtClassImplLoc,
437 IdentifierInfo *ClassName, SourceLocation ClassLoc,
438 IdentifierInfo *SuperClassname,
439 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000440 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000441 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000442 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000443 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000444 Diag(ClassLoc, diag::err_redefinition_different_kind,
445 ClassName->getName());
446 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
447 }
448 else {
449 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000450 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000451 if (!IDecl)
452 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
453 }
454
455 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000456 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000457 if (SuperClassname) {
458 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000459 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000460 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000461 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
462 SuperClassname->getName());
463 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
464 }
465 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000466 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000467 if (!SDecl)
468 Diag(SuperClassLoc, diag::err_undef_superclass,
469 SuperClassname->getName(), ClassName->getName());
470 else if (IDecl && IDecl->getSuperClass() != SDecl) {
471 // This implementation and its interface do not have the same
472 // super class.
473 Diag(SuperClassLoc, diag::err_conflicting_super_class,
474 SDecl->getName());
475 Diag(SDecl->getLocation(), diag::err_previous_definition);
476 }
477 }
478 }
479
480 if (!IDecl) {
481 // Legacy case of @implementation with no corresponding @interface.
482 // Build, chain & install the interface decl into the identifier.
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000483
484 // FIXME: Do we support attributes on the @implementation? If so
485 // we should copy them over.
Chris Lattner5cece462008-07-21 07:06:49 +0000486 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000487 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000488 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000489 IDecl->setSuperClass(SDecl);
490 IDecl->setLocEnd(ClassLoc);
491
492 // Remember that this needs to be removed when the scope is popped.
493 TUScope->AddDecl(IDecl);
494 }
495
Ted Kremenek42730c52008-01-07 19:49:32 +0000496 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000497 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
498 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000499
500 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000501 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000502 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000503 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
504 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000505 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000506 return IMPDecl;
507}
508
Ted Kremenek42730c52008-01-07 19:49:32 +0000509void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
510 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000511 SourceLocation RBrace) {
512 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000513 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000514 if (!IDecl)
515 return;
516 /// Check case of non-existing @interface decl.
517 /// (legacy objective-c @implementation decl without an @interface decl).
518 /// Add implementations's ivar to the synthesize class's ivar list.
519 if (IDecl->ImplicitInterfaceDecl()) {
520 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
521 return;
522 }
523 // If implementation has empty ivar list, just return.
524 if (numIvars == 0)
525 return;
526
527 assert(ivars && "missing @implementation ivars");
528
529 // Check interface's Ivar list against those in the implementation.
530 // names and types must match.
531 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000532 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000533 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000534 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
535 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000536 ObjCIvarDecl* ImplIvar = ivars[j++];
537 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 assert (ImplIvar && "missing implementation ivar");
539 assert (ClsIvar && "missing class ivar");
Chris Lattnerb5457862008-07-27 00:05:05 +0000540 if (Context.getCanonicalType(ImplIvar->getType()) !=
541 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000542 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
543 ImplIvar->getIdentifier()->getName());
544 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
545 ClsIvar->getIdentifier()->getName());
546 }
547 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
548 // as error.
549 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
550 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
551 ImplIvar->getIdentifier()->getName());
552 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
553 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000554 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000555 }
556 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000557 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000558
559 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000560 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000561 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000562 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000563}
564
Steve Naroffb4f48512008-02-10 21:38:56 +0000565void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
566 bool &IncompleteImpl) {
567 if (!IncompleteImpl) {
568 Diag(ImpLoc, diag::warn_incomplete_impl);
569 IncompleteImpl = true;
570 }
571 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
572}
573
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000574/// FIXME: Type hierarchies in Objective-C can be deep. We could most
575/// likely improve the efficiency of selector lookups and type
576/// checking by associating with each protocol / interface / category
577/// the flattened instance tables. If we used an immutable set to keep
578/// the table then it wouldn't add significant memory cost and it
579/// would be handy for lookups.
580
Steve Naroffb268d2a2008-02-08 22:06:17 +0000581/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000582/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000583void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
584 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000585 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000586 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000587 const llvm::DenseSet<Selector> &ClsMap,
588 ObjCInterfaceDecl *IDecl) {
589 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
590
591 // If a method lookup fails locally we still need to look and see if
592 // the method was implemented by a base class or an inherited
593 // protocol. This lookup is slow, but occurs rarely in correct code
594 // and otherwise would terminate in a warning.
595
Chris Lattner855e51f2007-12-12 07:09:47 +0000596 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000598 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000599 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000600 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
601 !InsMap.count(method->getSelector()) &&
602 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000603 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000604 }
605 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000606 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000607 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 ObjCMethodDecl *method = *I;
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000609 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
610 !ClsMap.count(method->getSelector()) &&
611 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroffb4f48512008-02-10 21:38:56 +0000612 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000613 }
Chris Lattner0be08822008-07-21 21:32:27 +0000614 // Check on this protocols's referenced protocols, recursively.
615 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
616 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000617 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000618}
619
Ted Kremenek42730c52008-01-07 19:49:32 +0000620void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
621 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000622 llvm::DenseSet<Selector> InsMap;
623 // Check and see if instance methods in class interface have been
624 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000625 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000626 E = IMPDecl->instmeth_end(); I != E; ++I)
627 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000628
629 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000631 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000632 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000633 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000634
Chris Lattner855e51f2007-12-12 07:09:47 +0000635 llvm::DenseSet<Selector> ClsMap;
636 // Check and see if class methods in class interface have been
637 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000638 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000639 E = IMPDecl->classmeth_end(); I != E; ++I)
640 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000641
Ted Kremenek42730c52008-01-07 19:49:32 +0000642 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000643 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000644 if (!ClsMap.count((*I)->getSelector()))
645 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000646
647 // Check the protocol list for unimplemented methods in the @implementation
648 // class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000649 const ObjCList<ObjCProtocolDecl> &Protocols =
650 IDecl->getReferencedProtocols();
651 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
652 E = Protocols.end(); I != E; ++I)
653 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000654 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000655}
656
657/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000658/// category interface are implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000659void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
660 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000661 llvm::DenseSet<Selector> InsMap;
662 // Check and see if instance methods in category interface have been
663 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000664 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000665 E = CatImplDecl->instmeth_end(); I != E; ++I)
666 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000667
668 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000669 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000670 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000671 if (!InsMap.count((*I)->getSelector()))
672 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
673
Chris Lattner855e51f2007-12-12 07:09:47 +0000674 llvm::DenseSet<Selector> ClsMap;
675 // Check and see if class methods in category interface have been
676 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000677 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000678 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
679 I != E; ++I)
680 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000681
Ted Kremenek42730c52008-01-07 19:49:32 +0000682 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000683 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000684 if (!ClsMap.count((*I)->getSelector()))
685 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000686
687 // Check the protocol list for unimplemented methods in the @implementation
688 // class.
Chris Lattner0be08822008-07-21 21:32:27 +0000689 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
690 E = CatClassDecl->protocol_end(); PI != E; ++PI)
691 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar1d706e92008-09-04 20:01:15 +0000692 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner855e51f2007-12-12 07:09:47 +0000693}
694
695/// ActOnForwardClassDeclaration -
696Action::DeclTy *
697Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
698 IdentifierInfo **IdentList, unsigned NumElts)
699{
Ted Kremenek42730c52008-01-07 19:49:32 +0000700 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000701
702 for (unsigned i = 0; i != NumElts; ++i) {
703 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000704 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000705 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffd2549972008-06-05 22:57:10 +0000706 // GCC apparently allows the following idiom:
707 //
708 // typedef NSObject < XCElementTogglerP > XCElementToggler;
709 // @class XCElementToggler;
710 //
711 // FIXME: Make an extension?
712 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
713 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
714 Diag(AtClassLoc, diag::err_redefinition_different_kind,
715 IdentList[i]->getName());
716 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
717 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000718 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000719 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000720 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner5cece462008-07-21 07:06:49 +0000721 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000722 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000723 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000724
725 // Remember that this needs to be removed when the scope is popped.
726 TUScope->AddDecl(IDecl);
727 }
728
729 Interfaces.push_back(IDecl);
730 }
731
Chris Lattnere29dc832008-03-16 20:34:23 +0000732 return ObjCClassDecl::Create(Context, AtClassLoc,
733 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000734}
735
736
737/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
738/// returns true, or false, accordingly.
739/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000740bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
741 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000742 if (Context.getCanonicalType(Method->getResultType()) !=
743 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000744 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000745 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000746 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
747 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000748 if (Context.getCanonicalType(ParamDecl->getType()) !=
749 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000750 return false;
751 }
752 return true;
753}
754
Ted Kremenek42730c52008-01-07 19:49:32 +0000755void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
756 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000757 if (!FirstMethod.Method) {
758 // Haven't seen a method with this selector name yet - add it.
759 FirstMethod.Method = Method;
760 FirstMethod.Next = 0;
761 } else {
762 // We've seen a method with this name, now check the type signature(s).
763 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
764
Ted Kremenek42730c52008-01-07 19:49:32 +0000765 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000766 Next = Next->Next)
767 match = MatchTwoMethodDeclarations(Method, Next->Method);
768
769 if (!match) {
770 // We have a new signature for an existing method - add it.
771 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000772 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000773 FirstMethod.Next = OMI;
774 }
775 }
776}
777
Ted Kremenek42730c52008-01-07 19:49:32 +0000778void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
779 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000780 if (!FirstMethod.Method) {
781 // Haven't seen a method with this selector name yet - add it.
782 FirstMethod.Method = Method;
783 FirstMethod.Next = 0;
784 } else {
785 // We've seen a method with this name, now check the type signature(s).
786 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
787
Ted Kremenek42730c52008-01-07 19:49:32 +0000788 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000789 Next = Next->Next)
790 match = MatchTwoMethodDeclarations(Method, Next->Method);
791
792 if (!match) {
793 // We have a new signature for an existing method - add it.
794 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000795 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000796 FirstMethod.Next = OMI;
797 }
798 }
799}
800
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000801// Note: For class/category implemenations, allMethods/allProperties is
802// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000803void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
804 DeclTy **allMethods, unsigned allNum,
805 DeclTy **allProperties, unsigned pNum) {
806 Decl *ClassDecl = static_cast<Decl *>(classDecl);
807
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000808 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
809 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000810 // should be true.
811 if (!ClassDecl)
812 return;
813
Ted Kremenek42730c52008-01-07 19:49:32 +0000814 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
815 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000816
Ted Kremenek42730c52008-01-07 19:49:32 +0000817 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
818 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000819
820 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000821 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
822 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000823 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000824
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000825 if (pNum != 0) {
Chris Lattnercffe3662008-03-16 21:23:50 +0000826 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
827 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000828 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
829 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
830 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000831 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000832 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000833 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeon0473be42008-07-05 02:01:25 +0000834 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000835
836 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000837 ObjCMethodDecl *Method =
838 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000839
840 if (!Method) continue; // Already issued a diagnostic.
841 if (Method->isInstance()) {
842 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000843 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000844 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
845 : false;
846 if (isInterfaceDeclKind && PrevMethod && !match
847 || checkIdenticalMethods && match) {
848 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
849 Method->getSelector().getName());
850 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
851 } else {
852 insMethods.push_back(Method);
853 InsMap[Method->getSelector()] = Method;
854 /// The following allows us to typecheck messages to "id".
855 AddInstanceMethodToGlobalPool(Method);
856 }
857 }
858 else {
859 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000860 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000861 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
862 : false;
863 if (isInterfaceDeclKind && PrevMethod && !match
864 || checkIdenticalMethods && match) {
865 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
866 Method->getSelector().getName());
867 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
868 } else {
869 clsMethods.push_back(Method);
870 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000871 /// The following allows us to typecheck messages to "Class".
872 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000873 }
874 }
875 }
876
Ted Kremenek42730c52008-01-07 19:49:32 +0000877 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000878 // Compares properties declared in this class to those of its
Fariborz Jahanianed986602008-05-01 00:03:38 +0000879 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000880 ComparePropertiesInBaseAndSuper(I);
881 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000882 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
883 e = I->classprop_end(); i != e; ++i)
884 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000885 I->addMethods(&insMethods[0], insMethods.size(),
886 &clsMethods[0], clsMethods.size(), AtEndLoc);
887
Ted Kremenek42730c52008-01-07 19:49:32 +0000888 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000889 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
890 e = P->classprop_end(); i != e; ++i)
891 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner855e51f2007-12-12 07:09:47 +0000892 P->addMethods(&insMethods[0], insMethods.size(),
893 &clsMethods[0], clsMethods.size(), AtEndLoc);
894 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000896 // FIXME: Need to compare properties to those in interface?
897
898 // FIXME: If we merge properties into class we should probably
899 // merge them into category as well?
900 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
901 e = C->classprop_end(); i != e; ++i)
902 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner855e51f2007-12-12 07:09:47 +0000903 C->addMethods(&insMethods[0], insMethods.size(),
904 &clsMethods[0], clsMethods.size(), AtEndLoc);
905 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 else if (ObjCImplementationDecl *IC =
907 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000908 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000909 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000910 ImplMethodsVsClassMethods(IC, IDecl);
911 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000912 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000913 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000914 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000915 // Find category interface decl and then check that all methods declared
Daniel Dunbar0c0160f2008-08-27 05:40:03 +0000916 // in this interface are implemented in the category @implementation.
Chris Lattner855e51f2007-12-12 07:09:47 +0000917 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000918 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000919 Categories; Categories = Categories->getNextClassCategory()) {
920 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
921 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
922 break;
923 }
924 }
925 }
926 }
927}
928
929
930/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
931/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000932static Decl::ObjCDeclQualifier
933CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
934 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
935 if (PQTVal & ObjCDeclSpec::DQ_In)
936 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
937 if (PQTVal & ObjCDeclSpec::DQ_Inout)
938 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
939 if (PQTVal & ObjCDeclSpec::DQ_Out)
940 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
941 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
942 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
943 if (PQTVal & ObjCDeclSpec::DQ_Byref)
944 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
945 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
946 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000947
948 return ret;
949}
950
951Sema::DeclTy *Sema::ActOnMethodDeclaration(
952 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000953 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000954 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000955 Selector Sel,
956 // optional arguments. The number of types/arguments is obtained
957 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000958 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000959 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
960 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000961 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000962
963 // Make sure we can establish a context for the method.
964 if (!ClassDecl) {
965 Diag(MethodLoc, diag::error_missing_method_context);
966 return 0;
967 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000968 QualType resultDeclType;
969
970 if (ReturnType)
971 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
972 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000973 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000974
Chris Lattner114add62008-03-16 00:49:28 +0000975 ObjCMethodDecl* ObjCMethod =
976 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbard8bd6822008-08-20 18:02:42 +0000977 ClassDecl,
Chris Lattner114add62008-03-16 00:49:28 +0000978 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000979 false,
Chris Lattner114add62008-03-16 00:49:28 +0000980 MethodDeclKind == tok::objc_optional ?
981 ObjCMethodDecl::Optional :
982 ObjCMethodDecl::Required);
983
Chris Lattnereee57c02008-04-04 06:12:32 +0000984 llvm::SmallVector<ParmVarDecl*, 16> Params;
985
986 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
987 // FIXME: arg->AttrList must be stored too!
988 QualType argType;
989
990 if (ArgTypes[i])
991 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
992 else
993 argType = Context.getObjCIdType();
994 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
995 SourceLocation(/*FIXME*/),
996 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000997 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000998 Param->setObjCDeclQualifier(
999 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1000 Params.push_back(Param);
1001 }
1002
Ted Kremenek42730c52008-01-07 19:49:32 +00001003 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1004 ObjCMethod->setObjCDeclQualifier(
1005 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1006 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbard1d847c2008-09-26 04:12:28 +00001007
1008 if (AttrList)
1009 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner855e51f2007-12-12 07:09:47 +00001010
1011 // For implementations (which can be very "coarse grain"), we add the
1012 // method now. This allows the AST to implement lookup methods that work
1013 // incrementally (without waiting until we parse the @end). It also allows
1014 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001015 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001016 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001017 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001018 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001019 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001020 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001021 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001022 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001023 }
1024 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001025 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001026 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001027 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001028 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001029 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001030 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001031 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001032 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001033 }
1034 }
1035 if (PrevMethod) {
1036 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001037 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1038 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001039 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1040 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001041 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001042}
1043
Daniel Dunbar540ff472008-09-23 21:53:23 +00001044void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1045 SourceLocation Loc,
1046 unsigned &Attributes) {
1047 // FIXME: Improve the reported location.
1048
1049 // readonly and readwrite conflict.
1050 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1051 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
1052 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1053 "readonly", "readwrite");
1054 Attributes ^= ObjCDeclSpec::DQ_PR_readonly;
1055 }
1056
1057 // Check for copy or retain on non-object types.
1058 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1059 !Context.isObjCObjectPointerType(PropertyTy)) {
1060 Diag(Loc, diag::err_objc_property_requires_object,
1061 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1062 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1063 }
1064
1065 // Check for more than one of { assign, copy, retain }.
1066 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1067 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1068 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1069 "assign", "copy");
1070 Attributes ^= ObjCDeclSpec::DQ_PR_copy;
1071 }
1072 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1073 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1074 "assign", "retain");
1075 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1076 }
1077 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1078 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1079 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1080 "copy", "retain");
1081 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1082 }
1083 }
1084
1085 // Warn if user supplied no assignment attribute, property is
1086 // readwrite, and this is an object type.
1087 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1088 ObjCDeclSpec::DQ_PR_retain)) &&
1089 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1090 Context.isObjCObjectPointerType(PropertyTy)) {
1091 // Skip this warning in gc-only mode.
1092 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1093 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1094
1095 // If non-gc code warn that this is likely inappropriate.
1096 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1097 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1098
1099 // FIXME: Implement warning dependent on NSCopying being
1100 // implemented. See also:
1101 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1102 // (please trim this list while you are at it).
1103 }
1104}
1105
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001106Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1107 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001108 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001109 Selector GetterSel,
1110 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001111 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001112 QualType T = GetTypeForDeclarator(FD.D, S);
Daniel Dunbar540ff472008-09-23 21:53:23 +00001113 unsigned Attributes = ODS.getPropertyAttributes();
1114
1115 // May modify Attributes.
1116 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1117
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001118 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1119 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001120 // Regardless of setter/getter attribute, we save the default getter/setter
1121 // selector names in anticipation of declaration of setter/getter methods.
1122 PDecl->setGetterName(GetterSel);
1123 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001124
Daniel Dunbar540ff472008-09-23 21:53:23 +00001125 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001126 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001127
Daniel Dunbar540ff472008-09-23 21:53:23 +00001128 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001129 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001130
Daniel Dunbar540ff472008-09-23 21:53:23 +00001131 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001132 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001133
Daniel Dunbar540ff472008-09-23 21:53:23 +00001134 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001135 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001136
Daniel Dunbar540ff472008-09-23 21:53:23 +00001137 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001138 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001139
Daniel Dunbar540ff472008-09-23 21:53:23 +00001140 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001141 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001142
Daniel Dunbar540ff472008-09-23 21:53:23 +00001143 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001144 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001145
Daniel Dunbar540ff472008-09-23 21:53:23 +00001146 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001147 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001148
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001149 if (MethodImplKind == tok::objc_required)
1150 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1151 else if (MethodImplKind == tok::objc_optional)
1152 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1153
Chris Lattner855e51f2007-12-12 07:09:47 +00001154 return PDecl;
1155}
1156
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001157/// ActOnPropertyImplDecl - This routine performs semantic checks and
1158/// builds the AST node for a property implementation declaration; declared
1159/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001160///
1161Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1162 SourceLocation PropertyLoc,
1163 bool Synthesize,
1164 DeclTy *ClassCatImpDecl,
1165 IdentifierInfo *PropertyId,
1166 IdentifierInfo *PropertyIvar) {
1167 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1168 // Make sure we have a context for the property implementation declaration.
1169 if (!ClassImpDecl) {
1170 Diag(AtLoc, diag::error_missing_property_context);
1171 return 0;
1172 }
1173 ObjCPropertyDecl *property = 0;
1174 ObjCInterfaceDecl* IDecl = 0;
1175 // Find the class or category class where this property must have
1176 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001177 ObjCImplementationDecl *IC = 0;
1178 ObjCCategoryImplDecl* CatImplClass = 0;
1179 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001180 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001181 // We always synthesize an interface for an implementation
1182 // without an interface decl. So, IDecl is always non-zero.
1183 assert(IDecl &&
1184 "ActOnPropertyImplDecl - @implementation without @interface");
1185
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001186 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001187 property = IDecl->FindPropertyDeclaration(PropertyId);
1188 if (!property) {
Daniel Dunbar540ff472008-09-23 21:53:23 +00001189 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001190 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001191 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001192 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001193 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001194 if (Synthesize) {
1195 Diag(AtLoc, diag::error_synthesize_category_decl);
1196 return 0;
1197 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001198 IDecl = CatImplClass->getClassInterface();
1199 if (!IDecl) {
1200 Diag(AtLoc, diag::error_missing_property_interface);
1201 return 0;
1202 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001203 ObjCCategoryDecl *Category =
1204 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1205
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001206 // If category for this implementation not found, it is an error which
1207 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001208 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001209 return 0;
1210 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001211 property = Category->FindPropertyDeclaration(PropertyId);
1212 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001213 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001214 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001215 return 0;
1216 }
1217 }
1218 else {
1219 Diag(AtLoc, diag::error_bad_property_context);
1220 return 0;
1221 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001222 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001223 // Check that we have a valid, previously declared ivar for @synthesize
1224 if (Synthesize) {
1225 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001226 if (!PropertyIvar)
1227 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001228 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001229 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001230 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001231 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1232 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001233 return 0;
1234 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001235 // Check that type of property and its ivar match.
Chris Lattnerc5cff302008-07-26 20:50:02 +00001236 if (Context.getCanonicalType(Ivar->getType()) !=
1237 Context.getCanonicalType(property->getType())) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001238 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1239 Ivar->getName());
1240 return 0;
1241 }
1242
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001243 } else if (PropertyIvar) {
1244 // @dynamic
1245 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1246 return 0;
1247 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001248 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001249 ObjCPropertyImplDecl *PIDecl =
1250 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1251 (Synthesize ?
Daniel Dunbar14117fc2008-08-26 04:47:31 +00001252 ObjCPropertyImplDecl::Synthesize
1253 : ObjCPropertyImplDecl::Dynamic),
1254 Ivar);
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001255 if (IC)
1256 IC->addPropertyImplementation(PIDecl);
1257 else
1258 CatImplClass->addPropertyImplementation(PIDecl);
1259
1260 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001261}