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; |
| 36 | |
| 37 | // Create Decl objects for each parameter, entrring them in the scope for |
| 38 | // binding to their use. |
| 39 | struct DeclaratorChunk::ParamInfo PI; |
| 40 | |
| 41 | // Insert the invisible arguments, self and _cmd! |
| 42 | PI.Ident = &Context.Idents.get("self"); |
| 43 | PI.IdentLoc = SourceLocation(); // synthesized vars have a null location. |
| 44 | PI.InvalidType = false; |
Nate Begeman | b7894b5 | 2008-02-17 21:20:31 +0000 | [diff] [blame] | 45 | PI.AttrList = 0; |
Gabor Greif | 3003753 | 2008-02-29 20:35:55 +0000 | [diff] [blame] | 46 | PI.TypeInfo = Context.getObjCIdType().getAsOpaquePtr(); |
| 47 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 48 | if (MDecl->isInstance()) { |
Gabor Greif | 3003753 | 2008-02-29 20:35:55 +0000 | [diff] [blame] | 49 | if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) { |
| 50 | // There may be no interface context due to error in declaration of the |
| 51 | // interface (which has been reported). Recover gracefully |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 52 | QualType selfTy = Context.getObjCInterfaceType(OID); |
| 53 | selfTy = Context.getPointerType(selfTy); |
| 54 | PI.TypeInfo = selfTy.getAsOpaquePtr(); |
| 55 | } |
Gabor Greif | 3003753 | 2008-02-29 20:35:55 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 58 | CurMethodDecl->setSelfDecl(ActOnParamDeclarator(PI, FnBodyScope)); |
| 59 | |
| 60 | PI.Ident = &Context.Idents.get("_cmd"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 61 | PI.TypeInfo = Context.getObjCSelType().getAsOpaquePtr(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 62 | ActOnParamDeclarator(PI, FnBodyScope); |
| 63 | |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 64 | for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 65 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 66 | PI.Ident = PDecl->getIdentifier(); |
| 67 | PI.IdentLoc = PDecl->getLocation(); // user vars have a real location. |
| 68 | PI.TypeInfo = PDecl->getType().getAsOpaquePtr(); |
Fariborz Jahanian | 2338d58 | 2008-01-21 22:59:53 +0000 | [diff] [blame] | 69 | MDecl->setParamDecl(i, ActOnParamDeclarator(PI, FnBodyScope)); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | Sema::DeclTy *Sema::ActOnStartClassInterface( |
| 74 | SourceLocation AtInterfaceLoc, |
| 75 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 76 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 77 | IdentifierInfo **ProtocolNames, unsigned NumProtocols, |
| 78 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
| 79 | assert(ClassName && "Missing class identifier"); |
| 80 | |
| 81 | // Check for another declaration kind with the same name. |
| 82 | ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 83 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 84 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 85 | ClassName->getName()); |
| 86 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 87 | } |
| 88 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 89 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 90 | if (IDecl) { |
| 91 | // Class already seen. Is it a forward declaration? |
| 92 | if (!IDecl->isForwardDecl()) |
| 93 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName()); |
| 94 | else { |
| 95 | IDecl->setLocation(AtInterfaceLoc); |
| 96 | IDecl->setForwardDecl(false); |
| 97 | IDecl->AllocIntfRefProtocols(NumProtocols); |
| 98 | } |
| 99 | } |
| 100 | else { |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 101 | IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols, |
| 102 | ClassName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 103 | |
| 104 | // Chain & install the interface decl into the identifier. |
| 105 | IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>()); |
| 106 | ClassName->setFETokenInfo(IDecl); |
| 107 | |
| 108 | // Remember that this needs to be removed when the scope is popped. |
| 109 | TUScope->AddDecl(IDecl); |
| 110 | } |
| 111 | |
| 112 | if (SuperName) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 113 | ObjCInterfaceDecl* SuperClassEntry = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 114 | // Check if a different kind of symbol declared in this scope. |
| 115 | PrevDecl = LookupInterfaceDecl(SuperName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 116 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 117 | Diag(SuperLoc, diag::err_redefinition_different_kind, |
| 118 | SuperName->getName()); |
| 119 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 120 | } |
| 121 | else { |
| 122 | // Check that super class is previously defined |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 123 | SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 124 | |
| 125 | if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) { |
| 126 | Diag(AtInterfaceLoc, diag::err_undef_superclass, |
| 127 | SuperClassEntry ? SuperClassEntry->getName() |
| 128 | : SuperName->getName(), |
| 129 | ClassName->getName()); |
| 130 | } |
| 131 | } |
| 132 | IDecl->setSuperClass(SuperClassEntry); |
| 133 | IDecl->setLocEnd(SuperLoc); |
| 134 | } else { // we have a root class. |
| 135 | IDecl->setLocEnd(ClassLoc); |
| 136 | } |
| 137 | |
| 138 | /// Check then save referenced protocols |
| 139 | if (NumProtocols) { |
| 140 | for (unsigned int i = 0; i != NumProtocols; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 141 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 142 | if (!RefPDecl || RefPDecl->isForwardDecl()) |
| 143 | Diag(ClassLoc, diag::warn_undef_protocolref, |
| 144 | ProtocolNames[i]->getName(), |
| 145 | ClassName->getName()); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 146 | IDecl->setIntfRefProtocols(i, RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 147 | } |
| 148 | IDecl->setLocEnd(EndProtoLoc); |
| 149 | } |
| 150 | return IDecl; |
| 151 | } |
| 152 | |
| 153 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
| 154 | /// @compaatibility_alias declaration. It sets up the alias relationships. |
| 155 | Sema::DeclTy *Sema::ActOnCompatiblityAlias( |
| 156 | SourceLocation AtCompatibilityAliasLoc, |
| 157 | IdentifierInfo *AliasName, SourceLocation AliasLocation, |
| 158 | IdentifierInfo *ClassName, SourceLocation ClassLocation) { |
| 159 | // Look for previous declaration of alias name |
| 160 | ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary, |
| 161 | AliasLocation, TUScope); |
| 162 | if (ADecl) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 163 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 164 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
| 165 | Diag(ADecl->getLocation(), diag::warn_previous_declaration); |
| 166 | } |
| 167 | else { |
| 168 | Diag(AliasLocation, diag::err_conflicting_aliasing_type, |
| 169 | AliasName->getName()); |
| 170 | Diag(ADecl->getLocation(), diag::err_previous_declaration); |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | // Check for class declaration |
| 175 | ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary, |
| 176 | ClassLocation, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 177 | if (!CDecl || !isa<ObjCInterfaceDecl>(CDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 178 | Diag(ClassLocation, diag::warn_undef_interface, |
| 179 | ClassName->getName()); |
| 180 | if (CDecl) |
| 181 | Diag(CDecl->getLocation(), diag::warn_previous_declaration); |
| 182 | return 0; |
| 183 | } |
| 184 | // Everything checked out, instantiate a new alias declaration ast |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 185 | ObjCCompatibleAliasDecl *AliasDecl = |
| 186 | new ObjCCompatibleAliasDecl(AtCompatibilityAliasLoc, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 187 | AliasName, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 188 | dyn_cast<ObjCInterfaceDecl>(CDecl)); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 189 | |
| 190 | // Chain & install the interface decl into the identifier. |
| 191 | AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>()); |
| 192 | AliasName->setFETokenInfo(AliasDecl); |
| 193 | return AliasDecl; |
| 194 | } |
| 195 | |
| 196 | Sema::DeclTy *Sema::ActOnStartProtocolInterface( |
| 197 | SourceLocation AtProtoInterfaceLoc, |
| 198 | IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc, |
| 199 | IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs, |
| 200 | SourceLocation EndProtoLoc) { |
| 201 | assert(ProtocolName && "Missing protocol identifier"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 202 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 203 | if (PDecl) { |
| 204 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 205 | if (!PDecl->isForwardDecl()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 206 | Diag(ProtocolLoc, diag::err_duplicate_protocol_def, |
| 207 | ProtocolName->getName()); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 208 | // Just return the protocol we already had. |
| 209 | // FIXME: don't leak the objects passed in! |
| 210 | return PDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 211 | } |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 212 | |
| 213 | PDecl->setForwardDecl(false); |
| 214 | PDecl->AllocReferencedProtocols(NumProtoRefs); |
| 215 | } else { |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 216 | PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs, |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame^] | 217 | ProtocolName); |
| 218 | PDecl->setForwardDecl(false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 219 | ObjCProtocols[ProtocolName] = PDecl; |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 221 | |
| 222 | if (NumProtoRefs) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame^] | 223 | /// Check then save referenced protocols. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 224 | for (unsigned int i = 0; i != NumProtoRefs; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 225 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 226 | if (!RefPDecl || RefPDecl->isForwardDecl()) |
| 227 | Diag(ProtocolLoc, diag::warn_undef_protocolref, |
| 228 | ProtoRefNames[i]->getName(), |
| 229 | ProtocolName->getName()); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 230 | PDecl->setReferencedProtocols(i, RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 231 | } |
| 232 | PDecl->setLocEnd(EndProtoLoc); |
| 233 | } |
| 234 | return PDecl; |
| 235 | } |
| 236 | |
| 237 | /// FindProtocolDeclaration - This routine looks up protocols and |
| 238 | /// issuer error if they are not declared. It returns list of protocol |
| 239 | /// declarations in its 'Protocols' argument. |
| 240 | void |
| 241 | Sema::FindProtocolDeclaration(SourceLocation TypeLoc, |
| 242 | IdentifierInfo **ProtocolId, |
| 243 | unsigned NumProtocols, |
| 244 | llvm::SmallVector<DeclTy *,8> &Protocols) { |
| 245 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 246 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 247 | if (!PDecl) |
| 248 | Diag(TypeLoc, diag::err_undeclared_protocol, |
| 249 | ProtocolId[i]->getName()); |
| 250 | else |
| 251 | Protocols.push_back(PDecl); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /// ActOnForwardProtocolDeclaration - |
| 256 | Action::DeclTy * |
| 257 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
| 258 | IdentifierInfo **IdentList, unsigned NumElts) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 259 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 260 | |
| 261 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame^] | 262 | IdentifierInfo *Ident = IdentList[i]; |
| 263 | ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; |
| 264 | if (PDecl == 0) { // Not already seen? |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 265 | // FIXME: Pass in the location of the identifier! |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame^] | 266 | PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | Protocols.push_back(PDecl); |
| 270 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 271 | return new ObjCForwardProtocolDecl(AtProtocolLoc, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 272 | &Protocols[0], Protocols.size()); |
| 273 | } |
| 274 | |
| 275 | Sema::DeclTy *Sema::ActOnStartCategoryInterface( |
| 276 | SourceLocation AtInterfaceLoc, |
| 277 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 278 | IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
| 279 | IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs, |
| 280 | SourceLocation EndProtoLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 281 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 283 | ObjCCategoryDecl *CDecl = new ObjCCategoryDecl(AtInterfaceLoc, NumProtoRefs, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 284 | CategoryName); |
| 285 | CDecl->setClassInterface(IDecl); |
Fariborz Jahanian | 7c453b3 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 286 | |
| 287 | /// Check that class of this category is already completely declared. |
| 288 | if (!IDecl || IDecl->isForwardDecl()) |
| 289 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
| 290 | else { |
| 291 | /// Check for duplicate interface declaration for this category |
| 292 | ObjCCategoryDecl *CDeclChain; |
| 293 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 294 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 295 | if (CDeclChain->getIdentifier() == CategoryName) { |
| 296 | Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(), |
| 297 | CategoryName->getName()); |
| 298 | break; |
| 299 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 301 | if (!CDeclChain) |
| 302 | CDecl->insertNextClassCategory(); |
Fariborz Jahanian | 7c453b3 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 303 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 304 | |
| 305 | if (NumProtoRefs) { |
| 306 | /// Check then save referenced protocols |
| 307 | for (unsigned int i = 0; i != NumProtoRefs; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 308 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 309 | if (!RefPDecl || RefPDecl->isForwardDecl()) { |
| 310 | Diag(CategoryLoc, diag::warn_undef_protocolref, |
| 311 | ProtoRefNames[i]->getName(), |
| 312 | CategoryName->getName()); |
| 313 | } |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 314 | CDecl->setCatReferencedProtocols(i, RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 315 | } |
| 316 | CDecl->setLocEnd(EndProtoLoc); |
| 317 | } |
| 318 | return CDecl; |
| 319 | } |
| 320 | |
| 321 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 322 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 323 | /// object. |
| 324 | Sema::DeclTy *Sema::ActOnStartCategoryImplementation( |
| 325 | SourceLocation AtCatImplLoc, |
| 326 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 327 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 328 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
| 329 | ObjCCategoryImplDecl *CDecl = new ObjCCategoryImplDecl(AtCatImplLoc, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 330 | CatName, IDecl); |
| 331 | /// Check that class of this category is already completely declared. |
| 332 | if (!IDecl || IDecl->isForwardDecl()) |
| 333 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
| 334 | |
| 335 | /// TODO: Check that CatName, category name, is not used in another |
| 336 | // implementation. |
| 337 | return CDecl; |
| 338 | } |
| 339 | |
| 340 | Sema::DeclTy *Sema::ActOnStartClassImplementation( |
| 341 | SourceLocation AtClassImplLoc, |
| 342 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 343 | IdentifierInfo *SuperClassname, |
| 344 | SourceLocation SuperClassLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 345 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 346 | // Check for another declaration kind with the same name. |
| 347 | ScopedDecl *PrevDecl = LookupInterfaceDecl(ClassName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 348 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 349 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 350 | ClassName->getName()); |
| 351 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 352 | } |
| 353 | else { |
| 354 | // Is there an interface declaration of this class; if not, warn! |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 355 | IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 356 | if (!IDecl) |
| 357 | Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName()); |
| 358 | } |
| 359 | |
| 360 | // Check that super class name is valid class name |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 361 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 362 | if (SuperClassname) { |
| 363 | // Check if a different kind of symbol declared in this scope. |
| 364 | PrevDecl = LookupInterfaceDecl(SuperClassname); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 365 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 366 | Diag(SuperClassLoc, diag::err_redefinition_different_kind, |
| 367 | SuperClassname->getName()); |
| 368 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 369 | } |
| 370 | else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 371 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 372 | if (!SDecl) |
| 373 | Diag(SuperClassLoc, diag::err_undef_superclass, |
| 374 | SuperClassname->getName(), ClassName->getName()); |
| 375 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 376 | // This implementation and its interface do not have the same |
| 377 | // super class. |
| 378 | Diag(SuperClassLoc, diag::err_conflicting_super_class, |
| 379 | SDecl->getName()); |
| 380 | Diag(SDecl->getLocation(), diag::err_previous_definition); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (!IDecl) { |
| 386 | // Legacy case of @implementation with no corresponding @interface. |
| 387 | // Build, chain & install the interface decl into the identifier. |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 388 | IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName, |
| 389 | false, true); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 390 | IDecl->setNext(ClassName->getFETokenInfo<ScopedDecl>()); |
| 391 | ClassName->setFETokenInfo(IDecl); |
| 392 | IDecl->setSuperClass(SDecl); |
| 393 | IDecl->setLocEnd(ClassLoc); |
| 394 | |
| 395 | // Remember that this needs to be removed when the scope is popped. |
| 396 | TUScope->AddDecl(IDecl); |
| 397 | } |
| 398 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 399 | ObjCImplementationDecl* IMPDecl = |
| 400 | new ObjCImplementationDecl(AtClassImplLoc, ClassName, IDecl, SDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 401 | |
| 402 | // Check that there is no duplicate implementation of this class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 403 | if (ObjCImplementations[ClassName]) |
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. |
| 587 | ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]); |
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); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 597 | // Chain & install the interface decl into the identifier. |
| 598 | IDecl->setNext(IdentList[i]->getFETokenInfo<ScopedDecl>()); |
| 599 | IdentList[i]->setFETokenInfo(IDecl); |
| 600 | |
| 601 | // Remember that this needs to be removed when the scope is popped. |
| 602 | TUScope->AddDecl(IDecl); |
| 603 | } |
| 604 | |
| 605 | Interfaces.push_back(IDecl); |
| 606 | } |
| 607 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 608 | return new ObjCClassDecl(AtClassLoc, &Interfaces[0], Interfaces.size()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
| 612 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 613 | /// returns true, or false, accordingly. |
| 614 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 615 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
| 616 | const ObjCMethodDecl *PrevMethod) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 617 | if (Method->getResultType().getCanonicalType() != |
| 618 | PrevMethod->getResultType().getCanonicalType()) |
| 619 | return false; |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 620 | for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 621 | ParmVarDecl *ParamDecl = Method->getParamDecl(i); |
| 622 | ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i); |
| 623 | if (ParamDecl->getCanonicalType() != PrevParamDecl->getCanonicalType()) |
| 624 | return false; |
| 625 | } |
| 626 | return true; |
| 627 | } |
| 628 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 629 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 630 | ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 631 | if (!FirstMethod.Method) { |
| 632 | // Haven't seen a method with this selector name yet - add it. |
| 633 | FirstMethod.Method = Method; |
| 634 | FirstMethod.Next = 0; |
| 635 | } else { |
| 636 | // We've seen a method with this name, now check the type signature(s). |
| 637 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 638 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 639 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 640 | Next = Next->Next) |
| 641 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 642 | |
| 643 | if (!match) { |
| 644 | // We have a new signature for an existing method - add it. |
| 645 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 646 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 647 | FirstMethod.Next = OMI; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 652 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 653 | ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 654 | if (!FirstMethod.Method) { |
| 655 | // Haven't seen a method with this selector name yet - add it. |
| 656 | FirstMethod.Method = Method; |
| 657 | FirstMethod.Next = 0; |
| 658 | } else { |
| 659 | // We've seen a method with this name, now check the type signature(s). |
| 660 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 661 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 662 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 663 | Next = Next->Next) |
| 664 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 665 | |
| 666 | if (!match) { |
| 667 | // We have a new signature for an existing method - add it. |
| 668 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 669 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 670 | FirstMethod.Next = OMI; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 675 | // Note: For class/category implemenations, allMethods/allProperties is |
| 676 | // always null. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 677 | void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, |
| 678 | DeclTy **allMethods, unsigned allNum, |
| 679 | DeclTy **allProperties, unsigned pNum) { |
| 680 | Decl *ClassDecl = static_cast<Decl *>(classDecl); |
| 681 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 682 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 683 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 684 | // should be true. |
| 685 | if (!ClassDecl) |
| 686 | return; |
| 687 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 688 | llvm::SmallVector<ObjCMethodDecl*, 32> insMethods; |
| 689 | llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 690 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 691 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 692 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 693 | |
| 694 | bool isInterfaceDeclKind = |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 695 | (isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 696 | || isa<ObjCProtocolDecl>(ClassDecl)); |
| 697 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 698 | |
| 699 | // TODO: property declaration in category and protocols. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 700 | if (pNum != 0 && isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 701 | ObjCPropertyDecl **properties = new ObjCPropertyDecl*[pNum]; |
| 702 | memcpy(properties, allProperties, pNum*sizeof(ObjCPropertyDecl*)); |
| 703 | dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setPropertyDecls(properties); |
| 704 | dyn_cast<ObjCInterfaceDecl>(ClassDecl)->setNumPropertyDecl(pNum); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 708 | ObjCMethodDecl *Method = |
| 709 | cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 710 | |
| 711 | if (!Method) continue; // Already issued a diagnostic. |
| 712 | if (Method->isInstance()) { |
| 713 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 714 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 715 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 716 | : false; |
| 717 | if (isInterfaceDeclKind && PrevMethod && !match |
| 718 | || checkIdenticalMethods && match) { |
| 719 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 720 | Method->getSelector().getName()); |
| 721 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 722 | } else { |
| 723 | insMethods.push_back(Method); |
| 724 | InsMap[Method->getSelector()] = Method; |
| 725 | /// The following allows us to typecheck messages to "id". |
| 726 | AddInstanceMethodToGlobalPool(Method); |
| 727 | } |
| 728 | } |
| 729 | else { |
| 730 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 731 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 732 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 733 | : false; |
| 734 | if (isInterfaceDeclKind && PrevMethod && !match |
| 735 | || checkIdenticalMethods && match) { |
| 736 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 737 | Method->getSelector().getName()); |
| 738 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 739 | } else { |
| 740 | clsMethods.push_back(Method); |
| 741 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 742 | /// The following allows us to typecheck messages to "Class". |
| 743 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 748 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 749 | I->addMethods(&insMethods[0], insMethods.size(), |
| 750 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 751 | } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 752 | P->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 (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 756 | C->addMethods(&insMethods[0], insMethods.size(), |
| 757 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 758 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 759 | else if (ObjCImplementationDecl *IC = |
| 760 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 761 | IC->setLocEnd(AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 762 | if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 763 | ImplMethodsVsClassMethods(IC, IDecl); |
| 764 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 765 | ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 766 | CatImplClass->setLocEnd(AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 767 | ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 768 | // Find category interface decl and then check that all methods declared |
| 769 | // in this interface is implemented in the category @implementation. |
| 770 | if (IDecl) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 771 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 772 | Categories; Categories = Categories->getNextClassCategory()) { |
| 773 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
| 774 | ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | |
| 783 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 784 | /// 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] | 785 | static Decl::ObjCDeclQualifier |
| 786 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 787 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 788 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 789 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 790 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 791 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 792 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 793 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 794 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 795 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 796 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 797 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 798 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 799 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 800 | |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | Sema::DeclTy *Sema::ActOnMethodDeclaration( |
| 805 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 806 | tok::TokenKind MethodType, DeclTy *classDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 807 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 808 | Selector Sel, |
| 809 | // optional arguments. The number of types/arguments is obtained |
| 810 | // from the Sel.getNumArgs(). |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 811 | ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 812 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 813 | bool isVariadic) { |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 814 | Decl *ClassDecl = static_cast<Decl*>(classDecl); |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 815 | |
| 816 | // Make sure we can establish a context for the method. |
| 817 | if (!ClassDecl) { |
| 818 | Diag(MethodLoc, diag::error_missing_method_context); |
| 819 | return 0; |
| 820 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 821 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 822 | |
| 823 | for (unsigned i = 0; i < Sel.getNumArgs(); i++) { |
| 824 | // FIXME: arg->AttrList must be stored too! |
| 825 | QualType argType; |
| 826 | |
| 827 | if (ArgTypes[i]) |
| 828 | argType = QualType::getFromOpaquePtr(ArgTypes[i]); |
| 829 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 830 | argType = Context.getObjCIdType(); |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 831 | ParmVarDecl* Param = ParmVarDecl::Create(Context, SourceLocation(/*FIXME*/), |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 832 | ArgNames[i], argType, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 833 | VarDecl::None, 0); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 834 | Param->setObjCDeclQualifier( |
| 835 | CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 836 | Params.push_back(Param); |
| 837 | } |
| 838 | QualType resultDeclType; |
| 839 | |
| 840 | if (ReturnType) |
| 841 | resultDeclType = QualType::getFromOpaquePtr(ReturnType); |
| 842 | else // get the type for "id". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 843 | resultDeclType = Context.getObjCIdType(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 845 | ObjCMethodDecl* ObjCMethod = |
| 846 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Chris Lattner | b06fa3b | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 847 | ClassDecl, AttrList, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 848 | MethodType == tok::minus, isVariadic, |
| 849 | MethodDeclKind == tok::objc_optional ? |
| 850 | ObjCMethodDecl::Optional : |
| 851 | ObjCMethodDecl::Required); |
| 852 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 853 | ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); |
| 854 | ObjCMethod->setObjCDeclQualifier( |
| 855 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 856 | const ObjCMethodDecl *PrevMethod = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 857 | |
| 858 | // For implementations (which can be very "coarse grain"), we add the |
| 859 | // method now. This allows the AST to implement lookup methods that work |
| 860 | // incrementally (without waiting until we parse the @end). It also allows |
| 861 | // us to flag multiple declaration errors as they occur. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 862 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 863 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 864 | if (MethodType == tok::minus) { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 865 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 866 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 867 | } else { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 868 | PrevMethod = ImpDecl->getClassMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 869 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 872 | else if (ObjCCategoryImplDecl *CatImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 873 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 874 | if (MethodType == tok::minus) { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 875 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 876 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 877 | } else { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 878 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 879 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | if (PrevMethod) { |
| 883 | // You can never have two method definitions with the same name. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 884 | Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl, |
| 885 | ObjCMethod->getSelector().getName()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 886 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 887 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 888 | return ObjCMethod; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 891 | Sema::DeclTy *Sema::ActOnAddObjCProperties(SourceLocation AtLoc, |
| 892 | DeclTy **allProperties, unsigned NumProperties, ObjCDeclSpec &DS) { |
| 893 | ObjCPropertyDecl *PDecl = new ObjCPropertyDecl(AtLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 894 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 895 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly) |
| 896 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 897 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 898 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) { |
| 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 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 903 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) { |
| 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 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 908 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign) |
| 909 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 910 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 911 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite) |
| 912 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 913 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 914 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain) |
| 915 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 916 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 917 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy) |
| 918 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 919 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 920 | if(DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic) |
| 921 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 922 | |
| 923 | PDecl->setNumPropertyDecls(NumProperties); |
| 924 | if (NumProperties != 0) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 925 | ObjCIvarDecl **properties = new ObjCIvarDecl*[NumProperties]; |
| 926 | memcpy(properties, allProperties, NumProperties*sizeof(ObjCIvarDecl*)); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 927 | PDecl->setPropertyDecls(properties); |
| 928 | } |
| 929 | return PDecl; |
| 930 | } |
| 931 | |