Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 7 | // |
| 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 Dunbar | 8d03cbe | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
| 18 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 22 | /// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 23 | /// and user declared, in the method definition's AST. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 24 | void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 25 | assert(getCurMethodDecl() == 0 && "Method parsing confused"); |
Steve Naroff | 3ac43f9 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 26 | 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 Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 31 | |
| 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 Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 37 | |
| 38 | // Allow all of Sema to see that we are entering a method definition. |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 39 | PushDeclContext(MDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 40 | |
| 41 | // Create Decl objects for each parameter, entrring them in the scope for |
| 42 | // binding to their use. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 43 | |
| 44 | // Insert the invisible arguments, self and _cmd! |
Daniel Dunbar | eaf91c3 | 2008-08-26 06:07:48 +0000 | [diff] [blame^] | 45 | MDecl->createImplicitParams(Context); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | eaf91c3 | 2008-08-26 06:07:48 +0000 | [diff] [blame^] | 47 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 48 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 50 | // Introduce all of the other parameters into this scope. |
Chris Lattner | 685d792 | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 51 | for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 52 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 53 | IdentifierInfo *II = PDecl->getIdentifier(); |
Argiris Kirtzidis | 43ce0be | 2008-04-27 13:30:35 +0000 | [diff] [blame] | 54 | if (II) |
| 55 | PushOnScopeChains(PDecl, FnBodyScope); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 59 | Sema::DeclTy *Sema:: |
| 60 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 61 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 62 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
Chris Lattner | ae1ae49 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 63 | DeclTy * const *ProtoRefs, unsigned NumProtoRefs, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 64 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 65 | assert(ClassName && "Missing class identifier"); |
| 66 | |
| 67 | // Check for another declaration kind with the same name. |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 68 | Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 69 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 70 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 71 | ClassName->getName()); |
| 72 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 73 | } |
| 74 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 75 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 76 | if (IDecl) { |
| 77 | // Class already seen. Is it a forward declaration? |
| 78 | if (!IDecl->isForwardDecl()) |
| 79 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName()); |
| 80 | else { |
| 81 | IDecl->setLocation(AtInterfaceLoc); |
| 82 | IDecl->setForwardDecl(false); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 84 | } else { |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 85 | IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 86 | ClassName, ClassLoc); |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 87 | if (AttrList) |
| 88 | ProcessDeclAttributeList(IDecl, AttrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 89 | |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 90 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 91 | // Remember that this needs to be removed when the scope is popped. |
| 92 | TUScope->AddDecl(IDecl); |
| 93 | } |
| 94 | |
| 95 | if (SuperName) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 96 | ObjCInterfaceDecl* SuperClassEntry = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 97 | // Check if a different kind of symbol declared in this scope. |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 98 | PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 99 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 100 | Diag(SuperLoc, diag::err_redefinition_different_kind, |
| 101 | SuperName->getName()); |
| 102 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 103 | } |
| 104 | else { |
| 105 | // Check that super class is previously defined |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 106 | SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 107 | |
| 108 | if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) { |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 109 | Diag(SuperLoc, diag::err_undef_superclass, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 110 | SuperClassEntry ? SuperClassEntry->getName() |
| 111 | : SuperName->getName(), |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 112 | ClassName->getName(), SourceRange(AtInterfaceLoc, ClassLoc)); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | IDecl->setSuperClass(SuperClassEntry); |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 116 | IDecl->setSuperClassLoc(SuperLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 117 | IDecl->setLocEnd(SuperLoc); |
| 118 | } else { // we have a root class. |
| 119 | IDecl->setLocEnd(ClassLoc); |
| 120 | } |
| 121 | |
| 122 | /// Check then save referenced protocols |
Chris Lattner | ae1ae49 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 123 | if (NumProtoRefs) { |
| 124 | IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 125 | IDecl->setLocEnd(EndProtoLoc); |
| 126 | } |
| 127 | return IDecl; |
| 128 | } |
| 129 | |
| 130 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
| 131 | /// @compaatibility_alias declaration. It sets up the alias relationships. |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 132 | Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
| 133 | IdentifierInfo *AliasName, |
| 134 | SourceLocation AliasLocation, |
| 135 | IdentifierInfo *ClassName, |
| 136 | SourceLocation ClassLocation) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 137 | // Look for previous declaration of alias name |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 138 | Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 139 | if (ADecl) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 140 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 141 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
| 142 | Diag(ADecl->getLocation(), diag::warn_previous_declaration); |
| 143 | } |
| 144 | else { |
| 145 | Diag(AliasLocation, diag::err_conflicting_aliasing_type, |
| 146 | AliasName->getName()); |
| 147 | Diag(ADecl->getLocation(), diag::err_previous_declaration); |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | // Check for class declaration |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 152 | Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 153 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 154 | if (CDecl == 0) { |
| 155 | Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName()); |
| 156 | if (CDeclU) |
| 157 | Diag(CDeclU->getLocation(), diag::warn_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 160 | |
| 161 | // Everything checked out, instantiate a new alias declaration AST. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 162 | ObjCCompatibleAliasDecl *AliasDecl = |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 163 | ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl); |
| 164 | |
| 165 | ObjCAliasDecls[AliasName] = AliasDecl; |
| 166 | TUScope->AddDecl(AliasDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 167 | return AliasDecl; |
| 168 | } |
| 169 | |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 170 | Sema::DeclTy * |
| 171 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 172 | IdentifierInfo *ProtocolName, |
| 173 | SourceLocation ProtocolLoc, |
| 174 | DeclTy * const *ProtoRefs, |
| 175 | unsigned NumProtoRefs, |
| 176 | SourceLocation EndProtoLoc) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 177 | assert(ProtocolName && "Missing protocol identifier"); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 178 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 179 | if (PDecl) { |
| 180 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 181 | if (!PDecl->isForwardDecl()) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 182 | Diag(ProtocolLoc, diag::err_duplicate_protocol_def, |
| 183 | ProtocolName->getName()); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 184 | // Just return the protocol we already had. |
| 185 | // FIXME: don't leak the objects passed in! |
| 186 | return PDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 187 | } |
Steve Naroff | 9a9e521 | 2008-08-13 16:39:22 +0000 | [diff] [blame] | 188 | // Make sure the cached decl gets a valid start location. |
| 189 | PDecl->setLocation(AtProtoInterfaceLoc); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 190 | PDecl->setForwardDecl(false); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 191 | } else { |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 192 | PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName); |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 193 | PDecl->setForwardDecl(false); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 194 | ObjCProtocols[ProtocolName] = PDecl; |
Chris Lattner | 180f7e2 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 196 | |
| 197 | if (NumProtoRefs) { |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 198 | /// Check then save referenced protocols. |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 199 | PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 200 | PDecl->setLocEnd(EndProtoLoc); |
| 201 | } |
| 202 | return PDecl; |
| 203 | } |
| 204 | |
| 205 | /// FindProtocolDeclaration - This routine looks up protocols and |
| 206 | /// issuer error if they are not declared. It returns list of protocol |
| 207 | /// declarations in its 'Protocols' argument. |
| 208 | void |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 209 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 210 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 211 | unsigned NumProtocols, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 212 | llvm::SmallVectorImpl<DeclTy*> &Protocols) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 213 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 214 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first]; |
| 215 | if (!PDecl) { |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 216 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol, |
| 217 | ProtocolId[i].first->getName()); |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 218 | continue; |
| 219 | } |
| 220 | |
| 221 | // If this is a forward declaration and we are supposed to warn in this |
| 222 | // case, do it. |
| 223 | if (WarnOnDeclarations && PDecl->isForwardDecl()) |
| 224 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref, |
| 225 | ProtocolId[i].first->getName()); |
| 226 | Protocols.push_back(PDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 230 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 231 | /// attributes and types and warns on a variety of inconsistancies. |
| 232 | /// |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 233 | void |
| 234 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 235 | ObjCPropertyDecl *SuperProperty, |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 236 | const char *inheritedName) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 237 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 238 | Property->getPropertyAttributes(); |
| 239 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 240 | SuperProperty->getPropertyAttributes(); |
| 241 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 242 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 243 | Diag(Property->getLocation(), diag::warn_readonly_property, |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 244 | Property->getName(), inheritedName); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 245 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 246 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 247 | Diag(Property->getLocation(), diag::warn_property_attribute, |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 248 | Property->getName(), "copy", inheritedName, |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 249 | SourceRange()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 250 | else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) |
| 251 | != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 252 | Diag(Property->getLocation(), diag::warn_property_attribute, |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 253 | Property->getName(), "retain", inheritedName, |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 254 | SourceRange()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 255 | |
| 256 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 257 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 258 | Diag(Property->getLocation(), diag::warn_property_attribute, |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 259 | Property->getName(), "atomic", inheritedName, |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 260 | SourceRange()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 261 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 262 | Diag(Property->getLocation(), diag::warn_property_attribute, |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 263 | Property->getName(), "setter", inheritedName, |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 264 | SourceRange()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 265 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 266 | Diag(Property->getLocation(), diag::warn_property_attribute, |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 267 | Property->getName(), "getter", inheritedName, |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 268 | SourceRange()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 269 | |
Chris Lattner | c5cff30 | 2008-07-26 20:50:02 +0000 | [diff] [blame] | 270 | if (Context.getCanonicalType(Property->getType()) != |
| 271 | Context.getCanonicalType(SuperProperty->getType())) |
Fariborz Jahanian | 513e30a | 2008-05-01 18:05:01 +0000 | [diff] [blame] | 272 | Diag(Property->getLocation(), diag::warn_property_type, |
| 273 | Property->getType().getAsString(), |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 274 | inheritedName); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 275 | |
| 276 | } |
| 277 | |
| 278 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 279 | /// declarations in base and its super class, if any, and issues |
| 280 | /// diagnostics in a variety of inconsistant situations. |
| 281 | /// |
| 282 | void |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 283 | Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 284 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 285 | if (!SDecl) |
| 286 | return; |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 287 | for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(), |
| 288 | E = SDecl->classprop_end(); S != E; ++S) { |
| 289 | ObjCPropertyDecl *SuperPDecl = (*S); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 290 | // Does property in super class has declaration in current class? |
| 291 | for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(), |
| 292 | E = IDecl->classprop_end(); I != E; ++I) { |
| 293 | ObjCPropertyDecl *PDecl = (*I); |
| 294 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 295 | DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 300 | /// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list |
| 301 | /// of properties declared in a protocol and adds them to the list |
| 302 | /// of properties for current class if it is not there already. |
| 303 | void |
| 304 | Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, |
| 305 | ObjCProtocolDecl *PDecl) |
| 306 | { |
| 307 | llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties; |
| 308 | for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(), |
| 309 | E = PDecl->classprop_end(); P != E; ++P) { |
| 310 | ObjCPropertyDecl *Pr = (*P); |
| 311 | ObjCInterfaceDecl::classprop_iterator CP, CE; |
| 312 | // Is this property already in class's list of properties? |
| 313 | for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end(); |
| 314 | CP != CE; ++CP) |
| 315 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 316 | break; |
| 317 | if (CP == CE) |
| 318 | // Add this property to list of properties for thie class. |
| 319 | mergeProperties.push_back(Pr); |
| 320 | else |
| 321 | // Property protocol already exist in class. Diagnose any mismatch. |
| 322 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getName()); |
| 323 | } |
| 324 | IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size()); |
| 325 | } |
| 326 | |
| 327 | /// MergeProtocolPropertiesIntoClass - This routine merges properties |
| 328 | /// declared in 'MergeItsProtocols' objects (which can be a class or an |
| 329 | /// inherited protocol into the list of properties for class 'IDecl' |
| 330 | /// |
| 331 | |
| 332 | void |
| 333 | Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl, |
| 334 | DeclTy *MergeItsProtocols) { |
| 335 | Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols); |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 336 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 337 | for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 338 | E = MDecl->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 339 | // Merge properties of class (*P) into IDECL's |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 340 | MergeOneProtocolPropertiesIntoClass(IDecl, *P); |
| 341 | |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 342 | // Go thru the list of protocols for this class and recursively merge |
| 343 | // their properties into this class as well. |
| 344 | for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), |
| 345 | E = IDecl->protocol_end(); P != E; ++P) |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 346 | MergeProtocolPropertiesIntoClass(IDecl, *P); |
| 347 | } else { |
Argiris Kirtzidis | fc1ea13 | 2008-07-21 09:18:38 +0000 | [diff] [blame] | 348 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 349 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 350 | E = MD->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 351 | MergeOneProtocolPropertiesIntoClass(IDecl, (*P)); |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 352 | } |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 355 | /// ActOnForwardProtocolDeclaration - |
| 356 | Action::DeclTy * |
| 357 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 358 | const IdentifierLocPair *IdentList, |
| 359 | unsigned NumElts) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 360 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 361 | |
| 362 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 363 | IdentifierInfo *Ident = IdentList[i].first; |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 364 | ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 365 | if (PDecl == 0) // Not already seen? |
| 366 | PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 367 | |
| 368 | Protocols.push_back(PDecl); |
| 369 | } |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 370 | return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc, |
| 371 | &Protocols[0], Protocols.size()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 374 | Sema::DeclTy *Sema:: |
| 375 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 376 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 377 | IdentifierInfo *CategoryName, |
| 378 | SourceLocation CategoryLoc, |
Chris Lattner | 45142b9 | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 379 | DeclTy * const *ProtoRefs, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 380 | unsigned NumProtoRefs, |
| 381 | SourceLocation EndProtoLoc) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 382 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 383 | |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 384 | ObjCCategoryDecl *CDecl = |
Chris Lattner | 321b5d1 | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 385 | ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 386 | CDecl->setClassInterface(IDecl); |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 387 | |
| 388 | /// Check that class of this category is already completely declared. |
| 389 | if (!IDecl || IDecl->isForwardDecl()) |
| 390 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
Steve Naroff | ac0580b | 2008-06-05 15:03:27 +0000 | [diff] [blame] | 391 | else { |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 392 | /// Check for duplicate interface declaration for this category |
| 393 | ObjCCategoryDecl *CDeclChain; |
| 394 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 395 | CDeclChain = CDeclChain->getNextClassCategory()) { |
Steve Naroff | ac0580b | 2008-06-05 15:03:27 +0000 | [diff] [blame] | 396 | if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { |
Steve Naroff | 0a7149f | 2008-06-05 04:33:44 +0000 | [diff] [blame] | 397 | Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(), |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 398 | CategoryName->getName()); |
| 399 | break; |
| 400 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 401 | } |
Steve Naroff | ac0580b | 2008-06-05 15:03:27 +0000 | [diff] [blame] | 402 | if (!CDeclChain) |
| 403 | CDecl->insertNextClassCategory(); |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 404 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 405 | |
| 406 | if (NumProtoRefs) { |
Chris Lattner | 45142b9 | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 407 | CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); |
| 408 | CDecl->setLocEnd(EndProtoLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 409 | } |
| 410 | return CDecl; |
| 411 | } |
| 412 | |
| 413 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 414 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 415 | /// object. |
| 416 | Sema::DeclTy *Sema::ActOnStartCategoryImplementation( |
| 417 | SourceLocation AtCatImplLoc, |
| 418 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 419 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 420 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 421 | ObjCCategoryImplDecl *CDecl = |
| 422 | ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 423 | /// Check that class of this category is already completely declared. |
| 424 | if (!IDecl || IDecl->isForwardDecl()) |
| 425 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
| 426 | |
| 427 | /// TODO: Check that CatName, category name, is not used in another |
| 428 | // implementation. |
| 429 | return CDecl; |
| 430 | } |
| 431 | |
| 432 | Sema::DeclTy *Sema::ActOnStartClassImplementation( |
| 433 | SourceLocation AtClassImplLoc, |
| 434 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 435 | IdentifierInfo *SuperClassname, |
| 436 | SourceLocation SuperClassLoc) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 437 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 438 | // Check for another declaration kind with the same name. |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 439 | Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 440 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 441 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 442 | ClassName->getName()); |
| 443 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 444 | } |
| 445 | else { |
| 446 | // Is there an interface declaration of this class; if not, warn! |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 447 | IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 448 | if (!IDecl) |
| 449 | Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName()); |
| 450 | } |
| 451 | |
| 452 | // Check that super class name is valid class name |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 453 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 454 | if (SuperClassname) { |
| 455 | // Check if a different kind of symbol declared in this scope. |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 456 | PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 457 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 458 | Diag(SuperClassLoc, diag::err_redefinition_different_kind, |
| 459 | SuperClassname->getName()); |
| 460 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 461 | } |
| 462 | else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 463 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 464 | if (!SDecl) |
| 465 | Diag(SuperClassLoc, diag::err_undef_superclass, |
| 466 | SuperClassname->getName(), ClassName->getName()); |
| 467 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 468 | // This implementation and its interface do not have the same |
| 469 | // super class. |
| 470 | Diag(SuperClassLoc, diag::err_conflicting_super_class, |
| 471 | SDecl->getName()); |
| 472 | Diag(SDecl->getLocation(), diag::err_previous_definition); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (!IDecl) { |
| 478 | // Legacy case of @implementation with no corresponding @interface. |
| 479 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 480 | |
| 481 | // FIXME: Do we support attributes on the @implementation? If so |
| 482 | // we should copy them over. |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 483 | IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName, |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 484 | ClassLoc, false, true); |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 485 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 486 | IDecl->setSuperClass(SDecl); |
| 487 | IDecl->setLocEnd(ClassLoc); |
| 488 | |
| 489 | // Remember that this needs to be removed when the scope is popped. |
| 490 | TUScope->AddDecl(IDecl); |
| 491 | } |
| 492 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 493 | ObjCImplementationDecl* IMPDecl = |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 494 | ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, |
| 495 | IDecl, SDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 496 | |
| 497 | // Check that there is no duplicate implementation of this class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 498 | if (ObjCImplementations[ClassName]) |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 499 | // FIXME: Don't leak everything! |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 500 | Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName()); |
| 501 | else // add it to the list. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 502 | ObjCImplementations[ClassName] = IMPDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 503 | return IMPDecl; |
| 504 | } |
| 505 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 506 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 507 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 508 | SourceLocation RBrace) { |
| 509 | assert(ImpDecl && "missing implementation decl"); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 510 | ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 511 | if (!IDecl) |
| 512 | return; |
| 513 | /// Check case of non-existing @interface decl. |
| 514 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 515 | /// Add implementations's ivar to the synthesize class's ivar list. |
| 516 | if (IDecl->ImplicitInterfaceDecl()) { |
| 517 | IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); |
| 518 | return; |
| 519 | } |
| 520 | // If implementation has empty ivar list, just return. |
| 521 | if (numIvars == 0) |
| 522 | return; |
| 523 | |
| 524 | assert(ivars && "missing @implementation ivars"); |
| 525 | |
| 526 | // Check interface's Ivar list against those in the implementation. |
| 527 | // names and types must match. |
| 528 | // |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 529 | unsigned j = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 530 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 531 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 532 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 533 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 534 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 535 | assert (ImplIvar && "missing implementation ivar"); |
| 536 | assert (ClsIvar && "missing class ivar"); |
Chris Lattner | b545786 | 2008-07-27 00:05:05 +0000 | [diff] [blame] | 537 | if (Context.getCanonicalType(ImplIvar->getType()) != |
| 538 | Context.getCanonicalType(ClsIvar->getType())) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 539 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type, |
| 540 | ImplIvar->getIdentifier()->getName()); |
| 541 | Diag(ClsIvar->getLocation(), diag::err_previous_definition, |
| 542 | ClsIvar->getIdentifier()->getName()); |
| 543 | } |
| 544 | // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed |
| 545 | // as error. |
| 546 | else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
| 547 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name, |
| 548 | ImplIvar->getIdentifier()->getName()); |
| 549 | Diag(ClsIvar->getLocation(), diag::err_previous_definition, |
| 550 | ClsIvar->getIdentifier()->getName()); |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 551 | return; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 552 | } |
| 553 | --numIvars; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 554 | } |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 555 | |
| 556 | if (numIvars > 0) |
Chris Lattner | 847de6f | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 557 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 558 | else if (IVI != IVE) |
Chris Lattner | 847de6f | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 559 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 562 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
| 563 | bool &IncompleteImpl) { |
| 564 | if (!IncompleteImpl) { |
| 565 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 566 | IncompleteImpl = true; |
| 567 | } |
| 568 | Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName()); |
| 569 | } |
| 570 | |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 571 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 572 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 573 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 574 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 575 | bool& IncompleteImpl, |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 576 | const llvm::DenseSet<Selector> &InsMap, |
| 577 | const llvm::DenseSet<Selector> &ClsMap) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 578 | // check unimplemented instance methods. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 579 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 580 | E = PDecl->instmeth_end(); I != E; ++I) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 581 | ObjCMethodDecl *method = *I; |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 582 | if (!InsMap.count(method->getSelector()) && |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 583 | method->getImplementationControl() != ObjCMethodDecl::Optional) |
| 584 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 585 | } |
| 586 | // check unimplemented class methods |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 587 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 588 | E = PDecl->classmeth_end(); I != E; ++I) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 589 | ObjCMethodDecl *method = *I; |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 590 | if (!ClsMap.count(method->getSelector()) && |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 591 | method->getImplementationControl() != ObjCMethodDecl::Optional) |
| 592 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 593 | } |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 594 | // Check on this protocols's referenced protocols, recursively. |
| 595 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 596 | E = PDecl->protocol_end(); PI != E; ++PI) |
| 597 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 600 | void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, |
| 601 | ObjCInterfaceDecl* IDecl) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 602 | llvm::DenseSet<Selector> InsMap; |
| 603 | // Check and see if instance methods in class interface have been |
| 604 | // implemented in the implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 605 | for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 606 | E = IMPDecl->instmeth_end(); I != E; ++I) |
| 607 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 608 | |
| 609 | bool IncompleteImpl = false; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 610 | for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 611 | E = IDecl->instmeth_end(); I != E; ++I) |
Fariborz Jahanian | 5f2e224 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 612 | if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 613 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 615 | llvm::DenseSet<Selector> ClsMap; |
| 616 | // Check and see if class methods in class interface have been |
| 617 | // implemented in the implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 618 | for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 619 | E = IMPDecl->classmeth_end(); I != E; ++I) |
| 620 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 621 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 622 | for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 623 | E = IDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 624 | if (!ClsMap.count((*I)->getSelector())) |
| 625 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 626 | |
| 627 | // Check the protocol list for unimplemented methods in the @implementation |
| 628 | // class. |
Chris Lattner | 8bcb525 | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 629 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 630 | IDecl->getReferencedProtocols(); |
| 631 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 632 | E = Protocols.end(); I != E; ++I) |
| 633 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *I, |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 634 | IncompleteImpl, InsMap, ClsMap); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the |
| 638 | /// category interface is implemented in the category @implementation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 639 | void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl, |
| 640 | ObjCCategoryDecl *CatClassDecl) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 641 | llvm::DenseSet<Selector> InsMap; |
| 642 | // Check and see if instance methods in category interface have been |
| 643 | // implemented in its implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 644 | for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 645 | E = CatImplDecl->instmeth_end(); I != E; ++I) |
| 646 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 647 | |
| 648 | bool IncompleteImpl = false; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 649 | for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 650 | E = CatClassDecl->instmeth_end(); I != E; ++I) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 651 | if (!InsMap.count((*I)->getSelector())) |
| 652 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
| 653 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 654 | llvm::DenseSet<Selector> ClsMap; |
| 655 | // Check and see if class methods in category interface have been |
| 656 | // implemented in its implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 657 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 658 | I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end(); |
| 659 | I != E; ++I) |
| 660 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 661 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 662 | for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 663 | E = CatClassDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 664 | if (!ClsMap.count((*I)->getSelector())) |
| 665 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 666 | |
| 667 | // Check the protocol list for unimplemented methods in the @implementation |
| 668 | // class. |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 669 | for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(), |
| 670 | E = CatClassDecl->protocol_end(); PI != E; ++PI) |
| 671 | CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl, |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 672 | InsMap, ClsMap); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /// ActOnForwardClassDeclaration - |
| 676 | Action::DeclTy * |
| 677 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
| 678 | IdentifierInfo **IdentList, unsigned NumElts) |
| 679 | { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 680 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 681 | |
| 682 | for (unsigned i = 0; i != NumElts; ++i) { |
| 683 | // Check for another declaration kind with the same name. |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 684 | Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 685 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | d254997 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 686 | // GCC apparently allows the following idiom: |
| 687 | // |
| 688 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 689 | // @class XCElementToggler; |
| 690 | // |
| 691 | // FIXME: Make an extension? |
| 692 | TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); |
| 693 | if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { |
| 694 | Diag(AtClassLoc, diag::err_redefinition_different_kind, |
| 695 | IdentList[i]->getName()); |
| 696 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 697 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 698 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 699 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 700 | if (!IDecl) { // Not already seen? Make a forward decl. |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 701 | IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i], |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 702 | SourceLocation(), true); |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 703 | ObjCInterfaceDecls[IdentList[i]] = IDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 704 | |
| 705 | // Remember that this needs to be removed when the scope is popped. |
| 706 | TUScope->AddDecl(IDecl); |
| 707 | } |
| 708 | |
| 709 | Interfaces.push_back(IDecl); |
| 710 | } |
| 711 | |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 712 | return ObjCClassDecl::Create(Context, AtClassLoc, |
| 713 | &Interfaces[0], Interfaces.size()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | |
| 717 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 718 | /// returns true, or false, accordingly. |
| 719 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 720 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
| 721 | const ObjCMethodDecl *PrevMethod) { |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 722 | if (Context.getCanonicalType(Method->getResultType()) != |
| 723 | Context.getCanonicalType(PrevMethod->getResultType())) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 724 | return false; |
Chris Lattner | 685d792 | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 725 | for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 726 | ParmVarDecl *ParamDecl = Method->getParamDecl(i); |
| 727 | ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i); |
Chris Lattner | 42a2174 | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 728 | if (Context.getCanonicalType(ParamDecl->getType()) != |
| 729 | Context.getCanonicalType(PrevParamDecl->getType())) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 730 | return false; |
| 731 | } |
| 732 | return true; |
| 733 | } |
| 734 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 735 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 736 | ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 737 | if (!FirstMethod.Method) { |
| 738 | // Haven't seen a method with this selector name yet - add it. |
| 739 | FirstMethod.Method = Method; |
| 740 | FirstMethod.Next = 0; |
| 741 | } else { |
| 742 | // We've seen a method with this name, now check the type signature(s). |
| 743 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 744 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 745 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 746 | Next = Next->Next) |
| 747 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 748 | |
| 749 | if (!match) { |
| 750 | // We have a new signature for an existing method - add it. |
| 751 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 752 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 753 | FirstMethod.Next = OMI; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 758 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 759 | ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 760 | if (!FirstMethod.Method) { |
| 761 | // Haven't seen a method with this selector name yet - add it. |
| 762 | FirstMethod.Method = Method; |
| 763 | FirstMethod.Next = 0; |
| 764 | } else { |
| 765 | // We've seen a method with this name, now check the type signature(s). |
| 766 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 767 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 768 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 769 | Next = Next->Next) |
| 770 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 771 | |
| 772 | if (!match) { |
| 773 | // We have a new signature for an existing method - add it. |
| 774 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 775 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 776 | FirstMethod.Next = OMI; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 781 | // Note: For class/category implemenations, allMethods/allProperties is |
| 782 | // always null. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 783 | void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, |
| 784 | DeclTy **allMethods, unsigned allNum, |
| 785 | DeclTy **allProperties, unsigned pNum) { |
| 786 | Decl *ClassDecl = static_cast<Decl *>(classDecl); |
| 787 | |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 788 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 789 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 790 | // should be true. |
| 791 | if (!ClassDecl) |
| 792 | return; |
| 793 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 794 | llvm::SmallVector<ObjCMethodDecl*, 32> insMethods; |
| 795 | llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 797 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 798 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 799 | |
| 800 | bool isInterfaceDeclKind = |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 801 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 802 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 803 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 804 | |
Seo Sanghyeon | 0473be4 | 2008-07-05 02:01:25 +0000 | [diff] [blame] | 805 | if (pNum != 0) { |
Chris Lattner | cffe366 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 806 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) |
| 807 | IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); |
Fariborz Jahanian | 8516e9a | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 808 | else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 809 | CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); |
| 810 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl)) |
| 811 | PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); |
Fariborz Jahanian | 52ff844 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 812 | else |
Fariborz Jahanian | 8516e9a | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 813 | assert(false && "ActOnAtEnd - property declaration misplaced"); |
Seo Sanghyeon | 0473be4 | 2008-07-05 02:01:25 +0000 | [diff] [blame] | 814 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 815 | |
| 816 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 817 | ObjCMethodDecl *Method = |
| 818 | cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 819 | |
| 820 | if (!Method) continue; // Already issued a diagnostic. |
| 821 | if (Method->isInstance()) { |
| 822 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 823 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 824 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 825 | : false; |
| 826 | if (isInterfaceDeclKind && PrevMethod && !match |
| 827 | || checkIdenticalMethods && match) { |
| 828 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 829 | Method->getSelector().getName()); |
| 830 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 831 | } else { |
| 832 | insMethods.push_back(Method); |
| 833 | InsMap[Method->getSelector()] = Method; |
| 834 | /// The following allows us to typecheck messages to "id". |
| 835 | AddInstanceMethodToGlobalPool(Method); |
| 836 | } |
| 837 | } |
| 838 | else { |
| 839 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 840 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 841 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 842 | : false; |
| 843 | if (isInterfaceDeclKind && PrevMethod && !match |
| 844 | || checkIdenticalMethods && match) { |
| 845 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 846 | Method->getSelector().getName()); |
| 847 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 848 | } else { |
| 849 | clsMethods.push_back(Method); |
| 850 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 851 | /// The following allows us to typecheck messages to "Class". |
| 852 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 857 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 858 | // Compares properties declaraed in this class to those of its |
| 859 | // super class. |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 860 | ComparePropertiesInBaseAndSuper(I); |
| 861 | MergeProtocolPropertiesIntoClass(I, I); |
Fariborz Jahanian | e4534e7 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 862 | for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(), |
| 863 | E = I->classprop_end(); P != E; ++P) { |
Steve Naroff | 638d6a4 | 2008-05-22 23:24:08 +0000 | [diff] [blame] | 864 | // FIXME: It would be really nice if we could avoid this. Injecting |
| 865 | // methods into the interface makes it hard to distinguish "real" methods |
| 866 | // from synthesized "property" methods (that aren't in the source). |
| 867 | // This complicicates the rewriter's life. |
Fariborz Jahanian | e4534e7 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 868 | I->addPropertyMethods(Context, *P, insMethods); |
| 869 | } |
| 870 | I->addMethods(&insMethods[0], insMethods.size(), |
| 871 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 872 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 873 | } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 874 | P->addMethods(&insMethods[0], insMethods.size(), |
| 875 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 876 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 877 | else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 878 | C->addMethods(&insMethods[0], insMethods.size(), |
| 879 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 880 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 881 | else if (ObjCImplementationDecl *IC = |
| 882 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 883 | IC->setLocEnd(AtEndLoc); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 884 | if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 885 | ImplMethodsVsClassMethods(IC, IDecl); |
| 886 | } else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 887 | ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 888 | CatImplClass->setLocEnd(AtEndLoc); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 889 | ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 890 | // Find category interface decl and then check that all methods declared |
| 891 | // in this interface is implemented in the category @implementation. |
| 892 | if (IDecl) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 893 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 894 | Categories; Categories = Categories->getNextClassCategory()) { |
| 895 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
| 896 | ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | |
| 905 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 906 | /// objective-c's type qualifier from the parser version of the same info. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 907 | static Decl::ObjCDeclQualifier |
| 908 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 909 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 910 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 911 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 912 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 913 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 914 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 915 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 916 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 917 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 918 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 919 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 920 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 921 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 922 | |
| 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | Sema::DeclTy *Sema::ActOnMethodDeclaration( |
| 927 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 928 | tok::TokenKind MethodType, DeclTy *classDecl, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 929 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 930 | Selector Sel, |
| 931 | // optional arguments. The number of types/arguments is obtained |
| 932 | // from the Sel.getNumArgs(). |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 933 | ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 934 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 935 | bool isVariadic) { |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 936 | Decl *ClassDecl = static_cast<Decl*>(classDecl); |
Steve Naroff | 70f1624 | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 937 | |
| 938 | // Make sure we can establish a context for the method. |
| 939 | if (!ClassDecl) { |
| 940 | Diag(MethodLoc, diag::error_missing_method_context); |
| 941 | return 0; |
| 942 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 943 | QualType resultDeclType; |
| 944 | |
| 945 | if (ReturnType) |
| 946 | resultDeclType = QualType::getFromOpaquePtr(ReturnType); |
| 947 | else // get the type for "id". |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 948 | resultDeclType = Context.getObjCIdType(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 949 | |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 950 | ObjCMethodDecl* ObjCMethod = |
| 951 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 952 | ClassDecl, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 953 | MethodType == tok::minus, isVariadic, |
Fariborz Jahanian | 5f2e224 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 954 | false, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 955 | MethodDeclKind == tok::objc_optional ? |
| 956 | ObjCMethodDecl::Optional : |
| 957 | ObjCMethodDecl::Required); |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 958 | if (AttrList) |
| 959 | ProcessDeclAttributeList(ObjCMethod, AttrList); |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 960 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 961 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 962 | |
| 963 | for (unsigned i = 0; i < Sel.getNumArgs(); i++) { |
| 964 | // FIXME: arg->AttrList must be stored too! |
| 965 | QualType argType; |
| 966 | |
| 967 | if (ArgTypes[i]) |
| 968 | argType = QualType::getFromOpaquePtr(ArgTypes[i]); |
| 969 | else |
| 970 | argType = Context.getObjCIdType(); |
| 971 | ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod, |
| 972 | SourceLocation(/*FIXME*/), |
| 973 | ArgNames[i], argType, |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 974 | VarDecl::None, 0, 0); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 975 | Param->setObjCDeclQualifier( |
| 976 | CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); |
| 977 | Params.push_back(Param); |
| 978 | } |
| 979 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 980 | ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); |
| 981 | ObjCMethod->setObjCDeclQualifier( |
| 982 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 983 | const ObjCMethodDecl *PrevMethod = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 984 | |
| 985 | // For implementations (which can be very "coarse grain"), we add the |
| 986 | // method now. This allows the AST to implement lookup methods that work |
| 987 | // incrementally (without waiting until we parse the @end). It also allows |
| 988 | // us to flag multiple declaration errors as they occur. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 989 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 990 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 991 | if (MethodType == tok::minus) { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 992 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 993 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 994 | } else { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 995 | PrevMethod = ImpDecl->getClassMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 996 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 999 | else if (ObjCCategoryImplDecl *CatImpDecl = |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1000 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1001 | if (MethodType == tok::minus) { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1002 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1003 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1004 | } else { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1005 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1006 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | if (PrevMethod) { |
| 1010 | // You can never have two method definitions with the same name. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1011 | Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl, |
| 1012 | ObjCMethod->getSelector().getName()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1013 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 1014 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1015 | return ObjCMethod; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Fariborz Jahanian | 0ceb4be | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 1018 | Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
| 1019 | FieldDeclarator &FD, |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1020 | ObjCDeclSpec &ODS, |
Fariborz Jahanian | b7080ae | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 1021 | Selector GetterSel, |
| 1022 | Selector SetterSel, |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1023 | tok::ObjCKeywordKind MethodImplKind) { |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1024 | QualType T = GetTypeForDeclarator(FD.D, S); |
Fariborz Jahanian | 0ceb4be | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 1025 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc, |
| 1026 | FD.D.getIdentifier(), T); |
Fariborz Jahanian | e4534e7 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 1027 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 1028 | // selector names in anticipation of declaration of setter/getter methods. |
| 1029 | PDecl->setGetterName(GetterSel); |
| 1030 | PDecl->setSetterName(SetterSel); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1031 | |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1032 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1033 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1034 | |
Fariborz Jahanian | e4534e7 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 1035 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1036 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1037 | |
Fariborz Jahanian | e4534e7 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 1038 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1039 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1040 | |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1041 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1042 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1043 | |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1044 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1045 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1046 | |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1047 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1048 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1049 | |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1050 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1051 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1052 | |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1053 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1054 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1055 | |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1056 | if (MethodImplKind == tok::objc_required) |
| 1057 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 1058 | else if (MethodImplKind == tok::objc_optional) |
| 1059 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
| 1060 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1061 | return PDecl; |
| 1062 | } |
| 1063 | |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1064 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 1065 | /// builds the AST node for a property implementation declaration; declared |
| 1066 | /// as @synthesize or @dynamic. |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1067 | /// |
| 1068 | Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, |
| 1069 | SourceLocation PropertyLoc, |
| 1070 | bool Synthesize, |
| 1071 | DeclTy *ClassCatImpDecl, |
| 1072 | IdentifierInfo *PropertyId, |
| 1073 | IdentifierInfo *PropertyIvar) { |
| 1074 | Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl); |
| 1075 | // Make sure we have a context for the property implementation declaration. |
| 1076 | if (!ClassImpDecl) { |
| 1077 | Diag(AtLoc, diag::error_missing_property_context); |
| 1078 | return 0; |
| 1079 | } |
| 1080 | ObjCPropertyDecl *property = 0; |
| 1081 | ObjCInterfaceDecl* IDecl = 0; |
| 1082 | // Find the class or category class where this property must have |
| 1083 | // a declaration. |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1084 | ObjCImplementationDecl *IC = 0; |
| 1085 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 1086 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1087 | IDecl = getObjCInterfaceDecl(IC->getIdentifier()); |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1088 | // We always synthesize an interface for an implementation |
| 1089 | // without an interface decl. So, IDecl is always non-zero. |
| 1090 | assert(IDecl && |
| 1091 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 1092 | |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1093 | // Look for this property declaration in the @implementation's @interface |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1094 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 1095 | if (!property) { |
| 1096 | Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName()); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1097 | return 0; |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1098 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1099 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1100 | else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1101 | if (Synthesize) { |
| 1102 | Diag(AtLoc, diag::error_synthesize_category_decl); |
| 1103 | return 0; |
| 1104 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1105 | IDecl = CatImplClass->getClassInterface(); |
| 1106 | if (!IDecl) { |
| 1107 | Diag(AtLoc, diag::error_missing_property_interface); |
| 1108 | return 0; |
| 1109 | } |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1110 | ObjCCategoryDecl *Category = |
| 1111 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 1112 | |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1113 | // If category for this implementation not found, it is an error which |
| 1114 | // has already been reported eralier. |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1115 | if (!Category) |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1116 | return 0; |
| 1117 | // Look for this property declaration in @implementation's category |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1118 | property = Category->FindPropertyDeclaration(PropertyId); |
| 1119 | if (!property) { |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1120 | Diag(PropertyLoc, diag::error_bad_category_property_decl, |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1121 | Category->getName()); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | } |
| 1125 | else { |
| 1126 | Diag(AtLoc, diag::error_bad_property_context); |
| 1127 | return 0; |
| 1128 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1129 | ObjCIvarDecl *Ivar = 0; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1130 | // Check that we have a valid, previously declared ivar for @synthesize |
| 1131 | if (Synthesize) { |
| 1132 | // @synthesize |
Fariborz Jahanian | 1f02800 | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 1133 | if (!PropertyIvar) |
| 1134 | PropertyIvar = PropertyId; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1135 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1136 | Ivar = IDecl->FindIvarDeclaration(PropertyIvar); |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1137 | if (!Ivar) { |
Fariborz Jahanian | 1f02800 | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 1138 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl, |
| 1139 | PropertyId->getName()); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1140 | return 0; |
| 1141 | } |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1142 | // Check that type of property and its ivar match. |
Chris Lattner | c5cff30 | 2008-07-26 20:50:02 +0000 | [diff] [blame] | 1143 | if (Context.getCanonicalType(Ivar->getType()) != |
| 1144 | Context.getCanonicalType(property->getType())) { |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1145 | Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(), |
| 1146 | Ivar->getName()); |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1150 | } else if (PropertyIvar) { |
| 1151 | // @dynamic |
| 1152 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
| 1153 | return 0; |
| 1154 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1155 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1156 | ObjCPropertyImplDecl *PIDecl = |
| 1157 | ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property, |
| 1158 | (Synthesize ? |
Daniel Dunbar | 14117fc | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 1159 | ObjCPropertyImplDecl::Synthesize |
| 1160 | : ObjCPropertyImplDecl::Dynamic), |
| 1161 | Ivar); |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1162 | if (IC) |
| 1163 | IC->addPropertyImplementation(PIDecl); |
| 1164 | else |
| 1165 | CatImplClass->addPropertyImplementation(PIDecl); |
| 1166 | |
| 1167 | return PIDecl; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1168 | } |