blob: 84a8e82750961028d6717e649e3719adfac1b540 [file] [log] [blame]
Chris Lattner855e51f2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner855e51f2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Parse/Scope.h"
18
19using namespace clang;
20
Ted Kremenek42730c52008-01-07 19:49:32 +000021/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
Chris Lattner855e51f2007-12-12 07:09:47 +000022/// and user declared, in the method definition's AST.
Ted Kremenek42730c52008-01-07 19:49:32 +000023void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
Chris Lattner855e51f2007-12-12 07:09:47 +000024 assert(CurFunctionDecl == 0 && "Method parsing confused");
Ted Kremenek42730c52008-01-07 19:49:32 +000025 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Chris Lattner855e51f2007-12-12 07:09:47 +000026 assert(MDecl != 0 && "Not a method declarator!");
Steve Narofffe9eb6a2007-12-18 01:30:32 +000027
28 // Allow the rest of sema to find private method decl implementations.
29 if (MDecl->isInstance())
30 AddInstanceMethodToGlobalPool(MDecl);
31 else
32 AddFactoryMethodToGlobalPool(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000033
34 // Allow all of Sema to see that we are entering a method definition.
35 CurMethodDecl = MDecl;
Chris Lattnerf3874bc2008-04-06 04:47:34 +000036 PushDeclContext(MDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000037
38 // Create Decl objects for each parameter, entrring them in the scope for
39 // binding to their use.
40 struct DeclaratorChunk::ParamInfo PI;
41
42 // Insert the invisible arguments, self and _cmd!
43 PI.Ident = &Context.Idents.get("self");
44 PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
Steve Naroff1a5027c2008-06-05 14:49:39 +000045 QualType selfTy;
Chris Lattner855e51f2007-12-12 07:09:47 +000046 if (MDecl->isInstance()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +000047 selfTy = Context.getObjCIdType();
Gabor Greif9b2e0f92008-02-29 20:35:55 +000048 if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
49 // There may be no interface context due to error in declaration of the
50 // interface (which has been reported). Recover gracefully
Chris Lattner3e254fb2008-04-08 04:40:51 +000051 selfTy = Context.getObjCInterfaceType(OID);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000052 selfTy = Context.getPointerType(selfTy);
Fariborz Jahanian6833b3b2008-01-10 20:33:58 +000053 }
Steve Naroff1a5027c2008-06-05 14:49:39 +000054 } else // we have a factory method.
55 selfTy = Context.getObjCClassType();
Chris Lattner3e254fb2008-04-08 04:40:51 +000056 CurMethodDecl->setSelfDecl(CreateImplicitParameter(FnBodyScope, PI.Ident,
57 PI.IdentLoc, selfTy));
Chris Lattner855e51f2007-12-12 07:09:47 +000058
59 PI.Ident = &Context.Idents.get("_cmd");
Chris Lattner3e254fb2008-04-08 04:40:51 +000060 CreateImplicitParameter(FnBodyScope, PI.Ident, PI.IdentLoc,
61 Context.getObjCSelType());
62
Chris Lattner97316c02008-04-10 02:22:51 +000063 // Introduce all of the other parameters into this scope.
Chris Lattner685d7922008-03-16 01:07:14 +000064 for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +000065 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +000066 IdentifierInfo *II = PDecl->getIdentifier();
Argiris Kirtzidis43ce0be2008-04-27 13:30:35 +000067 if (II)
68 PushOnScopeChains(PDecl, FnBodyScope);
Chris Lattner855e51f2007-12-12 07:09:47 +000069 }
70}
71
72Sema::DeclTy *Sema::ActOnStartClassInterface(
73 SourceLocation AtInterfaceLoc,
74 IdentifierInfo *ClassName, SourceLocation ClassLoc,
75 IdentifierInfo *SuperName, SourceLocation SuperLoc,
76 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
77 SourceLocation EndProtoLoc, AttributeList *AttrList) {
78 assert(ClassName && "Missing class identifier");
79
80 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +000081 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000082 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +000083 Diag(ClassLoc, diag::err_redefinition_different_kind,
84 ClassName->getName());
85 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
86 }
87
Ted Kremenek42730c52008-01-07 19:49:32 +000088 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +000089 if (IDecl) {
90 // Class already seen. Is it a forward declaration?
91 if (!IDecl->isForwardDecl())
92 Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
93 else {
94 IDecl->setLocation(AtInterfaceLoc);
95 IDecl->setForwardDecl(false);
96 IDecl->AllocIntfRefProtocols(NumProtocols);
97 }
98 }
99 else {
Chris Lattner0db541b2008-03-16 01:15:50 +0000100 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
Steve Naroff7c371742008-04-11 19:35:35 +0000101 ClassName, ClassLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000102
Steve Naroff15208162008-04-02 18:30:49 +0000103 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000104 // Remember that this needs to be removed when the scope is popped.
105 TUScope->AddDecl(IDecl);
106 }
107
108 if (SuperName) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000110 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000111 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000113 Diag(SuperLoc, diag::err_redefinition_different_kind,
114 SuperName->getName());
115 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
116 }
117 else {
118 // Check that super class is previously defined
Ted Kremenek42730c52008-01-07 19:49:32 +0000119 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000120
121 if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
122 Diag(AtInterfaceLoc, diag::err_undef_superclass,
123 SuperClassEntry ? SuperClassEntry->getName()
124 : SuperName->getName(),
125 ClassName->getName());
126 }
127 }
128 IDecl->setSuperClass(SuperClassEntry);
Steve Naroff7c371742008-04-11 19:35:35 +0000129 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000130 IDecl->setLocEnd(SuperLoc);
131 } else { // we have a root class.
132 IDecl->setLocEnd(ClassLoc);
133 }
134
135 /// Check then save referenced protocols
136 if (NumProtocols) {
137 for (unsigned int i = 0; i != NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000138 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000139 if (!RefPDecl || RefPDecl->isForwardDecl())
140 Diag(ClassLoc, diag::warn_undef_protocolref,
141 ProtocolNames[i]->getName(),
142 ClassName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000143 IDecl->setIntfRefProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000144 }
145 IDecl->setLocEnd(EndProtoLoc);
146 }
147 return IDecl;
148}
149
150/// ActOnCompatiblityAlias - this action is called after complete parsing of
151/// @compaatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe57c21a2008-04-01 23:04:06 +0000152Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
153 IdentifierInfo *AliasName,
154 SourceLocation AliasLocation,
155 IdentifierInfo *ClassName,
156 SourceLocation ClassLocation) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000157 // Look for previous declaration of alias name
Steve Naroff6384a012008-04-02 14:35:35 +0000158 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner855e51f2007-12-12 07:09:47 +0000159 if (ADecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000160 if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000161 Diag(AliasLocation, diag::warn_previous_alias_decl);
162 Diag(ADecl->getLocation(), diag::warn_previous_declaration);
163 }
164 else {
165 Diag(AliasLocation, diag::err_conflicting_aliasing_type,
166 AliasName->getName());
167 Diag(ADecl->getLocation(), diag::err_previous_declaration);
168 }
169 return 0;
170 }
171 // Check for class declaration
Steve Naroff6384a012008-04-02 14:35:35 +0000172 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner2d1c4312008-03-16 21:17:37 +0000173 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
174 if (CDecl == 0) {
175 Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
176 if (CDeclU)
177 Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
Chris Lattner855e51f2007-12-12 07:09:47 +0000178 return 0;
179 }
Chris Lattner2d1c4312008-03-16 21:17:37 +0000180
181 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremenek42730c52008-01-07 19:49:32 +0000182 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe57c21a2008-04-01 23:04:06 +0000183 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
184
185 ObjCAliasDecls[AliasName] = AliasDecl;
186 TUScope->AddDecl(AliasDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000187 return AliasDecl;
188}
189
190Sema::DeclTy *Sema::ActOnStartProtocolInterface(
191 SourceLocation AtProtoInterfaceLoc,
192 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
193 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
194 SourceLocation EndProtoLoc) {
195 assert(ProtocolName && "Missing protocol identifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000196 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner855e51f2007-12-12 07:09:47 +0000197 if (PDecl) {
198 // Protocol already seen. Better be a forward protocol declaration
Chris Lattnerc1881852008-03-16 01:25:17 +0000199 if (!PDecl->isForwardDecl()) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000200 Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
201 ProtocolName->getName());
Chris Lattnerc1881852008-03-16 01:25:17 +0000202 // Just return the protocol we already had.
203 // FIXME: don't leak the objects passed in!
204 return PDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000205 }
Chris Lattnerc1881852008-03-16 01:25:17 +0000206
207 PDecl->setForwardDecl(false);
208 PDecl->AllocReferencedProtocols(NumProtoRefs);
209 } else {
Chris Lattner180f7e22008-03-16 01:23:04 +0000210 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
Chris Lattner7afba9c2008-03-16 20:19:15 +0000211 ProtocolName);
212 PDecl->setForwardDecl(false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000213 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattner180f7e22008-03-16 01:23:04 +0000214 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000215
216 if (NumProtoRefs) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000217 /// Check then save referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000218 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000219 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000220 if (!RefPDecl || RefPDecl->isForwardDecl())
221 Diag(ProtocolLoc, diag::warn_undef_protocolref,
222 ProtoRefNames[i]->getName(),
223 ProtocolName->getName());
Fariborz Jahanian87829072007-12-20 19:24:10 +0000224 PDecl->setReferencedProtocols(i, RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000225 }
226 PDecl->setLocEnd(EndProtoLoc);
227 }
228 return PDecl;
229}
230
231/// FindProtocolDeclaration - This routine looks up protocols and
232/// issuer error if they are not declared. It returns list of protocol
233/// declarations in its 'Protocols' argument.
234void
235Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
236 IdentifierInfo **ProtocolId,
237 unsigned NumProtocols,
238 llvm::SmallVector<DeclTy *,8> &Protocols) {
239 for (unsigned i = 0; i != NumProtocols; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000241 if (!PDecl)
242 Diag(TypeLoc, diag::err_undeclared_protocol,
243 ProtocolId[i]->getName());
244 else
245 Protocols.push_back(PDecl);
246 }
247}
248
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000249/// DiagnosePropertyMismatch - Compares two properties for their
250/// attributes and types and warns on a variety of inconsistancies.
251///
Fariborz Jahanianed986602008-05-01 00:03:38 +0000252void
253Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
254 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000255 const char *inheritedName) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000256 ObjCPropertyDecl::PropertyAttributeKind CAttr =
257 Property->getPropertyAttributes();
258 ObjCPropertyDecl::PropertyAttributeKind SAttr =
259 SuperProperty->getPropertyAttributes();
260 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
261 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000262 Diag(Property->getLocation(), diag::warn_readonly_property,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000263 Property->getName(), inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000264 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
265 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000266 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000267 Property->getName(), "copy", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000268 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000269 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
270 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000271 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000272 Property->getName(), "retain", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000273 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000274
275 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
276 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Fariborz Jahanianed986602008-05-01 00:03:38 +0000277 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000278 Property->getName(), "atomic", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000279 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000280 if (Property->getSetterName() != SuperProperty->getSetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000281 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000282 Property->getName(), "setter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000283 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000284 if (Property->getGetterName() != SuperProperty->getGetterName())
Fariborz Jahanianed986602008-05-01 00:03:38 +0000285 Diag(Property->getLocation(), diag::warn_property_attribute,
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000286 Property->getName(), "getter", inheritedName,
Fariborz Jahanianed986602008-05-01 00:03:38 +0000287 SourceRange());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000288
Fariborz Jahanian513e30a2008-05-01 18:05:01 +0000289 if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
290 Diag(Property->getLocation(), diag::warn_property_type,
291 Property->getType().getAsString(),
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000292 inheritedName);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000293
294}
295
296/// ComparePropertiesInBaseAndSuper - This routine compares property
297/// declarations in base and its super class, if any, and issues
298/// diagnostics in a variety of inconsistant situations.
299///
300void
Fariborz Jahanianed986602008-05-01 00:03:38 +0000301Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000302 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
303 if (!SDecl)
304 return;
Fariborz Jahanianed986602008-05-01 00:03:38 +0000305 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
306 E = SDecl->classprop_end(); S != E; ++S) {
307 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000308 // Does property in super class has declaration in current class?
309 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
310 E = IDecl->classprop_end(); I != E; ++I) {
311 ObjCPropertyDecl *PDecl = (*I);
312 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000313 DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
Fariborz Jahanian1e89de32008-04-24 19:58:34 +0000314 }
315 }
316}
317
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000318/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
319/// of properties declared in a protocol and adds them to the list
320/// of properties for current class if it is not there already.
321void
322Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
323 ObjCProtocolDecl *PDecl)
324{
325 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
326 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
327 E = PDecl->classprop_end(); P != E; ++P) {
328 ObjCPropertyDecl *Pr = (*P);
329 ObjCInterfaceDecl::classprop_iterator CP, CE;
330 // Is this property already in class's list of properties?
331 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
332 CP != CE; ++CP)
333 if ((*CP)->getIdentifier() == Pr->getIdentifier())
334 break;
335 if (CP == CE)
336 // Add this property to list of properties for thie class.
337 mergeProperties.push_back(Pr);
338 else
339 // Property protocol already exist in class. Diagnose any mismatch.
340 DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
341 }
342 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
343}
344
345/// MergeProtocolPropertiesIntoClass - This routine merges properties
346/// declared in 'MergeItsProtocols' objects (which can be a class or an
347/// inherited protocol into the list of properties for class 'IDecl'
348///
349
350void
351Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
352 DeclTy *MergeItsProtocols) {
353 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
354 if (ObjCInterfaceDecl *MDecl =
355 dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
356 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
357 E = MDecl->protocol_end(); P != E; ++P)
358 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
359 // Merge properties of class (*P) into IDECL's
360 ;
361 // Go thru the list of protocols for this class and recursively merge
362 // their properties into this class as well.
363 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
364 E = IDecl->protocol_end(); P != E; ++P)
365 MergeProtocolPropertiesIntoClass(IDecl, (*P));
366 }
367 else if (ObjCProtocolDecl *MDecl =
368 dyn_cast<ObjCProtocolDecl>(ClassDecl))
369 for (ObjCProtocolDecl::protocol_iterator P = MDecl->protocol_begin(),
370 E = MDecl->protocol_end(); P != E; ++P)
371 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
372 else
373 assert(false && "MergeProtocolPropertiesIntoClass - bad object kind");
374}
375
Chris Lattner855e51f2007-12-12 07:09:47 +0000376/// ActOnForwardProtocolDeclaration -
377Action::DeclTy *
378Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
379 IdentifierInfo **IdentList, unsigned NumElts) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000380 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner855e51f2007-12-12 07:09:47 +0000381
382 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7afba9c2008-03-16 20:19:15 +0000383 IdentifierInfo *Ident = IdentList[i];
384 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
385 if (PDecl == 0) { // Not already seen?
Chris Lattner855e51f2007-12-12 07:09:47 +0000386 // FIXME: Pass in the location of the identifier!
Chris Lattner7afba9c2008-03-16 20:19:15 +0000387 PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
Chris Lattner855e51f2007-12-12 07:09:47 +0000388 }
389
390 Protocols.push_back(PDecl);
391 }
Chris Lattnere29dc832008-03-16 20:34:23 +0000392 return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
393 &Protocols[0], Protocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000394}
395
396Sema::DeclTy *Sema::ActOnStartCategoryInterface(
397 SourceLocation AtInterfaceLoc,
398 IdentifierInfo *ClassName, SourceLocation ClassLoc,
399 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
400 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
401 SourceLocation EndProtoLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000402 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000403
Chris Lattnere29dc832008-03-16 20:34:23 +0000404 ObjCCategoryDecl *CDecl =
Chris Lattner321b5d12008-03-16 20:47:45 +0000405 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner855e51f2007-12-12 07:09:47 +0000406 CDecl->setClassInterface(IDecl);
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000407
408 /// Check that class of this category is already completely declared.
409 if (!IDecl || IDecl->isForwardDecl())
410 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
Steve Naroffac0580b2008-06-05 15:03:27 +0000411 else {
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000412 /// Check for duplicate interface declaration for this category
413 ObjCCategoryDecl *CDeclChain;
414 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
415 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffac0580b2008-06-05 15:03:27 +0000416 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Steve Naroff0a7149f2008-06-05 04:33:44 +0000417 Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000418 CategoryName->getName());
419 break;
420 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000421 }
Steve Naroffac0580b2008-06-05 15:03:27 +0000422 if (!CDeclChain)
423 CDecl->insertNextClassCategory();
Fariborz Jahanian6669a582008-01-17 20:33:24 +0000424 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000425
426 if (NumProtoRefs) {
Chris Lattner321b5d12008-03-16 20:47:45 +0000427 llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
428 /// Check and then save the referenced protocols.
Chris Lattner855e51f2007-12-12 07:09:47 +0000429 for (unsigned int i = 0; i != NumProtoRefs; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000430 ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
Chris Lattner855e51f2007-12-12 07:09:47 +0000431 if (!RefPDecl || RefPDecl->isForwardDecl()) {
432 Diag(CategoryLoc, diag::warn_undef_protocolref,
433 ProtoRefNames[i]->getName(),
434 CategoryName->getName());
435 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000436 if (RefPDecl)
437 RefProtocols.push_back(RefPDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000438 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000439 if (!RefProtocols.empty())
440 CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000441 }
Chris Lattner321b5d12008-03-16 20:47:45 +0000442 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner855e51f2007-12-12 07:09:47 +0000443 return CDecl;
444}
445
446/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek42730c52008-01-07 19:49:32 +0000447/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner855e51f2007-12-12 07:09:47 +0000448/// object.
449Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
450 SourceLocation AtCatImplLoc,
451 IdentifierInfo *ClassName, SourceLocation ClassLoc,
452 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner1b6de332008-03-16 20:53:07 +0000454 ObjCCategoryImplDecl *CDecl =
455 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000456 /// Check that class of this category is already completely declared.
457 if (!IDecl || IDecl->isForwardDecl())
458 Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
459
460 /// TODO: Check that CatName, category name, is not used in another
461 // implementation.
462 return CDecl;
463}
464
465Sema::DeclTy *Sema::ActOnStartClassImplementation(
466 SourceLocation AtClassImplLoc,
467 IdentifierInfo *ClassName, SourceLocation ClassLoc,
468 IdentifierInfo *SuperClassname,
469 SourceLocation SuperClassLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000470 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000471 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000472 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000473 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000474 Diag(ClassLoc, diag::err_redefinition_different_kind,
475 ClassName->getName());
476 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
477 }
478 else {
479 // Is there an interface declaration of this class; if not, warn!
Ted Kremenek42730c52008-01-07 19:49:32 +0000480 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000481 if (!IDecl)
482 Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
483 }
484
485 // Check that super class name is valid class name
Ted Kremenek42730c52008-01-07 19:49:32 +0000486 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +0000487 if (SuperClassname) {
488 // Check if a different kind of symbol declared in this scope.
Steve Naroff6384a012008-04-02 14:35:35 +0000489 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000490 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000491 Diag(SuperClassLoc, diag::err_redefinition_different_kind,
492 SuperClassname->getName());
493 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
494 }
495 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000496 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000497 if (!SDecl)
498 Diag(SuperClassLoc, diag::err_undef_superclass,
499 SuperClassname->getName(), ClassName->getName());
500 else if (IDecl && IDecl->getSuperClass() != SDecl) {
501 // This implementation and its interface do not have the same
502 // super class.
503 Diag(SuperClassLoc, diag::err_conflicting_super_class,
504 SDecl->getName());
505 Diag(SDecl->getLocation(), diag::err_previous_definition);
506 }
507 }
508 }
509
510 if (!IDecl) {
511 // Legacy case of @implementation with no corresponding @interface.
512 // Build, chain & install the interface decl into the identifier.
Chris Lattner0db541b2008-03-16 01:15:50 +0000513 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
Steve Naroff7c371742008-04-11 19:35:35 +0000514 ClassLoc, false, true);
Steve Naroff15208162008-04-02 18:30:49 +0000515 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000516 IDecl->setSuperClass(SDecl);
517 IDecl->setLocEnd(ClassLoc);
518
519 // Remember that this needs to be removed when the scope is popped.
520 TUScope->AddDecl(IDecl);
521 }
522
Ted Kremenek42730c52008-01-07 19:49:32 +0000523 ObjCImplementationDecl* IMPDecl =
Chris Lattner1b6de332008-03-16 20:53:07 +0000524 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
525 IDecl, SDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000526
527 // Check that there is no duplicate implementation of this class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000528 if (ObjCImplementations[ClassName])
Chris Lattner1b6de332008-03-16 20:53:07 +0000529 // FIXME: Don't leak everything!
Chris Lattner855e51f2007-12-12 07:09:47 +0000530 Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
531 else // add it to the list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000532 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000533 return IMPDecl;
534}
535
Ted Kremenek42730c52008-01-07 19:49:32 +0000536void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
537 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner855e51f2007-12-12 07:09:47 +0000538 SourceLocation RBrace) {
539 assert(ImpDecl && "missing implementation decl");
Ted Kremenek42730c52008-01-07 19:49:32 +0000540 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner855e51f2007-12-12 07:09:47 +0000541 if (!IDecl)
542 return;
543 /// Check case of non-existing @interface decl.
544 /// (legacy objective-c @implementation decl without an @interface decl).
545 /// Add implementations's ivar to the synthesize class's ivar list.
546 if (IDecl->ImplicitInterfaceDecl()) {
547 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
548 return;
549 }
550 // If implementation has empty ivar list, just return.
551 if (numIvars == 0)
552 return;
553
554 assert(ivars && "missing @implementation ivars");
555
556 // Check interface's Ivar list against those in the implementation.
557 // names and types must match.
558 //
Chris Lattner855e51f2007-12-12 07:09:47 +0000559 unsigned j = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000560 ObjCInterfaceDecl::ivar_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000561 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
562 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000563 ObjCIvarDecl* ImplIvar = ivars[j++];
564 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner855e51f2007-12-12 07:09:47 +0000565 assert (ImplIvar && "missing implementation ivar");
566 assert (ClsIvar && "missing class ivar");
567 if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
568 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
569 ImplIvar->getIdentifier()->getName());
570 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
571 ClsIvar->getIdentifier()->getName());
572 }
573 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
574 // as error.
575 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
576 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
577 ImplIvar->getIdentifier()->getName());
578 Diag(ClsIvar->getLocation(), diag::err_previous_definition,
579 ClsIvar->getIdentifier()->getName());
Chris Lattner1cc669d2007-12-12 18:11:49 +0000580 return;
Chris Lattner855e51f2007-12-12 07:09:47 +0000581 }
582 --numIvars;
Chris Lattner855e51f2007-12-12 07:09:47 +0000583 }
Chris Lattner1cc669d2007-12-12 18:11:49 +0000584
585 if (numIvars > 0)
Chris Lattner847de6f2007-12-12 18:19:52 +0000586 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner1cc669d2007-12-12 18:11:49 +0000587 else if (IVI != IVE)
Chris Lattner847de6f2007-12-12 18:19:52 +0000588 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner855e51f2007-12-12 07:09:47 +0000589}
590
Steve Naroffb4f48512008-02-10 21:38:56 +0000591void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
592 bool &IncompleteImpl) {
593 if (!IncompleteImpl) {
594 Diag(ImpLoc, diag::warn_incomplete_impl);
595 IncompleteImpl = true;
596 }
597 Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
598}
599
Steve Naroffb268d2a2008-02-08 22:06:17 +0000600/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner855e51f2007-12-12 07:09:47 +0000601/// Declared in protocol, and those referenced by it.
Steve Naroffb268d2a2008-02-08 22:06:17 +0000602void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
603 ObjCProtocolDecl *PDecl,
Chris Lattner855e51f2007-12-12 07:09:47 +0000604 bool& IncompleteImpl,
Steve Naroffb268d2a2008-02-08 22:06:17 +0000605 const llvm::DenseSet<Selector> &InsMap,
606 const llvm::DenseSet<Selector> &ClsMap) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000607 // check unimplemented instance methods.
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000609 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000610 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000611 if (!InsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000612 method->getImplementationControl() != ObjCMethodDecl::Optional)
613 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000614 }
615 // check unimplemented class methods
Ted Kremenek42730c52008-01-07 19:49:32 +0000616 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000617 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000618 ObjCMethodDecl *method = *I;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000619 if (!ClsMap.count(method->getSelector()) &&
Steve Naroffb4f48512008-02-10 21:38:56 +0000620 method->getImplementationControl() != ObjCMethodDecl::Optional)
621 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff2ce399a2007-12-14 23:37:57 +0000622 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000623 // Check on this protocols's referenced protocols, recursively
Ted Kremenek42730c52008-01-07 19:49:32 +0000624 ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000625 for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000626 CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000627}
628
Ted Kremenek42730c52008-01-07 19:49:32 +0000629void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
630 ObjCInterfaceDecl* IDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000631 llvm::DenseSet<Selector> InsMap;
632 // Check and see if instance methods in class interface have been
633 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000634 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000635 E = IMPDecl->instmeth_end(); I != E; ++I)
636 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000637
638 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000640 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000641 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroffb4f48512008-02-10 21:38:56 +0000642 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner9d76c722007-12-12 17:58:05 +0000643
Chris Lattner855e51f2007-12-12 07:09:47 +0000644 llvm::DenseSet<Selector> ClsMap;
645 // Check and see if class methods in class interface have been
646 // implemented in the implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000647 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000648 E = IMPDecl->classmeth_end(); I != E; ++I)
649 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000650
Ted Kremenek42730c52008-01-07 19:49:32 +0000651 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000652 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000653 if (!ClsMap.count((*I)->getSelector()))
654 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000655
656 // Check the protocol list for unimplemented methods in the @implementation
657 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000658 ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000659 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
Steve Naroffb268d2a2008-02-08 22:06:17 +0000660 CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
661 IncompleteImpl, InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000662}
663
664/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
665/// category interface is implemented in the category @implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000666void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
667 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000668 llvm::DenseSet<Selector> InsMap;
669 // Check and see if instance methods in category interface have been
670 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000671 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner9d76c722007-12-12 17:58:05 +0000672 E = CatImplDecl->instmeth_end(); I != E; ++I)
673 InsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000674
675 bool IncompleteImpl = false;
Ted Kremenek42730c52008-01-07 19:49:32 +0000676 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000677 E = CatClassDecl->instmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000678 if (!InsMap.count((*I)->getSelector()))
679 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
680
Chris Lattner855e51f2007-12-12 07:09:47 +0000681 llvm::DenseSet<Selector> ClsMap;
682 // Check and see if class methods in category interface have been
683 // implemented in its implementation class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000684 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner9d76c722007-12-12 17:58:05 +0000685 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
686 I != E; ++I)
687 ClsMap.insert((*I)->getSelector());
Chris Lattner855e51f2007-12-12 07:09:47 +0000688
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000690 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroffb4f48512008-02-10 21:38:56 +0000691 if (!ClsMap.count((*I)->getSelector()))
692 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000693
694 // Check the protocol list for unimplemented methods in the @implementation
695 // class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000696 ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
Fariborz Jahanian87829072007-12-20 19:24:10 +0000697 for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000698 ObjCProtocolDecl* PDecl = protocols[i];
Steve Naroffb268d2a2008-02-08 22:06:17 +0000699 CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
700 InsMap, ClsMap);
Chris Lattner855e51f2007-12-12 07:09:47 +0000701 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000702}
703
704/// ActOnForwardClassDeclaration -
705Action::DeclTy *
706Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
707 IdentifierInfo **IdentList, unsigned NumElts)
708{
Ted Kremenek42730c52008-01-07 19:49:32 +0000709 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner855e51f2007-12-12 07:09:47 +0000710
711 for (unsigned i = 0; i != NumElts; ++i) {
712 // Check for another declaration kind with the same name.
Steve Naroff6384a012008-04-02 14:35:35 +0000713 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +0000714 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000715 Diag(AtClassLoc, diag::err_redefinition_different_kind,
716 IdentList[i]->getName());
717 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
718 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000719 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000720 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattner0db541b2008-03-16 01:15:50 +0000721 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
Steve Naroff7c371742008-04-11 19:35:35 +0000722 SourceLocation(), true);
Steve Naroff15208162008-04-02 18:30:49 +0000723 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner855e51f2007-12-12 07:09:47 +0000724
725 // Remember that this needs to be removed when the scope is popped.
726 TUScope->AddDecl(IDecl);
727 }
728
729 Interfaces.push_back(IDecl);
730 }
731
Chris Lattnere29dc832008-03-16 20:34:23 +0000732 return ObjCClassDecl::Create(Context, AtClassLoc,
733 &Interfaces[0], Interfaces.size());
Chris Lattner855e51f2007-12-12 07:09:47 +0000734}
735
736
737/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
738/// returns true, or false, accordingly.
739/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremenek42730c52008-01-07 19:49:32 +0000740bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
741 const ObjCMethodDecl *PrevMethod) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000742 if (Method->getResultType().getCanonicalType() !=
743 PrevMethod->getResultType().getCanonicalType())
744 return false;
Chris Lattner685d7922008-03-16 01:07:14 +0000745 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000746 ParmVarDecl *ParamDecl = Method->getParamDecl(i);
747 ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
Chris Lattner42a21742008-04-06 23:10:54 +0000748 if (Context.getCanonicalType(ParamDecl->getType()) !=
749 Context.getCanonicalType(PrevParamDecl->getType()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000750 return false;
751 }
752 return true;
753}
754
Ted Kremenek42730c52008-01-07 19:49:32 +0000755void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
756 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000757 if (!FirstMethod.Method) {
758 // Haven't seen a method with this selector name yet - add it.
759 FirstMethod.Method = Method;
760 FirstMethod.Next = 0;
761 } else {
762 // We've seen a method with this name, now check the type signature(s).
763 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
764
Ted Kremenek42730c52008-01-07 19:49:32 +0000765 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000766 Next = Next->Next)
767 match = MatchTwoMethodDeclarations(Method, Next->Method);
768
769 if (!match) {
770 // We have a new signature for an existing method - add it.
771 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000772 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000773 FirstMethod.Next = OMI;
774 }
775 }
776}
777
Ted Kremenek42730c52008-01-07 19:49:32 +0000778void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
779 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000780 if (!FirstMethod.Method) {
781 // Haven't seen a method with this selector name yet - add it.
782 FirstMethod.Method = Method;
783 FirstMethod.Next = 0;
784 } else {
785 // We've seen a method with this name, now check the type signature(s).
786 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
787
Ted Kremenek42730c52008-01-07 19:49:32 +0000788 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner855e51f2007-12-12 07:09:47 +0000789 Next = Next->Next)
790 match = MatchTwoMethodDeclarations(Method, Next->Method);
791
792 if (!match) {
793 // We have a new signature for an existing method - add it.
794 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek42730c52008-01-07 19:49:32 +0000795 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner855e51f2007-12-12 07:09:47 +0000796 FirstMethod.Next = OMI;
797 }
798 }
799}
800
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000801// Note: For class/category implemenations, allMethods/allProperties is
802// always null.
Chris Lattner855e51f2007-12-12 07:09:47 +0000803void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
804 DeclTy **allMethods, unsigned allNum,
805 DeclTy **allProperties, unsigned pNum) {
806 Decl *ClassDecl = static_cast<Decl *>(classDecl);
807
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000808 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
809 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner855e51f2007-12-12 07:09:47 +0000810 // should be true.
811 if (!ClassDecl)
812 return;
813
Ted Kremenek42730c52008-01-07 19:49:32 +0000814 llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
815 llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
Chris Lattner855e51f2007-12-12 07:09:47 +0000816
Ted Kremenek42730c52008-01-07 19:49:32 +0000817 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
818 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
Chris Lattner855e51f2007-12-12 07:09:47 +0000819
820 bool isInterfaceDeclKind =
Chris Lattner2d1c4312008-03-16 21:17:37 +0000821 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
822 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +0000823 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000824
Chris Lattner2d1c4312008-03-16 21:17:37 +0000825 if (pNum != 0)
Chris Lattnercffe3662008-03-16 21:23:50 +0000826 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
827 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000828 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
829 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
830 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
831 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian52ff8442008-04-16 21:08:45 +0000832 else
Fariborz Jahanian8516e9a2008-04-17 18:25:18 +0000833 assert(false && "ActOnAtEnd - property declaration misplaced");
Chris Lattner855e51f2007-12-12 07:09:47 +0000834
835 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000836 ObjCMethodDecl *Method =
837 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner855e51f2007-12-12 07:09:47 +0000838
839 if (!Method) continue; // Already issued a diagnostic.
840 if (Method->isInstance()) {
841 /// Check for instance method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000842 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000843 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
844 : false;
845 if (isInterfaceDeclKind && PrevMethod && !match
846 || checkIdenticalMethods && match) {
847 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
848 Method->getSelector().getName());
849 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
850 } else {
851 insMethods.push_back(Method);
852 InsMap[Method->getSelector()] = Method;
853 /// The following allows us to typecheck messages to "id".
854 AddInstanceMethodToGlobalPool(Method);
855 }
856 }
857 else {
858 /// Check for class method of the same name with incompatible types
Ted Kremenek42730c52008-01-07 19:49:32 +0000859 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner855e51f2007-12-12 07:09:47 +0000860 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
861 : false;
862 if (isInterfaceDeclKind && PrevMethod && !match
863 || checkIdenticalMethods && match) {
864 Diag(Method->getLocation(), diag::error_duplicate_method_decl,
865 Method->getSelector().getName());
866 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
867 } else {
868 clsMethods.push_back(Method);
869 ClsMap[Method->getSelector()] = Method;
Steve Narofffe9eb6a2007-12-18 01:30:32 +0000870 /// The following allows us to typecheck messages to "Class".
871 AddFactoryMethodToGlobalPool(Method);
Chris Lattner855e51f2007-12-12 07:09:47 +0000872 }
873 }
874 }
875
Ted Kremenek42730c52008-01-07 19:49:32 +0000876 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianed986602008-05-01 00:03:38 +0000877 // Compares properties declaraed in this class to those of its
878 // super class.
Fariborz Jahanian33973a22008-05-02 19:17:30 +0000879 ComparePropertiesInBaseAndSuper(I);
880 MergeProtocolPropertiesIntoClass(I, I);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000881 for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
882 E = I->classprop_end(); P != E; ++P) {
Steve Naroff638d6a42008-05-22 23:24:08 +0000883 // FIXME: It would be really nice if we could avoid this. Injecting
884 // methods into the interface makes it hard to distinguish "real" methods
885 // from synthesized "property" methods (that aren't in the source).
886 // This complicicates the rewriter's life.
Fariborz Jahaniane4534e72008-05-07 17:43:59 +0000887 I->addPropertyMethods(Context, *P, insMethods);
888 }
889 I->addMethods(&insMethods[0], insMethods.size(),
890 &clsMethods[0], clsMethods.size(), AtEndLoc);
891
Ted Kremenek42730c52008-01-07 19:49:32 +0000892 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000893 P->addMethods(&insMethods[0], insMethods.size(),
894 &clsMethods[0], clsMethods.size(), AtEndLoc);
895 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000896 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000897 C->addMethods(&insMethods[0], insMethods.size(),
898 &clsMethods[0], clsMethods.size(), AtEndLoc);
899 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000900 else if (ObjCImplementationDecl *IC =
901 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +0000902 IC->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner855e51f2007-12-12 07:09:47 +0000904 ImplMethodsVsClassMethods(IC, IDecl);
905 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner855e51f2007-12-12 07:09:47 +0000907 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000908 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner855e51f2007-12-12 07:09:47 +0000909 // Find category interface decl and then check that all methods declared
910 // in this interface is implemented in the category @implementation.
911 if (IDecl) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000912 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner855e51f2007-12-12 07:09:47 +0000913 Categories; Categories = Categories->getNextClassCategory()) {
914 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
915 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
916 break;
917 }
918 }
919 }
920 }
921}
922
923
924/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
925/// objective-c's type qualifier from the parser version of the same info.
Ted Kremenek42730c52008-01-07 19:49:32 +0000926static Decl::ObjCDeclQualifier
927CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
928 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
929 if (PQTVal & ObjCDeclSpec::DQ_In)
930 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
931 if (PQTVal & ObjCDeclSpec::DQ_Inout)
932 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
933 if (PQTVal & ObjCDeclSpec::DQ_Out)
934 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
935 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
936 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
937 if (PQTVal & ObjCDeclSpec::DQ_Byref)
938 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
939 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
940 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner855e51f2007-12-12 07:09:47 +0000941
942 return ret;
943}
944
945Sema::DeclTy *Sema::ActOnMethodDeclaration(
946 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner114add62008-03-16 00:49:28 +0000947 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremenek42730c52008-01-07 19:49:32 +0000948 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner855e51f2007-12-12 07:09:47 +0000949 Selector Sel,
950 // optional arguments. The number of types/arguments is obtained
951 // from the Sel.getNumArgs().
Ted Kremenek42730c52008-01-07 19:49:32 +0000952 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner855e51f2007-12-12 07:09:47 +0000953 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
954 bool isVariadic) {
Chris Lattner114add62008-03-16 00:49:28 +0000955 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroff70f16242008-02-29 21:48:07 +0000956
957 // Make sure we can establish a context for the method.
958 if (!ClassDecl) {
959 Diag(MethodLoc, diag::error_missing_method_context);
960 return 0;
961 }
Chris Lattner855e51f2007-12-12 07:09:47 +0000962 QualType resultDeclType;
963
964 if (ReturnType)
965 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
966 else // get the type for "id".
Ted Kremenek42730c52008-01-07 19:49:32 +0000967 resultDeclType = Context.getObjCIdType();
Chris Lattner855e51f2007-12-12 07:09:47 +0000968
Chris Lattner114add62008-03-16 00:49:28 +0000969 ObjCMethodDecl* ObjCMethod =
970 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Chris Lattnerf7355832008-03-16 00:58:16 +0000971 ClassDecl, AttrList,
Chris Lattner114add62008-03-16 00:49:28 +0000972 MethodType == tok::minus, isVariadic,
Fariborz Jahanian5f2e2242008-05-07 20:53:44 +0000973 false,
Chris Lattner114add62008-03-16 00:49:28 +0000974 MethodDeclKind == tok::objc_optional ?
975 ObjCMethodDecl::Optional :
976 ObjCMethodDecl::Required);
977
Chris Lattnereee57c02008-04-04 06:12:32 +0000978 llvm::SmallVector<ParmVarDecl*, 16> Params;
979
980 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
981 // FIXME: arg->AttrList must be stored too!
982 QualType argType;
983
984 if (ArgTypes[i])
985 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
986 else
987 argType = Context.getObjCIdType();
988 ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
989 SourceLocation(/*FIXME*/),
990 ArgNames[i], argType,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000991 VarDecl::None, 0, 0);
Chris Lattnereee57c02008-04-04 06:12:32 +0000992 Param->setObjCDeclQualifier(
993 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
994 Params.push_back(Param);
995 }
996
Ted Kremenek42730c52008-01-07 19:49:32 +0000997 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
998 ObjCMethod->setObjCDeclQualifier(
999 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1000 const ObjCMethodDecl *PrevMethod = 0;
Chris Lattner855e51f2007-12-12 07:09:47 +00001001
1002 // For implementations (which can be very "coarse grain"), we add the
1003 // method now. This allows the AST to implement lookup methods that work
1004 // incrementally (without waiting until we parse the @end). It also allows
1005 // us to flag multiple declaration errors as they occur.
Ted Kremenek42730c52008-01-07 19:49:32 +00001006 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001007 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001008 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001009 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001010 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001011 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001012 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001013 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001014 }
1015 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001016 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner114add62008-03-16 00:49:28 +00001017 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner855e51f2007-12-12 07:09:47 +00001018 if (MethodType == tok::minus) {
Steve Naroff74273de2007-12-19 22:27:04 +00001019 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001020 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001021 } else {
Steve Naroff74273de2007-12-19 22:27:04 +00001022 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremenek42730c52008-01-07 19:49:32 +00001023 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner855e51f2007-12-12 07:09:47 +00001024 }
1025 }
1026 if (PrevMethod) {
1027 // You can never have two method definitions with the same name.
Ted Kremenek42730c52008-01-07 19:49:32 +00001028 Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
1029 ObjCMethod->getSelector().getName());
Chris Lattner855e51f2007-12-12 07:09:47 +00001030 Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
1031 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001032 return ObjCMethod;
Chris Lattner855e51f2007-12-12 07:09:47 +00001033}
1034
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001035Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1036 FieldDeclarator &FD,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001037 ObjCDeclSpec &ODS,
Fariborz Jahanianb7080ae2008-05-06 18:09:04 +00001038 Selector GetterSel,
1039 Selector SetterSel,
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001040 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001041 QualType T = GetTypeForDeclarator(FD.D, S);
Fariborz Jahanian0ceb4be2008-04-14 23:36:35 +00001042 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1043 FD.D.getIdentifier(), T);
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001044 // Regardless of setter/getter attribute, we save the default getter/setter
1045 // selector names in anticipation of declaration of setter/getter methods.
1046 PDecl->setGetterName(GetterSel);
1047 PDecl->setSetterName(SetterSel);
Chris Lattner855e51f2007-12-12 07:09:47 +00001048
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001049 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek42730c52008-01-07 19:49:32 +00001050 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner855e51f2007-12-12 07:09:47 +00001051
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001052 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001053 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001054
Fariborz Jahaniane4534e72008-05-07 17:43:59 +00001055 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek42730c52008-01-07 19:49:32 +00001056 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner855e51f2007-12-12 07:09:47 +00001057
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001058 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
Ted Kremenek42730c52008-01-07 19:49:32 +00001059 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Chris Lattner855e51f2007-12-12 07:09:47 +00001060
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001061 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
Ted Kremenek42730c52008-01-07 19:49:32 +00001062 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner855e51f2007-12-12 07:09:47 +00001063
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001064 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek42730c52008-01-07 19:49:32 +00001065 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner855e51f2007-12-12 07:09:47 +00001066
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001067 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek42730c52008-01-07 19:49:32 +00001068 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner855e51f2007-12-12 07:09:47 +00001069
Fariborz Jahaniane7071722008-04-11 23:40:25 +00001070 if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek42730c52008-01-07 19:49:32 +00001071 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner855e51f2007-12-12 07:09:47 +00001072
Fariborz Jahanian4aa72a72008-05-05 18:51:55 +00001073 if (MethodImplKind == tok::objc_required)
1074 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1075 else if (MethodImplKind == tok::objc_optional)
1076 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1077
Chris Lattner855e51f2007-12-12 07:09:47 +00001078 return PDecl;
1079}
1080
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001081/// ActOnPropertyImplDecl - This routine performs semantic checks and
1082/// builds the AST node for a property implementation declaration; declared
1083/// as @synthesize or @dynamic.
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001084///
1085Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1086 SourceLocation PropertyLoc,
1087 bool Synthesize,
1088 DeclTy *ClassCatImpDecl,
1089 IdentifierInfo *PropertyId,
1090 IdentifierInfo *PropertyIvar) {
1091 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1092 // Make sure we have a context for the property implementation declaration.
1093 if (!ClassImpDecl) {
1094 Diag(AtLoc, diag::error_missing_property_context);
1095 return 0;
1096 }
1097 ObjCPropertyDecl *property = 0;
1098 ObjCInterfaceDecl* IDecl = 0;
1099 // Find the class or category class where this property must have
1100 // a declaration.
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001101 ObjCImplementationDecl *IC = 0;
1102 ObjCCategoryImplDecl* CatImplClass = 0;
1103 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001104 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001105 // We always synthesize an interface for an implementation
1106 // without an interface decl. So, IDecl is always non-zero.
1107 assert(IDecl &&
1108 "ActOnPropertyImplDecl - @implementation without @interface");
1109
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001110 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001111 property = IDecl->FindPropertyDeclaration(PropertyId);
1112 if (!property) {
1113 Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001114 return 0;
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001115 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001116 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001117 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001118 if (Synthesize) {
1119 Diag(AtLoc, diag::error_synthesize_category_decl);
1120 return 0;
1121 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001122 IDecl = CatImplClass->getClassInterface();
1123 if (!IDecl) {
1124 Diag(AtLoc, diag::error_missing_property_interface);
1125 return 0;
1126 }
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001127 ObjCCategoryDecl *Category =
1128 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1129
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001130 // If category for this implementation not found, it is an error which
1131 // has already been reported eralier.
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001132 if (!Category)
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001133 return 0;
1134 // Look for this property declaration in @implementation's category
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001135 property = Category->FindPropertyDeclaration(PropertyId);
1136 if (!property) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001137 Diag(PropertyLoc, diag::error_bad_category_property_decl,
Fariborz Jahanianef8a3df2008-04-21 19:04:53 +00001138 Category->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001139 return 0;
1140 }
1141 }
1142 else {
1143 Diag(AtLoc, diag::error_bad_property_context);
1144 return 0;
1145 }
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001146 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001147 // Check that we have a valid, previously declared ivar for @synthesize
1148 if (Synthesize) {
1149 // @synthesize
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001150 if (!PropertyIvar)
1151 PropertyIvar = PropertyId;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001152 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001153 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001154 if (!Ivar) {
Fariborz Jahanian1f028002008-04-21 21:57:36 +00001155 Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
1156 PropertyId->getName());
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001157 return 0;
1158 }
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001159 // Check that type of property and its ivar match.
1160 if (Ivar->getCanonicalType() != property->getCanonicalType()) {
1161 Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
1162 Ivar->getName());
1163 return 0;
1164 }
1165
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001166 } else if (PropertyIvar) {
1167 // @dynamic
1168 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1169 return 0;
1170 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001171 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahaniandc0569e2008-04-23 00:06:01 +00001172 ObjCPropertyImplDecl *PIDecl =
1173 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1174 (Synthesize ?
1175 ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
1176 : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
1177 Ivar);
1178 if (IC)
1179 IC->addPropertyImplementation(PIDecl);
1180 else
1181 CatImplClass->addPropertyImplementation(PIDecl);
1182
1183 return PIDecl;
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001184}
Chris Lattner2a1e2ed2008-04-11 07:00:53 +00001185