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