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