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