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