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