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