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