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