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