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