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