blob: dbb3cf22ad3d69e8e465ccaf6c43276bdd9b56e3 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner4d391482007-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 Dunbar12bc6922008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
Chris Lattner4d391482007-12-12 07:09:47 +000019
20using namespace clang;
21
Ted Kremeneka526c5c2008-01-07 19:49:32 +000022/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000023/// and user declared, in the method definition's AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000024void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000025 assert(getCurMethodDecl() == 0 && "Method parsing confused");
Steve Naroff394f3f42008-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 Naroffa56f6162007-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 Lattner4d391482007-12-12 07:09:47 +000037
38 // Allow all of Sema to see that we are entering a method definition.
Chris Lattnerb048c982008-04-06 04:47:34 +000039 PushDeclContext(MDecl);
Chris Lattner4d391482007-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 Lattner4d391482007-12-12 07:09:47 +000043
44 // Insert the invisible arguments, self and _cmd!
Daniel Dunbar451318c2008-08-26 06:07:48 +000045 MDecl->createImplicitParams(Context);
Chris Lattner4d391482007-12-12 07:09:47 +000046
Daniel Dunbar451318c2008-08-26 06:07:48 +000047 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000049
Chris Lattner8123a952008-04-10 02:22:51 +000050 // Introduce all of the other parameters into this scope.
Chris Lattner58cce3b2008-03-16 01:07:14 +000051 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +000052 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner04421082008-04-08 04:40:51 +000053 IdentifierInfo *II = PDecl->getIdentifier();
Argyrios Kyrtzidis642e38b2008-04-27 13:30:35 +000054 if (II)
55 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000056 }
57}
58
Chris Lattner7caeabd2008-07-21 22:17:28 +000059Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61 IdentifierInfo *ClassName, SourceLocation ClassLoc,
62 IdentifierInfo *SuperName, SourceLocation SuperLoc,
Chris Lattner06036d32008-07-26 04:13:19 +000063 DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000064 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000065 assert(ClassName && "Missing class identifier");
66
67 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +000068 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000069 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +000075 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-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 Lattner4d391482007-12-12 07:09:47 +000083 }
Chris Lattnerb752f282008-07-21 07:06:49 +000084 } else {
Daniel Dunbarf6414922008-08-20 18:02:42 +000085 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000086 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000087 if (AttrList)
88 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +000089
Steve Naroff31102512008-04-02 18:30:49 +000090 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +000096 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +000097 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +000098 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000099 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000106 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000107
108 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Chris Lattnerb752f282008-07-21 07:06:49 +0000109 Diag(SuperLoc, diag::err_undef_superclass,
Chris Lattner4d391482007-12-12 07:09:47 +0000110 SuperClassEntry ? SuperClassEntry->getName()
111 : SuperName->getName(),
Chris Lattnerb752f282008-07-21 07:06:49 +0000112 ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc));
Chris Lattner4d391482007-12-12 07:09:47 +0000113 }
114 }
115 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000116 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-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 Lattner06036d32008-07-26 04:13:19 +0000123 if (NumProtoRefs) {
124 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-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 Dunbar7ad1b1f2008-09-04 20:01:15 +0000131/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000132Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
133 IdentifierInfo *AliasName,
134 SourceLocation AliasLocation,
135 IdentifierInfo *ClassName,
136 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000137 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000138 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000139 if (ADecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000140 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner4d391482007-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 Naroffb327ce02008-04-02 14:35:35 +0000152 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattnerf8d17a52008-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 Lattner4d391482007-12-12 07:09:47 +0000158 return 0;
159 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000160
161 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000162 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000163 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
164
165 ObjCAliasDecls[AliasName] = AliasDecl;
166 TUScope->AddDecl(AliasDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000167 return AliasDecl;
168}
169
Chris Lattnere13b9592008-07-26 04:03:38 +0000170Sema::DeclTy *
171Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
172 IdentifierInfo *ProtocolName,
173 SourceLocation ProtocolLoc,
174 DeclTy * const *ProtoRefs,
175 unsigned NumProtoRefs,
176 SourceLocation EndProtoLoc) {
Chris Lattner4d391482007-12-12 07:09:47 +0000177 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000178 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000179 if (PDecl) {
180 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000181 if (!PDecl->isForwardDecl()) {
Chris Lattner4d391482007-12-12 07:09:47 +0000182 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
183 ProtocolName->getName());
Chris Lattner439e71f2008-03-16 01:25:17 +0000184 // Just return the protocol we already had.
185 // FIXME: don't leak the objects passed in!
186 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000187 }
Steve Narofff11b5082008-08-13 16:39:22 +0000188 // Make sure the cached decl gets a valid start location.
189 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000190 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000191 } else {
Chris Lattner780f3292008-07-21 21:32:27 +0000192 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattnerc8581052008-03-16 20:19:15 +0000193 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000195 }
Chris Lattner4d391482007-12-12 07:09:47 +0000196
197 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000198 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000199 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000200 PDecl->setLocEnd(EndProtoLoc);
201 }
202 return PDecl;
203}
204
205/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000206/// issues an error if they are not declared. It returns list of
207/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000208void
Chris Lattnere13b9592008-07-26 04:03:38 +0000209Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000210 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000211 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000212 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000213 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000214 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
215 if (!PDecl) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000216 Diag(ProtocolId[i].second, diag::err_undeclared_protocol,
217 ProtocolId[i].first->getName());
Chris Lattnereacc3922008-07-26 03:47:43 +0000218 continue;
219 }
220
221 // If this is a forward declaration and we are supposed to warn in this
222 // case, do it.
223 if (WarnOnDeclarations && PDecl->isForwardDecl())
224 Diag(ProtocolId[i].second, diag::warn_undef_protocolref,
225 ProtocolId[i].first->getName());
226 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000227 }
228}
229
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000230/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000231/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000232///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000233void
234Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
235 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000236 const char *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000237 ObjCPropertyDecl::PropertyAttributeKind CAttr =
238 Property->getPropertyAttributes();
239 ObjCPropertyDecl::PropertyAttributeKind SAttr =
240 SuperProperty->getPropertyAttributes();
241 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
242 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000243 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000244 Property->getName(), inheritedName);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000245 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
246 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000247 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000248 Property->getName(), "copy", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000249 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000250 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
251 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000252 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000253 Property->getName(), "retain", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000254 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000255
256 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
257 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000258 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000259 Property->getName(), "atomic", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000260 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000261 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000262 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000263 Property->getName(), "setter", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000264 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000265 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000266 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000267 Property->getName(), "getter", inheritedName,
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000268 SourceRange());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000269
Chris Lattner717250a2008-07-26 20:50:02 +0000270 if (Context.getCanonicalType(Property->getType()) !=
271 Context.getCanonicalType(SuperProperty->getType()))
Fariborz Jahanian34350962008-05-01 18:05:01 +0000272 Diag(Property->getLocation(), diag::warn_property_type,
273 Property->getType().getAsString(),
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000274 inheritedName);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000275
276}
277
278/// ComparePropertiesInBaseAndSuper - This routine compares property
279/// declarations in base and its super class, if any, and issues
280/// diagnostics in a variety of inconsistant situations.
281///
282void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000283Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000284 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
285 if (!SDecl)
286 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000287 // FIXME: O(N^2)
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000288 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
289 E = SDecl->classprop_end(); S != E; ++S) {
290 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000291 // Does property in super class has declaration in current class?
292 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
293 E = IDecl->classprop_end(); I != E; ++I) {
294 ObjCPropertyDecl *PDecl = (*I);
295 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000296 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000297 }
298 }
299}
300
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000301/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
302/// of properties declared in a protocol and adds them to the list
303/// of properties for current class if it is not there already.
304void
305Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
306 ObjCProtocolDecl *PDecl)
307{
308 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
309 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
310 E = PDecl->classprop_end(); P != E; ++P) {
311 ObjCPropertyDecl *Pr = (*P);
312 ObjCInterfaceDecl::classprop_iterator CP, CE;
313 // Is this property already in class's list of properties?
314 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
315 CP != CE; ++CP)
316 if ((*CP)->getIdentifier() == Pr->getIdentifier())
317 break;
318 if (CP == CE)
319 // Add this property to list of properties for thie class.
320 mergeProperties.push_back(Pr);
321 else
322 // Property protocol already exist in class. Diagnose any mismatch.
323 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
324 }
325 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
326}
327
328/// MergeProtocolPropertiesIntoClass - This routine merges properties
329/// declared in 'MergeItsProtocols' objects (which can be a class or an
330/// inherited protocol into the list of properties for class 'IDecl'
331///
332
333void
334Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
335 DeclTy *MergeItsProtocols) {
336 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Chris Lattnerb752f282008-07-21 07:06:49 +0000337 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000338 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
339 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000340 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000341 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
342
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000343 // Go thru the list of protocols for this class and recursively merge
344 // their properties into this class as well.
345 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
346 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000347 MergeProtocolPropertiesIntoClass(IDecl, *P);
348 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000349 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
350 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
351 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000352 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000353 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000354}
355
Chris Lattner4d391482007-12-12 07:09:47 +0000356/// ActOnForwardProtocolDeclaration -
357Action::DeclTy *
358Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000359 const IdentifierLocPair *IdentList,
360 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000361 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000362
363 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000364 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000365 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000366 if (PDecl == 0) // Not already seen?
367 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Chris Lattner4d391482007-12-12 07:09:47 +0000368
369 Protocols.push_back(PDecl);
370 }
Chris Lattner61f9d412008-03-16 20:34:23 +0000371 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
372 &Protocols[0], Protocols.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000373}
374
Chris Lattner7caeabd2008-07-21 22:17:28 +0000375Sema::DeclTy *Sema::
376ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
377 IdentifierInfo *ClassName, SourceLocation ClassLoc,
378 IdentifierInfo *CategoryName,
379 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000380 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000381 unsigned NumProtoRefs,
382 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000383 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000384
Chris Lattner61f9d412008-03-16 20:34:23 +0000385 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000386 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000387 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000388
389 /// Check that class of this category is already completely declared.
390 if (!IDecl || IDecl->isForwardDecl())
391 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffd100c802008-06-05 15:03:27 +0000392 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000393 /// Check for duplicate interface declaration for this category
394 ObjCCategoryDecl *CDeclChain;
395 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
396 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000397 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff74199b62008-06-05 04:33:44 +0000398 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000399 CategoryName->getName());
400 break;
401 }
Chris Lattner4d391482007-12-12 07:09:47 +0000402 }
Steve Naroffd100c802008-06-05 15:03:27 +0000403 if (!CDeclChain)
404 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000405 }
Chris Lattner4d391482007-12-12 07:09:47 +0000406
407 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000408 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
409 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000410 }
411 return CDecl;
412}
413
414/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000415/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000416/// object.
417Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
418 SourceLocation AtCatImplLoc,
419 IdentifierInfo *ClassName, SourceLocation ClassLoc,
420 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000421 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000422 ObjCCategoryImplDecl *CDecl =
423 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000424 /// Check that class of this category is already completely declared.
425 if (!IDecl || IDecl->isForwardDecl())
426 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
427
428 /// TODO: Check that CatName, category name, is not used in another
429 // implementation.
430 return CDecl;
431}
432
433Sema::DeclTy *Sema::ActOnStartClassImplementation(
434 SourceLocation AtClassImplLoc,
435 IdentifierInfo *ClassName, SourceLocation ClassLoc,
436 IdentifierInfo *SuperClassname,
437 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000438 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000439 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000440 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000441 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000442 Diag(ClassLoc, diag::err_redefinition_different_kind,
443 ClassName->getName());
444 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
445 }
446 else {
447 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000449 if (!IDecl)
450 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
451 }
452
453 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000454 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000455 if (SuperClassname) {
456 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000457 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000458 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000459 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
460 SuperClassname->getName());
461 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
462 }
463 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000464 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000465 if (!SDecl)
466 Diag(SuperClassLoc, diag::err_undef_superclass,
467 SuperClassname->getName(), ClassName->getName());
468 else if (IDecl && IDecl->getSuperClass() != SDecl) {
469 // This implementation and its interface do not have the same
470 // super class.
471 Diag(SuperClassLoc, diag::err_conflicting_super_class,
472 SDecl->getName());
473 Diag(SDecl->getLocation(), diag::err_previous_definition);
474 }
475 }
476 }
477
478 if (!IDecl) {
479 // Legacy case of @implementation with no corresponding @interface.
480 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000481
482 // FIXME: Do we support attributes on the @implementation? If so
483 // we should copy them over.
Chris Lattnerb752f282008-07-21 07:06:49 +0000484 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000485 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000486 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000487 IDecl->setSuperClass(SDecl);
488 IDecl->setLocEnd(ClassLoc);
489
490 // Remember that this needs to be removed when the scope is popped.
491 TUScope->AddDecl(IDecl);
492 }
493
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000494 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000495 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
496 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000497
498 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000499 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000500 // FIXME: Don't leak everything!
Chris Lattner4d391482007-12-12 07:09:47 +0000501 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
502 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000503 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000504 return IMPDecl;
505}
506
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000507void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
508 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000509 SourceLocation RBrace) {
510 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000511 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000512 if (!IDecl)
513 return;
514 /// Check case of non-existing @interface decl.
515 /// (legacy objective-c @implementation decl without an @interface decl).
516 /// Add implementations's ivar to the synthesize class's ivar list.
517 if (IDecl->ImplicitInterfaceDecl()) {
518 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
519 return;
520 }
521 // If implementation has empty ivar list, just return.
522 if (numIvars == 0)
523 return;
524
525 assert(ivars && "missing @implementation ivars");
526
527 // Check interface's Ivar list against those in the implementation.
528 // names and types must match.
529 //
Chris Lattner4d391482007-12-12 07:09:47 +0000530 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000531 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000532 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
533 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000534 ObjCIvarDecl* ImplIvar = ivars[j++];
535 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000536 assert (ImplIvar && "missing implementation ivar");
537 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000538 if (Context.getCanonicalType(ImplIvar->getType()) !=
539 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattner4d391482007-12-12 07:09:47 +0000540 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
541 ImplIvar->getIdentifier()->getName());
542 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
543 ClsIvar->getIdentifier()->getName());
544 }
545 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
546 // as error.
547 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
548 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
549 ImplIvar->getIdentifier()->getName());
550 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
551 ClsIvar->getIdentifier()->getName());
Chris Lattner609e4c72007-12-12 18:11:49 +0000552 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000553 }
554 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000555 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000556
557 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000558 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000559 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000560 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000561}
562
Steve Naroff3c2eb662008-02-10 21:38:56 +0000563void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
564 bool &IncompleteImpl) {
565 if (!IncompleteImpl) {
566 Diag(ImpLoc, diag::warn_incomplete_impl);
567 IncompleteImpl = true;
568 }
569 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
570}
571
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000572/// FIXME: Type hierarchies in Objective-C can be deep. We could most
573/// likely improve the efficiency of selector lookups and type
574/// checking by associating with each protocol / interface / category
575/// the flattened instance tables. If we used an immutable set to keep
576/// the table then it wouldn't add significant memory cost and it
577/// would be handy for lookups.
578
Steve Naroffefe7f362008-02-08 22:06:17 +0000579/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000580/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000581void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
582 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000583 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000584 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000585 const llvm::DenseSet<Selector> &ClsMap,
586 ObjCInterfaceDecl *IDecl) {
587 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
588
589 // If a method lookup fails locally we still need to look and see if
590 // the method was implemented by a base class or an inherited
591 // protocol. This lookup is slow, but occurs rarely in correct code
592 // and otherwise would terminate in a warning.
593
Chris Lattner4d391482007-12-12 07:09:47 +0000594 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000596 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000598 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
599 !InsMap.count(method->getSelector()) &&
600 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000601 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000602 }
603 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000605 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000606 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000607 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
608 !ClsMap.count(method->getSelector()) &&
609 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000610 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000611 }
Chris Lattner780f3292008-07-21 21:32:27 +0000612 // Check on this protocols's referenced protocols, recursively.
613 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
614 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000615 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000616}
617
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
619 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000620 llvm::DenseSet<Selector> InsMap;
621 // Check and see if instance methods in class interface have been
622 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000624 E = IMPDecl->instmeth_end(); I != E; ++I)
625 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000626
627 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000628 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000629 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000630 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000631 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000632
Chris Lattner4d391482007-12-12 07:09:47 +0000633 llvm::DenseSet<Selector> ClsMap;
634 // Check and see if class methods in class interface have been
635 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000636 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000637 E = IMPDecl->classmeth_end(); I != E; ++I)
638 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000639
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000641 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000642 if (!ClsMap.count((*I)->getSelector()))
643 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000644
645 // Check the protocol list for unimplemented methods in the @implementation
646 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000647 const ObjCList<ObjCProtocolDecl> &Protocols =
648 IDecl->getReferencedProtocols();
649 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
650 E = Protocols.end(); I != E; ++I)
651 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000652 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000653}
654
655/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000656/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000657void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
658 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000659 llvm::DenseSet<Selector> InsMap;
660 // Check and see if instance methods in category interface have been
661 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000662 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000663 E = CatImplDecl->instmeth_end(); I != E; ++I)
664 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000665
666 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000667 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000668 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000669 if (!InsMap.count((*I)->getSelector()))
670 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
671
Chris Lattner4d391482007-12-12 07:09:47 +0000672 llvm::DenseSet<Selector> ClsMap;
673 // Check and see if class methods in category interface have been
674 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000675 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000676 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
677 I != E; ++I)
678 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000679
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000680 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000681 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000682 if (!ClsMap.count((*I)->getSelector()))
683 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000684
685 // Check the protocol list for unimplemented methods in the @implementation
686 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000687 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
688 E = CatClassDecl->protocol_end(); PI != E; ++PI)
689 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000690 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000691}
692
693/// ActOnForwardClassDeclaration -
694Action::DeclTy *
695Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
696 IdentifierInfo **IdentList, unsigned NumElts)
697{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000698 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000699
700 for (unsigned i = 0; i != NumElts; ++i) {
701 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000702 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000703 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000704 // GCC apparently allows the following idiom:
705 //
706 // typedef NSObject < XCElementTogglerP > XCElementToggler;
707 // @class XCElementToggler;
708 //
709 // FIXME: Make an extension?
710 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
711 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
712 Diag(AtClassLoc, diag::err_redefinition_different_kind,
713 IdentList[i]->getName());
714 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
715 }
Chris Lattner4d391482007-12-12 07:09:47 +0000716 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000717 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000718 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000719 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000720 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000721 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000722
723 // Remember that this needs to be removed when the scope is popped.
724 TUScope->AddDecl(IDecl);
725 }
726
727 Interfaces.push_back(IDecl);
728 }
729
Chris Lattner61f9d412008-03-16 20:34:23 +0000730 return ObjCClassDecl::Create(Context, AtClassLoc,
731 &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000732}
733
734
735/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
736/// returns true, or false, accordingly.
737/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000738bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
739 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000740 if (Context.getCanonicalType(Method->getResultType()) !=
741 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000742 return false;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000743 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +0000744 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
745 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000746 if (Context.getCanonicalType(ParamDecl->getType()) !=
747 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000748 return false;
749 }
750 return true;
751}
752
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000753void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
754 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000755 if (!FirstMethod.Method) {
756 // Haven't seen a method with this selector name yet - add it.
757 FirstMethod.Method = Method;
758 FirstMethod.Next = 0;
759 } else {
760 // We've seen a method with this name, now check the type signature(s).
761 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
762
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000764 Next = Next->Next)
765 match = MatchTwoMethodDeclarations(Method, Next->Method);
766
767 if (!match) {
768 // We have a new signature for an existing method - add it.
769 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000771 FirstMethod.Next = OMI;
772 }
773 }
774}
775
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000776void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
777 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000778 if (!FirstMethod.Method) {
779 // Haven't seen a method with this selector name yet - add it.
780 FirstMethod.Method = Method;
781 FirstMethod.Next = 0;
782 } else {
783 // We've seen a method with this name, now check the type signature(s).
784 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
785
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000786 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000787 Next = Next->Next)
788 match = MatchTwoMethodDeclarations(Method, Next->Method);
789
790 if (!match) {
791 // We have a new signature for an existing method - add it.
792 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000793 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000794 FirstMethod.Next = OMI;
795 }
796 }
797}
798
Steve Naroffa56f6162007-12-18 01:30:32 +0000799// Note: For class/category implemenations, allMethods/allProperties is
800// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000801void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
802 DeclTy **allMethods, unsigned allNum,
803 DeclTy **allProperties, unsigned pNum) {
804 Decl *ClassDecl = static_cast<Decl *>(classDecl);
805
Steve Naroffa56f6162007-12-18 01:30:32 +0000806 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
807 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000808 // should be true.
809 if (!ClassDecl)
810 return;
811
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
813 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000814
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000815 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
816 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000817
818 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000819 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
820 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000821 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000822
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000823 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +0000824 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
825 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000826 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
827 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
828 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000829 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000830 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000831 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000832 }
Chris Lattner4d391482007-12-12 07:09:47 +0000833
834 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835 ObjCMethodDecl *Method =
836 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000837
838 if (!Method) continue; // Already issued a diagnostic.
839 if (Method->isInstance()) {
840 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000841 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000842 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
843 : false;
844 if (isInterfaceDeclKind && PrevMethod && !match
845 || checkIdenticalMethods && match) {
846 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
847 Method->getSelector().getName());
848 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
849 } else {
850 insMethods.push_back(Method);
851 InsMap[Method->getSelector()] = Method;
852 /// The following allows us to typecheck messages to "id".
853 AddInstanceMethodToGlobalPool(Method);
854 }
855 }
856 else {
857 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000858 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000859 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
860 : false;
861 if (isInterfaceDeclKind && PrevMethod && !match
862 || checkIdenticalMethods && match) {
863 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
864 Method->getSelector().getName());
865 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
866 } else {
867 clsMethods.push_back(Method);
868 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000869 /// The following allows us to typecheck messages to "Class".
870 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000871 }
872 }
873 }
874
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000875 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000876 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000877 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000878 ComparePropertiesInBaseAndSuper(I);
879 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000880 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
881 e = I->classprop_end(); i != e; ++i)
882 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000883 I->addMethods(&insMethods[0], insMethods.size(),
884 &clsMethods[0], clsMethods.size(), AtEndLoc);
885
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000887 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
888 e = P->classprop_end(); i != e; ++i)
889 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000890 P->addMethods(&insMethods[0], insMethods.size(),
891 &clsMethods[0], clsMethods.size(), AtEndLoc);
892 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000893 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000894 // FIXME: Need to compare properties to those in interface?
895
896 // FIXME: If we merge properties into class we should probably
897 // merge them into category as well?
898 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
899 e = C->classprop_end(); i != e; ++i)
900 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000901 C->addMethods(&insMethods[0], insMethods.size(),
902 &clsMethods[0], clsMethods.size(), AtEndLoc);
903 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904 else if (ObjCImplementationDecl *IC =
905 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000906 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000907 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000908 ImplMethodsVsClassMethods(IC, IDecl);
909 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000910 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000911 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000912 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000913 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000914 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +0000915 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000916 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000917 Categories; Categories = Categories->getNextClassCategory()) {
918 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
919 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
920 break;
921 }
922 }
923 }
924 }
925}
926
927
928/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
929/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000930static Decl::ObjCDeclQualifier
931CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
932 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
933 if (PQTVal & ObjCDeclSpec::DQ_In)
934 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
935 if (PQTVal & ObjCDeclSpec::DQ_Inout)
936 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
937 if (PQTVal & ObjCDeclSpec::DQ_Out)
938 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
939 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
940 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
941 if (PQTVal & ObjCDeclSpec::DQ_Byref)
942 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
943 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
944 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000945
946 return ret;
947}
948
949Sema::DeclTy *Sema::ActOnMethodDeclaration(
950 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000951 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000952 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000953 Selector Sel,
954 // optional arguments. The number of types/arguments is obtained
955 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000956 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000957 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
958 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000959 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +0000960
961 // Make sure we can establish a context for the method.
962 if (!ClassDecl) {
963 Diag(MethodLoc, diag::error_missing_method_context);
964 return 0;
965 }
Chris Lattner4d391482007-12-12 07:09:47 +0000966 QualType resultDeclType;
967
968 if (ReturnType)
969 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
970 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000972
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000973 ObjCMethodDecl* ObjCMethod =
974 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +0000975 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000976 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +0000977 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000978 MethodDeclKind == tok::objc_optional ?
979 ObjCMethodDecl::Optional :
980 ObjCMethodDecl::Required);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000981 if (AttrList)
982 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000983
Chris Lattner0ed844b2008-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 Lattner04421082008-04-08 04:40:51 +0000997 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000998 Param->setObjCDeclQualifier(
999 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1000 Params.push_back(Param);
1001 }
1002
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001003 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1004 ObjCMethod->setObjCDeclQualifier(
1005 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1006 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001007
1008 // For implementations (which can be very "coarse grain"), we add the
1009 // method now. This allows the AST to implement lookup methods that work
1010 // incrementally (without waiting until we parse the @end). It also allows
1011 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001012 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001013 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001014 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001015 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001017 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001018 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001020 }
1021 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001022 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001023 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001024 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001025 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001026 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001027 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001028 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001029 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001030 }
1031 }
1032 if (PrevMethod) {
1033 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001034 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1035 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +00001036 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1037 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001038 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001039}
1040
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001041void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1042 SourceLocation Loc,
1043 unsigned &Attributes) {
1044 // FIXME: Improve the reported location.
1045
1046 // readonly and readwrite conflict.
1047 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1048 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
1049 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1050 "readonly", "readwrite");
1051 Attributes ^= ObjCDeclSpec::DQ_PR_readonly;
1052 }
1053
1054 // Check for copy or retain on non-object types.
1055 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1056 !Context.isObjCObjectPointerType(PropertyTy)) {
1057 Diag(Loc, diag::err_objc_property_requires_object,
1058 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1059 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1060 }
1061
1062 // Check for more than one of { assign, copy, retain }.
1063 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1064 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1065 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1066 "assign", "copy");
1067 Attributes ^= ObjCDeclSpec::DQ_PR_copy;
1068 }
1069 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1070 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1071 "assign", "retain");
1072 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1073 }
1074 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1075 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1076 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive,
1077 "copy", "retain");
1078 Attributes ^= ObjCDeclSpec::DQ_PR_retain;
1079 }
1080 }
1081
1082 // Warn if user supplied no assignment attribute, property is
1083 // readwrite, and this is an object type.
1084 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1085 ObjCDeclSpec::DQ_PR_retain)) &&
1086 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1087 Context.isObjCObjectPointerType(PropertyTy)) {
1088 // Skip this warning in gc-only mode.
1089 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1090 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1091
1092 // If non-gc code warn that this is likely inappropriate.
1093 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1094 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1095
1096 // FIXME: Implement warning dependent on NSCopying being
1097 // implemented. See also:
1098 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1099 // (please trim this list while you are at it).
1100 }
1101}
1102
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001103Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1104 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001105 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001106 Selector GetterSel,
1107 Selector SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001108 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001109 QualType T = GetTypeForDeclarator(FD.D, S);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001110 unsigned Attributes = ODS.getPropertyAttributes();
1111
1112 // May modify Attributes.
1113 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1114
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001115 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1116 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001117 // Regardless of setter/getter attribute, we save the default getter/setter
1118 // selector names in anticipation of declaration of setter/getter methods.
1119 PDecl->setGetterName(GetterSel);
1120 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001121
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001122 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001123 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001124
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001125 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001126 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001127
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001128 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001129 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001130
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001131 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001132 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +00001133
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001134 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001135 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001136
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001137 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001138 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001139
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001140 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001141 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001142
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001143 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001144 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001145
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001146 if (MethodImplKind == tok::objc_required)
1147 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1148 else if (MethodImplKind == tok::objc_optional)
1149 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1150
Chris Lattner4d391482007-12-12 07:09:47 +00001151 return PDecl;
1152}
1153
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001154/// ActOnPropertyImplDecl - This routine performs semantic checks and
1155/// builds the AST node for a property implementation declaration; declared
1156/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001157///
1158Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1159 SourceLocation PropertyLoc,
1160 bool Synthesize,
1161 DeclTy *ClassCatImpDecl,
1162 IdentifierInfo *PropertyId,
1163 IdentifierInfo *PropertyIvar) {
1164 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1165 // Make sure we have a context for the property implementation declaration.
1166 if (!ClassImpDecl) {
1167 Diag(AtLoc, diag::error_missing_property_context);
1168 return 0;
1169 }
1170 ObjCPropertyDecl *property = 0;
1171 ObjCInterfaceDecl* IDecl = 0;
1172 // Find the class or category class where this property must have
1173 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001174 ObjCImplementationDecl *IC = 0;
1175 ObjCCategoryImplDecl* CatImplClass = 0;
1176 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001177 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001178 // We always synthesize an interface for an implementation
1179 // without an interface decl. So, IDecl is always non-zero.
1180 assert(IDecl &&
1181 "ActOnPropertyImplDecl - @implementation without @interface");
1182
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001183 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001184 property = IDecl->FindPropertyDeclaration(PropertyId);
1185 if (!property) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001186 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001187 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001188 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001189 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001190 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001191 if (Synthesize) {
1192 Diag(AtLoc, diag::error_synthesize_category_decl);
1193 return 0;
1194 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001195 IDecl = CatImplClass->getClassInterface();
1196 if (!IDecl) {
1197 Diag(AtLoc, diag::error_missing_property_interface);
1198 return 0;
1199 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001200 ObjCCategoryDecl *Category =
1201 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1202
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001203 // If category for this implementation not found, it is an error which
1204 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001205 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001206 return 0;
1207 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001208 property = Category->FindPropertyDeclaration(PropertyId);
1209 if (!property) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001210 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001211 Category->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001212 return 0;
1213 }
1214 }
1215 else {
1216 Diag(AtLoc, diag::error_bad_property_context);
1217 return 0;
1218 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001219 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001220 // Check that we have a valid, previously declared ivar for @synthesize
1221 if (Synthesize) {
1222 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001223 if (!PropertyIvar)
1224 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001225 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001226 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001227 if (!Ivar) {
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001228 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1229 PropertyId->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001230 return 0;
1231 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001232 // Check that type of property and its ivar match.
Chris Lattner717250a2008-07-26 20:50:02 +00001233 if (Context.getCanonicalType(Ivar->getType()) !=
1234 Context.getCanonicalType(property->getType())) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001235 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1236 Ivar->getName());
1237 return 0;
1238 }
1239
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001240 } else if (PropertyIvar) {
1241 // @dynamic
1242 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1243 return 0;
1244 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001245 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001246 ObjCPropertyImplDecl *PIDecl =
1247 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1248 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001249 ObjCPropertyImplDecl::Synthesize
1250 : ObjCPropertyImplDecl::Dynamic),
1251 Ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001252 if (IC)
1253 IC->addPropertyImplementation(PIDecl);
1254 else
1255 CatImplClass->addPropertyImplementation(PIDecl);
1256
1257 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001258}