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