blob: 69c45e9a647399c5080e5d46bd871de8436350cd [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.
Douglas Gregor44b43212008-12-11 16:49:14 +000039 PushDeclContext(FnBodyScope, 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!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000045 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
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);
Douglas Gregorf57172b2008-12-08 18:40:42 +000069 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000070 // Maybe we will complain about the shadowed template parameter.
71 DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72 // Just pretend that we didn't see the previous declaration.
73 PrevDecl = 0;
74 }
75
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000079 }
80
Ted Kremeneka526c5c2008-01-07 19:49:32 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +000082 if (IDecl) {
83 // Class already seen. Is it a forward declaration?
Steve Naroffcfe8bf32008-11-18 19:15:30 +000084 if (!IDecl->isForwardDecl()) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000085 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
Chris Lattnerb8b96af2008-11-23 22:46:27 +000086 Diag(IDecl->getLocation(), diag::note_previous_definition);
87
Steve Naroffcfe8bf32008-11-18 19:15:30 +000088 // Return the previous class interface.
89 // FIXME: don't leak the objects passed in!
90 return IDecl;
91 } else {
Chris Lattner4d391482007-12-12 07:09:47 +000092 IDecl->setLocation(AtInterfaceLoc);
93 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +000094 }
Chris Lattnerb752f282008-07-21 07:06:49 +000095 } else {
Daniel Dunbarf6414922008-08-20 18:02:42 +000096 IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
Steve Naroffd6a07aa2008-04-11 19:35:35 +000097 ClassName, ClassLoc);
Daniel Dunbarf6414922008-08-20 18:02:42 +000098 if (AttrList)
99 ProcessDeclAttributeList(IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000100
Steve Naroff31102512008-04-02 18:30:49 +0000101 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000102 // Remember that this needs to be removed when the scope is popped.
103 TUScope->AddDecl(IDecl);
104 }
105
106 if (SuperName) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000107 ObjCInterfaceDecl* SuperClassEntry = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000108 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000109 PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000111 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000112 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000113 }
114 else {
115 // Check that super class is previously defined
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000116 SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000117
118 if (!SuperClassEntry)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000119 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner3c73c412008-11-19 08:23:25 +0000120 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
121 else if (SuperClassEntry->isForwardDecl())
122 Diag(SuperLoc, diag::err_undef_superclass)
Chris Lattner08631c52008-11-23 21:45:46 +0000123 << SuperClassEntry->getDeclName() << ClassName
Chris Lattner3c73c412008-11-19 08:23:25 +0000124 << SourceRange(AtInterfaceLoc, ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000125 }
126 IDecl->setSuperClass(SuperClassEntry);
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000127 IDecl->setSuperClassLoc(SuperLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000128 IDecl->setLocEnd(SuperLoc);
129 } else { // we have a root class.
130 IDecl->setLocEnd(ClassLoc);
131 }
132
Steve Naroffcfe8bf32008-11-18 19:15:30 +0000133 /// Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000134 if (NumProtoRefs) {
135 IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000136 IDecl->setLocEnd(EndProtoLoc);
137 }
Anders Carlsson15281452008-11-04 16:57:32 +0000138
139 CheckObjCDeclScope(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000140 return IDecl;
141}
142
143/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000144/// @compatibility_alias declaration. It sets up the alias relationships.
Steve Naroffe8043c32008-04-01 23:04:06 +0000145Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
146 IdentifierInfo *AliasName,
147 SourceLocation AliasLocation,
148 IdentifierInfo *ClassName,
149 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000150 // Look for previous declaration of alias name
Steve Naroffb327ce02008-04-02 14:35:35 +0000151 Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000152 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000153 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000154 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000155 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000156 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000157 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000158 return 0;
159 }
160 // Check for class declaration
Steve Naroffb327ce02008-04-02 14:35:35 +0000161 Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000162 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
163 QualType T = TDecl->getUnderlyingType();
164 if (T->isObjCInterfaceType()) {
165 if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) {
166 ClassName = IDecl->getIdentifier();
167 CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
168 }
169 }
170 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000171 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
172 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000173 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000174 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000175 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000176 return 0;
177 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000178
179 // Everything checked out, instantiate a new alias declaration AST.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000180 ObjCCompatibleAliasDecl *AliasDecl =
Steve Naroffe8043c32008-04-01 23:04:06 +0000181 ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
182
183 ObjCAliasDecls[AliasName] = AliasDecl;
Anders Carlsson15281452008-11-04 16:57:32 +0000184
185 if (!CheckObjCDeclScope(AliasDecl))
186 TUScope->AddDecl(AliasDecl);
187
Chris Lattner4d391482007-12-12 07:09:47 +0000188 return AliasDecl;
189}
190
Chris Lattnere13b9592008-07-26 04:03:38 +0000191Sema::DeclTy *
192Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
193 IdentifierInfo *ProtocolName,
194 SourceLocation ProtocolLoc,
195 DeclTy * const *ProtoRefs,
196 unsigned NumProtoRefs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000197 SourceLocation EndProtoLoc,
198 AttributeList *AttrList) {
199 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000200 assert(ProtocolName && "Missing protocol identifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
Chris Lattner4d391482007-12-12 07:09:47 +0000202 if (PDecl) {
203 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000204 if (!PDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000205 Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000206 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000207 // Just return the protocol we already had.
208 // FIXME: don't leak the objects passed in!
209 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000210 }
Steve Narofff11b5082008-08-13 16:39:22 +0000211 // Make sure the cached decl gets a valid start location.
212 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000213 PDecl->setForwardDecl(false);
Chris Lattner439e71f2008-03-16 01:25:17 +0000214 } else {
Chris Lattner780f3292008-07-21 21:32:27 +0000215 PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
Chris Lattnerc8581052008-03-16 20:19:15 +0000216 PDecl->setForwardDecl(false);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000217 ObjCProtocols[ProtocolName] = PDecl;
Chris Lattnercca59d72008-03-16 01:23:04 +0000218 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000219 if (AttrList)
220 ProcessDeclAttributeList(PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000221 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000222 /// Check then save referenced protocols.
Chris Lattnere13b9592008-07-26 04:03:38 +0000223 PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
Chris Lattner4d391482007-12-12 07:09:47 +0000224 PDecl->setLocEnd(EndProtoLoc);
225 }
Anders Carlsson15281452008-11-04 16:57:32 +0000226
227 CheckObjCDeclScope(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000228 return PDecl;
229}
230
231/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000232/// issues an error if they are not declared. It returns list of
233/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000234void
Chris Lattnere13b9592008-07-26 04:03:38 +0000235Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000236 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000237 unsigned NumProtocols,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000238 llvm::SmallVectorImpl<DeclTy*> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000239 for (unsigned i = 0; i != NumProtocols; ++i) {
Chris Lattnereacc3922008-07-26 03:47:43 +0000240 ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
241 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000242 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000243 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000244 continue;
245 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000246 for (const Attr *attr = PDecl->getAttrs(); attr; attr = attr->getNext()) {
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000247 if (attr->getKind() == Attr::Unavailable)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000248 Diag(ProtocolId[i].second, diag::warn_unavailable) <<
249 PDecl->getDeclName();
Fariborz Jahanianfc820a42008-12-29 19:57:17 +0000250 if (attr->getKind() == Attr::Deprecated)
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000251 Diag(ProtocolId[i].second, diag::warn_deprecated) <<
252 PDecl->getDeclName();
253 }
Chris Lattnereacc3922008-07-26 03:47:43 +0000254
255 // If this is a forward declaration and we are supposed to warn in this
256 // case, do it.
257 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000258 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000259 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000260 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000261 }
262}
263
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000264/// DiagnosePropertyMismatch - Compares two properties for their
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000265/// attributes and types and warns on a variety of inconsistencies.
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000266///
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000267void
268Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
269 ObjCPropertyDecl *SuperProperty,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000270 const IdentifierInfo *inheritedName) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000271 ObjCPropertyDecl::PropertyAttributeKind CAttr =
272 Property->getPropertyAttributes();
273 ObjCPropertyDecl::PropertyAttributeKind SAttr =
274 SuperProperty->getPropertyAttributes();
275 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
276 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000277 Diag(Property->getLocation(), diag::warn_readonly_property)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000278 << Property->getDeclName() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000279 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
280 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000281 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000282 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000283 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
284 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000285 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000286 << Property->getDeclName() << "retain" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000287
288 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
289 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000290 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000291 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000292 if (Property->getSetterName() != SuperProperty->getSetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000293 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000294 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000295 if (Property->getGetterName() != SuperProperty->getGetterName())
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000296 Diag(Property->getLocation(), diag::warn_property_attribute)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000297 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000298
Chris Lattner717250a2008-07-26 20:50:02 +0000299 if (Context.getCanonicalType(Property->getType()) !=
300 Context.getCanonicalType(SuperProperty->getType()))
Chris Lattnerc9c7c4e2008-11-18 22:52:51 +0000301 Diag(Property->getLocation(), diag::warn_property_type)
Chris Lattner8ec03f52008-11-24 03:54:41 +0000302 << Property->getType() << inheritedName;
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000303
304}
305
306/// ComparePropertiesInBaseAndSuper - This routine compares property
307/// declarations in base and its super class, if any, and issues
308/// diagnostics in a variety of inconsistant situations.
309///
310void
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000311Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000312 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
313 if (!SDecl)
314 return;
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000315 // FIXME: O(N^2)
Fariborz Jahanian02edb982008-05-01 00:03:38 +0000316 for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
317 E = SDecl->classprop_end(); S != E; ++S) {
318 ObjCPropertyDecl *SuperPDecl = (*S);
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000319 // Does property in super class has declaration in current class?
320 for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
321 E = IDecl->classprop_end(); I != E; ++I) {
322 ObjCPropertyDecl *PDecl = (*I);
323 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000324 DiagnosePropertyMismatch(PDecl, SuperPDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000325 SDecl->getIdentifier());
Fariborz Jahanianb5e02242008-04-24 19:58:34 +0000326 }
327 }
328}
329
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000330/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
331/// of properties declared in a protocol and adds them to the list
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000332/// of properties for current class/category if it is not there already.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000333void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000334Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
Chris Lattner8ec03f52008-11-24 03:54:41 +0000335 ObjCProtocolDecl *PDecl) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000336 llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000337 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
338 if (!IDecl) {
339 // Category
340 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
341 assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
342 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
343 E = PDecl->classprop_end(); P != E; ++P) {
344 ObjCPropertyDecl *Pr = (*P);
345 ObjCCategoryDecl::classprop_iterator CP, CE;
346 // Is this property already in category's list of properties?
347 for (CP = CatDecl->classprop_begin(), CE = CatDecl->classprop_end();
348 CP != CE; ++CP)
349 if ((*CP)->getIdentifier() == Pr->getIdentifier())
350 break;
351 if (CP == CE)
352 // Add this property to list of properties for thie class.
353 mergeProperties.push_back(Pr);
354 else
355 // Property protocol already exist in class. Diagnose any mismatch.
356 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
357 }
358 CatDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
359 return;
360 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000361 for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
362 E = PDecl->classprop_end(); P != E; ++P) {
363 ObjCPropertyDecl *Pr = (*P);
364 ObjCInterfaceDecl::classprop_iterator CP, CE;
365 // Is this property already in class's list of properties?
366 for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
367 CP != CE; ++CP)
368 if ((*CP)->getIdentifier() == Pr->getIdentifier())
369 break;
370 if (CP == CE)
371 // Add this property to list of properties for thie class.
372 mergeProperties.push_back(Pr);
373 else
374 // Property protocol already exist in class. Diagnose any mismatch.
Chris Lattner8ec03f52008-11-24 03:54:41 +0000375 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000376 }
377 IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
378}
379
380/// MergeProtocolPropertiesIntoClass - This routine merges properties
381/// declared in 'MergeItsProtocols' objects (which can be a class or an
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000382/// inherited protocol into the list of properties for class/category 'CDecl'
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000383///
384
385void
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000386Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl,
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000387 DeclTy *MergeItsProtocols) {
388 Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +0000389 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
390
391 if (!IDecl) {
392 // Category
393 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
394 assert (CatDecl && "MergeProtocolPropertiesIntoClass");
395 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
396 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
397 E = MDecl->protocol_end(); P != E; ++P)
398 // Merge properties of category (*P) into IDECL's
399 MergeOneProtocolPropertiesIntoClass(CatDecl, *P);
400
401 // Go thru the list of protocols for this category and recursively merge
402 // their properties into this class as well.
403 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
404 E = CatDecl->protocol_end(); P != E; ++P)
405 MergeProtocolPropertiesIntoClass(CatDecl, *P);
406 } else {
407 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
408 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
409 E = MD->protocol_end(); P != E; ++P)
410 MergeOneProtocolPropertiesIntoClass(CatDecl, (*P));
411 }
412 return;
413 }
414
Chris Lattnerb752f282008-07-21 07:06:49 +0000415 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000416 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
417 E = MDecl->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000418 // Merge properties of class (*P) into IDECL's
Chris Lattnerb752f282008-07-21 07:06:49 +0000419 MergeOneProtocolPropertiesIntoClass(IDecl, *P);
420
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000421 // Go thru the list of protocols for this class and recursively merge
422 // their properties into this class as well.
423 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
424 E = IDecl->protocol_end(); P != E; ++P)
Chris Lattnerb752f282008-07-21 07:06:49 +0000425 MergeProtocolPropertiesIntoClass(IDecl, *P);
426 } else {
Argyrios Kyrtzidise8f0d302008-07-21 09:18:38 +0000427 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
428 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
429 E = MD->protocol_end(); P != E; ++P)
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000430 MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
Chris Lattnerb752f282008-07-21 07:06:49 +0000431 }
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +0000432}
433
Chris Lattner4d391482007-12-12 07:09:47 +0000434/// ActOnForwardProtocolDeclaration -
435Action::DeclTy *
436Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000437 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000438 unsigned NumElts,
439 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000440 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Chris Lattner4d391482007-12-12 07:09:47 +0000441
442 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000443 IdentifierInfo *Ident = IdentList[i].first;
Chris Lattnerc8581052008-03-16 20:19:15 +0000444 ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
Chris Lattner7caeabd2008-07-21 22:17:28 +0000445 if (PDecl == 0) // Not already seen?
446 PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000447 if (attrList)
448 ProcessDeclAttributeList(PDecl, attrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000449 Protocols.push_back(PDecl);
450 }
Anders Carlsson15281452008-11-04 16:57:32 +0000451
452 ObjCForwardProtocolDecl *PDecl =
453 ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
454 &Protocols[0], Protocols.size());
455
456 CheckObjCDeclScope(PDecl);
457 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000458}
459
Chris Lattner7caeabd2008-07-21 22:17:28 +0000460Sema::DeclTy *Sema::
461ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
462 IdentifierInfo *ClassName, SourceLocation ClassLoc,
463 IdentifierInfo *CategoryName,
464 SourceLocation CategoryLoc,
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000465 DeclTy * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000466 unsigned NumProtoRefs,
467 SourceLocation EndProtoLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000468 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner4d391482007-12-12 07:09:47 +0000469
Chris Lattner61f9d412008-03-16 20:34:23 +0000470 ObjCCategoryDecl *CDecl =
Chris Lattner68c82cf2008-03-16 20:47:45 +0000471 ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
Chris Lattner4d391482007-12-12 07:09:47 +0000472 CDecl->setClassInterface(IDecl);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000473
474 /// Check that class of this category is already completely declared.
475 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000476 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Steve Naroffd100c802008-06-05 15:03:27 +0000477 else {
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000478 /// Check for duplicate interface declaration for this category
479 ObjCCategoryDecl *CDeclChain;
480 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
481 CDeclChain = CDeclChain->getNextClassCategory()) {
Steve Naroffd100c802008-06-05 15:03:27 +0000482 if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000483 Diag(CategoryLoc, diag::warn_dup_category_def)
Chris Lattner3c73c412008-11-19 08:23:25 +0000484 << ClassName << CategoryName;
Chris Lattner6ff0fc32008-11-23 22:38:38 +0000485 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000486 break;
487 }
Chris Lattner4d391482007-12-12 07:09:47 +0000488 }
Steve Naroffd100c802008-06-05 15:03:27 +0000489 if (!CDeclChain)
490 CDecl->insertNextClassCategory();
Fariborz Jahanian7c453b32008-01-17 20:33:24 +0000491 }
Chris Lattner4d391482007-12-12 07:09:47 +0000492
493 if (NumProtoRefs) {
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000494 CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
495 CDecl->setLocEnd(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000496 }
Anders Carlsson15281452008-11-04 16:57:32 +0000497
498 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000499 return CDecl;
500}
501
502/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000503/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000504/// object.
505Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
506 SourceLocation AtCatImplLoc,
507 IdentifierInfo *ClassName, SourceLocation ClassLoc,
508 IdentifierInfo *CatName, SourceLocation CatLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000509 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
Chris Lattner75c9cae2008-03-16 20:53:07 +0000510 ObjCCategoryImplDecl *CDecl =
511 ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000512 /// Check that class of this category is already completely declared.
513 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000514 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000515
516 /// TODO: Check that CatName, category name, is not used in another
517 // implementation.
Steve Naroffe84a8642008-09-28 14:55:53 +0000518 ObjCCategoryImpls.push_back(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000519
520 CheckObjCDeclScope(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000521 return CDecl;
522}
523
524Sema::DeclTy *Sema::ActOnStartClassImplementation(
525 SourceLocation AtClassImplLoc,
526 IdentifierInfo *ClassName, SourceLocation ClassLoc,
527 IdentifierInfo *SuperClassname,
528 SourceLocation SuperClassLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000530 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000531 Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000532 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000533 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000534 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000535 }
536 else {
537 // Is there an interface declaration of this class; if not, warn!
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000538 IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000539 if (!IDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000540 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000541 }
542
543 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000544 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000545 if (SuperClassname) {
546 // Check if a different kind of symbol declared in this scope.
Steve Naroffb327ce02008-04-02 14:35:35 +0000547 PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000548 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000549 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
550 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000551 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000552 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000553 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000554 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000555 Diag(SuperClassLoc, diag::err_undef_superclass)
556 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000557 else if (IDecl && IDecl->getSuperClass() != SDecl) {
558 // This implementation and its interface do not have the same
559 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000560 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000561 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000562 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000563 }
564 }
565 }
566
567 if (!IDecl) {
568 // Legacy case of @implementation with no corresponding @interface.
569 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000570
571 // FIXME: Do we support attributes on the @implementation? If so
572 // we should copy them over.
Chris Lattnerb752f282008-07-21 07:06:49 +0000573 IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000574 ClassLoc, false, true);
Steve Naroff31102512008-04-02 18:30:49 +0000575 ObjCInterfaceDecls[ClassName] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000576 IDecl->setSuperClass(SDecl);
577 IDecl->setLocEnd(ClassLoc);
578
579 // Remember that this needs to be removed when the scope is popped.
580 TUScope->AddDecl(IDecl);
581 }
582
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000583 ObjCImplementationDecl* IMPDecl =
Chris Lattner75c9cae2008-03-16 20:53:07 +0000584 ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
585 IDecl, SDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000586
Anders Carlsson15281452008-11-04 16:57:32 +0000587 if (CheckObjCDeclScope(IMPDecl))
588 return IMPDecl;
589
Chris Lattner4d391482007-12-12 07:09:47 +0000590 // Check that there is no duplicate implementation of this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591 if (ObjCImplementations[ClassName])
Chris Lattner75c9cae2008-03-16 20:53:07 +0000592 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000593 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000594 else // add it to the list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 ObjCImplementations[ClassName] = IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000596 return IMPDecl;
597}
598
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000599void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
600 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000601 SourceLocation RBrace) {
602 assert(ImpDecl && "missing implementation decl");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000603 ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
Chris Lattner4d391482007-12-12 07:09:47 +0000604 if (!IDecl)
605 return;
606 /// Check case of non-existing @interface decl.
607 /// (legacy objective-c @implementation decl without an @interface decl).
608 /// Add implementations's ivar to the synthesize class's ivar list.
609 if (IDecl->ImplicitInterfaceDecl()) {
610 IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
611 return;
612 }
613 // If implementation has empty ivar list, just return.
614 if (numIvars == 0)
615 return;
616
617 assert(ivars && "missing @implementation ivars");
618
619 // Check interface's Ivar list against those in the implementation.
620 // names and types must match.
621 //
Chris Lattner4d391482007-12-12 07:09:47 +0000622 unsigned j = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000624 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
625 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000626 ObjCIvarDecl* ImplIvar = ivars[j++];
627 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000628 assert (ImplIvar && "missing implementation ivar");
629 assert (ClsIvar && "missing class ivar");
Chris Lattner1b63eef2008-07-27 00:05:05 +0000630 if (Context.getCanonicalType(ImplIvar->getType()) !=
631 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000632 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000633 << ImplIvar->getIdentifier()
634 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000635 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000636 }
637 // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
638 // as error.
639 else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000640 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000641 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000642 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner609e4c72007-12-12 18:11:49 +0000643 return;
Chris Lattner4d391482007-12-12 07:09:47 +0000644 }
645 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000646 }
Chris Lattner609e4c72007-12-12 18:11:49 +0000647
648 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000649 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000650 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000651 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000652}
653
Steve Naroff3c2eb662008-02-10 21:38:56 +0000654void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
655 bool &IncompleteImpl) {
656 if (!IncompleteImpl) {
657 Diag(ImpLoc, diag::warn_incomplete_impl);
658 IncompleteImpl = true;
659 }
Chris Lattner08631c52008-11-23 21:45:46 +0000660 Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000661}
662
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000663void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
664 ObjCMethodDecl *IntfMethodDecl) {
665 bool err = false;
666 QualType ImpMethodQType =
667 Context.getCanonicalType(ImpMethodDecl->getResultType());
668 QualType IntfMethodQType =
669 Context.getCanonicalType(IntfMethodDecl->getResultType());
670 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
671 err = true;
672 else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
673 IF=IntfMethodDecl->param_begin(),
674 EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
675 ImpMethodQType = Context.getCanonicalType((*IM)->getType());
676 IntfMethodQType = Context.getCanonicalType((*IF)->getType());
677 if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
678 err = true;
679 break;
680 }
681 }
682 if (err) {
683 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
684 << ImpMethodDecl->getDeclName();
685 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
686 }
687}
688
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000689/// FIXME: Type hierarchies in Objective-C can be deep. We could most
690/// likely improve the efficiency of selector lookups and type
691/// checking by associating with each protocol / interface / category
692/// the flattened instance tables. If we used an immutable set to keep
693/// the table then it wouldn't add significant memory cost and it
694/// would be handy for lookups.
695
Steve Naroffefe7f362008-02-08 22:06:17 +0000696/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000697/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000698void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
699 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000700 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000701 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000702 const llvm::DenseSet<Selector> &ClsMap,
703 ObjCInterfaceDecl *IDecl) {
704 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
705
706 // If a method lookup fails locally we still need to look and see if
707 // the method was implemented by a base class or an inherited
708 // protocol. This lookup is slow, but occurs rarely in correct code
709 // and otherwise would terminate in a warning.
710
Chris Lattner4d391482007-12-12 07:09:47 +0000711 // check unimplemented instance methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000712 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000713 E = PDecl->instmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000714 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000715 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniane793a6e2008-11-24 22:16:00 +0000716 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000717 (!Super || !Super->lookupInstanceMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000718 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Chris Lattner4d391482007-12-12 07:09:47 +0000719 }
720 // check unimplemented class methods
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000722 E = PDecl->classmeth_end(); I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000723 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000724 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
725 !ClsMap.count(method->getSelector()) &&
726 (!Super || !Super->lookupClassMethod(method->getSelector())))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000727 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000728 }
Chris Lattner780f3292008-07-21 21:32:27 +0000729 // Check on this protocols's referenced protocols, recursively.
730 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
731 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000732 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000733}
734
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000735void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
736 ObjCInterfaceDecl* IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000737 llvm::DenseSet<Selector> InsMap;
738 // Check and see if instance methods in class interface have been
739 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000740 for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000741 E = IMPDecl->instmeth_end(); I != E; ++I)
742 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000743
744 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000745 for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000746 E = IDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian46070342008-05-07 20:53:44 +0000747 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000748 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000749 else {
Fariborz Jahaniande739412008-12-05 01:35:25 +0000750 ObjCMethodDecl *ImpMethodDecl =
751 IMPDecl->getInstanceMethod((*I)->getSelector());
752 ObjCMethodDecl *IntfMethodDecl =
753 IDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000754 assert(IntfMethodDecl &&
755 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
756 // ImpMethodDecl may be null as in a @dynamic property.
757 if (ImpMethodDecl)
758 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahaniande739412008-12-05 01:35:25 +0000759 }
Chris Lattner4c525092007-12-12 17:58:05 +0000760
Chris Lattner4d391482007-12-12 07:09:47 +0000761 llvm::DenseSet<Selector> ClsMap;
762 // Check and see if class methods in class interface have been
763 // implemented in the implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000764 for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000765 E = IMPDecl->classmeth_end(); I != E; ++I)
766 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000767
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000768 for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000769 E = IDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000770 if (!ClsMap.count((*I)->getSelector()))
771 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000772 else {
773 ObjCMethodDecl *ImpMethodDecl =
774 IMPDecl->getClassMethod((*I)->getSelector());
775 ObjCMethodDecl *IntfMethodDecl =
776 IDecl->getClassMethod((*I)->getSelector());
777 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
778 }
779
Chris Lattner4d391482007-12-12 07:09:47 +0000780
781 // Check the protocol list for unimplemented methods in the @implementation
782 // class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000783 const ObjCList<ObjCProtocolDecl> &Protocols =
784 IDecl->getReferencedProtocols();
785 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
786 E = Protocols.end(); I != E; ++I)
787 CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000788 IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000789}
790
791/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000792/// category interface are implemented in the category @implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000793void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
794 ObjCCategoryDecl *CatClassDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000795 llvm::DenseSet<Selector> InsMap;
796 // Check and see if instance methods in category interface have been
797 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000798 for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
Chris Lattner4c525092007-12-12 17:58:05 +0000799 E = CatImplDecl->instmeth_end(); I != E; ++I)
800 InsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000801
802 bool IncompleteImpl = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803 for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000804 E = CatClassDecl->instmeth_end(); I != E; ++I)
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000805 if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
Steve Naroff3c2eb662008-02-10 21:38:56 +0000806 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000807 else {
808 ObjCMethodDecl *ImpMethodDecl =
809 CatImplDecl->getInstanceMethod((*I)->getSelector());
810 ObjCMethodDecl *IntfMethodDecl =
811 CatClassDecl->getInstanceMethod((*I)->getSelector());
Fariborz Jahanian804058e2008-12-22 19:05:31 +0000812 assert(IntfMethodDecl &&
813 "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods");
814 // ImpMethodDecl may be null as in a @dynamic property.
815 if (ImpMethodDecl)
816 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000817 }
Steve Naroff3c2eb662008-02-10 21:38:56 +0000818
Chris Lattner4d391482007-12-12 07:09:47 +0000819 llvm::DenseSet<Selector> ClsMap;
820 // Check and see if class methods in category interface have been
821 // implemented in its implementation class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000823 I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
824 I != E; ++I)
825 ClsMap.insert((*I)->getSelector());
Chris Lattner4d391482007-12-12 07:09:47 +0000826
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000827 for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000828 E = CatClassDecl->classmeth_end(); I != E; ++I)
Steve Naroff3c2eb662008-02-10 21:38:56 +0000829 if (!ClsMap.count((*I)->getSelector()))
830 WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000831 else {
832 ObjCMethodDecl *ImpMethodDecl =
833 CatImplDecl->getClassMethod((*I)->getSelector());
834 ObjCMethodDecl *IntfMethodDecl =
835 CatClassDecl->getClassMethod((*I)->getSelector());
836 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
837 }
Chris Lattner4d391482007-12-12 07:09:47 +0000838 // Check the protocol list for unimplemented methods in the @implementation
839 // class.
Chris Lattner780f3292008-07-21 21:32:27 +0000840 for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
841 E = CatClassDecl->protocol_end(); PI != E; ++PI)
842 CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000843 InsMap, ClsMap, CatClassDecl->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +0000844}
845
846/// ActOnForwardClassDeclaration -
847Action::DeclTy *
848Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
849 IdentifierInfo **IdentList, unsigned NumElts)
850{
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Chris Lattner4d391482007-12-12 07:09:47 +0000852
853 for (unsigned i = 0; i != NumElts; ++i) {
854 // Check for another declaration kind with the same name.
Steve Naroffb327ce02008-04-02 14:35:35 +0000855 Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000856 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000857 // Maybe we will complain about the shadowed template parameter.
858 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
859 // Just pretend that we didn't see the previous declaration.
860 PrevDecl = 0;
861 }
862
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000863 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +0000864 // GCC apparently allows the following idiom:
865 //
866 // typedef NSObject < XCElementTogglerP > XCElementToggler;
867 // @class XCElementToggler;
868 //
869 // FIXME: Make an extension?
870 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
871 if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000872 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +0000873 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroffc7333882008-06-05 22:57:10 +0000874 }
Chris Lattner4d391482007-12-12 07:09:47 +0000875 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000876 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000877 if (!IDecl) { // Not already seen? Make a forward decl.
Chris Lattnerb752f282008-07-21 07:06:49 +0000878 IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
Steve Naroffd6a07aa2008-04-11 19:35:35 +0000879 SourceLocation(), true);
Steve Naroff31102512008-04-02 18:30:49 +0000880 ObjCInterfaceDecls[IdentList[i]] = IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000881
882 // Remember that this needs to be removed when the scope is popped.
883 TUScope->AddDecl(IDecl);
884 }
885
886 Interfaces.push_back(IDecl);
887 }
888
Anders Carlsson15281452008-11-04 16:57:32 +0000889 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
890 &Interfaces[0],
891 Interfaces.size());
892
893 CheckObjCDeclScope(CDecl);
894 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000895}
896
897
898/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
899/// returns true, or false, accordingly.
900/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000901bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000902 const ObjCMethodDecl *PrevMethod,
903 bool matchBasedOnSizeAndAlignment) {
904 QualType T1 = Context.getCanonicalType(Method->getResultType());
905 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
906
907 if (T1 != T2) {
908 // The result types are different.
909 if (!matchBasedOnSizeAndAlignment)
Chris Lattner4d391482007-12-12 07:09:47 +0000910 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000911 // Incomplete types don't have a size and alignment.
912 if (T1->isIncompleteType() || T2->isIncompleteType())
913 return false;
914 // Check is based on size and alignment.
915 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
916 return false;
917 }
918 for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
919 T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
920 T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
921 if (T1 != T2) {
922 // The result types are different.
923 if (!matchBasedOnSizeAndAlignment)
924 return false;
925 // Incomplete types don't have a size and alignment.
926 if (T1->isIncompleteType() || T2->isIncompleteType())
927 return false;
928 // Check is based on size and alignment.
929 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
930 return false;
931 }
Chris Lattner4d391482007-12-12 07:09:47 +0000932 }
933 return true;
934}
935
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
937 ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000938 if (!FirstMethod.Method) {
939 // Haven't seen a method with this selector name yet - add it.
940 FirstMethod.Method = Method;
941 FirstMethod.Next = 0;
942 } else {
943 // We've seen a method with this name, now check the type signature(s).
944 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
945
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000946 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000947 Next = Next->Next)
948 match = MatchTwoMethodDeclarations(Method, Next->Method);
949
950 if (!match) {
951 // We have a new signature for an existing method - add it.
952 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Chris Lattner077bf5e2008-11-24 03:33:13 +0000953 FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
Chris Lattner4d391482007-12-12 07:09:47 +0000954 }
955 }
956}
957
Steve Naroff6f5f41c2008-10-21 10:50:19 +0000958// FIXME: Finish implementing -Wno-strict-selector-match.
Steve Naroff037cda52008-09-30 14:38:43 +0000959ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
960 SourceRange R) {
961 ObjCMethodList &MethList = InstanceMethodPool[Sel];
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000962 bool issueWarning = false;
Steve Naroff037cda52008-09-30 14:38:43 +0000963
964 if (MethList.Method && MethList.Next) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +0000965 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
966 // This checks if the methods differ by size & alignment.
967 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
968 issueWarning = true;
969 }
970 if (issueWarning && (MethList.Method && MethList.Next)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000971 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Chris Lattner1326a3d2008-11-23 23:26:13 +0000972 Diag(MethList.Method->getLocStart(), diag::note_using_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000973 << MethList.Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000974 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
Chris Lattner1326a3d2008-11-23 23:26:13 +0000975 Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000976 << Next->Method->getSourceRange();
Steve Naroff037cda52008-09-30 14:38:43 +0000977 }
978 return MethList.Method;
979}
980
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000981void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
982 ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +0000983 if (!FirstMethod.Method) {
984 // Haven't seen a method with this selector name yet - add it.
985 FirstMethod.Method = Method;
986 FirstMethod.Next = 0;
987 } else {
988 // We've seen a method with this name, now check the type signature(s).
989 bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
990
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000991 for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Chris Lattner4d391482007-12-12 07:09:47 +0000992 Next = Next->Next)
993 match = MatchTwoMethodDeclarations(Method, Next->Method);
994
995 if (!match) {
996 // We have a new signature for an existing method - add it.
997 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
Chris Lattner4d391482007-12-12 07:09:47 +0000999 FirstMethod.Next = OMI;
1000 }
1001 }
1002}
1003
Steve Naroff0701bbb2009-01-08 17:28:14 +00001004/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1005/// have the property type and issue diagnostics if they don't.
1006/// Also synthesize a getter/setter method if none exist (and update the
1007/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1008/// methods is the "right" thing to do.
1009void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1010 ObjCContainerDecl *CD) {
1011 ObjCMethodDecl *GetterMethod, *SetterMethod;
1012
1013 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1014 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1015
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001016 if (GetterMethod &&
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001017 GetterMethod->getResultType() != property->getType()) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001018 Diag(property->getLocation(),
1019 diag::err_accessor_property_type_mismatch)
1020 << property->getDeclName()
1021 << GetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001022 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1023 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001024
1025 if (SetterMethod) {
Fariborz Jahanian5dd41292008-12-06 23:12:49 +00001026 if (Context.getCanonicalType(SetterMethod->getResultType())
1027 != Context.VoidTy)
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001028 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1029 if (SetterMethod->getNumParams() != 1 ||
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001030 (SetterMethod->getParamDecl(0)->getType() != property->getType())) {
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001031 Diag(property->getLocation(),
1032 diag::err_accessor_property_type_mismatch)
1033 << property->getDeclName()
1034 << SetterMethod->getSelector().getAsIdentifierInfo();
Fariborz Jahanian196d0ed2008-12-06 21:48:16 +00001035 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1036 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001037 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001038
1039 // Synthesize getter/setter methods if none exist.
1040 // Add any synthesized methods to the global pool. This allows us to
1041 // handle the following, which is supported by GCC (and part of the design).
1042 //
1043 // @interface Foo
1044 // @property double bar;
1045 // @end
1046 //
1047 // void thisIsUnfortunate() {
1048 // id foo;
1049 // double bar = [foo bar];
1050 // }
1051 //
1052 CD->getPropertyMethods(Context, property, GetterMethod, SetterMethod);
1053 if (GetterMethod) {
1054 CD->addDecl(Context, GetterMethod);
1055 AddInstanceMethodToGlobalPool(GetterMethod);
1056 }
1057 if (SetterMethod) {
1058 CD->addDecl(Context, SetterMethod);
1059 AddInstanceMethodToGlobalPool(SetterMethod);
1060 }
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001061}
1062
Steve Naroffa56f6162007-12-18 01:30:32 +00001063// Note: For class/category implemenations, allMethods/allProperties is
1064// always null.
Chris Lattner4d391482007-12-12 07:09:47 +00001065void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
1066 DeclTy **allMethods, unsigned allNum,
1067 DeclTy **allProperties, unsigned pNum) {
1068 Decl *ClassDecl = static_cast<Decl *>(classDecl);
1069
Steve Naroffa56f6162007-12-18 01:30:32 +00001070 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1071 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001072 // should be true.
1073 if (!ClassDecl)
1074 return;
1075
Chris Lattner4d391482007-12-12 07:09:47 +00001076 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001077 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1078 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001079 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001080
Steve Naroff0701bbb2009-01-08 17:28:14 +00001081
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001082 if (pNum != 0) {
Chris Lattner55d13b42008-03-16 21:23:50 +00001083 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
1084 IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001085 else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1086 CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
1087 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001088 PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
Fariborz Jahanian7e7e3872008-04-16 21:08:45 +00001089 else
Fariborz Jahanian3dd4ba42008-04-17 18:25:18 +00001090 assert(false && "ActOnAtEnd - property declaration misplaced");
Seo Sanghyeonfe5042e2008-07-05 02:01:25 +00001091 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001092
1093 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
1094 assert(DC && "Missing DeclContext");
1095
1096 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1097 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1098 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1099
Chris Lattner4d391482007-12-12 07:09:47 +00001100 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001101 ObjCMethodDecl *Method =
1102 cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
Chris Lattner4d391482007-12-12 07:09:47 +00001103
1104 if (!Method) continue; // Already issued a diagnostic.
1105 if (Method->isInstance()) {
1106 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001107 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001108 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1109 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001110 if ((isInterfaceDeclKind && PrevMethod && !match)
1111 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001112 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001113 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001114 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001115 } else {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001116 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001117 InsMap[Method->getSelector()] = Method;
1118 /// The following allows us to typecheck messages to "id".
1119 AddInstanceMethodToGlobalPool(Method);
1120 }
1121 }
1122 else {
1123 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001124 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Chris Lattner4d391482007-12-12 07:09:47 +00001125 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1126 : false;
Eli Friedman82b4e762008-12-16 20:15:50 +00001127 if ((isInterfaceDeclKind && PrevMethod && !match)
1128 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001129 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001130 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001131 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001132 } else {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001133 DC->addDecl(Context, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001134 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001135 /// The following allows us to typecheck messages to "Class".
1136 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001137 }
1138 }
1139 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001140 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001141 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001142 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001143 ComparePropertiesInBaseAndSuper(I);
1144 MergeProtocolPropertiesIntoClass(I, I);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001145 for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001146 e = I->classprop_end(); i != e; ++i) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001147 ProcessPropertyDecl((*i), I);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001148 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001149 I->setAtEndLoc(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001150 } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001151 for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001152 e = P->classprop_end(); i != e; ++i) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001153 ProcessPropertyDecl((*i), P);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001154 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001155 P->setAtEndLoc(AtEndLoc);
Chris Lattner4d391482007-12-12 07:09:47 +00001156 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001157 else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001158 // Categories are used to extend the class by declaring new methods.
1159 // By the same token, they are also used to add new properties. No
1160 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001161
Fariborz Jahanian1ac2bc42008-12-06 23:03:39 +00001162 // Merge protocol properties into category
1163 MergeProtocolPropertiesIntoClass(C, C);
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001164 for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001165 e = C->classprop_end(); i != e; ++i) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001166 ProcessPropertyDecl((*i), C);
Fariborz Jahanianf3cd3fd2008-12-02 18:39:49 +00001167 }
Steve Naroff0701bbb2009-01-08 17:28:14 +00001168 C->setAtEndLoc(AtEndLoc);
Chris Lattner4d391482007-12-12 07:09:47 +00001169 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001170 else if (ObjCImplementationDecl *IC =
1171 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001172 IC->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001173 if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
Chris Lattner4d391482007-12-12 07:09:47 +00001174 ImplMethodsVsClassMethods(IC, IDecl);
1175 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001176 ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001177 CatImplClass->setLocEnd(AtEndLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001178 ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001179 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001180 // in this interface are implemented in the category @implementation.
Chris Lattner4d391482007-12-12 07:09:47 +00001181 if (IDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001182 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001183 Categories; Categories = Categories->getNextClassCategory()) {
1184 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1185 ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1186 break;
1187 }
1188 }
1189 }
1190 }
1191}
1192
1193
1194/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1195/// objective-c's type qualifier from the parser version of the same info.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001196static Decl::ObjCDeclQualifier
1197CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1198 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1199 if (PQTVal & ObjCDeclSpec::DQ_In)
1200 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1201 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1202 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1203 if (PQTVal & ObjCDeclSpec::DQ_Out)
1204 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1205 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1206 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1207 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1208 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1209 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1210 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001211
1212 return ret;
1213}
1214
1215Sema::DeclTy *Sema::ActOnMethodDeclaration(
1216 SourceLocation MethodLoc, SourceLocation EndLoc,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001217 tok::TokenKind MethodType, DeclTy *classDecl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001218 ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001219 Selector Sel,
1220 // optional arguments. The number of types/arguments is obtained
1221 // from the Sel.getNumArgs().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001222 ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
Chris Lattner4d391482007-12-12 07:09:47 +00001223 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1224 bool isVariadic) {
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001225 Decl *ClassDecl = static_cast<Decl*>(classDecl);
Steve Naroffda323ad2008-02-29 21:48:07 +00001226
1227 // Make sure we can establish a context for the method.
1228 if (!ClassDecl) {
1229 Diag(MethodLoc, diag::error_missing_method_context);
1230 return 0;
1231 }
Chris Lattner4d391482007-12-12 07:09:47 +00001232 QualType resultDeclType;
1233
1234 if (ReturnType)
1235 resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1236 else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001237 resultDeclType = Context.getObjCIdType();
Chris Lattner4d391482007-12-12 07:09:47 +00001238
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001239 ObjCMethodDecl* ObjCMethod =
1240 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Steve Naroff0701bbb2009-01-08 17:28:14 +00001241 dyn_cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001242 MethodType == tok::minus, isVariadic,
Fariborz Jahanian46070342008-05-07 20:53:44 +00001243 false,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001244 MethodDeclKind == tok::objc_optional ?
1245 ObjCMethodDecl::Optional :
1246 ObjCMethodDecl::Required);
1247
Chris Lattner0ed844b2008-04-04 06:12:32 +00001248 llvm::SmallVector<ParmVarDecl*, 16> Params;
1249
1250 for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1251 // FIXME: arg->AttrList must be stored too!
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001252 QualType argType, originalArgType;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001253
Steve Naroff6082c622008-12-09 19:36:17 +00001254 if (ArgTypes[i]) {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001255 argType = QualType::getFromOpaquePtr(ArgTypes[i]);
Steve Naroff6082c622008-12-09 19:36:17 +00001256 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001257 if (argType->isArrayType()) { // (char *[]) -> (char **)
1258 originalArgType = argType;
Steve Naroff6082c622008-12-09 19:36:17 +00001259 argType = Context.getArrayDecayedType(argType);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001260 }
Steve Naroff6082c622008-12-09 19:36:17 +00001261 else if (argType->isFunctionType())
1262 argType = Context.getPointerType(argType);
1263 } else
Chris Lattner0ed844b2008-04-04 06:12:32 +00001264 argType = Context.getObjCIdType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001265 ParmVarDecl* Param;
1266 if (originalArgType.isNull())
1267 Param = ParmVarDecl::Create(Context, ObjCMethod,
1268 SourceLocation(/*FIXME*/),
1269 ArgNames[i], argType,
1270 VarDecl::None, 0, 0);
1271 else
1272 Param = ParmVarWithOriginalTypeDecl::Create(Context, ObjCMethod,
1273 SourceLocation(/*FIXME*/),
1274 ArgNames[i], argType, originalArgType,
1275 VarDecl::None, 0, 0);
1276
Chris Lattner0ed844b2008-04-04 06:12:32 +00001277 Param->setObjCDeclQualifier(
1278 CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1279 Params.push_back(Param);
1280 }
1281
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001282 ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1283 ObjCMethod->setObjCDeclQualifier(
1284 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1285 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001286
1287 if (AttrList)
1288 ProcessDeclAttributeList(ObjCMethod, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +00001289
1290 // For implementations (which can be very "coarse grain"), we add the
1291 // method now. This allows the AST to implement lookup methods that work
1292 // incrementally (without waiting until we parse the @end). It also allows
1293 // us to flag multiple declaration errors as they occur.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001294 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001295 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001296 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001297 PrevMethod = ImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001299 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001300 PrevMethod = ImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001301 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001302 }
1303 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001304 else if (ObjCCategoryImplDecl *CatImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001305 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001306 if (MethodType == tok::minus) {
Steve Naroff94a5c332007-12-19 22:27:04 +00001307 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001308 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001309 } else {
Steve Naroff94a5c332007-12-19 22:27:04 +00001310 PrevMethod = CatImpDecl->getClassMethod(Sel);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001311 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001312 }
1313 }
1314 if (PrevMethod) {
1315 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001316 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001317 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001318 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001319 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001320 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001321}
1322
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001323void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1324 SourceLocation Loc,
1325 unsigned &Attributes) {
1326 // FIXME: Improve the reported location.
1327
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001328 // readonly and readwrite/assign/retain/copy conflict.
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001329 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001330 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1331 ObjCDeclSpec::DQ_PR_assign |
1332 ObjCDeclSpec::DQ_PR_copy |
1333 ObjCDeclSpec::DQ_PR_retain))) {
1334 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1335 "readwrite" :
1336 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1337 "assign" :
1338 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1339 "copy" : "retain";
1340
Fariborz Jahanianba45da82008-12-08 19:28:10 +00001341 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1342 diag::err_objc_property_attr_mutually_exclusive :
1343 diag::warn_objc_property_attr_mutually_exclusive)
Fariborz Jahanian567c8df2008-12-06 01:12:43 +00001344 << "readonly" << which;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001345 }
1346
1347 // Check for copy or retain on non-object types.
1348 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1349 !Context.isObjCObjectPointerType(PropertyTy)) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001350 Diag(Loc, diag::err_objc_property_requires_object)
1351 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001352 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1353 }
1354
1355 // Check for more than one of { assign, copy, retain }.
1356 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1357 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001358 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1359 << "assign" << "copy";
1360 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001361 }
1362 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001363 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1364 << "assign" << "retain";
1365 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001366 }
1367 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1368 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001369 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1370 << "copy" << "retain";
1371 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001372 }
1373 }
1374
1375 // Warn if user supplied no assignment attribute, property is
1376 // readwrite, and this is an object type.
1377 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1378 ObjCDeclSpec::DQ_PR_retain)) &&
1379 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1380 Context.isObjCObjectPointerType(PropertyTy)) {
1381 // Skip this warning in gc-only mode.
1382 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1383 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1384
1385 // If non-gc code warn that this is likely inappropriate.
1386 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1387 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1388
1389 // FIXME: Implement warning dependent on NSCopying being
1390 // implemented. See also:
1391 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1392 // (please trim this list while you are at it).
1393 }
1394}
1395
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001396Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1397 FieldDeclarator &FD,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001398 ObjCDeclSpec &ODS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +00001399 Selector GetterSel,
1400 Selector SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001401 DeclTy *ClassCategory,
1402 bool *isOverridingProperty,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001403 tok::ObjCKeywordKind MethodImplKind) {
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001404 unsigned Attributes = ODS.getPropertyAttributes();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001405 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1406 // default is readwrite!
1407 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1408 // property is defaulted to 'assign' if it is readwrite and is
1409 // not retain or copy
1410 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1411 (isReadWrite &&
1412 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1413 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1414 QualType T = GetTypeForDeclarator(FD.D, S);
1415 Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001416
1417 // May modify Attributes.
1418 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001419
1420 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1421 if (!CDecl->getIdentifier()) {
1422 // This is an anonymous category. property requires special
1423 // handling.
1424 if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1425 if (ObjCPropertyDecl *PIDecl =
1426 ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1427 // property 'PIDecl's readonly attribute will be over-ridden
1428 // with anonymous category's readwrite property attribute!
1429 unsigned PIkind = PIDecl->getPropertyAttributes();
1430 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian9bfb2a22008-12-08 18:47:29 +00001431 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001432 (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1433 Diag(AtLoc, diag::warn_property_attr_mismatch);
1434 PIDecl->makeitReadWriteAttribute();
1435 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1436 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1437 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1438 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1439 PIDecl->setSetterName(SetterSel);
1440 // FIXME: use a common routine with addPropertyMethods.
1441 ObjCMethodDecl *SetterDecl =
1442 ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1443 Context.VoidTy,
1444 ICDecl,
1445 true, false, true,
1446 ObjCMethodDecl::Required);
1447 ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1448 SetterDecl,
1449 SourceLocation(),
1450 FD.D.getIdentifier(),
1451 T,
1452 VarDecl::None,
1453 0, 0);
1454 SetterDecl->setMethodParams(&Argument, 1);
1455 PIDecl->setSetterMethodDecl(SetterDecl);
1456 }
1457 else
Fariborz Jahanian06de37b2008-12-04 22:56:16 +00001458 Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001459 *isOverridingProperty = true;
1460 return 0;
1461 }
Fariborz Jahanianb16308f2008-11-26 20:33:54 +00001462 // No matching property found in the main class. Just fall thru
1463 // and add property to the anonymous category. It looks like
1464 // it works as is. This category becomes just like a category
1465 // for its primary class.
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001466 } else {
1467 Diag(CDecl->getLocation(), diag::err_continuation_class);
1468 *isOverridingProperty = true;
1469 return 0;
1470 }
1471 }
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001472
Fariborz Jahanian105ec4b2008-12-16 17:51:01 +00001473 Type *t = T.getTypePtr();
1474 if (t->isArrayType() || t->isFunctionType())
1475 Diag(AtLoc, diag::err_property_type) << T;
1476
Fariborz Jahanian1de1e742008-04-14 23:36:35 +00001477 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1478 FD.D.getIdentifier(), T);
Fariborz Jahanian33de3f02008-05-07 17:43:59 +00001479 // Regardless of setter/getter attribute, we save the default getter/setter
1480 // selector names in anticipation of declaration of setter/getter methods.
1481 PDecl->setGetterName(GetterSel);
1482 PDecl->setSetterName(SetterSel);
Chris Lattner4d391482007-12-12 07:09:47 +00001483
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001484 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Chris Lattner4d391482007-12-12 07:09:47 +00001486
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001487 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001488 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Chris Lattner4d391482007-12-12 07:09:47 +00001489
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001490 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001491 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Chris Lattner4d391482007-12-12 07:09:47 +00001492
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001493 if (isReadWrite)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001494 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Chris Lattner4d391482007-12-12 07:09:47 +00001495
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001496 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001497 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Chris Lattner4d391482007-12-12 07:09:47 +00001498
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001499 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Chris Lattner4d391482007-12-12 07:09:47 +00001501
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +00001502 if (isAssign)
1503 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1504
Daniel Dunbar95e61fb2008-09-23 21:53:23 +00001505 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001506 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Chris Lattner4d391482007-12-12 07:09:47 +00001507
Fariborz Jahanian46b55e52008-05-05 18:51:55 +00001508 if (MethodImplKind == tok::objc_required)
1509 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1510 else if (MethodImplKind == tok::objc_optional)
1511 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1512
Chris Lattner4d391482007-12-12 07:09:47 +00001513 return PDecl;
1514}
1515
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001516/// ActOnPropertyImplDecl - This routine performs semantic checks and
1517/// builds the AST node for a property implementation declaration; declared
1518/// as @synthesize or @dynamic.
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001519///
1520Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1521 SourceLocation PropertyLoc,
1522 bool Synthesize,
1523 DeclTy *ClassCatImpDecl,
1524 IdentifierInfo *PropertyId,
1525 IdentifierInfo *PropertyIvar) {
1526 Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1527 // Make sure we have a context for the property implementation declaration.
1528 if (!ClassImpDecl) {
1529 Diag(AtLoc, diag::error_missing_property_context);
1530 return 0;
1531 }
1532 ObjCPropertyDecl *property = 0;
1533 ObjCInterfaceDecl* IDecl = 0;
1534 // Find the class or category class where this property must have
1535 // a declaration.
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001536 ObjCImplementationDecl *IC = 0;
1537 ObjCCategoryImplDecl* CatImplClass = 0;
1538 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001539 IDecl = getObjCInterfaceDecl(IC->getIdentifier());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001540 // We always synthesize an interface for an implementation
1541 // without an interface decl. So, IDecl is always non-zero.
1542 assert(IDecl &&
1543 "ActOnPropertyImplDecl - @implementation without @interface");
1544
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001545 // Look for this property declaration in the @implementation's @interface
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001546 property = IDecl->FindPropertyDeclaration(PropertyId);
1547 if (!property) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001548 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001549 return 0;
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001550 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001551 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001552 else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001553 if (Synthesize) {
1554 Diag(AtLoc, diag::error_synthesize_category_decl);
1555 return 0;
1556 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001557 IDecl = CatImplClass->getClassInterface();
1558 if (!IDecl) {
1559 Diag(AtLoc, diag::error_missing_property_interface);
1560 return 0;
1561 }
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001562 ObjCCategoryDecl *Category =
1563 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1564
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001565 // If category for this implementation not found, it is an error which
1566 // has already been reported eralier.
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001567 if (!Category)
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001568 return 0;
1569 // Look for this property declaration in @implementation's category
Fariborz Jahanian559c0c42008-04-21 19:04:53 +00001570 property = Category->FindPropertyDeclaration(PropertyId);
1571 if (!property) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001572 Diag(PropertyLoc, diag::error_bad_category_property_decl)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001573 << Category->getDeclName();
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001574 return 0;
1575 }
1576 }
1577 else {
1578 Diag(AtLoc, diag::error_bad_property_context);
1579 return 0;
1580 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001581 ObjCIvarDecl *Ivar = 0;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001582 // Check that we have a valid, previously declared ivar for @synthesize
1583 if (Synthesize) {
1584 // @synthesize
Fariborz Jahanian6cdf16d2008-04-21 21:57:36 +00001585 if (!PropertyIvar)
1586 PropertyIvar = PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001587 // Check that this is a previously declared 'ivar' in 'IDecl' interface
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001588 Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001589 if (!Ivar) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001590 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001591 return 0;
1592 }
Steve Naroff3ce52d62008-09-30 10:07:56 +00001593 QualType PropType = Context.getCanonicalType(property->getType());
1594 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1595
Steve Narofffbbe0ac2008-09-30 00:24:17 +00001596 // Check that type of property and its ivar are type compatible.
Steve Naroff3ce52d62008-09-30 10:07:56 +00001597 if (PropType != IvarType) {
Steve Naroff4fa4ab62008-10-16 14:59:30 +00001598 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
Chris Lattner5dc266a2008-11-20 06:13:02 +00001599 Diag(PropertyLoc, diag::error_property_ivar_type)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001600 << property->getDeclName() << Ivar->getDeclName();
Steve Naroff3ce52d62008-09-30 10:07:56 +00001601 return 0;
1602 }
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001603 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001604 } else if (PropertyIvar) {
1605 // @dynamic
1606 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1607 return 0;
1608 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001609 assert (property && "ActOnPropertyImplDecl - property declaration missing");
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001610 ObjCPropertyImplDecl *PIDecl =
1611 ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1612 (Synthesize ?
Daniel Dunbar9f0afd42008-08-26 04:47:31 +00001613 ObjCPropertyImplDecl::Synthesize
1614 : ObjCPropertyImplDecl::Dynamic),
1615 Ivar);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001616 if (IC) {
1617 if (Synthesize)
1618 if (ObjCPropertyImplDecl *PPIDecl =
1619 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1620 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1621 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1622 << PropertyIvar;
1623 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1624 }
1625
1626 if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1627 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1628 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1629 return 0;
1630 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001631 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001632 }
1633 else {
1634 if (Synthesize)
1635 if (ObjCPropertyImplDecl *PPIDecl =
1636 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1637 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1638 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1639 << PropertyIvar;
1640 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1641 }
1642
1643 if (ObjCPropertyImplDecl *PPIDecl =
1644 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1645 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1646 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1647 return 0;
1648 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001649 CatImplClass->addPropertyImplementation(PIDecl);
Fariborz Jahanianae6f6fd2008-12-05 22:32:48 +00001650 }
Fariborz Jahanian628b96f2008-04-23 00:06:01 +00001651
1652 return PIDecl;
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001653}
Anders Carlsson15281452008-11-04 16:57:32 +00001654
Chris Lattnercc98eac2008-12-17 07:13:27 +00001655bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001656 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001657 return false;
1658
1659 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1660 D->setInvalidDecl();
1661
1662 return true;
1663}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001664
1665/// Collect the instance variables declared in an Objective-C object. Used in
1666/// the creation of structures from objects using the @defs directive.
1667/// FIXME: This should be consolidated with CollectObjCIvars as it is also
1668/// part of the AST generation logic of @defs.
1669static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record,
1670 ASTContext& Ctx,
1671 llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) {
1672 if (Class->getSuperClass())
1673 CollectIvars(Class->getSuperClass(), Record, Ctx, ivars);
1674
1675 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
1676 for (ObjCInterfaceDecl::ivar_iterator
1677 I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) {
1678
1679 ObjCIvarDecl* ID = *I;
1680 ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record,
1681 ID->getLocation(),
1682 ID->getIdentifier(),
1683 ID->getType(),
1684 ID->getBitWidth()));
1685 }
1686}
1687
1688/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1689/// instance variables of ClassName into Decls.
1690void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart,
1691 IdentifierInfo *ClassName,
1692 llvm::SmallVectorImpl<DeclTy*> &Decls) {
1693 // Check that ClassName is a valid class
1694 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
1695 if (!Class) {
1696 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1697 return;
1698 }
1699 // Collect the instance variables
1700 CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls);
1701
1702 // Introduce all of these fields into the appropriate scope.
1703 for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin();
1704 D != Decls.end(); ++D) {
1705 FieldDecl *FD = cast<FieldDecl>((Decl*)*D);
1706 if (getLangOptions().CPlusPlus)
1707 PushOnScopeChains(cast<FieldDecl>(FD), S);
1708 else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD))
1709 Record->addDecl(Context, FD);
1710 }
1711}
1712