Chris Lattner | 4d39148 | 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 | 0bc735f | 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 | 4d39148 | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 21 | /// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 22 | /// and user declared, in the method definition's AST. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 23 | void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 24 | assert(CurFunctionDecl == 0 && "Method parsing confused"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 25 | ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 26 | assert(MDecl != 0 && "Not a method declarator!"); |
Steve Naroff | a56f616 | 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 | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 33 | |
| 34 | // Allow all of Sema to see that we are entering a method definition. |
| 35 | CurMethodDecl = MDecl; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame^] | 36 | PushContextDecl(MDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 37 | |
| 38 | // Create Decl objects for each parameter, entrring them in the scope for |
| 39 | // binding to their use. |
| 40 | struct DeclaratorChunk::ParamInfo PI; |
| 41 | |
| 42 | // Insert the invisible arguments, self and _cmd! |
| 43 | PI.Ident = &Context.Idents.get("self"); |
| 44 | PI.IdentLoc = SourceLocation(); // synthesized vars have a null location. |
| 45 | PI.InvalidType = false; |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 46 | PI.AttrList = 0; |
Gabor Greif | 3003753 | 2008-02-29 20:35:55 +0000 | [diff] [blame] | 47 | PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr(); |
| 48 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 49 | if (MDecl->isInstance()) { |
Gabor Greif | 3003753 | 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 |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 53 | QualType selfTy = Context.getObjCInterfaceType(OID); |
| 54 | selfTy = Context.getPointerType(selfTy); |
| 55 | PI.TypeInfo = selfTy.getAsOpaquePtr(); |
| 56 | } |
Gabor Greif | 3003753 | 2008-02-29 20:35:55 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 59 | CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope)); |
| 60 | |
| 61 | PI.Ident = &Context.Idents.get("_cmd"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 62 | PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 63 | ActOnParamDeclarator(PI, FnBodyScope); |
| 64 | |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 65 | for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 66 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 67 | PI.Ident = PDecl->getIdentifier(); |
| 68 | PI.IdentLoc = PDecl->getLocation(); // user vars have a real location. |
| 69 | PI.TypeInfo = PDecl->getType().getAsOpaquePtr(); |
Fariborz Jahanian | 2338d58 | 2008-01-21 22:59:53 +0000 | [diff] [blame] | 70 | MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope)); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | Sema::DeclTy *Sema::ActOnStartClassInterface( |
| 75 | SourceLocation AtInterfaceLoc, |
| 76 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 77 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 78 | IdentifierInfo **ProtocolNames, unsigned NumProtocols, |
| 79 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
| 80 | assert(ClassName && "Missing class identifier"); |
| 81 | |
| 82 | // Check for another declaration kind with the same name. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 83 | Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 84 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 85 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 86 | ClassName->getName()); |
| 87 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 88 | } |
| 89 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 90 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 91 | if (IDecl) { |
| 92 | // Class already seen. Is it a forward declaration? |
| 93 | if (!IDecl->isForwardDecl()) |
| 94 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName()); |
| 95 | else { |
| 96 | IDecl->setLocation(AtInterfaceLoc); |
| 97 | IDecl->setForwardDecl(false); |
| 98 | IDecl->AllocIntfRefProtocols(NumProtocols); |
| 99 | } |
| 100 | } |
| 101 | else { |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 102 | IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols, |
| 103 | ClassName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 104 | |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 105 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 106 | // Remember that this needs to be removed when the scope is popped. |
| 107 | TUScope->AddDecl(IDecl); |
| 108 | } |
| 109 | |
| 110 | if (SuperName) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 111 | ObjCInterfaceDecl* SuperClassEntry = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 112 | // Check if a different kind of symbol declared in this scope. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 113 | PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 114 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 115 | Diag(SuperLoc, diag::err_redefinition_different_kind, |
| 116 | SuperName->getName()); |
| 117 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 118 | } |
| 119 | else { |
| 120 | // Check that super class is previously defined |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 121 | SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 122 | |
| 123 | if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) { |
| 124 | Diag(AtInterfaceLoc, diag::err_undef_superclass, |
| 125 | SuperClassEntry ? SuperClassEntry->getName() |
| 126 | : SuperName->getName(), |
| 127 | ClassName->getName()); |
| 128 | } |
| 129 | } |
| 130 | IDecl->setSuperClass(SuperClassEntry); |
| 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) { |
| 138 | for (unsigned int i = 0; i != NumProtocols; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 139 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 140 | if (!RefPDecl || RefPDecl->isForwardDecl()) |
| 141 | Diag(ClassLoc, diag::warn_undef_protocolref, |
| 142 | ProtocolNames[i]->getName(), |
| 143 | ClassName->getName()); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 144 | IDecl->setIntfRefProtocols(i, RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 145 | } |
| 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 | e8043c3 | 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 | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 158 | // Look for previous declaration of alias name |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 159 | Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 160 | if (ADecl) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 161 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) { |
Chris Lattner | 4d39148 | 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 | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 173 | Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Chris Lattner | f8d17a5 | 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 | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 179 | return 0; |
| 180 | } |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 181 | |
| 182 | // Everything checked out, instantiate a new alias declaration AST. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 183 | ObjCCompatibleAliasDecl *AliasDecl = |
Steve Naroff | e8043c3 | 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 | 4d39148 | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 197 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 198 | if (PDecl) { |
| 199 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 200 | if (!PDecl->isForwardDecl()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 201 | Diag(ProtocolLoc, diag::err_duplicate_protocol_def, |
| 202 | ProtocolName->getName()); |
Chris Lattner | 439e71f | 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 | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 207 | |
| 208 | PDecl->setForwardDecl(false); |
| 209 | PDecl->AllocReferencedProtocols(NumProtoRefs); |
| 210 | } else { |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 211 | PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs, |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 212 | ProtocolName); |
| 213 | PDecl->setForwardDecl(false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 214 | ObjCProtocols[ProtocolName] = PDecl; |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 216 | |
| 217 | if (NumProtoRefs) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 218 | /// Check then save referenced protocols. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 219 | for (unsigned int i = 0; i != NumProtoRefs; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 220 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]]; |
Chris Lattner | 4d39148 | 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 | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 225 | PDecl->setReferencedProtocols(i, RefPDecl); |
Chris Lattner | 4d39148 | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 241 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]]; |
Chris Lattner | 4d39148 | 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 | |
| 250 | /// ActOnForwardProtocolDeclaration - |
| 251 | Action::DeclTy * |
| 252 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
| 253 | IdentifierInfo **IdentList, unsigned NumElts) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 254 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 255 | |
| 256 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 257 | IdentifierInfo *Ident = IdentList[i]; |
| 258 | ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; |
| 259 | if (PDecl == 0) { // Not already seen? |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 260 | // FIXME: Pass in the location of the identifier! |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 261 | PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | Protocols.push_back(PDecl); |
| 265 | } |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 266 | return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc, |
| 267 | &Protocols[0], Protocols.size()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | Sema::DeclTy *Sema::ActOnStartCategoryInterface( |
| 271 | SourceLocation AtInterfaceLoc, |
| 272 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 273 | IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
| 274 | IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs, |
| 275 | SourceLocation EndProtoLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 276 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 278 | ObjCCategoryDecl *CDecl = |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 279 | ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 280 | CDecl->setClassInterface(IDecl); |
Fariborz Jahanian | 7c453b3 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 281 | |
| 282 | /// Check that class of this category is already completely declared. |
| 283 | if (!IDecl || IDecl->isForwardDecl()) |
| 284 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
| 285 | else { |
| 286 | /// Check for duplicate interface declaration for this category |
| 287 | ObjCCategoryDecl *CDeclChain; |
| 288 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 289 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 290 | if (CDeclChain->getIdentifier() == CategoryName) { |
| 291 | Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(), |
| 292 | CategoryName->getName()); |
| 293 | break; |
| 294 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 295 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 296 | if (!CDeclChain) |
| 297 | CDecl->insertNextClassCategory(); |
Fariborz Jahanian | 7c453b3 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 298 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 299 | |
| 300 | if (NumProtoRefs) { |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 301 | llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols; |
| 302 | /// Check and then save the referenced protocols. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 303 | for (unsigned int i = 0; i != NumProtoRefs; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 304 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 305 | if (!RefPDecl || RefPDecl->isForwardDecl()) { |
| 306 | Diag(CategoryLoc, diag::warn_undef_protocolref, |
| 307 | ProtoRefNames[i]->getName(), |
| 308 | CategoryName->getName()); |
| 309 | } |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 310 | if (RefPDecl) |
| 311 | RefProtocols.push_back(RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 313 | if (!RefProtocols.empty()) |
| 314 | CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 315 | } |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 316 | CDecl->setLocEnd(EndProtoLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 317 | return CDecl; |
| 318 | } |
| 319 | |
| 320 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 321 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 322 | /// object. |
| 323 | Sema::DeclTy *Sema::ActOnStartCategoryImplementation( |
| 324 | SourceLocation AtCatImplLoc, |
| 325 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 326 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 327 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 328 | ObjCCategoryImplDecl *CDecl = |
| 329 | ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 330 | /// Check that class of this category is already completely declared. |
| 331 | if (!IDecl || IDecl->isForwardDecl()) |
| 332 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
| 333 | |
| 334 | /// TODO: Check that CatName, category name, is not used in another |
| 335 | // implementation. |
| 336 | return CDecl; |
| 337 | } |
| 338 | |
| 339 | Sema::DeclTy *Sema::ActOnStartClassImplementation( |
| 340 | SourceLocation AtClassImplLoc, |
| 341 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 342 | IdentifierInfo *SuperClassname, |
| 343 | SourceLocation SuperClassLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 344 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 345 | // Check for another declaration kind with the same name. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 346 | Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 347 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 348 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 349 | ClassName->getName()); |
| 350 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 351 | } |
| 352 | else { |
| 353 | // Is there an interface declaration of this class; if not, warn! |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 354 | IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 355 | if (!IDecl) |
| 356 | Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName()); |
| 357 | } |
| 358 | |
| 359 | // Check that super class name is valid class name |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 360 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 361 | if (SuperClassname) { |
| 362 | // Check if a different kind of symbol declared in this scope. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 363 | PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 364 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 365 | Diag(SuperClassLoc, diag::err_redefinition_different_kind, |
| 366 | SuperClassname->getName()); |
| 367 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 368 | } |
| 369 | else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 370 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 371 | if (!SDecl) |
| 372 | Diag(SuperClassLoc, diag::err_undef_superclass, |
| 373 | SuperClassname->getName(), ClassName->getName()); |
| 374 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 375 | // This implementation and its interface do not have the same |
| 376 | // super class. |
| 377 | Diag(SuperClassLoc, diag::err_conflicting_super_class, |
| 378 | SDecl->getName()); |
| 379 | Diag(SDecl->getLocation(), diag::err_previous_definition); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (!IDecl) { |
| 385 | // Legacy case of @implementation with no corresponding @interface. |
| 386 | // Build, chain & install the interface decl into the identifier. |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 387 | IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName, |
| 388 | false, true); |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 389 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 390 | IDecl->setSuperClass(SDecl); |
| 391 | IDecl->setLocEnd(ClassLoc); |
| 392 | |
| 393 | // Remember that this needs to be removed when the scope is popped. |
| 394 | TUScope->AddDecl(IDecl); |
| 395 | } |
| 396 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 397 | ObjCImplementationDecl* IMPDecl = |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 398 | ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, |
| 399 | IDecl, SDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 400 | |
| 401 | // Check that there is no duplicate implementation of this class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 402 | if (ObjCImplementations[ClassName]) |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 403 | // FIXME: Don't leak everything! |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 404 | Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName()); |
| 405 | else // add it to the list. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 406 | ObjCImplementations[ClassName] = IMPDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 407 | return IMPDecl; |
| 408 | } |
| 409 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 410 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 411 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 412 | SourceLocation RBrace) { |
| 413 | assert(ImpDecl && "missing implementation decl"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 414 | ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 415 | if (!IDecl) |
| 416 | return; |
| 417 | /// Check case of non-existing @interface decl. |
| 418 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 419 | /// Add implementations's ivar to the synthesize class's ivar list. |
| 420 | if (IDecl->ImplicitInterfaceDecl()) { |
| 421 | IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); |
| 422 | return; |
| 423 | } |
| 424 | // If implementation has empty ivar list, just return. |
| 425 | if (numIvars == 0) |
| 426 | return; |
| 427 | |
| 428 | assert(ivars && "missing @implementation ivars"); |
| 429 | |
| 430 | // Check interface's Ivar list against those in the implementation. |
| 431 | // names and types must match. |
| 432 | // |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 433 | unsigned j = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 434 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 435 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 436 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 437 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 438 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 439 | assert (ImplIvar && "missing implementation ivar"); |
| 440 | assert (ClsIvar && "missing class ivar"); |
| 441 | if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) { |
| 442 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type, |
| 443 | ImplIvar->getIdentifier()->getName()); |
| 444 | Diag(ClsIvar->getLocation(), diag::err_previous_definition, |
| 445 | ClsIvar->getIdentifier()->getName()); |
| 446 | } |
| 447 | // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed |
| 448 | // as error. |
| 449 | else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
| 450 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name, |
| 451 | ImplIvar->getIdentifier()->getName()); |
| 452 | Diag(ClsIvar->getLocation(), diag::err_previous_definition, |
| 453 | ClsIvar->getIdentifier()->getName()); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 454 | return; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 455 | } |
| 456 | --numIvars; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 458 | |
| 459 | if (numIvars > 0) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 460 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 461 | else if (IVI != IVE) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 462 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 465 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
| 466 | bool &IncompleteImpl) { |
| 467 | if (!IncompleteImpl) { |
| 468 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 469 | IncompleteImpl = true; |
| 470 | } |
| 471 | Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName()); |
| 472 | } |
| 473 | |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 474 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 475 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 476 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 477 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 478 | bool& IncompleteImpl, |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 479 | const llvm::DenseSet<Selector> &InsMap, |
| 480 | const llvm::DenseSet<Selector> &ClsMap) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 481 | // check unimplemented instance methods. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 482 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 483 | E = PDecl->instmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 484 | ObjCMethodDecl *method = *I; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 485 | if (!InsMap.count(method->getSelector()) && |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 486 | method->getImplementationControl() != ObjCMethodDecl::Optional) |
| 487 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 488 | } |
| 489 | // check unimplemented class methods |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 490 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 491 | E = PDecl->classmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 492 | ObjCMethodDecl *method = *I; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 493 | if (!ClsMap.count(method->getSelector()) && |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 494 | method->getImplementationControl() != ObjCMethodDecl::Optional) |
| 495 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 496 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 497 | // Check on this protocols's referenced protocols, recursively |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 498 | ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols(); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 499 | for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++) |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 500 | CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 503 | void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, |
| 504 | ObjCInterfaceDecl* IDecl) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 505 | llvm::DenseSet<Selector> InsMap; |
| 506 | // Check and see if instance methods in class interface have been |
| 507 | // implemented in the implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 508 | for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 509 | E = IMPDecl->instmeth_end(); I != E; ++I) |
| 510 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 511 | |
| 512 | bool IncompleteImpl = false; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 513 | for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 514 | E = IDecl->instmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 515 | if (!InsMap.count((*I)->getSelector())) |
| 516 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 517 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 518 | llvm::DenseSet<Selector> ClsMap; |
| 519 | // Check and see if class methods in class interface have been |
| 520 | // implemented in the implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 521 | for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 522 | E = IMPDecl->classmeth_end(); I != E; ++I) |
| 523 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 524 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 525 | for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 526 | E = IDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 527 | if (!ClsMap.count((*I)->getSelector())) |
| 528 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 529 | |
| 530 | // Check the protocol list for unimplemented methods in the @implementation |
| 531 | // class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 532 | ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols(); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 533 | for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 534 | CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i], |
| 535 | IncompleteImpl, InsMap, ClsMap); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the |
| 539 | /// category interface is implemented in the category @implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 540 | void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl, |
| 541 | ObjCCategoryDecl *CatClassDecl) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 542 | llvm::DenseSet<Selector> InsMap; |
| 543 | // Check and see if instance methods in category interface have been |
| 544 | // implemented in its implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 545 | for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 546 | E = CatImplDecl->instmeth_end(); I != E; ++I) |
| 547 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 548 | |
| 549 | bool IncompleteImpl = false; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 550 | for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 551 | E = CatClassDecl->instmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 552 | if (!InsMap.count((*I)->getSelector())) |
| 553 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
| 554 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 555 | llvm::DenseSet<Selector> ClsMap; |
| 556 | // Check and see if class methods in category interface have been |
| 557 | // implemented in its implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 558 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 559 | I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end(); |
| 560 | I != E; ++I) |
| 561 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 562 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 563 | for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 564 | E = CatClassDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 565 | if (!ClsMap.count((*I)->getSelector())) |
| 566 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 567 | |
| 568 | // Check the protocol list for unimplemented methods in the @implementation |
| 569 | // class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 570 | ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols(); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 571 | for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 572 | ObjCProtocolDecl* PDecl = protocols[i]; |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 573 | CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl, |
| 574 | InsMap, ClsMap); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 575 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /// ActOnForwardClassDeclaration - |
| 579 | Action::DeclTy * |
| 580 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
| 581 | IdentifierInfo **IdentList, unsigned NumElts) |
| 582 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 583 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 584 | |
| 585 | for (unsigned i = 0; i != NumElts; ++i) { |
| 586 | // Check for another declaration kind with the same name. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 587 | Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 588 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 589 | Diag(AtClassLoc, diag::err_redefinition_different_kind, |
| 590 | IdentList[i]->getName()); |
| 591 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 592 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 593 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 594 | if (!IDecl) { // Not already seen? Make a forward decl. |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 595 | IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i], |
| 596 | true); |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 597 | ObjCInterfaceDecls[IdentList[i]] = IDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 598 | |
| 599 | // Remember that this needs to be removed when the scope is popped. |
| 600 | TUScope->AddDecl(IDecl); |
| 601 | } |
| 602 | |
| 603 | Interfaces.push_back(IDecl); |
| 604 | } |
| 605 | |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 606 | return ObjCClassDecl::Create(Context, AtClassLoc, |
| 607 | &Interfaces[0], Interfaces.size()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | |
| 611 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 612 | /// returns true, or false, accordingly. |
| 613 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 614 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
| 615 | const ObjCMethodDecl *PrevMethod) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 616 | if (Method->getResultType().getCanonicalType() != |
| 617 | PrevMethod->getResultType().getCanonicalType()) |
| 618 | return false; |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 619 | for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 620 | ParmVarDecl *ParamDecl = Method->getParamDecl(i); |
| 621 | ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i); |
| 622 | if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType()) |
| 623 | return false; |
| 624 | } |
| 625 | return true; |
| 626 | } |
| 627 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 628 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 629 | ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 630 | if (!FirstMethod.Method) { |
| 631 | // Haven't seen a method with this selector name yet - add it. |
| 632 | FirstMethod.Method = Method; |
| 633 | FirstMethod.Next = 0; |
| 634 | } else { |
| 635 | // We've seen a method with this name, now check the type signature(s). |
| 636 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 637 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 638 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 639 | Next = Next->Next) |
| 640 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 641 | |
| 642 | if (!match) { |
| 643 | // We have a new signature for an existing method - add it. |
| 644 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 645 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 646 | FirstMethod.Next = OMI; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 651 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 652 | ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 653 | if (!FirstMethod.Method) { |
| 654 | // Haven't seen a method with this selector name yet - add it. |
| 655 | FirstMethod.Method = Method; |
| 656 | FirstMethod.Next = 0; |
| 657 | } else { |
| 658 | // We've seen a method with this name, now check the type signature(s). |
| 659 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 660 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 661 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 662 | Next = Next->Next) |
| 663 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 664 | |
| 665 | if (!match) { |
| 666 | // We have a new signature for an existing method - add it. |
| 667 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 668 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 669 | FirstMethod.Next = OMI; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 674 | // Note: For class/category implemenations, allMethods/allProperties is |
| 675 | // always null. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 676 | void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, |
| 677 | DeclTy **allMethods, unsigned allNum, |
| 678 | DeclTy **allProperties, unsigned pNum) { |
| 679 | Decl *ClassDecl = static_cast<Decl *>(classDecl); |
| 680 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 681 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 682 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 683 | // should be true. |
| 684 | if (!ClassDecl) |
| 685 | return; |
| 686 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 687 | llvm::SmallVector<ObjCMethodDecl*, 32> insMethods; |
| 688 | llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 689 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 690 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 691 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 692 | |
| 693 | bool isInterfaceDeclKind = |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 694 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 695 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 696 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 697 | |
| 698 | // TODO: property declaration in category and protocols. |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 699 | if (pNum != 0) |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 700 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) |
| 701 | IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 702 | |
| 703 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 704 | ObjCMethodDecl *Method = |
| 705 | cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 706 | |
| 707 | if (!Method) continue; // Already issued a diagnostic. |
| 708 | if (Method->isInstance()) { |
| 709 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 710 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 711 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 712 | : false; |
| 713 | if (isInterfaceDeclKind && PrevMethod && !match |
| 714 | || checkIdenticalMethods && match) { |
| 715 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 716 | Method->getSelector().getName()); |
| 717 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 718 | } else { |
| 719 | insMethods.push_back(Method); |
| 720 | InsMap[Method->getSelector()] = Method; |
| 721 | /// The following allows us to typecheck messages to "id". |
| 722 | AddInstanceMethodToGlobalPool(Method); |
| 723 | } |
| 724 | } |
| 725 | else { |
| 726 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 727 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 728 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 729 | : false; |
| 730 | if (isInterfaceDeclKind && PrevMethod && !match |
| 731 | || checkIdenticalMethods && match) { |
| 732 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 733 | Method->getSelector().getName()); |
| 734 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 735 | } else { |
| 736 | clsMethods.push_back(Method); |
| 737 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 738 | /// The following allows us to typecheck messages to "Class". |
| 739 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 744 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 745 | I->addMethods(&insMethods[0], insMethods.size(), |
| 746 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 747 | } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 748 | P->addMethods(&insMethods[0], insMethods.size(), |
| 749 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 750 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 751 | else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 752 | C->addMethods(&insMethods[0], insMethods.size(), |
| 753 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 754 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 755 | else if (ObjCImplementationDecl *IC = |
| 756 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 757 | IC->setLocEnd(AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 758 | if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 759 | ImplMethodsVsClassMethods(IC, IDecl); |
| 760 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 761 | ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 762 | CatImplClass->setLocEnd(AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 763 | ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 764 | // Find category interface decl and then check that all methods declared |
| 765 | // in this interface is implemented in the category @implementation. |
| 766 | if (IDecl) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 767 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 768 | Categories; Categories = Categories->getNextClassCategory()) { |
| 769 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
| 770 | ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | |
| 779 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 780 | /// objective-c's type qualifier from the parser version of the same info. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 781 | static Decl::ObjCDeclQualifier |
| 782 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 783 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 784 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 785 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 786 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 787 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 788 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 789 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 790 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 791 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 792 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 793 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 794 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 795 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 796 | |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | Sema::DeclTy *Sema::ActOnMethodDeclaration( |
| 801 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 802 | tok::TokenKind MethodType, DeclTy *classDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 803 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 804 | Selector Sel, |
| 805 | // optional arguments. The number of types/arguments is obtained |
| 806 | // from the Sel.getNumArgs(). |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 807 | ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 808 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 809 | bool isVariadic) { |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 810 | Decl *ClassDecl = static_cast<Decl*>(classDecl); |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 811 | |
| 812 | // Make sure we can establish a context for the method. |
| 813 | if (!ClassDecl) { |
| 814 | Diag(MethodLoc, diag::error_missing_method_context); |
| 815 | return 0; |
| 816 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 817 | QualType resultDeclType; |
| 818 | |
| 819 | if (ReturnType) |
| 820 | resultDeclType = QualType::getFromOpaquePtr(ReturnType); |
| 821 | else // get the type for "id". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 822 | resultDeclType = Context.getObjCIdType(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 824 | ObjCMethodDecl* ObjCMethod = |
| 825 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Chris Lattner | b06fa3b | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 826 | ClassDecl, AttrList, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 827 | MethodType == tok::minus, isVariadic, |
| 828 | MethodDeclKind == tok::objc_optional ? |
| 829 | ObjCMethodDecl::Optional : |
| 830 | ObjCMethodDecl::Required); |
| 831 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame^] | 832 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 833 | |
| 834 | for (unsigned i = 0; i < Sel.getNumArgs(); i++) { |
| 835 | // FIXME: arg->AttrList must be stored too! |
| 836 | QualType argType; |
| 837 | |
| 838 | if (ArgTypes[i]) |
| 839 | argType = QualType::getFromOpaquePtr(ArgTypes[i]); |
| 840 | else |
| 841 | argType = Context.getObjCIdType(); |
| 842 | ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod, |
| 843 | SourceLocation(/*FIXME*/), |
| 844 | ArgNames[i], argType, |
| 845 | VarDecl::None, 0); |
| 846 | Param->setObjCDeclQualifier( |
| 847 | CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); |
| 848 | Params.push_back(Param); |
| 849 | } |
| 850 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 851 | ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); |
| 852 | ObjCMethod->setObjCDeclQualifier( |
| 853 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 854 | const ObjCMethodDecl *PrevMethod = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 855 | |
| 856 | // For implementations (which can be very "coarse grain"), we add the |
| 857 | // method now. This allows the AST to implement lookup methods that work |
| 858 | // incrementally (without waiting until we parse the @end). It also allows |
| 859 | // us to flag multiple declaration errors as they occur. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 860 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 861 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 862 | if (MethodType == tok::minus) { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 863 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 864 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 865 | } else { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 866 | PrevMethod = ImpDecl->getClassMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 867 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 870 | else if (ObjCCategoryImplDecl *CatImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 871 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 872 | if (MethodType == tok::minus) { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 873 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 874 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 875 | } else { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 876 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 877 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | if (PrevMethod) { |
| 881 | // You can never have two method definitions with the same name. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 882 | Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl, |
| 883 | ObjCMethod->getSelector().getName()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 884 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 885 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 886 | return ObjCMethod; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 889 | Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc, |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 890 | DeclTy **allProperties, |
| 891 | unsigned NumProperties, |
| 892 | ObjCDeclSpec &DS) { |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 893 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 894 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 895 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 896 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 897 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 898 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 899 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 900 | PDecl->setGetterName(DS.getGetterName()); |
| 901 | } |
| 902 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 903 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 904 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 905 | PDecl->setSetterName(DS.getSetterName()); |
| 906 | } |
| 907 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 908 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 909 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 910 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 911 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 912 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 913 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 914 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 915 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 916 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 917 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 918 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 919 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 920 | if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 921 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 922 | |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 923 | if (NumProperties != 0) |
| 924 | PDecl->setPropertyDeclLists((ObjCIvarDecl**)allProperties, NumProperties); |
| 925 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 926 | return PDecl; |
| 927 | } |
| 928 | |