blob: 3562ba9f09a788e8d0b459171088c725e7594d5c [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
131/// @compaatibility_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
206/// issuer error if they are not declared. It returns list of protocol
207/// declarations in its 'Protocols' argument.
208void
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,
585 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner4d391482007-12-12 07:09:47 +0000586 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000588 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000590 if (!InsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000591 method->getImplementationControl() != ObjCMethodDecl::Optional)
592 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000593 }
594 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000596 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 ObjCMethodDecl *method = *I;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000598 if (!ClsMap.count(method->getSelector()) &&
Steve Naroff3c2eb662008-02-10 21:38:56 +0000599 method->getImplementationControl() != ObjCMethodDecl::Optional)
600 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000601 }
Chris Lattner780f3292008-07-21 21:32:27 +0000602 // Check on this protocols's referenced protocols, recursively.
603 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
604 E = PDecl->protocol_end(); PI != E; ++PI)
605 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000606}
607
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000608void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
609 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000610 llvm::DenseSet<Selector> InsMap;
611 // Check and see if instance methods in class interface have been
612 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000613 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000614 E = IMPDecl->instmeth_end(); I != E; ++I)
615 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000616
617 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000619 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000620 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000621 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4c525092007-12-12 17:58:05 +0000622
Chris Lattner4d391482007-12-12 07:09:47 +0000623 llvm::DenseSet<Selector> ClsMap;
624 // Check and see if class methods in class interface have been
625 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000626 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000627 E = IMPDecl->classmeth_end(); I != E; ++I)
628 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000629
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000631 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000632 if (!ClsMap.count((*I)->getSelector()))
633 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000634
635 // Check the protocol list for unimplemented methods in the @implementation
636 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000637 const ObjCList<ObjCProtocolDecl> &Protocols =
638 IDecl->getReferencedProtocols();
639 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
640 E = Protocols.end(); I != E; ++I)
641 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Steve Naroffefe7f362008-02-08 22:06:17 +0000642 IncompleteImpl, InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000643}
644
645/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
646/// category interface is implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
648 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000649 llvm::DenseSet<Selector> InsMap;
650 // Check and see if instance methods in category interface have been
651 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000652 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000653 E = CatImplDecl->instmeth_end(); I != E; ++I)
654 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000655
656 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000657 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000658 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000659 if (!InsMap.count((*I)->getSelector()))
660 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
661
Chris Lattner4d391482007-12-12 07:09:47 +0000662 llvm::DenseSet<Selector> ClsMap;
663 // Check and see if class methods in category interface have been
664 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000665 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000666 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
667 I != E; ++I)
668 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000669
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000670 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000671 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000672 if (!ClsMap.count((*I)->getSelector()))
673 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000674
675 // Check the protocol list for unimplemented methods in the @implementation
676 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000677 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
678 E = CatClassDecl->protocol_end(); PI != E; ++PI)
679 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000680 InsMap, ClsMap);
Chris Lattner4d391482007-12-12 07:09:47 +0000681}
682
683/// ActOnForwardClassDeclaration -
684Action::DeclTy *
685Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
686 IdentifierInfo **IdentList, unsigned NumElts)
687{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000688 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000689
690 for (unsigned i = 0; i != NumElts; ++i) {
691 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000692 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000694 // GCC apparently allows the following idiom:
695 //
696 // typedef NSObject < XCElementTogglerP > XCElementToggler;
697 // @class XCElementToggler;
698 //
699 // FIXME: Make an extension?
700 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
701 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
702 Diag(AtClassLoc, diag::err_redefinition_different_kind,
703 IdentList[i]->getName());
704 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
705 }
Chris Lattner4d391482007-12-12 07:09:47 +0000706 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000707 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000708 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000709 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000710 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000711 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000712
713 // Remember that this needs to be removed when the scope is popped.
714 TUScope->AddDecl(IDecl);
715 }
716
717 Interfaces.push_back(IDecl);
718 }
719
Chris Lattner61f9d412008-03-16 20:34:23 +0000720 return ObjCClassDecl::Create(Context, AtClassLoc,
721 &Interfaces[0], Interfaces.size());
Chris Lattner4d391482007-12-12 07:09:47 +0000722}
723
724
725/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
726/// returns true, or false, accordingly.
727/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000728bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
729 const ObjCMethodDecl *PrevMethod) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000730 if (Context.getCanonicalType(Method->getResultType()) !=
731 Context.getCanonicalType(PrevMethod->getResultType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000732 return false;
Chris Lattner58cce3b2008-03-16 01:07:14 +0000733 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner4d391482007-12-12 07:09:47 +0000734 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
735 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner8bcfc5b2008-04-06 23:10:54 +0000736 if (Context.getCanonicalType(ParamDecl->getType()) !=
737 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner4d391482007-12-12 07:09:47 +0000738 return false;
739 }
740 return true;
741}
742
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000743void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
744 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000745 if (!FirstMethod.Method) {
746 // Haven't seen a method with this selector name yet - add it.
747 FirstMethod.Method = Method;
748 FirstMethod.Next = 0;
749 } else {
750 // We've seen a method with this name, now check the type signature(s).
751 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
752
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000753 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000754 Next = Next->Next)
755 match = MatchTwoMethodDeclarations(Method, Next->Method);
756
757 if (!match) {
758 // We have a new signature for an existing method - add it.
759 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000760 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000761 FirstMethod.Next = OMI;
762 }
763 }
764}
765
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000766void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
767 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000768 if (!FirstMethod.Method) {
769 // Haven't seen a method with this selector name yet - add it.
770 FirstMethod.Method = Method;
771 FirstMethod.Next = 0;
772 } else {
773 // We've seen a method with this name, now check the type signature(s).
774 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
775
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000776 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000777 Next = Next->Next)
778 match = MatchTwoMethodDeclarations(Method, Next->Method);
779
780 if (!match) {
781 // We have a new signature for an existing method - add it.
782 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000783 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000784 FirstMethod.Next = OMI;
785 }
786 }
787}
788
Steve Naroffa56f6162007-12-18 01:30:32 +0000789// Note: For class/category implemenations, allMethods/allProperties is
790// always null.
Chris Lattner4d391482007-12-12 07:09:47 +0000791void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
792 DeclTy **allMethods, unsigned allNum,
793 DeclTy **allProperties, unsigned pNum) {
794 Decl *ClassDecl = static_cast<Decl *>(classDecl);
795
Steve Naroffa56f6162007-12-18 01:30:32 +0000796 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
797 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +0000798 // should be true.
799 if (!ClassDecl)
800 return;
801
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000802 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
803 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner4d391482007-12-12 07:09:47 +0000804
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000805 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
806 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner4d391482007-12-12 07:09:47 +0000807
808 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000809 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
810 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000811 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000812
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000813 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +0000814 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
815 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000816 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
817 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
818 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000819 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +0000820 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +0000821 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +0000822 }
Chris Lattner4d391482007-12-12 07:09:47 +0000823
824 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000825 ObjCMethodDecl *Method =
826 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +0000827
828 if (!Method) continue; // Already issued a diagnostic.
829 if (Method->isInstance()) {
830 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000831 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000832 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
833 : false;
834 if (isInterfaceDeclKind && PrevMethod && !match
835 || checkIdenticalMethods && match) {
836 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
837 Method->getSelector().getName());
838 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
839 } else {
840 insMethods.push_back(Method);
841 InsMap[Method->getSelector()] = Method;
842 /// The following allows us to typecheck messages to "id".
843 AddInstanceMethodToGlobalPool(Method);
844 }
845 }
846 else {
847 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000848 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000849 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
850 : false;
851 if (isInterfaceDeclKind && PrevMethod && !match
852 || checkIdenticalMethods && match) {
853 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
854 Method->getSelector().getName());
855 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
856 } else {
857 clsMethods.push_back(Method);
858 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +0000859 /// The following allows us to typecheck messages to "Class".
860 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +0000861 }
862 }
863 }
864
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000865 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000866 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000867 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000868 ComparePropertiesInBaseAndSuper(I);
869 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000870 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
871 e = I->classprop_end(); i != e; ++i)
872 I->addPropertyMethods(Context, *i, insMethods);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000873 I->addMethods(&insMethods[0], insMethods.size(),
874 &clsMethods[0], clsMethods.size(), AtEndLoc);
875
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000876 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000877 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
878 e = P->classprop_end(); i != e; ++i)
879 P->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000880 P->addMethods(&insMethods[0], insMethods.size(),
881 &clsMethods[0], clsMethods.size(), AtEndLoc);
882 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000883 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000884 // FIXME: Need to compare properties to those in interface?
885
886 // FIXME: If we merge properties into class we should probably
887 // merge them into category as well?
888 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
889 e = C->classprop_end(); i != e; ++i)
890 C->addPropertyMethods(Context, *i, insMethods);
Chris Lattner4d391482007-12-12 07:09:47 +0000891 C->addMethods(&insMethods[0], insMethods.size(),
892 &clsMethods[0], clsMethods.size(), AtEndLoc);
893 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000894 else if (ObjCImplementationDecl *IC =
895 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000896 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000897 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +0000898 ImplMethodsVsClassMethods(IC, IDecl);
899 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000900 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000901 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000902 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000903 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000904 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +0000905 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000906 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +0000907 Categories; Categories = Categories->getNextClassCategory()) {
908 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
909 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
910 break;
911 }
912 }
913 }
914 }
915}
916
917
918/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
919/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000920static Decl::ObjCDeclQualifier
921CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
922 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
923 if (PQTVal & ObjCDeclSpec::DQ_In)
924 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
925 if (PQTVal & ObjCDeclSpec::DQ_Inout)
926 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
927 if (PQTVal & ObjCDeclSpec::DQ_Out)
928 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
929 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
930 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
931 if (PQTVal & ObjCDeclSpec::DQ_Byref)
932 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
933 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
934 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +0000935
936 return ret;
937}
938
939Sema::DeclTy *Sema::ActOnMethodDeclaration(
940 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000941 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000942 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +0000943 Selector Sel,
944 // optional arguments. The number of types/arguments is obtained
945 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000946 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +0000947 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
948 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000949 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +0000950
951 // Make sure we can establish a context for the method.
952 if (!ClassDecl) {
953 Diag(MethodLoc, diag::error_missing_method_context);
954 return 0;
955 }
Chris Lattner4d391482007-12-12 07:09:47 +0000956 QualType resultDeclType;
957
958 if (ReturnType)
959 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
960 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000961 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +0000962
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000963 ObjCMethodDecl* ObjCMethod =
964 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Daniel Dunbarf6414922008-08-20 18:02:42 +0000965 ClassDecl,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000966 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +0000967 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000968 MethodDeclKind == tok::objc_optional ?
969 ObjCMethodDecl::Optional :
970 ObjCMethodDecl::Required);
Daniel Dunbarf6414922008-08-20 18:02:42 +0000971 if (AttrList)
972 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner6c4ae5d2008-03-16 00:49:28 +0000973
Chris Lattner0ed844b2008-04-04 06:12:32 +0000974 llvm::SmallVector<ParmVarDecl*, 16> Params;
975
976 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
977 // FIXME: arg->AttrList must be stored too!
978 QualType argType;
979
980 if (ArgTypes[i])
981 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
982 else
983 argType = Context.getObjCIdType();
984 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
985 SourceLocation(/*FIXME*/),
986 ArgNames[i], argType,
Chris Lattner04421082008-04-08 04:40:51 +0000987 VarDecl::None, 0, 0);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000988 Param->setObjCDeclQualifier(
989 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
990 Params.push_back(Param);
991 }
992
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000993 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
994 ObjCMethod->setObjCDeclQualifier(
995 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
996 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000997
998 // For implementations (which can be very "coarse grain"), we add the
999 // method now. This allows the AST to implement lookup methods that work
1000 // incrementally (without waiting until we parse the @end). It also allows
1001 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001002 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001003 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001004 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001005 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001007 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001008 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001010 }
1011 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001012 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001013 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001014 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001015 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001017 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001018 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001020 }
1021 }
1022 if (PrevMethod) {
1023 // You can never have two method definitions with the same name.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001024 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1025 ObjCMethod->getSelector().getName());
Chris Lattner4d391482007-12-12 07:09:47 +00001026 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1027 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001029}
1030
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001031Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1032 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001033 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001034 Selector GetterSel,
1035 Selector SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001036 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001037 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001038 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1039 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001040 // Regardless of setter/getter attribute, we save the default getter/setter
1041 // selector names in anticipation of declaration of setter/getter methods.
1042 PDecl->setGetterName(GetterSel);
1043 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001044
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001045 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001046 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001047
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001048 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001049 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001050
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001051 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001052 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001053
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001054 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001055 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner4d391482007-12-12 07:09:47 +00001056
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001057 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001058 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001059
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001060 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001061 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001062
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001063 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001065
Fariborz Jahaniandae1a1a2008-04-11 23:40:25 +00001066 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001067 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001068
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001069 if (MethodImplKind == tok::objc_required)
1070 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1071 else if (MethodImplKind == tok::objc_optional)
1072 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1073
Chris Lattner4d391482007-12-12 07:09:47 +00001074 return PDecl;
1075}
1076
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001077/// ActOnPropertyImplDecl - This routine performs semantic checks and
1078/// builds the AST node for a property implementation declaration; declared
1079/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001080///
1081Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1082 SourceLocation PropertyLoc,
1083 bool Synthesize,
1084 DeclTy *ClassCatImpDecl,
1085 IdentifierInfo *PropertyId,
1086 IdentifierInfo *PropertyIvar) {
1087 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1088 // Make sure we have a context for the property implementation declaration.
1089 if (!ClassImpDecl) {
1090 Diag(AtLoc, diag::error_missing_property_context);
1091 return 0;
1092 }
1093 ObjCPropertyDecl *property = 0;
1094 ObjCInterfaceDecl* IDecl = 0;
1095 // Find the class or category class where this property must have
1096 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001097 ObjCImplementationDecl *IC = 0;
1098 ObjCCategoryImplDecl* CatImplClass = 0;
1099 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001100 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001101 // We always synthesize an interface for an implementation
1102 // without an interface decl. So, IDecl is always non-zero.
1103 assert(IDecl &&
1104 "ActOnPropertyImplDecl - @implementation without @interface");
1105
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001106 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001107 property = IDecl->FindPropertyDeclaration(PropertyId);
1108 if (!property) {
1109 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001110 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001111 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001112 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001113 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001114 if (Synthesize) {
1115 Diag(AtLoc, diag::error_synthesize_category_decl);
1116 return 0;
1117 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001118 IDecl = CatImplClass->getClassInterface();
1119 if (!IDecl) {
1120 Diag(AtLoc, diag::error_missing_property_interface);
1121 return 0;
1122 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001123 ObjCCategoryDecl *Category =
1124 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1125
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001126 // If category for this implementation not found, it is an error which
1127 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001128 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001129 return 0;
1130 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001131 property = Category->FindPropertyDeclaration(PropertyId);
1132 if (!property) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001133 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001134 Category->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001135 return 0;
1136 }
1137 }
1138 else {
1139 Diag(AtLoc, diag::error_bad_property_context);
1140 return 0;
1141 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001142 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001143 // Check that we have a valid, previously declared ivar for @synthesize
1144 if (Synthesize) {
1145 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001146 if (!PropertyIvar)
1147 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001148 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001149 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001150 if (!Ivar) {
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001151 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1152 PropertyId->getName());
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001153 return 0;
1154 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001155 // Check that type of property and its ivar match.
Chris Lattner717250a2008-07-26 20:50:02 +00001156 if (Context.getCanonicalType(Ivar->getType()) !=
1157 Context.getCanonicalType(property->getType())) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001158 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1159 Ivar->getName());
1160 return 0;
1161 }
1162
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001163 } else if (PropertyIvar) {
1164 // @dynamic
1165 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1166 return 0;
1167 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001168 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001169 ObjCPropertyImplDecl *PIDecl =
1170 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1171 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001172 ObjCPropertyImplDecl::Synthesize
1173 : ObjCPropertyImplDecl::Dynamic),
1174 Ivar);
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001175 if (IC)
1176 IC->addPropertyImplementation(PIDecl);
1177 else
1178 CatImplClass->addPropertyImplementation(PIDecl);
1179
1180 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001181}