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