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