Chris Lattner | da463fe | 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 | 5b12ab8 | 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 | da463fe | 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" |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 16 | #include "clang/Sema/ExternalSemaSource.h" |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 34fb672 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 23 | /// \brief Look for an Objective-C class in the translation unit. |
| 24 | /// |
| 25 | /// \param Id The name of the Objective-C class we're looking for. If |
| 26 | /// typo-correction fixes this name, the Id will be updated |
| 27 | /// to the fixed name. |
| 28 | /// |
| 29 | /// \param IdLoc The location of the name in the translation unit. |
| 30 | /// |
| 31 | /// \param TypoCorrection If true, this routine will attempt typo correction |
| 32 | /// if there is no class with the given name. |
| 33 | /// |
| 34 | /// \returns The declaration of the named Objective-C class, which is also the |
| 35 | /// definition if one is available, or NULL if the class could not be found. |
| 36 | ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id, |
| 37 | SourceLocation IdLoc, |
| 38 | bool TypoCorrection) { |
| 39 | // The third "scope" argument is 0 since we aren't enabling lazy built-in |
| 40 | // creation from this context. |
| 41 | NamedDecl *Decl = LookupSingleName(TUScope, Id, IdLoc, LookupOrdinaryName); |
| 42 | |
| 43 | if (!Decl && TypoCorrection) { |
| 44 | // Perform typo correction at the given location, but only if we |
| 45 | // find an Objective-C class name. |
| 46 | LookupResult R(*this, Id, IdLoc, LookupOrdinaryName); |
| 47 | if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && |
| 48 | (Decl = R.getAsSingle<ObjCInterfaceDecl>())) { |
| 49 | Diag(IdLoc, diag::err_undef_interface_suggest) |
| 50 | << Id << Decl->getDeclName() |
| 51 | << FixItHint::CreateReplacement(IdLoc, Decl->getNameAsString()); |
| 52 | Diag(Decl->getLocation(), diag::note_previous_decl) |
| 53 | << Decl->getDeclName(); |
| 54 | |
| 55 | Id = Decl->getIdentifier(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(Decl); |
| 60 | if (IDecl) { |
| 61 | if (ObjCInterfaceDecl *Def = IDecl->getDefinition()) |
| 62 | IDecl = Def; |
| 63 | } |
| 64 | return IDecl; |
| 65 | } |
| 66 | |
Steve Naroff | 70f41d6 | 2009-02-28 16:59:13 +0000 | [diff] [blame] | 67 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 68 | /// and user declared, in the method definition's AST. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 69 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) { |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 70 | assert(getCurMethodDecl() == 0 && "Method parsing confused"); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 71 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Steve Naroff | 542cd5d | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 73 | // If we don't have a valid method decl, simply return. |
| 74 | if (!MDecl) |
| 75 | return; |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 76 | |
| 77 | // Allow the rest of sema to find private method decl implementations. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 78 | if (MDecl->isInstanceMethod()) |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 79 | AddInstanceMethodToGlobalPool(MDecl, true); |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 80 | else |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 81 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 82 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 83 | // Allow all of Sema to see that we are entering a method definition. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 84 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 85 | PushFunctionScope(); |
| 86 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 87 | // Create Decl objects for each parameter, entrring them in the scope for |
| 88 | // binding to their use. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 89 | |
| 90 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 91 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Daniel Dunbar | 279d1cc | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 93 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 94 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 96 | // Introduce all of the other parameters into this scope. |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 97 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 98 | E = MDecl->param_end(); PI != E; ++PI) |
| 99 | if ((*PI)->getIdentifier()) |
| 100 | PushOnScopeChains(*PI, FnBodyScope); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 103 | Sema::DeclPtrTy Sema:: |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 104 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 105 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 106 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 107 | const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 108 | const SourceLocation *ProtoLocs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 109 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 110 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 112 | bool Invalid = false; |
| 113 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 114 | // Check for another declaration kind with the same name. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 115 | NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 116 | LookupOrdinaryName, ForRedeclaration); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 118 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 119 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 120 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 121 | // Set the new decl invalid and ignore the old. |
| 122 | Invalid = true; |
| 123 | PrevDecl = 0; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 124 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 126 | ObjCInterfaceDecl *ODecl = cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 127 | if (ODecl) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 128 | // Class already seen. Is it a forward declaration? |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 129 | if (ObjCInterfaceDecl *Def = ODecl->getDefinition()) { |
| 130 | Invalid = true; |
| 131 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) << Def->getDeclName(); |
| 132 | Diag(Def->getLocation(), diag::note_previous_definition); |
Chris Lattner | e6447ef | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 133 | |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 134 | // Return the previous class interface and ignore the new one. |
| 135 | return DeclPtrTy::make(ODecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 137 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 139 | ObjCInterfaceDecl *IDecl = |
| 140 | ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 141 | ClassName, ClassLoc, ODecl); |
| 142 | if (Invalid) |
| 143 | IDecl->setInvalidDecl(); |
| 144 | |
| 145 | if (AttrList) |
| 146 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
| 147 | |
| 148 | PushOnScopeChains(IDecl, TUScope); |
| 149 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 150 | if (SuperName) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 151 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 152 | PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 153 | LookupOrdinaryName); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 154 | |
| 155 | if (!PrevDecl) { |
| 156 | // Try to correct for a typo in the superclass name. |
| 157 | LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 158 | if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 159 | (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) { |
| 160 | Diag(SuperLoc, diag::err_undef_superclass_suggest) |
| 161 | << SuperName << ClassName << PrevDecl->getDeclName(); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 162 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) |
| 163 | << PrevDecl->getDeclName(); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 167 | // Since we just pushed IDecl on the scope chain, if PrevDecl is the same |
| 168 | // class, it will be the same declaration. |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 169 | if (PrevDecl == IDecl) { |
| 170 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 171 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 172 | IDecl->setLocEnd(ClassLoc); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 173 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | ObjCInterfaceDecl *SuperClassDecl = |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 175 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 176 | |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 177 | // Diagnose classes that inherit from deprecated classes. |
| 178 | if (SuperClassDecl) |
| 179 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 181 | if (PrevDecl && SuperClassDecl == 0) { |
| 182 | // The previous declaration was not a class decl. Check if we have a |
| 183 | // typedef. If we do, get the underlying class type. |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 184 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)){ |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 185 | QualType T = TDecl->getUnderlyingType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 186 | if (T->isObjCObjectType()) { |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 187 | if (NamedDecl *NDecl = T->getAs<ObjCObjectType>()->getInterface()) |
| 188 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(NDecl); |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 192 | // This handles the following case: |
| 193 | // |
| 194 | // typedef int SuperClass; |
| 195 | // @interface MyClass : SuperClass {} @end |
| 196 | // |
| 197 | if (!SuperClassDecl) { |
| 198 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 199 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | 189d41f | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 203 | if (SuperClassDecl) { |
| 204 | if (ObjCInterfaceDecl *Def = SuperClassDecl->getDefinition()) |
| 205 | SuperClassDecl = Def; |
| 206 | } |
| 207 | |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 208 | if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 209 | if (!SuperClassDecl) |
| 210 | Diag(SuperLoc, diag::err_undef_superclass) |
| 211 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 212 | else if (SuperClassDecl->isForwardDecl()) |
| 213 | Diag(SuperLoc, diag::err_undef_superclass) |
| 214 | << SuperClassDecl->getDeclName() << ClassName |
| 215 | << SourceRange(AtInterfaceLoc, ClassLoc); |
Steve Naroff | 189d41f | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 216 | } |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 217 | IDecl->setSuperClass(SuperClassDecl); |
| 218 | IDecl->setSuperClassLoc(SuperLoc); |
| 219 | IDecl->setLocEnd(SuperLoc); |
Steve Naroff | 189d41f | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 221 | } else { // we have a root class. |
| 222 | IDecl->setLocEnd(ClassLoc); |
| 223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Steve Naroff | 119f60e | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 225 | /// Check then save referenced protocols. |
Chris Lattner | df59f5a | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 226 | if (NumProtoRefs) { |
Chris Lattner | 2229872 | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 227 | IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 228 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 229 | IDecl->setLocEnd(EndProtoLoc); |
| 230 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 232 | CheckObjCDeclScope(IDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 233 | return DeclPtrTy::make(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 237 | /// @compatibility_alias declaration. It sets up the alias relationships. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 238 | Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | IdentifierInfo *AliasName, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 240 | SourceLocation AliasLocation, |
| 241 | IdentifierInfo *ClassName, |
| 242 | SourceLocation ClassLocation) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 243 | // Look for previous declaration of alias name |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 244 | NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 245 | LookupOrdinaryName, ForRedeclaration); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 246 | if (ADecl) { |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 247 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 248 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 249 | else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 250 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 251 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 252 | return DeclPtrTy(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 253 | } |
| 254 | // Check for class declaration |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 255 | NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 256 | LookupOrdinaryName, ForRedeclaration); |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 257 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { |
| 258 | QualType T = TDecl->getUnderlyingType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 259 | if (T->isObjCObjectType()) { |
| 260 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 261 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 262 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 263 | LookupOrdinaryName, ForRedeclaration); |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 267 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 268 | if (CDecl == 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 269 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 270 | if (CDeclU) |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 271 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 272 | return DeclPtrTy(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 273 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 275 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 277 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 279 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 280 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 282 | return DeclPtrTy::make(AliasDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 285 | void Sema::CheckForwardProtocolDeclarationForCircularDependency( |
| 286 | IdentifierInfo *PName, |
| 287 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | const ObjCList<ObjCProtocolDecl> &PList) { |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 289 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 290 | E = PList.end(); I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 292 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 293 | Ploc)) { |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 294 | if (PDecl->getIdentifier() == PName) { |
| 295 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 296 | Diag(PrevLoc, diag::note_previous_definition); |
| 297 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 299 | PDecl->getLocation(), PDecl->getReferencedProtocols()); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 304 | Sema::DeclPtrTy |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 305 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 306 | IdentifierInfo *ProtocolName, |
| 307 | SourceLocation ProtocolLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 308 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 309 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 310 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 311 | SourceLocation EndProtoLoc, |
| 312 | AttributeList *AttrList) { |
| 313 | // FIXME: Deal with AttrList. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 314 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 315 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 316 | if (PDecl) { |
| 317 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | 5074f8f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 318 | if (!PDecl->isForwardDecl()) { |
Fariborz Jahanian | 54d569c | 2009-04-06 23:43:32 +0000 | [diff] [blame] | 319 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
Chris Lattner | e6447ef | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 320 | Diag(PDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 5074f8f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 321 | // Just return the protocol we already had. |
| 322 | // FIXME: don't leak the objects passed in! |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 323 | return DeclPtrTy::make(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 324 | } |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 325 | ObjCList<ObjCProtocolDecl> PList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 327 | CheckForwardProtocolDeclarationForCircularDependency( |
| 328 | ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Steve Naroff | eb03dac | 2008-08-13 16:39:22 +0000 | [diff] [blame] | 330 | // Make sure the cached decl gets a valid start location. |
| 331 | PDecl->setLocation(AtProtoInterfaceLoc); |
Chris Lattner | 5074f8f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 332 | PDecl->setForwardDecl(false); |
Chris Lattner | 5074f8f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 333 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 335 | AtProtoInterfaceLoc,ProtocolName); |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 336 | PushOnScopeChains(PDecl, TUScope); |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 337 | PDecl->setForwardDecl(false); |
Chris Lattner | f87ca0a | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 338 | } |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 339 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 340 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 341 | if (NumProtoRefs) { |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 342 | /// Check then save referenced protocols. |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 343 | PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
| 344 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 345 | PDecl->setLocEnd(EndProtoLoc); |
| 346 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
| 348 | CheckObjCDeclScope(PDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 349 | return DeclPtrTy::make(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 353 | /// issues an error if they are not declared. It returns list of |
| 354 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 355 | void |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 356 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 357 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 358 | unsigned NumProtocols, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 359 | llvm::SmallVectorImpl<DeclPtrTy> &Protocols) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 360 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 361 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first, |
| 362 | ProtocolId[i].second); |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 363 | if (!PDecl) { |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 364 | LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second, |
| 365 | LookupObjCProtocolName); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 366 | if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 367 | (PDecl = R.getAsSingle<ObjCProtocolDecl>())) { |
| 368 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest) |
| 369 | << ProtocolId[i].first << R.getLookupName(); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 370 | Diag(PDecl->getLocation(), diag::note_previous_decl) |
| 371 | << PDecl->getDeclName(); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | if (!PDecl) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 376 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 377 | << ProtocolId[i].first; |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 378 | continue; |
| 379 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 381 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 382 | |
| 383 | // If this is a forward declaration and we are supposed to warn in this |
| 384 | // case, do it. |
| 385 | if (WarnOnDeclarations && PDecl->isForwardDecl()) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 386 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 387 | << ProtocolId[i].first; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 388 | Protocols.push_back(DeclPtrTy::make(PDecl)); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Fariborz Jahanian | abf63e7b | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 392 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 393 | /// a class method in its extension. |
| 394 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 396 | ObjCInterfaceDecl *ID) { |
| 397 | if (!ID) |
| 398 | return; // Possibly due to previous error |
| 399 | |
| 400 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 401 | for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(), |
| 402 | e = ID->meth_end(); i != e; ++i) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 403 | ObjCMethodDecl *MD = *i; |
| 404 | MethodMap[MD->getSelector()] = MD; |
| 405 | } |
| 406 | |
| 407 | if (MethodMap.empty()) |
| 408 | return; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 409 | for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(), |
| 410 | e = CAT->meth_end(); i != e; ++i) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 411 | ObjCMethodDecl *Method = *i; |
| 412 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
| 413 | if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
| 414 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 415 | << Method->getDeclName(); |
| 416 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
Chris Lattner | dac168d | 2009-04-12 08:43:13 +0000 | [diff] [blame] | 421 | /// ActOnForwardProtocolDeclaration - Handle @protocol foo; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 422 | Action::DeclPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 423 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 424 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 425 | unsigned NumElts, |
| 426 | AttributeList *attrList) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 427 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 428 | llvm::SmallVector<SourceLocation, 8> ProtoLocs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 430 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 431 | IdentifierInfo *Ident = IdentList[i].first; |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 432 | ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 433 | if (PDecl == 0) { // Not already seen? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 435 | IdentList[i].second, Ident); |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 436 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 437 | } |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 438 | if (attrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 439 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 440 | Protocols.push_back(PDecl); |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 441 | ProtoLocs.push_back(IdentList[i].second); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 442 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
| 444 | ObjCForwardProtocolDecl *PDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 445 | ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 446 | Protocols.data(), Protocols.size(), |
| 447 | ProtoLocs.data()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 448 | CurContext->addDecl(PDecl); |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 449 | CheckObjCDeclScope(PDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 450 | return DeclPtrTy::make(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 453 | Sema::DeclPtrTy Sema:: |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 454 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 455 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 456 | IdentifierInfo *CategoryName, |
| 457 | SourceLocation CategoryLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 458 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 459 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 460 | const SourceLocation *ProtoLocs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 461 | SourceLocation EndProtoLoc) { |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 462 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 463 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 464 | |
| 465 | /// Check that class of this category is already completely declared. |
| 466 | if (!IDecl || IDecl->isForwardDecl()) { |
| 467 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 468 | // the enclosing method declarations. We mark the decl invalid |
| 469 | // to make it clear that this isn't a valid AST. |
| 470 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 471 | ClassLoc, CategoryLoc, CategoryName); |
| 472 | CDecl->setInvalidDecl(); |
| 473 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
| 474 | return DeclPtrTy::make(CDecl); |
| 475 | } |
| 476 | |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 477 | if (!CategoryName && IDecl->getImplementation()) { |
| 478 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| 479 | Diag(IDecl->getImplementation()->getLocation(), |
| 480 | diag::note_implementation_declared); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 483 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 484 | ClassLoc, CategoryLoc, CategoryName); |
| 485 | // FIXME: PushOnScopeChains? |
| 486 | CurContext->addDecl(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 487 | |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 488 | CDecl->setClassInterface(IDecl); |
| 489 | // Insert class extension to the list of class's categories. |
| 490 | if (!CategoryName) |
| 491 | CDecl->insertNextClassCategory(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Chris Lattner | ced903b | 2009-02-16 21:30:01 +0000 | [diff] [blame] | 493 | // If the interface is deprecated, warn about it. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 494 | (void)DiagnoseUseOfDecl(IDecl, ClassLoc); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 495 | |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 496 | if (CategoryName) { |
| 497 | /// Check for duplicate interface declaration for this category |
| 498 | ObjCCategoryDecl *CDeclChain; |
| 499 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 500 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 501 | if (CDeclChain->getIdentifier() == CategoryName) { |
| 502 | // Class extensions can be declared multiple times. |
| 503 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 504 | << ClassName << CategoryName; |
| 505 | Diag(CDeclChain->getLocation(), diag::note_previous_definition); |
| 506 | break; |
| 507 | } |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 508 | } |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 509 | if (!CDeclChain) |
| 510 | CDecl->insertNextClassCategory(); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 511 | } |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 512 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 513 | if (NumProtoRefs) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 514 | CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 515 | ProtoLocs, Context); |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 516 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 517 | if (CDecl->IsClassExtension()) |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 518 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 519 | NumProtoRefs, ProtoLocs, |
| 520 | Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 521 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 523 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 524 | return DeclPtrTy::make(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 528 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 529 | /// object. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 530 | Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 531 | SourceLocation AtCatImplLoc, |
| 532 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 533 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 534 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 535 | ObjCCategoryDecl *CatIDecl = 0; |
| 536 | if (IDecl) { |
| 537 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 538 | if (!CatIDecl) { |
| 539 | // Category @implementation with no corresponding @interface. |
| 540 | // Create and install one. |
| 541 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(), |
Douglas Gregor | 071676f | 2010-01-16 16:38:58 +0000 | [diff] [blame] | 542 | SourceLocation(), SourceLocation(), |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 543 | CatName); |
| 544 | CatIDecl->setClassInterface(IDecl); |
| 545 | CatIDecl->insertNextClassCategory(); |
| 546 | } |
| 547 | } |
| 548 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | ObjCCategoryImplDecl *CDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 550 | ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, |
| 551 | IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 552 | /// Check that class of this category is already completely declared. |
| 553 | if (!IDecl || IDecl->isForwardDecl()) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 554 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 556 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 557 | CurContext->addDecl(CDecl); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 558 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 559 | /// Check that CatName, category name, is not used in another implementation. |
| 560 | if (CatIDecl) { |
| 561 | if (CatIDecl->getImplementation()) { |
| 562 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 563 | << CatName; |
| 564 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 565 | diag::note_previous_definition); |
| 566 | } else |
| 567 | CatIDecl->setImplementation(CDecl); |
| 568 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 570 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 571 | return DeclPtrTy::make(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 574 | Sema::DeclPtrTy Sema::ActOnStartClassImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 575 | SourceLocation AtClassImplLoc, |
| 576 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | IdentifierInfo *SuperClassname, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 578 | SourceLocation SuperClassLoc) { |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 579 | ObjCInterfaceDecl *IDecl = 0, *ODecl = 0; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 580 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 581 | NamedDecl *PrevDecl |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 582 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 583 | ForRedeclaration); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 584 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 585 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 586 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 587 | } else if ((ODecl = cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
| 588 | // If we can't find a definition of the interface, warn. |
| 589 | if (!(IDecl = ODecl->getDefinition())) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 590 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
Fariborz Jahanian | 6f0f25b | 2009-04-23 21:49:04 +0000 | [diff] [blame] | 591 | } |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 592 | } else { |
| 593 | // We did not find anything with the name ClassName; try to correct for |
| 594 | // typos in the class name. |
| 595 | LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 596 | if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 597 | (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) { |
Douglas Gregor | 10f1e4d | 2010-01-06 23:44:25 +0000 | [diff] [blame] | 598 | // Suggest the (potentially) correct interface name. However, put the |
| 599 | // fix-it hint itself in a separate note, since changing the name in |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 600 | // the warning would make the fix-it change semantics. Also, don't |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 601 | // provide a code-modification hint or use the typo name for recovery, |
| 602 | // because this is just a warning. The program may actually be correct. |
| 603 | Diag(ClassLoc, diag::warn_undef_interface_suggest) |
| 604 | << ClassName << R.getLookupName(); |
Douglas Gregor | 10f1e4d | 2010-01-06 23:44:25 +0000 | [diff] [blame] | 605 | Diag(IDecl->getLocation(), diag::note_previous_decl) |
| 606 | << R.getLookupName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 607 | << FixItHint::CreateReplacement(ClassLoc, |
| 608 | R.getLookupName().getAsString()); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 609 | IDecl = 0; |
| 610 | } else { |
| 611 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 612 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 613 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 615 | // Check that super class name is valid class name |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 616 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 617 | if (SuperClassname) { |
| 618 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 619 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 620 | LookupOrdinaryName); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 621 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 622 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 623 | << SuperClassname; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 624 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 625 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 627 | if (!SDecl) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 628 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 629 | << SuperClassname << ClassName; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 630 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 631 | // This implementation and its interface do not have the same |
| 632 | // super class. |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 633 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 634 | << SDecl->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 635 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 640 | if (!IDecl) { |
| 641 | // Legacy case of @implementation with no corresponding @interface. |
| 642 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | 73a73f5 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 643 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 644 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 645 | // copy them over. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 647 | ClassName, ClassLoc, ODecl, false, true); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 648 | IDecl->setSuperClass(SDecl); |
| 649 | IDecl->setLocEnd(ClassLoc); |
Douglas Gregor | ac345a3 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 650 | |
| 651 | PushOnScopeChains(IDecl, TUScope); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 652 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
| 654 | ObjCImplementationDecl* IMPDecl = |
| 655 | ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 656 | IDecl, SDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 658 | if (CheckObjCDeclScope(IMPDecl)) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 659 | return DeclPtrTy::make(IMPDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 661 | // Check that there is no duplicate implementation of this class. |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 662 | if (IDecl && IDecl->getImplementation()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 663 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 43cee935 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 664 | Diag(IDecl->getImplementation()->getLocation(), |
| 665 | diag::note_previous_definition); |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 666 | } else { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 667 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 668 | PushOnScopeChains(IMPDecl, TUScope); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 669 | } |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 671 | return DeclPtrTy::make(IMPDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 674 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 675 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 676 | SourceLocation RBrace) { |
| 677 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 678 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 679 | if (!IDecl) |
| 680 | return; |
| 681 | /// Check case of non-existing @interface decl. |
| 682 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 683 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 684 | if (IDecl->isImplicitInterfaceDecl()) { |
Chris Lattner | 2229872 | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 685 | IDecl->setLocEnd(RBrace); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 686 | // Add ivar's to class's DeclContext. |
| 687 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | c0309cd | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 688 | ivars[i]->setLexicalDeclContext(ImpDecl); |
| 689 | IDecl->makeDeclVisibleInContext(ivars[i], false); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 690 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 693 | return; |
| 694 | } |
| 695 | // If implementation has empty ivar list, just return. |
| 696 | if (numIvars == 0) |
| 697 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 699 | assert(ivars && "missing @implementation ivars"); |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 700 | if (LangOpts.ObjCNonFragileABI2) { |
| 701 | if (ImpDecl->getSuperClass()) |
| 702 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 703 | for (unsigned i = 0; i < numIvars; i++) { |
| 704 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 705 | if (const ObjCIvarDecl *ClsIvar = |
| 706 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 707 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 708 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 709 | continue; |
| 710 | } |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 711 | // Instance ivar to Implementation's DeclContext. |
| 712 | ImplIvar->setLexicalDeclContext(ImpDecl); |
| 713 | IDecl->makeDeclVisibleInContext(ImplIvar, false); |
| 714 | ImpDecl->addDecl(ImplIvar); |
| 715 | } |
| 716 | return; |
| 717 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 718 | // Check interface's Ivar list against those in the implementation. |
| 719 | // names and types must match. |
| 720 | // |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 721 | unsigned j = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 723 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 724 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 725 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 726 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 727 | assert (ImplIvar && "missing implementation ivar"); |
| 728 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 730 | // First, make sure the types match. |
Chris Lattner | 35ffe33 | 2008-07-27 00:05:05 +0000 | [diff] [blame] | 731 | if (Context.getCanonicalType(ImplIvar->getType()) != |
| 732 | Context.getCanonicalType(ClsIvar->getType())) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 733 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 734 | << ImplIvar->getIdentifier() |
| 735 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 736 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 737 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { |
| 738 | Expr *ImplBitWidth = ImplIvar->getBitWidth(); |
| 739 | Expr *ClsBitWidth = ClsIvar->getBitWidth(); |
Eli Friedman | 1c4a175 | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 740 | if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() != |
| 741 | ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) { |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 742 | Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) |
| 743 | << ImplIvar->getIdentifier(); |
| 744 | Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); |
| 745 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | } |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 747 | // Make sure the names are identical. |
| 748 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 749 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 750 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 751 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 752 | } |
| 753 | --numIvars; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 754 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 756 | if (numIvars > 0) |
Chris Lattner | 83021e9 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 757 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 758 | else if (IVI != IVE) |
Chris Lattner | 83021e9 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 759 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 762 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 763 | bool &IncompleteImpl, unsigned DiagID) { |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 764 | if (!IncompleteImpl) { |
| 765 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 766 | IncompleteImpl = true; |
| 767 | } |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 768 | Diag(method->getLocation(), DiagID) |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 769 | << method->getDeclName(); |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 772 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 773 | ObjCMethodDecl *IntfMethodDecl) { |
Chris Lattner | a9d0ffe | 2009-04-11 18:01:59 +0000 | [diff] [blame] | 774 | if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(), |
Fariborz Jahanian | 0c20bdd | 2009-05-14 23:52:54 +0000 | [diff] [blame] | 775 | ImpMethodDecl->getResultType()) && |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 776 | !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(), |
| 777 | ImpMethodDecl->getResultType())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types) |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 779 | << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType() |
| 780 | << ImpMethodDecl->getResultType(); |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 781 | Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition); |
| 782 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 784 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
| 785 | IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end(); |
| 786 | IM != EM; ++IM, ++IF) { |
Fariborz Jahanian | 41e803d | 2009-11-18 18:56:09 +0000 | [diff] [blame] | 787 | QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType(); |
| 788 | QualType ParmImpTy = (*IM)->getType().getUnqualifiedType(); |
| 789 | if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) || |
| 790 | Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy)) |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 791 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
| 793 | Diag((*IM)->getLocation(), diag::warn_conflicting_param_types) |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 794 | << ImpMethodDecl->getDeclName() << (*IF)->getType() |
| 795 | << (*IM)->getType(); |
Chris Lattner | 5300acc | 2009-04-11 20:14:49 +0000 | [diff] [blame] | 796 | Diag((*IF)->getLocation(), diag::note_previous_definition); |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 797 | } |
Fariborz Jahanian | 5981b04 | 2010-05-21 23:28:58 +0000 | [diff] [blame] | 798 | if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) { |
| 799 | Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic); |
| 800 | Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration); |
| 801 | } |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 804 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 805 | /// improve the efficiency of selector lookups and type checking by associating |
| 806 | /// with each protocol / interface / category the flattened instance tables. If |
| 807 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 808 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 809 | |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 810 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 811 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 812 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 813 | ObjCProtocolDecl *PDecl, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 814 | bool& IncompleteImpl, |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 815 | const llvm::DenseSet<Selector> &InsMap, |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 816 | const llvm::DenseSet<Selector> &ClsMap, |
Fariborz Jahanian | 2e8074b | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 817 | ObjCContainerDecl *CDecl) { |
| 818 | ObjCInterfaceDecl *IDecl; |
| 819 | if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 820 | IDecl = C->getClassInterface(); |
| 821 | else |
| 822 | IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 823 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| 824 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 825 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 826 | ObjCInterfaceDecl *NSIDecl = 0; |
| 827 | if (getLangOptions().NeXTRuntime) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | // check to see if class implements forwardInvocation method and objects |
| 829 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 830 | // from one object to another. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | // Under such conditions, which means that every method possible is |
| 832 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 833 | // found" warnings. |
| 834 | // FIXME: Use a general GetUnarySelector method for this. |
| 835 | IdentifierInfo* II = &Context.Idents.get("forwardInvocation"); |
| 836 | Selector fISelector = Context.Selectors.getSelector(1, &II); |
| 837 | if (InsMap.count(fISelector)) |
| 838 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 839 | // need be implemented in the implementation. |
| 840 | NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy")); |
| 841 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 843 | // If a method lookup fails locally we still need to look and see if |
| 844 | // the method was implemented by a base class or an inherited |
| 845 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 846 | // and otherwise would terminate in a warning. |
| 847 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 848 | // check unimplemented instance methods. |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 849 | if (!NSIDecl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 851 | E = PDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 852 | ObjCMethodDecl *method = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 854 | !method->isSynthesized() && !InsMap.count(method->getSelector()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | (!Super || |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 856 | !Super->lookupInstanceMethod(method->getSelector()))) { |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 857 | // Ugly, but necessary. Method declared in protcol might have |
| 858 | // have been synthesized due to a property declared in the class which |
| 859 | // uses the protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | ObjCMethodDecl *MethodInClass = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 861 | IDecl->lookupInstanceMethod(method->getSelector()); |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 862 | if (!MethodInClass || !MethodInClass->isSynthesized()) { |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 863 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
| 864 | if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) { |
| 865 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); |
| 866 | Diag(CDecl->getLocation(), diag::note_required_for_protocol_at) |
| 867 | << PDecl->getDeclName(); |
| 868 | } |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 869 | } |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 872 | // check unimplemented class methods |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 874 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 875 | I != E; ++I) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 876 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 877 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 878 | !ClsMap.count(method->getSelector()) && |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 879 | (!Super || !Super->lookupClassMethod(method->getSelector()))) { |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 880 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
| 881 | if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) { |
| 882 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); |
| 883 | Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) << |
| 884 | PDecl->getDeclName(); |
| 885 | } |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 886 | } |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 887 | } |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 888 | // Check on this protocols's referenced protocols, recursively. |
| 889 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 890 | E = PDecl->protocol_end(); PI != E; ++PI) |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 891 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 894 | /// MatchAllMethodDeclarations - Check methods declaraed in interface or |
| 895 | /// or protocol against those declared in their implementations. |
| 896 | /// |
| 897 | void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap, |
| 898 | const llvm::DenseSet<Selector> &ClsMap, |
| 899 | llvm::DenseSet<Selector> &InsMapSeen, |
| 900 | llvm::DenseSet<Selector> &ClsMapSeen, |
| 901 | ObjCImplDecl* IMPDecl, |
| 902 | ObjCContainerDecl* CDecl, |
| 903 | bool &IncompleteImpl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | bool ImmediateClass) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 905 | // Check and see if instance methods in class interface have been |
| 906 | // implemented in the implementation class. If so, their types match. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 907 | for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), |
| 908 | E = CDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 909 | if (InsMapSeen.count((*I)->getSelector())) |
| 910 | continue; |
| 911 | InsMapSeen.insert((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | if (!(*I)->isSynthesized() && |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 913 | !InsMap.count((*I)->getSelector())) { |
| 914 | if (ImmediateClass) |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 915 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, |
| 916 | diag::note_undef_method_impl); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 917 | continue; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 918 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | ObjCMethodDecl *ImpMethodDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 920 | IMPDecl->getInstanceMethod((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | ObjCMethodDecl *IntfMethodDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 922 | CDecl->getInstanceMethod((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | assert(IntfMethodDecl && |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 924 | "IntfMethodDecl is null in ImplMethodsVsClassMethods"); |
| 925 | // ImpMethodDecl may be null as in a @dynamic property. |
| 926 | if (ImpMethodDecl) |
| 927 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 928 | } |
| 929 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 931 | // Check and see if class methods in class interface have been |
| 932 | // implemented in the implementation class. If so, their types match. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | for (ObjCInterfaceDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 934 | I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 935 | if (ClsMapSeen.count((*I)->getSelector())) |
| 936 | continue; |
| 937 | ClsMapSeen.insert((*I)->getSelector()); |
| 938 | if (!ClsMap.count((*I)->getSelector())) { |
| 939 | if (ImmediateClass) |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 940 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, |
| 941 | diag::note_undef_method_impl); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 942 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 943 | ObjCMethodDecl *ImpMethodDecl = |
| 944 | IMPDecl->getClassMethod((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | ObjCMethodDecl *IntfMethodDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 946 | CDecl->getClassMethod((*I)->getSelector()); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 947 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 948 | } |
| 949 | } |
| 950 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
| 951 | // Check for any implementation of a methods declared in protocol. |
| 952 | for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), |
| 953 | E = I->protocol_end(); PI != E; ++PI) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 955 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 956 | (*PI), IncompleteImpl, false); |
| 957 | if (I->getSuperClass()) |
| 958 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 960 | I->getSuperClass(), IncompleteImpl, false); |
| 961 | } |
| 962 | } |
| 963 | |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 964 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | ObjCContainerDecl* CDecl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 966 | bool IncompleteImpl) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 967 | llvm::DenseSet<Selector> InsMap; |
| 968 | // Check and see if instance methods in class interface have been |
| 969 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 971 | I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 972 | InsMap.insert((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 974 | // Check and see if properties declared in the interface have either 1) |
| 975 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 976 | // of the property in the @implementation. |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 977 | if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2) |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 978 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap); |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 979 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 980 | llvm::DenseSet<Selector> ClsMap; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 982 | I = IMPDecl->classmeth_begin(), |
| 983 | E = IMPDecl->classmeth_end(); I != E; ++I) |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 984 | ClsMap.insert((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 986 | // Check for type conflict of methods declared in a class/protocol and |
| 987 | // its implementation; if any. |
| 988 | llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 990 | IMPDecl, CDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 991 | IncompleteImpl, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 993 | // Check the protocol list for unimplemented methods in the @implementation |
| 994 | // class. |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 995 | // Check and see if class methods in class interface have been |
| 996 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 998 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 999 | for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1000 | E = I->protocol_end(); PI != E; ++PI) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1002 | InsMap, ClsMap, I); |
| 1003 | // Check class extensions (unnamed categories) |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1004 | for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension(); |
| 1005 | Categories; Categories = Categories->getNextClassExtension()) |
| 1006 | ImplMethodsVsClassMethods(S, IMPDecl, |
| 1007 | const_cast<ObjCCategoryDecl*>(Categories), |
| 1008 | IncompleteImpl); |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1009 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1010 | // For extended class, unimplemented methods in its protocols will |
| 1011 | // be reported in the primary class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1012 | if (!C->IsClassExtension()) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1013 | for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), |
| 1014 | E = C->protocol_end(); PI != E; ++PI) |
| 1015 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Fariborz Jahanian | 2e8074b | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 1016 | InsMap, ClsMap, CDecl); |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1017 | // Report unimplemented properties in the category as well. |
| 1018 | // When reporting on missing setter/getters, do not report when |
| 1019 | // setter/getter is implemented in category's primary class |
| 1020 | // implementation. |
| 1021 | if (ObjCInterfaceDecl *ID = C->getClassInterface()) |
| 1022 | if (ObjCImplDecl *IMP = ID->getImplementation()) { |
| 1023 | for (ObjCImplementationDecl::instmeth_iterator |
| 1024 | I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I) |
| 1025 | InsMap.insert((*I)->getSelector()); |
| 1026 | } |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1027 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap); |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1028 | } |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1029 | } else |
| 1030 | assert(false && "invalid ObjCContainerDecl type."); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | /// ActOnForwardClassDeclaration - |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1034 | Action::DeclPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1035 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1036 | IdentifierInfo **IdentList, |
Ted Kremenek | a26da85 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 1037 | SourceLocation *IdentLocs, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1038 | unsigned NumElts) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1039 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1041 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1042 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1043 | NamedDecl *PrevDecl |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1044 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1045 | LookupOrdinaryName, ForRedeclaration); |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1046 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1047 | // Maybe we will complain about the shadowed template parameter. |
| 1048 | DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); |
| 1049 | // Just pretend that we didn't see the previous declaration. |
| 1050 | PrevDecl = 0; |
| 1051 | } |
| 1052 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1053 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1054 | // GCC apparently allows the following idiom: |
| 1055 | // |
| 1056 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 1057 | // @class XCElementToggler; |
| 1058 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | // FIXME: Make an extension? |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1060 | TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1061 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1062 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1063 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1064 | } else { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1065 | // a forward class declaration matching a typedef name of a class refers |
| 1066 | // to the underlying class. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1067 | if (const ObjCObjectType *OI = |
| 1068 | TDD->getUnderlyingType()->getAs<ObjCObjectType>()) |
| 1069 | PrevDecl = OI->getInterface(); |
Fariborz Jahanian | 0d45181 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 1070 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1071 | } |
Sebastian Redl | ab6a088 | 2010-08-09 21:55:28 +0000 | [diff] [blame] | 1072 | ObjCInterfaceDecl *ODecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 1073 | ObjCInterfaceDecl *IDecl = |
| 1074 | ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
| 1075 | IdentList[i], IdentLocs[i], ODecl, true); |
| 1076 | |
| 1077 | // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to |
| 1078 | // the current DeclContext. This prevents clients that walk DeclContext |
| 1079 | // from seeing the imaginary ObjCInterfaceDecl until it is actually |
| 1080 | // declared later (if at all). We also take care to explicitly make |
| 1081 | // sure this declaration is visible for name lookup. |
| 1082 | PushOnScopeChains(IDecl, TUScope, false); |
| 1083 | CurContext->makeDeclVisibleInContext(IDecl, true); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1084 | |
| 1085 | Interfaces.push_back(IDecl); |
| 1086 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1088 | assert(Interfaces.size() == NumElts); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1089 | ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1090 | Interfaces.data(), IdentLocs, |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1091 | Interfaces.size()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1092 | CurContext->addDecl(CDecl); |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1093 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1094 | return DeclPtrTy::make(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | |
| 1098 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 1099 | /// returns true, or false, accordingly. |
| 1100 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1102 | const ObjCMethodDecl *PrevMethod, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1103 | bool matchBasedOnSizeAndAlignment, |
| 1104 | bool matchBasedOnStrictEqulity) { |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1105 | QualType T1 = Context.getCanonicalType(Method->getResultType()); |
| 1106 | QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1108 | if (T1 != T2) { |
| 1109 | // The result types are different. |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1110 | if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1111 | return false; |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1112 | // Incomplete types don't have a size and alignment. |
| 1113 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1114 | return false; |
| 1115 | // Check is based on size and alignment. |
| 1116 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1117 | return false; |
| 1118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1120 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1121 | E = Method->param_end(); |
| 1122 | ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1124 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1125 | assert(PrevI != PrevMethod->param_end() && "Param mismatch"); |
| 1126 | T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1127 | T2 = Context.getCanonicalType((*PrevI)->getType()); |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1128 | if (T1 != T2) { |
| 1129 | // The result types are different. |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1130 | if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity) |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1131 | return false; |
| 1132 | // Incomplete types don't have a size and alignment. |
| 1133 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1134 | return false; |
| 1135 | // Check is based on size and alignment. |
| 1136 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1137 | return false; |
| 1138 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1139 | } |
| 1140 | return true; |
| 1141 | } |
| 1142 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1143 | /// \brief Read the contents of the method pool for a given selector from |
| 1144 | /// external storage. |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1145 | /// |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1146 | /// This routine should only be called once, when the method pool has no entry |
| 1147 | /// for this selector. |
| 1148 | Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1149 | assert(ExternalSource && "We need an external AST source"); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1150 | assert(MethodPool.find(Sel) == MethodPool.end() && |
| 1151 | "Selector data already loaded into the method pool"); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1152 | |
| 1153 | // Read the method list from the external source. |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1154 | GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1156 | return MethodPool.insert(std::make_pair(Sel, Methods)).first; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1159 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
| 1160 | bool instance) { |
| 1161 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
| 1162 | if (Pos == MethodPool.end()) { |
| 1163 | if (ExternalSource) |
| 1164 | Pos = ReadMethodPool(Method->getSelector()); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1165 | else |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1166 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 1167 | GlobalMethods())).first; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1168 | } |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1169 | Method->setDefined(impl); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1170 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1171 | if (Entry.Method == 0) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1172 | // Haven't seen a method with this selector name yet - add it. |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1173 | Entry.Method = Method; |
| 1174 | Entry.Next = 0; |
| 1175 | return; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1176 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1178 | // We've seen a method with this name, see if we have already seen this type |
| 1179 | // signature. |
| 1180 | for (ObjCMethodList *List = &Entry; List; List = List->Next) |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1181 | if (MatchTwoMethodDeclarations(Method, List->Method)) { |
| 1182 | List->Method->setDefined(impl); |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1183 | return; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1184 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1186 | // We have a new signature for an existing method - add it. |
| 1187 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | da4abf1 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 1188 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
| 1189 | Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1192 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1193 | bool receiverIdOrClass, |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1194 | bool warn, bool instance) { |
| 1195 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 1196 | if (Pos == MethodPool.end()) { |
| 1197 | if (ExternalSource) |
| 1198 | Pos = ReadMethodPool(Sel); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1199 | else |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1203 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1205 | bool strictSelectorMatch = receiverIdOrClass && warn && |
| 1206 | (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl) != |
| 1207 | Diagnostic::Ignored); |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1208 | if (warn && MethList.Method && MethList.Next) { |
| 1209 | bool issueWarning = false; |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1210 | if (strictSelectorMatch) |
| 1211 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) { |
| 1212 | // This checks if the methods differ in type mismatch. |
| 1213 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true)) |
| 1214 | issueWarning = true; |
| 1215 | } |
| 1216 | |
| 1217 | if (!issueWarning) |
| 1218 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) { |
| 1219 | // This checks if the methods differ by size & alignment. |
| 1220 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
| 1221 | issueWarning = true; |
| 1222 | } |
| 1223 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1224 | if (issueWarning) { |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1225 | if (strictSelectorMatch) |
| 1226 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 1227 | else |
| 1228 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1229 | Diag(MethList.Method->getLocStart(), diag::note_using) |
| 1230 | << MethList.Method->getSourceRange(); |
| 1231 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1232 | Diag(Next->Method->getLocStart(), diag::note_also_found) |
| 1233 | << Next->Method->getSourceRange(); |
| 1234 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1235 | } |
| 1236 | return MethList.Method; |
| 1237 | } |
| 1238 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1239 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1240 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 1241 | if (Pos == MethodPool.end()) |
| 1242 | return 0; |
| 1243 | |
| 1244 | GlobalMethods &Methods = Pos->second; |
| 1245 | |
| 1246 | if (Methods.first.Method && Methods.first.Method->isDefined()) |
| 1247 | return Methods.first.Method; |
| 1248 | if (Methods.second.Method && Methods.second.Method->isDefined()) |
| 1249 | return Methods.second.Method; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1253 | /// CompareMethodParamsInBaseAndSuper - This routine compares methods with |
| 1254 | /// identical selector names in current and its super classes and issues |
| 1255 | /// a warning if any of their argument types are incompatible. |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1256 | void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl, |
| 1257 | ObjCMethodDecl *Method, |
| 1258 | bool IsInstance) { |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1259 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 1260 | if (ID == 0) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1262 | while (ObjCInterfaceDecl *SD = ID->getSuperClass()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | ObjCMethodDecl *SuperMethodDecl = |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1264 | SD->lookupMethod(Method->getSelector(), IsInstance); |
| 1265 | if (SuperMethodDecl == 0) { |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1266 | ID = SD; |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1267 | continue; |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1268 | } |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1269 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1270 | E = Method->param_end(); |
| 1271 | ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin(); |
| 1272 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1273 | // Number of parameters are the same and is guaranteed by selector match. |
| 1274 | assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch"); |
| 1275 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1276 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 1277 | // If type of arguement of method in this class does not match its |
| 1278 | // respective argument type in the super class method, issue warning; |
| 1279 | if (!Context.typesAreCompatible(T1, T2)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1281 | << T1 << T2; |
| 1282 | Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration); |
| 1283 | return; |
| 1284 | } |
| 1285 | } |
| 1286 | ID = SD; |
| 1287 | } |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 1290 | /// DiagnoseDuplicateIvars - |
| 1291 | /// Check for duplicate ivars in the entire class at the start of |
| 1292 | /// @implementation. This becomes necesssary because class extension can |
| 1293 | /// add ivars to a class in random order which will not be known until |
| 1294 | /// class's @implementation is seen. |
| 1295 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
| 1296 | ObjCInterfaceDecl *SID) { |
| 1297 | for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(), |
| 1298 | IVE = ID->ivar_end(); IVI != IVE; ++IVI) { |
| 1299 | ObjCIvarDecl* Ivar = (*IVI); |
| 1300 | if (Ivar->isInvalidDecl()) |
| 1301 | continue; |
| 1302 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 1303 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 1304 | if (prevIvar) { |
| 1305 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 1306 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 1307 | Ivar->setInvalidDecl(); |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1313 | // Note: For class/category implemenations, allMethods/allProperties is |
| 1314 | // always null. |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1315 | void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1316 | DeclPtrTy classDecl, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1317 | DeclPtrTy *allMethods, unsigned allNum, |
| 1318 | DeclPtrTy *allProperties, unsigned pNum, |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1319 | DeclGroupPtrTy *allTUVars, unsigned tuvNum) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1320 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1321 | |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1322 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 1323 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1324 | // should be true. |
| 1325 | if (!ClassDecl) |
| 1326 | return; |
Fariborz Jahanian | 9290ede | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1327 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | bool isInterfaceDeclKind = |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1329 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 1330 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1331 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1332 | |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1333 | if (!isInterfaceDeclKind && AtEnd.isInvalid()) { |
| 1334 | // FIXME: This is wrong. We shouldn't be pretending that there is |
| 1335 | // an '@end' in the declaration. |
| 1336 | SourceLocation L = ClassDecl->getLocation(); |
| 1337 | AtEnd.setBegin(L); |
| 1338 | AtEnd.setEnd(L); |
| 1339 | Diag(L, diag::warn_missing_atend); |
Fariborz Jahanian | 9290ede | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1342 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1343 | |
| 1344 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 1345 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 1346 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 1347 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1348 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1349 | ObjCMethodDecl *Method = |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1350 | cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>()); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1351 | |
| 1352 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1353 | if (Method->isInstanceMethod()) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1354 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1355 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1357 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1359 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1360 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1361 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1362 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1363 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1364 | DC->addDecl(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1365 | InsMap[Method->getSelector()] = Method; |
| 1366 | /// The following allows us to typecheck messages to "id". |
| 1367 | AddInstanceMethodToGlobalPool(Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | // verify that the instance method conforms to the same definition of |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1369 | // parent methods if it shadows one. |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1370 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1371 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1372 | } else { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1373 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1374 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1376 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1378 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1379 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1380 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1381 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1382 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1383 | DC->addDecl(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1384 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1385 | /// The following allows us to typecheck messages to "Class". |
| 1386 | AddFactoryMethodToGlobalPool(Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | // verify that the class method conforms to the same definition of |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1388 | // parent methods if it shadows one. |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1389 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1393 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | // Compares properties declared in this class to those of its |
Fariborz Jahanian | 7cf1886 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 1395 | // super class. |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 1396 | ComparePropertiesInBaseAndSuper(I); |
Fariborz Jahanian | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1397 | CompareProperties(I, DeclPtrTy::make(I)); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1398 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1399 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | // By the same token, they are also used to add new properties. No |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1401 | // need to compare the added property to those in the class. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1402 | |
Fariborz Jahanian | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1403 | // Compare protocol properties with those in category |
| 1404 | CompareProperties(C, DeclPtrTy::make(C)); |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1405 | if (C->IsClassExtension()) |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1406 | DiagnoseClassExtensionDupMethods(C, C->getClassInterface()); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1407 | } |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1408 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1409 | if (CDecl->getIdentifier()) |
| 1410 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 1411 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 1412 | // and adds them to the DeclContext and global method pools. |
| 1413 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 1414 | E = CDecl->prop_end(); |
| 1415 | I != E; ++I) |
| 1416 | ProcessPropertyDecl(*I, CDecl); |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1417 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1418 | } |
| 1419 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1420 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1421 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1422 | if (LangOpts.ObjCNonFragileABI2) |
| 1423 | DefaultSynthesizeProperties(S, IC, IDecl); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1424 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1425 | AtomicPropertySetterGetterRules(IC, IDecl); |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 1426 | if (LangOpts.ObjCNonFragileABI2) |
| 1427 | while (IDecl->getSuperClass()) { |
| 1428 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 1429 | IDecl = IDecl->getSuperClass(); |
| 1430 | } |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1431 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1432 | SetIvarInitializers(IC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1434 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1435 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1437 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1438 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 41fd42e | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1439 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1440 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1441 | Categories; Categories = Categories->getNextClassCategory()) { |
| 1442 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1443 | ImplMethodsVsClassMethods(S, CatImplClass, Categories); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1449 | if (isInterfaceDeclKind) { |
| 1450 | // Reject invalid vardecls. |
| 1451 | for (unsigned i = 0; i != tuvNum; i++) { |
| 1452 | DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); |
| 1453 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 1454 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 1455 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 42959b2 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 1456 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1457 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1458 | } |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1459 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | |
| 1463 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 1464 | /// objective-c's type qualifier from the parser version of the same info. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | static Decl::ObjCDeclQualifier |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1466 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 1467 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 1468 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 1469 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 1470 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 1471 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 1472 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 1473 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 1474 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 1475 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 1476 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 1477 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 1478 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 1479 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1480 | |
| 1481 | return ret; |
| 1482 | } |
| 1483 | |
Ted Kremenek | 36712b2 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 1484 | static inline |
| 1485 | bool containsInvalidMethodImplAttribute(const AttributeList *A) { |
| 1486 | // The 'ibaction' attribute is allowed on method definitions because of |
| 1487 | // how the IBAction macro is used on both method declarations and definitions. |
| 1488 | // If the method definitions contains any other attributes, return true. |
| 1489 | while (A && A->getKind() == AttributeList::AT_IBAction) |
| 1490 | A = A->getNext(); |
| 1491 | return A != NULL; |
| 1492 | } |
| 1493 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1494 | Sema::DeclPtrTy Sema::ActOnMethodDeclaration( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1495 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1496 | tok::TokenKind MethodType, DeclPtrTy classDecl, |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1497 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1498 | Selector Sel, |
| 1499 | // optional arguments. The number of types/arguments is obtained |
| 1500 | // from the Sel.getNumArgs(). |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1501 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1502 | DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1503 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 1504 | bool isVariadic) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1505 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1506 | |
| 1507 | // Make sure we can establish a context for the method. |
| 1508 | if (!ClassDecl) { |
| 1509 | Diag(MethodLoc, diag::error_missing_method_context); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1510 | getLabelMap().clear(); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1511 | return DeclPtrTy(); |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1512 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1513 | QualType resultDeclType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 1515 | TypeSourceInfo *ResultTInfo = 0; |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1516 | if (ReturnType) { |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 1517 | resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1519 | // Methods cannot return interface types. All ObjC objects are |
| 1520 | // passed by reference. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1521 | if (resultDeclType->isObjCObjectType()) { |
Chris Lattner | de5a531 | 2009-04-11 19:08:56 +0000 | [diff] [blame] | 1522 | Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) |
| 1523 | << 0 << resultDeclType; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1524 | return DeclPtrTy(); |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } else // get the type for "id". |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1527 | resultDeclType = Context.getObjCIdType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
| 1529 | ObjCMethodDecl* ObjCMethod = |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1530 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 1531 | ResultTInfo, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | cast<DeclContext>(ClassDecl), |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1533 | MethodType == tok::minus, isVariadic, |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1534 | false, false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | MethodDeclKind == tok::objc_optional ? |
| 1536 | ObjCMethodDecl::Optional : |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1537 | ObjCMethodDecl::Required); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1539 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Chris Lattner | 23b0faf | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 1541 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1542 | QualType ArgType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1543 | TypeSourceInfo *DI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1545 | if (ArgInfo[i].Type == 0) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1546 | ArgType = Context.getObjCIdType(); |
| 1547 | DI = 0; |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1548 | } else { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1549 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Steve Naroff | b0498ee | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1550 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1551 | ArgType = adjustParameterType(ArgType); |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1552 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1554 | ParmVarDecl* Param |
| 1555 | = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, |
| 1556 | ArgInfo[i].Name, ArgType, DI, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1557 | VarDecl::None, VarDecl::None, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1559 | if (ArgType->isObjCObjectType()) { |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1560 | Diag(ArgInfo[i].NameLoc, |
| 1561 | diag::err_object_cannot_be_passed_returned_by_value) |
| 1562 | << 1 << ArgType; |
| 1563 | Param->setInvalidDecl(); |
| 1564 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1566 | Param->setObjCDeclQualifier( |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1567 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1569 | // Apply the attributes to the parameter. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1570 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1572 | Params.push_back(Param); |
| 1573 | } |
| 1574 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1575 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
| 1576 | ParmVarDecl *Param = CParamInfo[i].Param.getAs<ParmVarDecl>(); |
| 1577 | QualType ArgType = Param->getType(); |
| 1578 | if (ArgType.isNull()) |
| 1579 | ArgType = Context.getObjCIdType(); |
| 1580 | else |
| 1581 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
| 1582 | ArgType = adjustParameterType(ArgType); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1583 | if (ArgType->isObjCObjectType()) { |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1584 | Diag(Param->getLocation(), |
| 1585 | diag::err_object_cannot_be_passed_returned_by_value) |
| 1586 | << 1 << ArgType; |
| 1587 | Param->setInvalidDecl(); |
| 1588 | } |
| 1589 | Param->setDeclContext(ObjCMethod); |
Douglas Gregor | 114e55d | 2010-08-07 12:29:18 +0000 | [diff] [blame] | 1590 | if (Param->getDeclName()) |
| 1591 | IdResolver.RemoveDecl(Param); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1592 | Params.push_back(Param); |
| 1593 | } |
| 1594 | |
Fariborz Jahanian | cdabb31 | 2010-04-09 15:40:42 +0000 | [diff] [blame] | 1595 | ObjCMethod->setMethodParams(Context, Params.data(), Params.size(), |
| 1596 | Sel.getNumArgs()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1597 | ObjCMethod->setObjCDeclQualifier( |
| 1598 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 1599 | const ObjCMethodDecl *PrevMethod = 0; |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1600 | |
| 1601 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1602 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1604 | const ObjCMethodDecl *InterfaceMD = 0; |
| 1605 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | // For implementations (which can be very "coarse grain"), we add the |
| 1607 | // method now. This allows the AST to implement lookup methods that work |
| 1608 | // incrementally (without waiting until we parse the @end). It also allows |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1609 | // us to flag multiple declaration errors as they occur. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1611 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1612 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1613 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 1614 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1615 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1616 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 1617 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1618 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1619 | InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel, |
| 1620 | MethodType == tok::minus); |
Ted Kremenek | 36712b2 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 1621 | if (containsInvalidMethodImplAttribute(AttrList)) |
Fariborz Jahanian | 2bd617c | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 1622 | Diag(EndLoc, diag::warn_attribute_method_def); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | } else if (ObjCCategoryImplDecl *CatImpDecl = |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1624 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1625 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1626 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
| 1627 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1628 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1629 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
| 1630 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1631 | } |
Ted Kremenek | 36712b2 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 1632 | if (containsInvalidMethodImplAttribute(AttrList)) |
Fariborz Jahanian | 2bd617c | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 1633 | Diag(EndLoc, diag::warn_attribute_method_def); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1634 | } |
| 1635 | if (PrevMethod) { |
| 1636 | // You can never have two method definitions with the same name. |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1637 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1638 | << ObjCMethod->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1639 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1641 | |
| 1642 | // If the interface declared this method, and it was deprecated there, |
| 1643 | // mark it deprecated here. |
| 1644 | if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>()) |
| 1645 | ObjCMethod->addAttr(::new (Context) DeprecatedAttr()); |
| 1646 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1647 | return DeclPtrTy::make(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1650 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Douglas Gregor | 6ad0ef5 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 1651 | if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1652 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1654 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 1655 | D->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1660 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 1661 | /// instance variables of ClassName into Decls. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1663 | IdentifierInfo *ClassName, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1664 | llvm::SmallVectorImpl<DeclPtrTy> &Decls) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1665 | // Check that ClassName is a valid class |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1666 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1667 | if (!Class) { |
| 1668 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 1669 | return; |
| 1670 | } |
Fariborz Jahanian | ece1b2b | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 1671 | if (LangOpts.ObjCNonFragileABI) { |
| 1672 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 1673 | return; |
| 1674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1676 | // Collect the instance variables |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 1677 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 1678 | Context.CollectObjCIvars(Class, RecFields); |
| 1679 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
| 1680 | for (unsigned i = 0; i < RecFields.size(); i++) { |
| 1681 | FieldDecl* ID = RecFields[i]; |
| 1682 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()); |
| 1683 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(), |
| 1684 | ID->getIdentifier(), ID->getType(), |
| 1685 | ID->getBitWidth()); |
| 1686 | Decls.push_back(Sema::DeclPtrTy::make(FD)); |
| 1687 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1689 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1690 | for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1691 | D != Decls.end(); ++D) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1692 | FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1693 | if (getLangOptions().CPlusPlus) |
| 1694 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1695 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1696 | Record->addDecl(FD); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1700 | /// \brief Build a type-check a new Objective-C exception variable declaration. |
| 1701 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, |
| 1702 | QualType T, |
| 1703 | IdentifierInfo *Name, |
| 1704 | SourceLocation NameLoc, |
| 1705 | bool Invalid) { |
| 1706 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 1707 | // duration shall not be qualified by an address-space qualifier." |
| 1708 | // Since all parameters have automatic store duration, they can not have |
| 1709 | // an address space. |
| 1710 | if (T.getAddressSpace() != 0) { |
| 1711 | Diag(NameLoc, diag::err_arg_with_address_space); |
| 1712 | Invalid = true; |
| 1713 | } |
| 1714 | |
| 1715 | // An @catch parameter must be an unqualified object pointer type; |
| 1716 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 1717 | if (Invalid) { |
| 1718 | // Don't do any further checking. |
Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 1719 | } else if (T->isDependentType()) { |
| 1720 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1721 | } else if (!T->isObjCObjectPointerType()) { |
| 1722 | Invalid = true; |
| 1723 | Diag(NameLoc ,diag::err_catch_param_not_objc_type); |
| 1724 | } else if (T->isObjCQualifiedIdType()) { |
| 1725 | Invalid = true; |
| 1726 | Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm); |
| 1727 | } |
| 1728 | |
| 1729 | VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo, |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 1730 | VarDecl::None, VarDecl::None); |
| 1731 | New->setExceptionVariable(true); |
| 1732 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1733 | if (Invalid) |
| 1734 | New->setInvalidDecl(); |
| 1735 | return New; |
| 1736 | } |
| 1737 | |
Douglas Gregor | e11ee11 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 1738 | Sema::DeclPtrTy Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1739 | const DeclSpec &DS = D.getDeclSpec(); |
| 1740 | |
| 1741 | // We allow the "register" storage class on exception variables because |
| 1742 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 1743 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 1744 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 1745 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
| 1746 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
| 1747 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
| 1748 | << DS.getStorageClassSpec(); |
| 1749 | } |
| 1750 | if (D.getDeclSpec().isThreadSpecified()) |
| 1751 | Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread); |
| 1752 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1753 | |
| 1754 | DiagnoseFunctionSpecifiers(D); |
| 1755 | |
| 1756 | // Check that there are no default arguments inside the type of this |
| 1757 | // exception object (C++ only). |
| 1758 | if (getLangOptions().CPlusPlus) |
| 1759 | CheckExtraCXXDefaultArguments(D); |
| 1760 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1761 | TagDecl *OwnedDecl = 0; |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 1762 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl); |
| 1763 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1764 | |
| 1765 | if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) { |
| 1766 | // Objective-C++: Types shall not be defined in exception types. |
| 1767 | Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type) |
| 1768 | << Context.getTypeDeclType(OwnedDecl); |
| 1769 | } |
| 1770 | |
| 1771 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(), |
| 1772 | D.getIdentifierLoc(), |
| 1773 | D.isInvalidType()); |
| 1774 | |
| 1775 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 1776 | if (D.getCXXScopeSpec().isSet()) { |
| 1777 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 1778 | << D.getCXXScopeSpec().getRange(); |
| 1779 | New->setInvalidDecl(); |
| 1780 | } |
| 1781 | |
| 1782 | // Add the parameter declaration into this scope. |
| 1783 | S->AddDecl(DeclPtrTy::make(New)); |
| 1784 | if (D.getIdentifier()) |
| 1785 | IdResolver.AddDecl(New); |
| 1786 | |
| 1787 | ProcessDeclAttributes(S, New, D); |
| 1788 | |
| 1789 | if (New->hasAttr<BlocksAttr>()) |
| 1790 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
| 1791 | return DeclPtrTy::make(New); |
Douglas Gregor | e11ee11 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 1792 | } |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 1793 | |
| 1794 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1795 | /// initialization. |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 1796 | void Sema::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI, |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1797 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 1798 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1799 | E = OI->ivar_end(); I != E; ++I) { |
| 1800 | ObjCIvarDecl *Iv = (*I); |
| 1801 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 1802 | if (QT->isRecordType()) |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1803 | Ivars.push_back(*I); |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | // Find ivars to construct/destruct in class extension. |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1807 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1808 | CDecl = CDecl->getNextClassExtension()) { |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 1809 | for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 1810 | E = CDecl->ivar_end(); I != E; ++I) { |
| 1811 | ObjCIvarDecl *Iv = (*I); |
| 1812 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 1813 | if (QT->isRecordType()) |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1814 | Ivars.push_back(*I); |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | // Also add any ivar defined in this class's implementation. This |
| 1819 | // includes synthesized ivars. |
| 1820 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) { |
| 1821 | for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 1822 | E = ImplDecl->ivar_end(); I != E; ++I) { |
| 1823 | ObjCIvarDecl *Iv = (*I); |
| 1824 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 1825 | if (QT->isRecordType()) |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1826 | Ivars.push_back(*I); |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1830 | |
| 1831 | void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, |
| 1832 | CXXBaseOrMemberInitializer ** initializers, |
| 1833 | unsigned numInitializers) { |
| 1834 | if (numInitializers > 0) { |
| 1835 | NumIvarInitializers = numInitializers; |
| 1836 | CXXBaseOrMemberInitializer **ivarInitializers = |
| 1837 | new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers]; |
| 1838 | memcpy(ivarInitializers, initializers, |
| 1839 | numInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1840 | IvarInitializers = ivarInitializers; |
| 1841 | } |
| 1842 | } |
| 1843 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1844 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
| 1845 | if (ReferencedSelectors.empty()) |
| 1846 | return; |
| 1847 | for (llvm::DenseMap<Selector, SourceLocation>::iterator S = |
| 1848 | ReferencedSelectors.begin(), |
| 1849 | E = ReferencedSelectors.end(); S != E; ++S) { |
| 1850 | Selector Sel = (*S).first; |
| 1851 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
| 1852 | Diag((*S).second, diag::warn_unimplemented_selector) << Sel; |
| 1853 | } |
| 1854 | return; |
| 1855 | } |