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