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