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