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