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; |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 36 | PushDeclContext(MDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 37 | |
| 38 | // Create Decl objects for each parameter, entrring them in the scope for |
| 39 | // binding to their use. |
| 40 | struct DeclaratorChunk::ParamInfo PI; |
| 41 | |
| 42 | // Insert the invisible arguments, self and _cmd! |
| 43 | PI.Ident = &Context.Idents.get("self"); |
| 44 | PI.IdentLoc = SourceLocation(); // synthesized vars have a null location. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 45 | QualType selfTy = Context.getObjCIdType(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 46 | if (MDecl->isInstance()) { |
Gabor Greif | 3003753 | 2008-02-29 20:35:55 +0000 | [diff] [blame] | 47 | if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) { |
| 48 | // There may be no interface context due to error in declaration of the |
| 49 | // interface (which has been reported). Recover gracefully |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 50 | selfTy = Context.getObjCInterfaceType(OID); |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 51 | selfTy = Context.getPointerType(selfTy); |
Fariborz Jahanian | 20552d2 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 52 | } |
Gabor Greif | 3003753 | 2008-02-29 20:35:55 +0000 | [diff] [blame] | 53 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 54 | CurMethodDecl->setSelfDecl(CreateImplicitParameter(FnBodyScope, PI.Ident, |
| 55 | PI.IdentLoc, selfTy)); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 56 | |
| 57 | PI.Ident = &Context.Idents.get("_cmd"); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 58 | CreateImplicitParameter(FnBodyScope, PI.Ident, PI.IdentLoc, |
| 59 | Context.getObjCSelType()); |
| 60 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 61 | // Introduce all of the other parameters into this scope. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 62 | for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 63 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 64 | IdentifierInfo *II = PDecl->getIdentifier(); |
Argyrios Kyrtzidis | 642e38b | 2008-04-27 13:30:35 +0000 | [diff] [blame] | 65 | if (II) |
| 66 | PushOnScopeChains(PDecl, FnBodyScope); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | Sema::DeclTy *Sema::ActOnStartClassInterface( |
| 71 | SourceLocation AtInterfaceLoc, |
| 72 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 73 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 74 | IdentifierInfo **ProtocolNames, unsigned NumProtocols, |
| 75 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
| 76 | assert(ClassName && "Missing class identifier"); |
| 77 | |
| 78 | // Check for another declaration kind with the same name. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 79 | Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 80 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 81 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 82 | ClassName->getName()); |
| 83 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 84 | } |
| 85 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 86 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 87 | if (IDecl) { |
| 88 | // Class already seen. Is it a forward declaration? |
| 89 | if (!IDecl->isForwardDecl()) |
| 90 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName()); |
| 91 | else { |
| 92 | IDecl->setLocation(AtInterfaceLoc); |
| 93 | IDecl->setForwardDecl(false); |
| 94 | IDecl->AllocIntfRefProtocols(NumProtocols); |
| 95 | } |
| 96 | } |
| 97 | else { |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 98 | IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols, |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 99 | ClassName, ClassLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 100 | |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 101 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 102 | // Remember that this needs to be removed when the scope is popped. |
| 103 | TUScope->AddDecl(IDecl); |
| 104 | } |
| 105 | |
| 106 | if (SuperName) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 107 | ObjCInterfaceDecl* SuperClassEntry = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 108 | // Check if a different kind of symbol declared in this scope. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 109 | PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 110 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 111 | Diag(SuperLoc, diag::err_redefinition_different_kind, |
| 112 | SuperName->getName()); |
| 113 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 114 | } |
| 115 | else { |
| 116 | // Check that super class is previously defined |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 117 | SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 118 | |
| 119 | if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) { |
| 120 | Diag(AtInterfaceLoc, diag::err_undef_superclass, |
| 121 | SuperClassEntry ? SuperClassEntry->getName() |
| 122 | : SuperName->getName(), |
| 123 | ClassName->getName()); |
| 124 | } |
| 125 | } |
| 126 | IDecl->setSuperClass(SuperClassEntry); |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 127 | IDecl->setSuperClassLoc(SuperLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 128 | IDecl->setLocEnd(SuperLoc); |
| 129 | } else { // we have a root class. |
| 130 | IDecl->setLocEnd(ClassLoc); |
| 131 | } |
| 132 | |
| 133 | /// Check then save referenced protocols |
| 134 | if (NumProtocols) { |
| 135 | for (unsigned int i = 0; i != NumProtocols; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 136 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 137 | if (!RefPDecl || RefPDecl->isForwardDecl()) |
| 138 | Diag(ClassLoc, diag::warn_undef_protocolref, |
| 139 | ProtocolNames[i]->getName(), |
| 140 | ClassName->getName()); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 141 | IDecl->setIntfRefProtocols(i, RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 142 | } |
| 143 | IDecl->setLocEnd(EndProtoLoc); |
| 144 | } |
| 145 | return IDecl; |
| 146 | } |
| 147 | |
| 148 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
| 149 | /// @compaatibility_alias declaration. It sets up the alias relationships. |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 150 | Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
| 151 | IdentifierInfo *AliasName, |
| 152 | SourceLocation AliasLocation, |
| 153 | IdentifierInfo *ClassName, |
| 154 | SourceLocation ClassLocation) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 155 | // Look for previous declaration of alias name |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 156 | Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 157 | if (ADecl) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 158 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 159 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
| 160 | Diag(ADecl->getLocation(), diag::warn_previous_declaration); |
| 161 | } |
| 162 | else { |
| 163 | Diag(AliasLocation, diag::err_conflicting_aliasing_type, |
| 164 | AliasName->getName()); |
| 165 | Diag(ADecl->getLocation(), diag::err_previous_declaration); |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | // Check for class declaration |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 170 | Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 171 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 172 | if (CDecl == 0) { |
| 173 | Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName()); |
| 174 | if (CDeclU) |
| 175 | Diag(CDeclU->getLocation(), diag::warn_previous_declaration); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | } |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 178 | |
| 179 | // Everything checked out, instantiate a new alias declaration AST. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 180 | ObjCCompatibleAliasDecl *AliasDecl = |
Steve Naroff | e8043c3 | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 181 | ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl); |
| 182 | |
| 183 | ObjCAliasDecls[AliasName] = AliasDecl; |
| 184 | TUScope->AddDecl(AliasDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 185 | return AliasDecl; |
| 186 | } |
| 187 | |
| 188 | Sema::DeclTy *Sema::ActOnStartProtocolInterface( |
| 189 | SourceLocation AtProtoInterfaceLoc, |
| 190 | IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc, |
| 191 | IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs, |
| 192 | SourceLocation EndProtoLoc) { |
| 193 | assert(ProtocolName && "Missing protocol identifier"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 194 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 195 | if (PDecl) { |
| 196 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 197 | if (!PDecl->isForwardDecl()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 198 | Diag(ProtocolLoc, diag::err_duplicate_protocol_def, |
| 199 | ProtocolName->getName()); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 200 | // Just return the protocol we already had. |
| 201 | // FIXME: don't leak the objects passed in! |
| 202 | return PDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 204 | |
| 205 | PDecl->setForwardDecl(false); |
| 206 | PDecl->AllocReferencedProtocols(NumProtoRefs); |
| 207 | } else { |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 208 | PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs, |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 209 | ProtocolName); |
| 210 | PDecl->setForwardDecl(false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 211 | ObjCProtocols[ProtocolName] = PDecl; |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 212 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 213 | |
| 214 | if (NumProtoRefs) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 215 | /// Check then save referenced protocols. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 216 | for (unsigned int i = 0; i != NumProtoRefs; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 217 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 218 | if (!RefPDecl || RefPDecl->isForwardDecl()) |
| 219 | Diag(ProtocolLoc, diag::warn_undef_protocolref, |
| 220 | ProtoRefNames[i]->getName(), |
| 221 | ProtocolName->getName()); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 222 | PDecl->setReferencedProtocols(i, RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 223 | } |
| 224 | PDecl->setLocEnd(EndProtoLoc); |
| 225 | } |
| 226 | return PDecl; |
| 227 | } |
| 228 | |
| 229 | /// FindProtocolDeclaration - This routine looks up protocols and |
| 230 | /// issuer error if they are not declared. It returns list of protocol |
| 231 | /// declarations in its 'Protocols' argument. |
| 232 | void |
| 233 | Sema::FindProtocolDeclaration(SourceLocation TypeLoc, |
| 234 | IdentifierInfo **ProtocolId, |
| 235 | unsigned NumProtocols, |
| 236 | llvm::SmallVector<DeclTy *,8> &Protocols) { |
| 237 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 238 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 239 | if (!PDecl) |
| 240 | Diag(TypeLoc, diag::err_undeclared_protocol, |
| 241 | ProtocolId[i]->getName()); |
| 242 | else |
| 243 | Protocols.push_back(PDecl); |
| 244 | } |
| 245 | } |
| 246 | |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 247 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 248 | /// attributes and types and warns on a variety of inconsistancies. |
| 249 | /// |
| 250 | // TODO: Incomplete. |
| 251 | // |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 252 | void |
| 253 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 254 | ObjCPropertyDecl *SuperProperty, |
| 255 | ObjCInterfaceDecl *SuperIDecl) { |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 256 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 257 | Property->getPropertyAttributes(); |
| 258 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 259 | SuperProperty->getPropertyAttributes(); |
| 260 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 261 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 262 | Diag(Property->getLocation(), diag::warn_readonly_property, |
| 263 | Property->getName(), SuperIDecl->getName()); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 264 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 265 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 266 | Diag(Property->getLocation(), diag::warn_property_attribute, |
| 267 | Property->getName(), "copy", SuperIDecl->getName(), |
| 268 | SourceRange()); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 269 | else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) |
| 270 | != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 271 | Diag(Property->getLocation(), diag::warn_property_attribute, |
| 272 | Property->getName(), "retain", SuperIDecl->getName(), |
| 273 | SourceRange()); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 274 | |
| 275 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 276 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 277 | Diag(Property->getLocation(), diag::warn_property_attribute, |
| 278 | Property->getName(), "atomic", SuperIDecl->getName(), |
| 279 | SourceRange()); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 280 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 281 | Diag(Property->getLocation(), diag::warn_property_attribute, |
| 282 | Property->getName(), "setter", SuperIDecl->getName(), |
| 283 | SourceRange()); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 284 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 285 | Diag(Property->getLocation(), diag::warn_property_attribute, |
| 286 | Property->getName(), "getter", SuperIDecl->getName(), |
| 287 | SourceRange()); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 288 | |
| 289 | if (Property->getCanonicalType() != SuperProperty->getCanonicalType()) { |
| 290 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 291 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readonly)) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 292 | // && objc_compare_types(...)) |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 293 | ; |
| 294 | else |
| 295 | ; // |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 301 | /// declarations in base and its super class, if any, and issues |
| 302 | /// diagnostics in a variety of inconsistant situations. |
| 303 | /// |
| 304 | void |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 305 | Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 306 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 307 | if (!SDecl) |
| 308 | return; |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 309 | for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(), |
| 310 | E = SDecl->classprop_end(); S != E; ++S) { |
| 311 | ObjCPropertyDecl *SuperPDecl = (*S); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 312 | // Does property in super class has declaration in current class? |
| 313 | for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(), |
| 314 | E = IDecl->classprop_end(); I != E; ++I) { |
| 315 | ObjCPropertyDecl *PDecl = (*I); |
| 316 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 317 | DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 322 | /// ActOnForwardProtocolDeclaration - |
| 323 | Action::DeclTy * |
| 324 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
| 325 | IdentifierInfo **IdentList, unsigned NumElts) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 326 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 327 | |
| 328 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 329 | IdentifierInfo *Ident = IdentList[i]; |
| 330 | ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; |
| 331 | if (PDecl == 0) { // Not already seen? |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 332 | // FIXME: Pass in the location of the identifier! |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 333 | PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | Protocols.push_back(PDecl); |
| 337 | } |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 338 | return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc, |
| 339 | &Protocols[0], Protocols.size()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | Sema::DeclTy *Sema::ActOnStartCategoryInterface( |
| 343 | SourceLocation AtInterfaceLoc, |
| 344 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 345 | IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
| 346 | IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs, |
| 347 | SourceLocation EndProtoLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 348 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 350 | ObjCCategoryDecl *CDecl = |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 351 | ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 352 | CDecl->setClassInterface(IDecl); |
Fariborz Jahanian | 7c453b3 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 353 | |
| 354 | /// Check that class of this category is already completely declared. |
| 355 | if (!IDecl || IDecl->isForwardDecl()) |
| 356 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
| 357 | else { |
| 358 | /// Check for duplicate interface declaration for this category |
| 359 | ObjCCategoryDecl *CDeclChain; |
| 360 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 361 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 362 | if (CDeclChain->getIdentifier() == CategoryName) { |
| 363 | Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(), |
| 364 | CategoryName->getName()); |
| 365 | break; |
| 366 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 368 | if (!CDeclChain) |
| 369 | CDecl->insertNextClassCategory(); |
Fariborz Jahanian | 7c453b3 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 371 | |
| 372 | if (NumProtoRefs) { |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 373 | llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols; |
| 374 | /// Check and then save the referenced protocols. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 375 | for (unsigned int i = 0; i != NumProtoRefs; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 376 | ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 377 | if (!RefPDecl || RefPDecl->isForwardDecl()) { |
| 378 | Diag(CategoryLoc, diag::warn_undef_protocolref, |
| 379 | ProtoRefNames[i]->getName(), |
| 380 | CategoryName->getName()); |
| 381 | } |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 382 | if (RefPDecl) |
| 383 | RefProtocols.push_back(RefPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 384 | } |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 385 | if (!RefProtocols.empty()) |
| 386 | CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | 68c82cf | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 388 | CDecl->setLocEnd(EndProtoLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 389 | return CDecl; |
| 390 | } |
| 391 | |
| 392 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 393 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 394 | /// object. |
| 395 | Sema::DeclTy *Sema::ActOnStartCategoryImplementation( |
| 396 | SourceLocation AtCatImplLoc, |
| 397 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 398 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 399 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 400 | ObjCCategoryImplDecl *CDecl = |
| 401 | ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 402 | /// Check that class of this category is already completely declared. |
| 403 | if (!IDecl || IDecl->isForwardDecl()) |
| 404 | Diag(ClassLoc, diag::err_undef_interface, ClassName->getName()); |
| 405 | |
| 406 | /// TODO: Check that CatName, category name, is not used in another |
| 407 | // implementation. |
| 408 | return CDecl; |
| 409 | } |
| 410 | |
| 411 | Sema::DeclTy *Sema::ActOnStartClassImplementation( |
| 412 | SourceLocation AtClassImplLoc, |
| 413 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 414 | IdentifierInfo *SuperClassname, |
| 415 | SourceLocation SuperClassLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 416 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 417 | // Check for another declaration kind with the same name. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 418 | Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 419 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 420 | Diag(ClassLoc, diag::err_redefinition_different_kind, |
| 421 | ClassName->getName()); |
| 422 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 423 | } |
| 424 | else { |
| 425 | // Is there an interface declaration of this class; if not, warn! |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 426 | IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 427 | if (!IDecl) |
| 428 | Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName()); |
| 429 | } |
| 430 | |
| 431 | // Check that super class name is valid class name |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 432 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 433 | if (SuperClassname) { |
| 434 | // Check if a different kind of symbol declared in this scope. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 435 | PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 436 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 437 | Diag(SuperClassLoc, diag::err_redefinition_different_kind, |
| 438 | SuperClassname->getName()); |
| 439 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 440 | } |
| 441 | else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 442 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 443 | if (!SDecl) |
| 444 | Diag(SuperClassLoc, diag::err_undef_superclass, |
| 445 | SuperClassname->getName(), ClassName->getName()); |
| 446 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 447 | // This implementation and its interface do not have the same |
| 448 | // super class. |
| 449 | Diag(SuperClassLoc, diag::err_conflicting_super_class, |
| 450 | SDecl->getName()); |
| 451 | Diag(SDecl->getLocation(), diag::err_previous_definition); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (!IDecl) { |
| 457 | // Legacy case of @implementation with no corresponding @interface. |
| 458 | // Build, chain & install the interface decl into the identifier. |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 459 | IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName, |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 460 | ClassLoc, false, true); |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 461 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 462 | IDecl->setSuperClass(SDecl); |
| 463 | IDecl->setLocEnd(ClassLoc); |
| 464 | |
| 465 | // Remember that this needs to be removed when the scope is popped. |
| 466 | TUScope->AddDecl(IDecl); |
| 467 | } |
| 468 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 469 | ObjCImplementationDecl* IMPDecl = |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 470 | ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, |
| 471 | IDecl, SDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 472 | |
| 473 | // Check that there is no duplicate implementation of this class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 474 | if (ObjCImplementations[ClassName]) |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 475 | // FIXME: Don't leak everything! |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 476 | Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName()); |
| 477 | else // add it to the list. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 478 | ObjCImplementations[ClassName] = IMPDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 479 | return IMPDecl; |
| 480 | } |
| 481 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 482 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 483 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 484 | SourceLocation RBrace) { |
| 485 | assert(ImpDecl && "missing implementation decl"); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 486 | ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 487 | if (!IDecl) |
| 488 | return; |
| 489 | /// Check case of non-existing @interface decl. |
| 490 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 491 | /// Add implementations's ivar to the synthesize class's ivar list. |
| 492 | if (IDecl->ImplicitInterfaceDecl()) { |
| 493 | IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); |
| 494 | return; |
| 495 | } |
| 496 | // If implementation has empty ivar list, just return. |
| 497 | if (numIvars == 0) |
| 498 | return; |
| 499 | |
| 500 | assert(ivars && "missing @implementation ivars"); |
| 501 | |
| 502 | // Check interface's Ivar list against those in the implementation. |
| 503 | // names and types must match. |
| 504 | // |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 505 | unsigned j = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 506 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 507 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 508 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 509 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 510 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 511 | assert (ImplIvar && "missing implementation ivar"); |
| 512 | assert (ClsIvar && "missing class ivar"); |
| 513 | if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) { |
| 514 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type, |
| 515 | ImplIvar->getIdentifier()->getName()); |
| 516 | Diag(ClsIvar->getLocation(), diag::err_previous_definition, |
| 517 | ClsIvar->getIdentifier()->getName()); |
| 518 | } |
| 519 | // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed |
| 520 | // as error. |
| 521 | else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
| 522 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name, |
| 523 | ImplIvar->getIdentifier()->getName()); |
| 524 | Diag(ClsIvar->getLocation(), diag::err_previous_definition, |
| 525 | ClsIvar->getIdentifier()->getName()); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 526 | return; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 527 | } |
| 528 | --numIvars; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 529 | } |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 530 | |
| 531 | if (numIvars > 0) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 532 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 533 | else if (IVI != IVE) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 534 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 537 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
| 538 | bool &IncompleteImpl) { |
| 539 | if (!IncompleteImpl) { |
| 540 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 541 | IncompleteImpl = true; |
| 542 | } |
| 543 | Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName()); |
| 544 | } |
| 545 | |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 546 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 547 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 548 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 549 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 550 | bool& IncompleteImpl, |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 551 | const llvm::DenseSet<Selector> &InsMap, |
| 552 | const llvm::DenseSet<Selector> &ClsMap) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 553 | // check unimplemented instance methods. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 554 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 555 | E = PDecl->instmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 556 | ObjCMethodDecl *method = *I; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 557 | if (!InsMap.count(method->getSelector()) && |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 558 | method->getImplementationControl() != ObjCMethodDecl::Optional) |
| 559 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 560 | } |
| 561 | // check unimplemented class methods |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 562 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 563 | E = PDecl->classmeth_end(); I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 564 | ObjCMethodDecl *method = *I; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 565 | if (!ClsMap.count(method->getSelector()) && |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 566 | method->getImplementationControl() != ObjCMethodDecl::Optional) |
| 567 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 568 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 569 | // Check on this protocols's referenced protocols, recursively |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 570 | ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols(); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 571 | for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++) |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 572 | CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 575 | void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, |
| 576 | ObjCInterfaceDecl* IDecl) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 577 | llvm::DenseSet<Selector> InsMap; |
| 578 | // Check and see if instance methods in class interface have been |
| 579 | // implemented in the implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 580 | for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 581 | E = IMPDecl->instmeth_end(); I != E; ++I) |
| 582 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 583 | |
| 584 | bool IncompleteImpl = false; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 585 | for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 586 | E = IDecl->instmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 587 | if (!InsMap.count((*I)->getSelector())) |
| 588 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 590 | llvm::DenseSet<Selector> ClsMap; |
| 591 | // Check and see if class methods in class interface have been |
| 592 | // implemented in the implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 593 | for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 594 | E = IMPDecl->classmeth_end(); I != E; ++I) |
| 595 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 597 | for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 598 | E = IDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 599 | if (!ClsMap.count((*I)->getSelector())) |
| 600 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 601 | |
| 602 | // Check the protocol list for unimplemented methods in the @implementation |
| 603 | // class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 604 | ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols(); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 605 | for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 606 | CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i], |
| 607 | IncompleteImpl, InsMap, ClsMap); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the |
| 611 | /// category interface is implemented in the category @implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 612 | void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl, |
| 613 | ObjCCategoryDecl *CatClassDecl) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 614 | llvm::DenseSet<Selector> InsMap; |
| 615 | // Check and see if instance methods in category interface have been |
| 616 | // implemented in its implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 617 | for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(), |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 618 | E = CatImplDecl->instmeth_end(); I != E; ++I) |
| 619 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 620 | |
| 621 | bool IncompleteImpl = false; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 622 | for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 623 | E = CatClassDecl->instmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 624 | if (!InsMap.count((*I)->getSelector())) |
| 625 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
| 626 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 627 | llvm::DenseSet<Selector> ClsMap; |
| 628 | // Check and see if class methods in category interface have been |
| 629 | // implemented in its implementation class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 630 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 631 | I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end(); |
| 632 | I != E; ++I) |
| 633 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 635 | for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 636 | E = CatClassDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 637 | if (!ClsMap.count((*I)->getSelector())) |
| 638 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 639 | |
| 640 | // Check the protocol list for unimplemented methods in the @implementation |
| 641 | // class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 642 | ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols(); |
Fariborz Jahanian | c395bda | 2007-12-20 19:24:10 +0000 | [diff] [blame] | 643 | for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 644 | ObjCProtocolDecl* PDecl = protocols[i]; |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 645 | CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl, |
| 646 | InsMap, ClsMap); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 647 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /// ActOnForwardClassDeclaration - |
| 651 | Action::DeclTy * |
| 652 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
| 653 | IdentifierInfo **IdentList, unsigned NumElts) |
| 654 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 655 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 656 | |
| 657 | for (unsigned i = 0; i != NumElts; ++i) { |
| 658 | // Check for another declaration kind with the same name. |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 659 | Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 660 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 661 | Diag(AtClassLoc, diag::err_redefinition_different_kind, |
| 662 | IdentList[i]->getName()); |
| 663 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 664 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 665 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 666 | if (!IDecl) { // Not already seen? Make a forward decl. |
Chris Lattner | 0e77ba0 | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 667 | IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i], |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 668 | SourceLocation(), true); |
Steve Naroff | 3110251 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 669 | ObjCInterfaceDecls[IdentList[i]] = IDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 670 | |
| 671 | // Remember that this needs to be removed when the scope is popped. |
| 672 | TUScope->AddDecl(IDecl); |
| 673 | } |
| 674 | |
| 675 | Interfaces.push_back(IDecl); |
| 676 | } |
| 677 | |
Chris Lattner | 61f9d41 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 678 | return ObjCClassDecl::Create(Context, AtClassLoc, |
| 679 | &Interfaces[0], Interfaces.size()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
| 683 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 684 | /// returns true, or false, accordingly. |
| 685 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 686 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
| 687 | const ObjCMethodDecl *PrevMethod) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 688 | if (Method->getResultType().getCanonicalType() != |
| 689 | PrevMethod->getResultType().getCanonicalType()) |
| 690 | return false; |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 691 | for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 692 | ParmVarDecl *ParamDecl = Method->getParamDecl(i); |
| 693 | ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i); |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 694 | if (Context.getCanonicalType(ParamDecl->getType()) != |
| 695 | Context.getCanonicalType(PrevParamDecl->getType())) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 696 | return false; |
| 697 | } |
| 698 | return true; |
| 699 | } |
| 700 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 701 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 702 | ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 703 | if (!FirstMethod.Method) { |
| 704 | // Haven't seen a method with this selector name yet - add it. |
| 705 | FirstMethod.Method = Method; |
| 706 | FirstMethod.Next = 0; |
| 707 | } else { |
| 708 | // We've seen a method with this name, now check the type signature(s). |
| 709 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 710 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 711 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 712 | Next = Next->Next) |
| 713 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 714 | |
| 715 | if (!match) { |
| 716 | // We have a new signature for an existing method - add it. |
| 717 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 718 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 719 | FirstMethod.Next = OMI; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 724 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 725 | ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 726 | if (!FirstMethod.Method) { |
| 727 | // Haven't seen a method with this selector name yet - add it. |
| 728 | FirstMethod.Method = Method; |
| 729 | FirstMethod.Next = 0; |
| 730 | } else { |
| 731 | // We've seen a method with this name, now check the type signature(s). |
| 732 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 733 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 734 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 735 | Next = Next->Next) |
| 736 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 737 | |
| 738 | if (!match) { |
| 739 | // We have a new signature for an existing method - add it. |
| 740 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 741 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 742 | FirstMethod.Next = OMI; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 747 | // Note: For class/category implemenations, allMethods/allProperties is |
| 748 | // always null. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 749 | void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, |
| 750 | DeclTy **allMethods, unsigned allNum, |
| 751 | DeclTy **allProperties, unsigned pNum) { |
| 752 | Decl *ClassDecl = static_cast<Decl *>(classDecl); |
| 753 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 754 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 755 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 756 | // should be true. |
| 757 | if (!ClassDecl) |
| 758 | return; |
| 759 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 760 | llvm::SmallVector<ObjCMethodDecl*, 32> insMethods; |
| 761 | llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 762 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 763 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 764 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 765 | |
| 766 | bool isInterfaceDeclKind = |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 767 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 768 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 769 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 770 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 771 | if (pNum != 0) |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 772 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) |
| 773 | IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 774 | else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 775 | CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); |
| 776 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl)) |
| 777 | PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum); |
Fariborz Jahanian | 7e7e387 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 778 | else |
Fariborz Jahanian | 3dd4ba4 | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 779 | assert(false && "ActOnAtEnd - property declaration misplaced"); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 780 | |
| 781 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 782 | ObjCMethodDecl *Method = |
| 783 | cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 784 | |
| 785 | if (!Method) continue; // Already issued a diagnostic. |
| 786 | if (Method->isInstance()) { |
| 787 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 788 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 789 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 790 | : false; |
| 791 | if (isInterfaceDeclKind && PrevMethod && !match |
| 792 | || checkIdenticalMethods && match) { |
| 793 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 794 | Method->getSelector().getName()); |
| 795 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 796 | } else { |
| 797 | insMethods.push_back(Method); |
| 798 | InsMap[Method->getSelector()] = Method; |
| 799 | /// The following allows us to typecheck messages to "id". |
| 800 | AddInstanceMethodToGlobalPool(Method); |
| 801 | } |
| 802 | } |
| 803 | else { |
| 804 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 805 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 806 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 807 | : false; |
| 808 | if (isInterfaceDeclKind && PrevMethod && !match |
| 809 | || checkIdenticalMethods && match) { |
| 810 | Diag(Method->getLocation(), diag::error_duplicate_method_decl, |
| 811 | Method->getSelector().getName()); |
| 812 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 813 | } else { |
| 814 | clsMethods.push_back(Method); |
| 815 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 816 | /// The following allows us to typecheck messages to "Class". |
| 817 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 822 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 823 | I->addMethods(&insMethods[0], insMethods.size(), |
| 824 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame^] | 825 | // Compares properties declaraed in this class to those of its |
| 826 | // super class. |
| 827 | ComparePropertiesInBaseAndSuper (I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 828 | } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 829 | P->addMethods(&insMethods[0], insMethods.size(), |
| 830 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 831 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 832 | else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 833 | C->addMethods(&insMethods[0], insMethods.size(), |
| 834 | &clsMethods[0], clsMethods.size(), AtEndLoc); |
| 835 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 836 | else if (ObjCImplementationDecl *IC = |
| 837 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 838 | IC->setLocEnd(AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 839 | if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 840 | ImplMethodsVsClassMethods(IC, IDecl); |
| 841 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 842 | ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 843 | CatImplClass->setLocEnd(AtEndLoc); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 844 | ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 845 | // Find category interface decl and then check that all methods declared |
| 846 | // in this interface is implemented in the category @implementation. |
| 847 | if (IDecl) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 848 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 849 | Categories; Categories = Categories->getNextClassCategory()) { |
| 850 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
| 851 | ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); |
| 852 | break; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | |
| 860 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 861 | /// 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] | 862 | static Decl::ObjCDeclQualifier |
| 863 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 864 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 865 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 866 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 867 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 868 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 869 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 870 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 871 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 872 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 873 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 874 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 875 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 876 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | Sema::DeclTy *Sema::ActOnMethodDeclaration( |
| 882 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 883 | tok::TokenKind MethodType, DeclTy *classDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 884 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 885 | Selector Sel, |
| 886 | // optional arguments. The number of types/arguments is obtained |
| 887 | // from the Sel.getNumArgs(). |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 888 | ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 889 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 890 | bool isVariadic) { |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 891 | Decl *ClassDecl = static_cast<Decl*>(classDecl); |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 892 | |
| 893 | // Make sure we can establish a context for the method. |
| 894 | if (!ClassDecl) { |
| 895 | Diag(MethodLoc, diag::error_missing_method_context); |
| 896 | return 0; |
| 897 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 898 | QualType resultDeclType; |
| 899 | |
| 900 | if (ReturnType) |
| 901 | resultDeclType = QualType::getFromOpaquePtr(ReturnType); |
| 902 | else // get the type for "id". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 903 | resultDeclType = Context.getObjCIdType(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 904 | |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 905 | ObjCMethodDecl* ObjCMethod = |
| 906 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Chris Lattner | b06fa3b | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 907 | ClassDecl, AttrList, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 908 | MethodType == tok::minus, isVariadic, |
| 909 | MethodDeclKind == tok::objc_optional ? |
| 910 | ObjCMethodDecl::Optional : |
| 911 | ObjCMethodDecl::Required); |
| 912 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 913 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 914 | |
| 915 | for (unsigned i = 0; i < Sel.getNumArgs(); i++) { |
| 916 | // FIXME: arg->AttrList must be stored too! |
| 917 | QualType argType; |
| 918 | |
| 919 | if (ArgTypes[i]) |
| 920 | argType = QualType::getFromOpaquePtr(ArgTypes[i]); |
| 921 | else |
| 922 | argType = Context.getObjCIdType(); |
| 923 | ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod, |
| 924 | SourceLocation(/*FIXME*/), |
| 925 | ArgNames[i], argType, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 926 | VarDecl::None, 0, 0); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 927 | Param->setObjCDeclQualifier( |
| 928 | CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); |
| 929 | Params.push_back(Param); |
| 930 | } |
| 931 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 932 | ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); |
| 933 | ObjCMethod->setObjCDeclQualifier( |
| 934 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 935 | const ObjCMethodDecl *PrevMethod = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 936 | |
| 937 | // For implementations (which can be very "coarse grain"), we add the |
| 938 | // method now. This allows the AST to implement lookup methods that work |
| 939 | // incrementally (without waiting until we parse the @end). It also allows |
| 940 | // us to flag multiple declaration errors as they occur. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 941 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 942 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 943 | if (MethodType == tok::minus) { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 944 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 945 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 946 | } else { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 947 | PrevMethod = ImpDecl->getClassMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 948 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 949 | } |
| 950 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 951 | else if (ObjCCategoryImplDecl *CatImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 952 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 953 | if (MethodType == tok::minus) { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 954 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 955 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 956 | } else { |
Steve Naroff | 94a5c33 | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 957 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 958 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | if (PrevMethod) { |
| 962 | // You can never have two method definitions with the same name. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 963 | Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl, |
| 964 | ObjCMethod->getSelector().getName()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 965 | Diag(PrevMethod->getLocation(), diag::err_previous_declaration); |
| 966 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 967 | return ObjCMethod; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 970 | Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
| 971 | FieldDeclarator &FD, |
| 972 | ObjCDeclSpec &ODS) { |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 973 | QualType T = GetTypeForDeclarator(FD.D, S); |
Fariborz Jahanian | 1de1e74 | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 974 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc, |
| 975 | FD.D.getIdentifier(), T); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 976 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 977 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 978 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 979 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 980 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 981 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 982 | PDecl->setGetterName(ODS.getGetterName()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 985 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 986 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 987 | PDecl->setSetterName(ODS.getSetterName()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 990 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 991 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 992 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 993 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 994 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 995 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 996 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 997 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 998 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 999 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1000 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1001 | |
Fariborz Jahanian | dae1a1a | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 1002 | if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1003 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1005 | return PDecl; |
| 1006 | } |
| 1007 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1008 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 1009 | /// builds the AST node for a property implementation declaration; declared |
| 1010 | /// as @synthesize or @dynamic. |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1011 | /// |
| 1012 | Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, |
| 1013 | SourceLocation PropertyLoc, |
| 1014 | bool Synthesize, |
| 1015 | DeclTy *ClassCatImpDecl, |
| 1016 | IdentifierInfo *PropertyId, |
| 1017 | IdentifierInfo *PropertyIvar) { |
| 1018 | Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl); |
| 1019 | // Make sure we have a context for the property implementation declaration. |
| 1020 | if (!ClassImpDecl) { |
| 1021 | Diag(AtLoc, diag::error_missing_property_context); |
| 1022 | return 0; |
| 1023 | } |
| 1024 | ObjCPropertyDecl *property = 0; |
| 1025 | ObjCInterfaceDecl* IDecl = 0; |
| 1026 | // Find the class or category class where this property must have |
| 1027 | // a declaration. |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1028 | ObjCImplementationDecl *IC = 0; |
| 1029 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 1030 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1031 | IDecl = getObjCInterfaceDecl(IC->getIdentifier()); |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1032 | // We always synthesize an interface for an implementation |
| 1033 | // without an interface decl. So, IDecl is always non-zero. |
| 1034 | assert(IDecl && |
| 1035 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 1036 | |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1037 | // Look for this property declaration in the @implementation's @interface |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1038 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 1039 | if (!property) { |
| 1040 | Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName()); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1041 | return 0; |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1042 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1043 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1044 | else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1045 | if (Synthesize) { |
| 1046 | Diag(AtLoc, diag::error_synthesize_category_decl); |
| 1047 | return 0; |
| 1048 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1049 | IDecl = CatImplClass->getClassInterface(); |
| 1050 | if (!IDecl) { |
| 1051 | Diag(AtLoc, diag::error_missing_property_interface); |
| 1052 | return 0; |
| 1053 | } |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1054 | ObjCCategoryDecl *Category = |
| 1055 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 1056 | |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1057 | // If category for this implementation not found, it is an error which |
| 1058 | // has already been reported eralier. |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1059 | if (!Category) |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1060 | return 0; |
| 1061 | // Look for this property declaration in @implementation's category |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1062 | property = Category->FindPropertyDeclaration(PropertyId); |
| 1063 | if (!property) { |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1064 | Diag(PropertyLoc, diag::error_bad_category_property_decl, |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1065 | Category->getName()); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | } |
| 1069 | else { |
| 1070 | Diag(AtLoc, diag::error_bad_property_context); |
| 1071 | return 0; |
| 1072 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1073 | ObjCIvarDecl *Ivar = 0; |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1074 | // Check that we have a valid, previously declared ivar for @synthesize |
| 1075 | if (Synthesize) { |
| 1076 | // @synthesize |
Fariborz Jahanian | 6cdf16d | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 1077 | if (!PropertyIvar) |
| 1078 | PropertyIvar = PropertyId; |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1079 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1080 | Ivar = IDecl->FindIvarDeclaration(PropertyIvar); |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1081 | if (!Ivar) { |
Fariborz Jahanian | 6cdf16d | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 1082 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl, |
| 1083 | PropertyId->getName()); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1086 | // Check that type of property and its ivar match. |
| 1087 | if (Ivar->getCanonicalType() != property->getCanonicalType()) { |
| 1088 | Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(), |
| 1089 | Ivar->getName()); |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1093 | } else if (PropertyIvar) { |
| 1094 | // @dynamic |
| 1095 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
| 1096 | return 0; |
| 1097 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1098 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1099 | ObjCPropertyImplDecl *PIDecl = |
| 1100 | ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property, |
| 1101 | (Synthesize ? |
| 1102 | ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE |
| 1103 | : ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC), |
| 1104 | Ivar); |
| 1105 | if (IC) |
| 1106 | IC->addPropertyImplementation(PIDecl); |
| 1107 | else |
| 1108 | CatImplClass->addPropertyImplementation(PIDecl); |
| 1109 | |
| 1110 | return PIDecl; |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1111 | } |
Chris Lattner | 7f925cc | 2008-04-11 07:00:53 +0000 | [diff] [blame] | 1112 | |