Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective C declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 16 | #include "clang/Sema/ExternalSemaSource.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ScopeInfo.h" |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseSet.h" |
| 24 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 27 | static void DiagnoseObjCImplementedDeprecations(Sema &S, |
| 28 | NamedDecl *ND, |
| 29 | SourceLocation ImplLoc, |
| 30 | int select) { |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 31 | if (ND && ND->getAttr<DeprecatedAttr>()) { |
Fariborz Jahanian | 98d810e | 2011-02-16 00:30:31 +0000 | [diff] [blame] | 32 | S.Diag(ImplLoc, diag::warn_deprecated_def) << select; |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 33 | if (select == 0) |
| 34 | S.Diag(ND->getLocation(), diag::note_method_declared_at); |
| 35 | else |
| 36 | S.Diag(ND->getLocation(), diag::note_previous_decl) << "class"; |
| 37 | } |
| 38 | } |
| 39 | |
Steve Naroff | ebf6443 | 2009-02-28 16:59:13 +0000 | [diff] [blame] | 40 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 41 | /// and user declared, in the method definition's AST. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 42 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 43 | assert(getCurMethodDecl() == 0 && "Method parsing confused"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 44 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 46 | // If we don't have a valid method decl, simply return. |
| 47 | if (!MDecl) |
| 48 | return; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 49 | |
| 50 | // Allow the rest of sema to find private method decl implementations. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 51 | if (MDecl->isInstanceMethod()) |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 52 | AddInstanceMethodToGlobalPool(MDecl, true); |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 53 | else |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 54 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 55 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 56 | // Allow all of Sema to see that we are entering a method definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 57 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 58 | PushFunctionScope(); |
| 59 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 60 | // Create Decl objects for each parameter, entrring them in the scope for |
| 61 | // binding to their use. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 62 | |
| 63 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 64 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Daniel Dunbar | 451318c | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 66 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 67 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 69 | // Introduce all of the other parameters into this scope. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 70 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
Fariborz Jahanian | 23c0104 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 71 | E = MDecl->param_end(); PI != E; ++PI) { |
| 72 | ParmVarDecl *Param = (*PI); |
| 73 | if (!Param->isInvalidDecl() && |
| 74 | RequireCompleteType(Param->getLocation(), Param->getType(), |
| 75 | diag::err_typecheck_decl_incomplete_type)) |
| 76 | Param->setInvalidDecl(); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 77 | if ((*PI)->getIdentifier()) |
| 78 | PushOnScopeChains(*PI, FnBodyScope); |
Fariborz Jahanian | 23c0104 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 79 | } |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 80 | // Warn on implementating deprecated methods under |
| 81 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 82 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) |
| 83 | if (ObjCMethodDecl *IMD = |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 84 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod())) |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 85 | DiagnoseObjCImplementedDeprecations(*this, |
| 86 | dyn_cast<NamedDecl>(IMD), |
| 87 | MDecl->getLocation(), 0); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 90 | Decl *Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 91 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 92 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 93 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 94 | Decl * const *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 95 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 96 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 97 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 99 | // Check for another declaration kind with the same name. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 100 | NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 101 | LookupOrdinaryName, ForRedeclaration); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 103 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 104 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 105 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 108 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 109 | if (IDecl) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 110 | // Class already seen. Is it a forward declaration? |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 111 | if (!IDecl->isForwardDecl()) { |
| 112 | IDecl->setInvalidDecl(); |
| 113 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); |
| 114 | Diag(IDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | b8b96af | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 115 | |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 116 | // Return the previous class interface. |
| 117 | // FIXME: don't leak the objects passed in! |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 118 | return IDecl; |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 119 | } else { |
| 120 | IDecl->setLocation(AtInterfaceLoc); |
| 121 | IDecl->setForwardDecl(false); |
| 122 | IDecl->setClassLoc(ClassLoc); |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 123 | // If the forward decl was in a PCH, we need to write it again in a |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 124 | // dependent AST file. |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 125 | IDecl->setChangedSinceDeserialization(true); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 126 | |
| 127 | // Since this ObjCInterfaceDecl was created by a forward declaration, |
| 128 | // we now add it to the DeclContext since it wasn't added before |
| 129 | // (see ActOnForwardClassDeclaration). |
| 130 | IDecl->setLexicalDeclContext(CurContext); |
| 131 | CurContext->addDecl(IDecl); |
| 132 | |
| 133 | if (AttrList) |
| 134 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 135 | } |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 136 | } else { |
| 137 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 138 | ClassName, ClassLoc); |
| 139 | if (AttrList) |
| 140 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
| 141 | |
| 142 | PushOnScopeChains(IDecl, TUScope); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 145 | if (SuperName) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 146 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 147 | PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 148 | LookupOrdinaryName); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 149 | |
| 150 | if (!PrevDecl) { |
| 151 | // Try to correct for a typo in the superclass name. |
| 152 | LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 153 | if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 154 | (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) { |
| 155 | Diag(SuperLoc, diag::err_undef_superclass_suggest) |
| 156 | << SuperName << ClassName << PrevDecl->getDeclName(); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 157 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) |
| 158 | << PrevDecl->getDeclName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 162 | if (PrevDecl == IDecl) { |
| 163 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 164 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 165 | IDecl->setLocEnd(ClassLoc); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 166 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | ObjCInterfaceDecl *SuperClassDecl = |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 168 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 169 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 170 | // Diagnose classes that inherit from deprecated classes. |
| 171 | if (SuperClassDecl) |
| 172 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 174 | if (PrevDecl && SuperClassDecl == 0) { |
| 175 | // The previous declaration was not a class decl. Check if we have a |
| 176 | // typedef. If we do, get the underlying class type. |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 177 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 178 | QualType T = TDecl->getUnderlyingType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 179 | if (T->isObjCObjectType()) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 180 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) |
| 181 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 185 | // This handles the following case: |
| 186 | // |
| 187 | // typedef int SuperClass; |
| 188 | // @interface MyClass : SuperClass {} @end |
| 189 | // |
| 190 | if (!SuperClassDecl) { |
| 191 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 192 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 196 | if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 197 | if (!SuperClassDecl) |
| 198 | Diag(SuperLoc, diag::err_undef_superclass) |
| 199 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 200 | else if (SuperClassDecl->isForwardDecl()) |
| 201 | Diag(SuperLoc, diag::err_undef_superclass) |
| 202 | << SuperClassDecl->getDeclName() << ClassName |
| 203 | << SourceRange(AtInterfaceLoc, ClassLoc); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 204 | } |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 205 | IDecl->setSuperClass(SuperClassDecl); |
| 206 | IDecl->setSuperClassLoc(SuperLoc); |
| 207 | IDecl->setLocEnd(SuperLoc); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 208 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 209 | } else { // we have a root class. |
| 210 | IDecl->setLocEnd(ClassLoc); |
| 211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 213 | // Check then save referenced protocols. |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 214 | if (NumProtoRefs) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 215 | IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 216 | ProtoLocs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 217 | IDecl->setLocEnd(EndProtoLoc); |
| 218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 220 | CheckObjCDeclScope(IDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 221 | return IDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 225 | /// @compatibility_alias declaration. It sets up the alias relationships. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 226 | Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
| 227 | IdentifierInfo *AliasName, |
| 228 | SourceLocation AliasLocation, |
| 229 | IdentifierInfo *ClassName, |
| 230 | SourceLocation ClassLocation) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 231 | // Look for previous declaration of alias name |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 232 | NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 233 | LookupOrdinaryName, ForRedeclaration); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 234 | if (ADecl) { |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 235 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 236 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 237 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 238 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 239 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 240 | return 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 241 | } |
| 242 | // Check for class declaration |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 243 | NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 244 | LookupOrdinaryName, ForRedeclaration); |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 245 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { |
| 246 | QualType T = TDecl->getUnderlyingType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 247 | if (T->isObjCObjectType()) { |
| 248 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 249 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 250 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 251 | LookupOrdinaryName, ForRedeclaration); |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 255 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 256 | if (CDecl == 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 257 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 258 | if (CDeclU) |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 259 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 260 | return 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 263 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 265 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 267 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 268 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 269 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 270 | return AliasDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 273 | void Sema::CheckForwardProtocolDeclarationForCircularDependency( |
| 274 | IdentifierInfo *PName, |
| 275 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | const ObjCList<ObjCProtocolDecl> &PList) { |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 277 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 278 | E = PList.end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 280 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 281 | Ploc)) { |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 282 | if (PDecl->getIdentifier() == PName) { |
| 283 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 284 | Diag(PrevLoc, diag::note_previous_definition); |
| 285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 287 | PDecl->getLocation(), PDecl->getReferencedProtocols()); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 292 | Decl * |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 293 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 294 | IdentifierInfo *ProtocolName, |
| 295 | SourceLocation ProtocolLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 296 | Decl * const *ProtoRefs, |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 297 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 298 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 299 | SourceLocation EndProtoLoc, |
| 300 | AttributeList *AttrList) { |
| 301 | // FIXME: Deal with AttrList. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 302 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 303 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 304 | if (PDecl) { |
| 305 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 306 | if (!PDecl->isForwardDecl()) { |
Fariborz Jahanian | e2573e5 | 2009-04-06 23:43:32 +0000 | [diff] [blame] | 307 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
Chris Lattner | b8b96af | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 308 | Diag(PDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 309 | // Just return the protocol we already had. |
| 310 | // FIXME: don't leak the objects passed in! |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 311 | return PDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 312 | } |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 313 | ObjCList<ObjCProtocolDecl> PList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 315 | CheckForwardProtocolDeclarationForCircularDependency( |
| 316 | ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Steve Naroff | f11b508 | 2008-08-13 16:39:22 +0000 | [diff] [blame] | 318 | // Make sure the cached decl gets a valid start location. |
| 319 | PDecl->setLocation(AtProtoInterfaceLoc); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 320 | PDecl->setForwardDecl(false); |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 321 | CurContext->addDecl(PDecl); |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 322 | // Repeat in dependent AST files. |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 323 | PDecl->setChangedSinceDeserialization(true); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 324 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 326 | AtProtoInterfaceLoc,ProtocolName); |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 327 | PushOnScopeChains(PDecl, TUScope); |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 328 | PDecl->setForwardDecl(false); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 329 | } |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 330 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 331 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 332 | if (NumProtoRefs) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 333 | /// Check then save referenced protocols. |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 334 | PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
| 335 | ProtoLocs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 336 | PDecl->setLocEnd(EndProtoLoc); |
| 337 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
| 339 | CheckObjCDeclScope(PDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 340 | return PDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 344 | /// issues an error if they are not declared. It returns list of |
| 345 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 346 | void |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 347 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 348 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 349 | unsigned NumProtocols, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 350 | llvm::SmallVectorImpl<Decl *> &Protocols) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 351 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 352 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first, |
| 353 | ProtocolId[i].second); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 354 | if (!PDecl) { |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 355 | LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second, |
| 356 | LookupObjCProtocolName); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 357 | if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 358 | (PDecl = R.getAsSingle<ObjCProtocolDecl>())) { |
| 359 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest) |
| 360 | << ProtocolId[i].first << R.getLookupName(); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 361 | Diag(PDecl->getLocation(), diag::note_previous_decl) |
| 362 | << PDecl->getDeclName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | if (!PDecl) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 367 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 368 | << ProtocolId[i].first; |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 369 | continue; |
| 370 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 372 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 373 | |
| 374 | // If this is a forward declaration and we are supposed to warn in this |
| 375 | // case, do it. |
| 376 | if (WarnOnDeclarations && PDecl->isForwardDecl()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 377 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 378 | << ProtocolId[i].first; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 379 | Protocols.push_back(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Fariborz Jahanian | 78c39c7 | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 383 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 384 | /// a class method in its extension. |
| 385 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 387 | ObjCInterfaceDecl *ID) { |
| 388 | if (!ID) |
| 389 | return; // Possibly due to previous error |
| 390 | |
| 391 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 392 | for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(), |
| 393 | e = ID->meth_end(); i != e; ++i) { |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 394 | ObjCMethodDecl *MD = *i; |
| 395 | MethodMap[MD->getSelector()] = MD; |
| 396 | } |
| 397 | |
| 398 | if (MethodMap.empty()) |
| 399 | return; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 400 | for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(), |
| 401 | e = CAT->meth_end(); i != e; ++i) { |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 402 | ObjCMethodDecl *Method = *i; |
| 403 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
| 404 | if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
| 405 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 406 | << Method->getDeclName(); |
| 407 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Chris Lattner | 58fe03b | 2009-04-12 08:43:13 +0000 | [diff] [blame] | 412 | /// ActOnForwardProtocolDeclaration - Handle @protocol foo; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 413 | Decl * |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 414 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 415 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 416 | unsigned NumElts, |
| 417 | AttributeList *attrList) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 418 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 419 | llvm::SmallVector<SourceLocation, 8> ProtoLocs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 421 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 422 | IdentifierInfo *Ident = IdentList[i].first; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 423 | ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second); |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 424 | bool isNew = false; |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 425 | if (PDecl == 0) { // Not already seen? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 427 | IdentList[i].second, Ident); |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 428 | PushOnScopeChains(PDecl, TUScope, false); |
| 429 | isNew = true; |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 430 | } |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 431 | if (attrList) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 432 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 433 | if (!isNew) |
| 434 | PDecl->setChangedSinceDeserialization(true); |
| 435 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 436 | Protocols.push_back(PDecl); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 437 | ProtoLocs.push_back(IdentList[i].second); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
| 440 | ObjCForwardProtocolDecl *PDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 441 | ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 442 | Protocols.data(), Protocols.size(), |
| 443 | ProtoLocs.data()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 444 | CurContext->addDecl(PDecl); |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 445 | CheckObjCDeclScope(PDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 446 | return PDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 447 | } |
| 448 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 449 | Decl *Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 450 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 451 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 452 | IdentifierInfo *CategoryName, |
| 453 | SourceLocation CategoryLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 454 | Decl * const *ProtoRefs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 455 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 456 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 457 | SourceLocation EndProtoLoc) { |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 458 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 459 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 460 | |
| 461 | /// Check that class of this category is already completely declared. |
| 462 | if (!IDecl || IDecl->isForwardDecl()) { |
| 463 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 464 | // the enclosing method declarations. We mark the decl invalid |
| 465 | // to make it clear that this isn't a valid AST. |
| 466 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 467 | ClassLoc, CategoryLoc, CategoryName); |
| 468 | CDecl->setInvalidDecl(); |
| 469 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 470 | return CDecl; |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 473 | if (!CategoryName && IDecl->getImplementation()) { |
| 474 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| 475 | Diag(IDecl->getImplementation()->getLocation(), |
| 476 | diag::note_implementation_declared); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 479 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 480 | ClassLoc, CategoryLoc, CategoryName); |
| 481 | // FIXME: PushOnScopeChains? |
| 482 | CurContext->addDecl(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 483 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 484 | CDecl->setClassInterface(IDecl); |
| 485 | // Insert class extension to the list of class's categories. |
| 486 | if (!CategoryName) |
| 487 | CDecl->insertNextClassCategory(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 16b34b4 | 2009-02-16 21:30:01 +0000 | [diff] [blame] | 489 | // If the interface is deprecated, warn about it. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 490 | (void)DiagnoseUseOfDecl(IDecl, ClassLoc); |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 491 | |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 492 | if (CategoryName) { |
| 493 | /// Check for duplicate interface declaration for this category |
| 494 | ObjCCategoryDecl *CDeclChain; |
| 495 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 496 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 497 | if (CDeclChain->getIdentifier() == CategoryName) { |
| 498 | // Class extensions can be declared multiple times. |
| 499 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 500 | << ClassName << CategoryName; |
| 501 | Diag(CDeclChain->getLocation(), diag::note_previous_definition); |
| 502 | break; |
| 503 | } |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 504 | } |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 505 | if (!CDeclChain) |
| 506 | CDecl->insertNextClassCategory(); |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 507 | } |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 509 | if (NumProtoRefs) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 510 | CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 511 | ProtoLocs, Context); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 512 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 513 | if (CDecl->IsClassExtension()) |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 514 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs, |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 515 | NumProtoRefs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 516 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 518 | CheckObjCDeclScope(CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 519 | return CDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 523 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 524 | /// object. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 525 | Decl *Sema::ActOnStartCategoryImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 526 | SourceLocation AtCatImplLoc, |
| 527 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 528 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 529 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 530 | ObjCCategoryDecl *CatIDecl = 0; |
| 531 | if (IDecl) { |
| 532 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 533 | if (!CatIDecl) { |
| 534 | // Category @implementation with no corresponding @interface. |
| 535 | // Create and install one. |
| 536 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(), |
Douglas Gregor | 3db211b | 2010-01-16 16:38:58 +0000 | [diff] [blame] | 537 | SourceLocation(), SourceLocation(), |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 538 | CatName); |
| 539 | CatIDecl->setClassInterface(IDecl); |
| 540 | CatIDecl->insertNextClassCategory(); |
| 541 | } |
| 542 | } |
| 543 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | ObjCCategoryImplDecl *CDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 545 | ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, |
| 546 | IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 547 | /// Check that class of this category is already completely declared. |
| 548 | if (!IDecl || IDecl->isForwardDecl()) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 549 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 551 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 552 | CurContext->addDecl(CDecl); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 553 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 554 | /// Check that CatName, category name, is not used in another implementation. |
| 555 | if (CatIDecl) { |
| 556 | if (CatIDecl->getImplementation()) { |
| 557 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 558 | << CatName; |
| 559 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 560 | diag::note_previous_definition); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 561 | } else { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 562 | CatIDecl->setImplementation(CDecl); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 563 | // Warn on implementating category of deprecated class under |
| 564 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 565 | DiagnoseObjCImplementedDeprecations(*this, |
| 566 | dyn_cast<NamedDecl>(IDecl), |
| 567 | CDecl->getLocation(), 2); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 568 | } |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 569 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 571 | CheckObjCDeclScope(CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 572 | return CDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 573 | } |
| 574 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 575 | Decl *Sema::ActOnStartClassImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 576 | SourceLocation AtClassImplLoc, |
| 577 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | IdentifierInfo *SuperClassname, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 579 | SourceLocation SuperClassLoc) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 580 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 581 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 582 | NamedDecl *PrevDecl |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 583 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 584 | ForRedeclaration); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 585 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 586 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 587 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 588 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
| 589 | // If this is a forward declaration of an interface, warn. |
| 590 | if (IDecl->isForwardDecl()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 591 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 592 | IDecl = 0; |
Fariborz Jahanian | 77a6be4 | 2009-04-23 21:49:04 +0000 | [diff] [blame] | 593 | } |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 594 | } else { |
| 595 | // We did not find anything with the name ClassName; try to correct for |
| 596 | // typos in the class name. |
| 597 | LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 598 | if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) && |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 599 | (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) { |
Douglas Gregor | a6f2638 | 2010-01-06 23:44:25 +0000 | [diff] [blame] | 600 | // Suggest the (potentially) correct interface name. However, put the |
| 601 | // fix-it hint itself in a separate note, since changing the name in |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 602 | // the warning would make the fix-it change semantics.However, don't |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 603 | // provide a code-modification hint or use the typo name for recovery, |
| 604 | // because this is just a warning. The program may actually be correct. |
| 605 | Diag(ClassLoc, diag::warn_undef_interface_suggest) |
| 606 | << ClassName << R.getLookupName(); |
Douglas Gregor | a6f2638 | 2010-01-06 23:44:25 +0000 | [diff] [blame] | 607 | Diag(IDecl->getLocation(), diag::note_previous_decl) |
| 608 | << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 609 | << FixItHint::CreateReplacement(ClassLoc, |
| 610 | R.getLookupName().getAsString()); |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 611 | IDecl = 0; |
| 612 | } else { |
| 613 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 614 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 615 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 617 | // Check that super class name is valid class name |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 618 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 619 | if (SuperClassname) { |
| 620 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 621 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 622 | LookupOrdinaryName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 623 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 624 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 625 | << SuperClassname; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 626 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 627 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 629 | if (!SDecl) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 630 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 631 | << SuperClassname << ClassName; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 632 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 633 | // This implementation and its interface do not have the same |
| 634 | // super class. |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 635 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 636 | << SDecl->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 637 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 642 | if (!IDecl) { |
| 643 | // Legacy case of @implementation with no corresponding @interface. |
| 644 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | f641492 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 645 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 646 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 647 | // copy them over. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 649 | ClassName, ClassLoc, false, true); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 650 | IDecl->setSuperClass(SDecl); |
| 651 | IDecl->setLocEnd(ClassLoc); |
Douglas Gregor | 8b9fb30 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 652 | |
| 653 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 654 | } else { |
| 655 | // Mark the interface as being completed, even if it was just as |
| 656 | // @class ....; |
| 657 | // declaration; the user cannot reopen it. |
| 658 | IDecl->setForwardDecl(false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
| 661 | ObjCImplementationDecl* IMPDecl = |
| 662 | ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 663 | IDecl, SDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 665 | if (CheckObjCDeclScope(IMPDecl)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 666 | return IMPDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 668 | // Check that there is no duplicate implementation of this class. |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 669 | if (IDecl->getImplementation()) { |
| 670 | // FIXME: Don't leak everything! |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 671 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 672 | Diag(IDecl->getImplementation()->getLocation(), |
| 673 | diag::note_previous_definition); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 674 | } else { // add it to the list. |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 675 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 676 | PushOnScopeChains(IMPDecl, TUScope); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 677 | // Warn on implementating deprecated class under |
| 678 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 679 | DiagnoseObjCImplementedDeprecations(*this, |
| 680 | dyn_cast<NamedDecl>(IDecl), |
| 681 | IMPDecl->getLocation(), 1); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 682 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 683 | return IMPDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 686 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 687 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 688 | SourceLocation RBrace) { |
| 689 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 690 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 691 | if (!IDecl) |
| 692 | return; |
| 693 | /// Check case of non-existing @interface decl. |
| 694 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 695 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 696 | if (IDecl->isImplicitInterfaceDecl()) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 697 | IDecl->setLocEnd(RBrace); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 698 | // Add ivar's to class's DeclContext. |
| 699 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | 2f14c4d | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 700 | ivars[i]->setLexicalDeclContext(ImpDecl); |
| 701 | IDecl->makeDeclVisibleInContext(ivars[i], false); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 702 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | // If implementation has empty ivar list, just return. |
| 708 | if (numIvars == 0) |
| 709 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 711 | assert(ivars && "missing @implementation ivars"); |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 712 | if (LangOpts.ObjCNonFragileABI2) { |
| 713 | if (ImpDecl->getSuperClass()) |
| 714 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 715 | for (unsigned i = 0; i < numIvars; i++) { |
| 716 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 717 | if (const ObjCIvarDecl *ClsIvar = |
| 718 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 719 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 720 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 721 | continue; |
| 722 | } |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 723 | // Instance ivar to Implementation's DeclContext. |
| 724 | ImplIvar->setLexicalDeclContext(ImpDecl); |
| 725 | IDecl->makeDeclVisibleInContext(ImplIvar, false); |
| 726 | ImpDecl->addDecl(ImplIvar); |
| 727 | } |
| 728 | return; |
| 729 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 730 | // Check interface's Ivar list against those in the implementation. |
| 731 | // names and types must match. |
| 732 | // |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 733 | unsigned j = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 735 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 736 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 737 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 738 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 739 | assert (ImplIvar && "missing implementation ivar"); |
| 740 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 742 | // First, make sure the types match. |
Chris Lattner | 1b63eef | 2008-07-27 00:05:05 +0000 | [diff] [blame] | 743 | if (Context.getCanonicalType(ImplIvar->getType()) != |
| 744 | Context.getCanonicalType(ClsIvar->getType())) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 745 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 746 | << ImplIvar->getIdentifier() |
| 747 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 748 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 749 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { |
| 750 | Expr *ImplBitWidth = ImplIvar->getBitWidth(); |
| 751 | Expr *ClsBitWidth = ClsIvar->getBitWidth(); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 752 | if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() != |
| 753 | ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) { |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 754 | Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) |
| 755 | << ImplIvar->getIdentifier(); |
| 756 | Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); |
| 757 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | } |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 759 | // Make sure the names are identical. |
| 760 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 761 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 762 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 763 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 764 | } |
| 765 | --numIvars; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 766 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 768 | if (numIvars > 0) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 769 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 770 | else if (IVI != IVE) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 771 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 774 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 775 | bool &IncompleteImpl, unsigned DiagID) { |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 776 | if (!IncompleteImpl) { |
| 777 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 778 | IncompleteImpl = true; |
| 779 | } |
Fariborz Jahanian | 61c8d3e | 2010-10-29 23:20:05 +0000 | [diff] [blame] | 780 | if (DiagID == diag::warn_unimplemented_protocol_method) |
| 781 | Diag(ImpLoc, DiagID) << method->getDeclName(); |
| 782 | else |
| 783 | Diag(method->getLocation(), DiagID) << method->getDeclName(); |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 784 | } |
| 785 | |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 786 | /// Determines if type B can be substituted for type A. Returns true if we can |
| 787 | /// guarantee that anything that the user will do to an object of type A can |
| 788 | /// also be done to an object of type B. This is trivially true if the two |
| 789 | /// types are the same, or if B is a subclass of A. It becomes more complex |
| 790 | /// in cases where protocols are involved. |
| 791 | /// |
| 792 | /// Object types in Objective-C describe the minimum requirements for an |
| 793 | /// object, rather than providing a complete description of a type. For |
| 794 | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| 795 | /// The principle of substitutability means that we may use an instance of A |
| 796 | /// anywhere that we may use an instance of B - it will implement all of the |
| 797 | /// ivars of B and all of the methods of B. |
| 798 | /// |
| 799 | /// This substitutability is important when type checking methods, because |
| 800 | /// the implementation may have stricter type definitions than the interface. |
| 801 | /// The interface specifies minimum requirements, but the implementation may |
| 802 | /// have more accurate ones. For example, a method may privately accept |
| 803 | /// instances of B, but only publish that it accepts instances of A. Any |
| 804 | /// object passed to it will be type checked against B, and so will implicitly |
| 805 | /// by a valid A*. Similarly, a method may return a subclass of the class that |
| 806 | /// it is declared as returning. |
| 807 | /// |
| 808 | /// This is most important when considering subclassing. A method in a |
| 809 | /// subclass must accept any object as an argument that its superclass's |
| 810 | /// implementation accepts. It may, however, accept a more general type |
| 811 | /// without breaking substitutability (i.e. you can still use the subclass |
| 812 | /// anywhere that you can use the superclass, but not vice versa). The |
| 813 | /// converse requirement applies to return types: the return type for a |
| 814 | /// subclass method must be a valid object of the kind that the superclass |
| 815 | /// advertises, but it may be specified more accurately. This avoids the need |
| 816 | /// for explicit down-casting by callers. |
| 817 | /// |
| 818 | /// Note: This is a stricter requirement than for assignment. |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 819 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 820 | const ObjCObjectPointerType *A, |
| 821 | const ObjCObjectPointerType *B, |
| 822 | bool rejectId) { |
| 823 | // Reject a protocol-unqualified id. |
| 824 | if (rejectId && B->isObjCIdType()) return false; |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 825 | |
| 826 | // If B is a qualified id, then A must also be a qualified id and it must |
| 827 | // implement all of the protocols in B. It may not be a qualified class. |
| 828 | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| 829 | // stricter definition so it is not substitutable for id<A>. |
| 830 | if (B->isObjCQualifiedIdType()) { |
| 831 | return A->isObjCQualifiedIdType() && |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 832 | Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), |
| 833 | QualType(B,0), |
| 834 | false); |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* |
| 838 | // id is a special type that bypasses type checking completely. We want a |
| 839 | // warning when it is used in one place but not another. |
| 840 | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| 841 | |
| 842 | |
| 843 | // If B is a qualified id, then A must also be a qualified id (which it isn't |
| 844 | // if we've got this far) |
| 845 | if (B->isObjCQualifiedIdType()) return false; |
| 846 | */ |
| 847 | |
| 848 | // Now we know that A and B are (potentially-qualified) class types. The |
| 849 | // normal rules for assignment apply. |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 850 | return Context.canAssignObjCInterfaces(A, B); |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 851 | } |
| 852 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 853 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 854 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 855 | } |
| 856 | |
| 857 | static void CheckMethodOverrideReturn(Sema &S, |
| 858 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 859 | ObjCMethodDecl *MethodDecl, |
| 860 | bool IsProtocolMethodDecl) { |
| 861 | if (IsProtocolMethodDecl && |
| 862 | (MethodDecl->getObjCDeclQualifier() != |
| 863 | MethodImpl->getObjCDeclQualifier())) { |
| 864 | S.Diag(MethodImpl->getLocation(), |
| 865 | diag::warn_conflicting_ret_type_modifiers) |
| 866 | << MethodImpl->getDeclName() |
| 867 | << getTypeRange(MethodImpl->getResultTypeSourceInfo()); |
| 868 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
| 869 | << getTypeRange(MethodDecl->getResultTypeSourceInfo()); |
| 870 | } |
| 871 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 872 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(), |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 873 | MethodDecl->getResultType())) |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 874 | return; |
| 875 | |
| 876 | unsigned DiagID = diag::warn_conflicting_ret_types; |
| 877 | |
| 878 | // Mismatches between ObjC pointers go into a different warning |
| 879 | // category, and sometimes they're even completely whitelisted. |
| 880 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 881 | MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) { |
| 882 | if (const ObjCObjectPointerType *IfacePtrTy = |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 883 | MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 884 | // Allow non-matching return types as long as they don't violate |
| 885 | // the principle of substitutability. Specifically, we permit |
| 886 | // return types that are subclasses of the declared return type, |
| 887 | // or that are more-qualified versions of the declared type. |
| 888 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
| 889 | return; |
| 890 | |
| 891 | DiagID = diag::warn_non_covariant_ret_types; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | S.Diag(MethodImpl->getLocation(), DiagID) |
| 896 | << MethodImpl->getDeclName() |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 897 | << MethodDecl->getResultType() |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 898 | << MethodImpl->getResultType() |
| 899 | << getTypeRange(MethodImpl->getResultTypeSourceInfo()); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 900 | S.Diag(MethodDecl->getLocation(), diag::note_previous_definition) |
| 901 | << getTypeRange(MethodDecl->getResultTypeSourceInfo()); |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static void CheckMethodOverrideParam(Sema &S, |
| 905 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 906 | ObjCMethodDecl *MethodDecl, |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 907 | ParmVarDecl *ImplVar, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 908 | ParmVarDecl *IfaceVar, |
| 909 | bool IsProtocolMethodDecl) { |
| 910 | if (IsProtocolMethodDecl && |
| 911 | (ImplVar->getObjCDeclQualifier() != |
| 912 | IfaceVar->getObjCDeclQualifier())) { |
| 913 | S.Diag(ImplVar->getLocation(), |
| 914 | diag::warn_conflicting_param_modifiers) |
| 915 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 916 | << MethodImpl->getDeclName(); |
| 917 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
| 918 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 919 | } |
| 920 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 921 | QualType ImplTy = ImplVar->getType(); |
| 922 | QualType IfaceTy = IfaceVar->getType(); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 923 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 924 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
| 925 | return; |
| 926 | |
| 927 | unsigned DiagID = diag::warn_conflicting_param_types; |
| 928 | |
| 929 | // Mismatches between ObjC pointers go into a different warning |
| 930 | // category, and sometimes they're even completely whitelisted. |
| 931 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 932 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 933 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 934 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 935 | // Allow non-matching argument types as long as they don't |
| 936 | // violate the principle of substitutability. Specifically, the |
| 937 | // implementation must accept any objects that the superclass |
| 938 | // accepts, however it may also accept others. |
| 939 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
| 940 | return; |
| 941 | |
| 942 | DiagID = diag::warn_non_contravariant_param_types; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | S.Diag(ImplVar->getLocation(), DiagID) |
| 947 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 948 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
| 949 | S.Diag(IfaceVar->getLocation(), diag::note_previous_definition) |
| 950 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 951 | } |
| 952 | |
| 953 | |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 954 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 955 | ObjCMethodDecl *MethodDecl, |
| 956 | bool IsProtocolMethodDecl) { |
| 957 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 958 | IsProtocolMethodDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 960 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 961 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(); |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 962 | IM != EM; ++IM, ++IF) |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 963 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
| 964 | IsProtocolMethodDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 966 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
Fariborz Jahanian | 561da7e | 2010-05-21 23:28:58 +0000 | [diff] [blame] | 967 | Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 968 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 561da7e | 2010-05-21 23:28:58 +0000 | [diff] [blame] | 969 | } |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 972 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 973 | /// improve the efficiency of selector lookups and type checking by associating |
| 974 | /// with each protocol / interface / category the flattened instance tables. If |
| 975 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 976 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 977 | |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 978 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 979 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 980 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 981 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 982 | bool& IncompleteImpl, |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 983 | const llvm::DenseSet<Selector> &InsMap, |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 984 | const llvm::DenseSet<Selector> &ClsMap, |
Fariborz Jahanian | f283859 | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 985 | ObjCContainerDecl *CDecl) { |
| 986 | ObjCInterfaceDecl *IDecl; |
| 987 | if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 988 | IDecl = C->getClassInterface(); |
| 989 | else |
| 990 | IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 991 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| 992 | |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 993 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 994 | ObjCInterfaceDecl *NSIDecl = 0; |
| 995 | if (getLangOptions().NeXTRuntime) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | // check to see if class implements forwardInvocation method and objects |
| 997 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 998 | // from one object to another. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | // Under such conditions, which means that every method possible is |
| 1000 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1001 | // found" warnings. |
| 1002 | // FIXME: Use a general GetUnarySelector method for this. |
| 1003 | IdentifierInfo* II = &Context.Idents.get("forwardInvocation"); |
| 1004 | Selector fISelector = Context.Selectors.getSelector(1, &II); |
| 1005 | if (InsMap.count(fISelector)) |
| 1006 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 1007 | // need be implemented in the implementation. |
| 1008 | NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy")); |
| 1009 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1011 | // If a method lookup fails locally we still need to look and see if |
| 1012 | // the method was implemented by a base class or an inherited |
| 1013 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 1014 | // and otherwise would terminate in a warning. |
| 1015 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1016 | // check unimplemented instance methods. |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1017 | if (!NSIDecl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1019 | E = PDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1020 | ObjCMethodDecl *method = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1022 | !method->isSynthesized() && !InsMap.count(method->getSelector()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | (!Super || |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1024 | !Super->lookupInstanceMethod(method->getSelector()))) { |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1025 | // Ugly, but necessary. Method declared in protcol might have |
| 1026 | // have been synthesized due to a property declared in the class which |
| 1027 | // uses the protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | ObjCMethodDecl *MethodInClass = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1029 | IDecl->lookupInstanceMethod(method->getSelector()); |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1030 | if (!MethodInClass || !MethodInClass->isSynthesized()) { |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1031 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1032 | if (Diags.getDiagnosticLevel(DIAG, ImpLoc) |
| 1033 | != Diagnostic::Ignored) { |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1034 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); |
Fariborz Jahanian | 61c8d3e | 2010-10-29 23:20:05 +0000 | [diff] [blame] | 1035 | Diag(method->getLocation(), diag::note_method_declared_at); |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1036 | Diag(CDecl->getLocation(), diag::note_required_for_protocol_at) |
| 1037 | << PDecl->getDeclName(); |
| 1038 | } |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1039 | } |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1042 | // check unimplemented class methods |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1044 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1045 | I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1046 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1047 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 1048 | !ClsMap.count(method->getSelector()) && |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1049 | (!Super || !Super->lookupClassMethod(method->getSelector()))) { |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1050 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1051 | if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) { |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1052 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); |
Fariborz Jahanian | 61c8d3e | 2010-10-29 23:20:05 +0000 | [diff] [blame] | 1053 | Diag(method->getLocation(), diag::note_method_declared_at); |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1054 | Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) << |
| 1055 | PDecl->getDeclName(); |
| 1056 | } |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1057 | } |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1058 | } |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 1059 | // Check on this protocols's referenced protocols, recursively. |
| 1060 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1061 | E = PDecl->protocol_end(); PI != E; ++PI) |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1062 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1065 | /// MatchAllMethodDeclarations - Check methods declaraed in interface or |
| 1066 | /// or protocol against those declared in their implementations. |
| 1067 | /// |
| 1068 | void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap, |
| 1069 | const llvm::DenseSet<Selector> &ClsMap, |
| 1070 | llvm::DenseSet<Selector> &InsMapSeen, |
| 1071 | llvm::DenseSet<Selector> &ClsMapSeen, |
| 1072 | ObjCImplDecl* IMPDecl, |
| 1073 | ObjCContainerDecl* CDecl, |
| 1074 | bool &IncompleteImpl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | bool ImmediateClass) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1076 | // Check and see if instance methods in class interface have been |
| 1077 | // implemented in the implementation class. If so, their types match. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1078 | for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), |
| 1079 | E = CDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1080 | if (InsMapSeen.count((*I)->getSelector())) |
| 1081 | continue; |
| 1082 | InsMapSeen.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | if (!(*I)->isSynthesized() && |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1084 | !InsMap.count((*I)->getSelector())) { |
| 1085 | if (ImmediateClass) |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1086 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, |
| 1087 | diag::note_undef_method_impl); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1088 | continue; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1089 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | ObjCMethodDecl *ImpMethodDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1091 | IMPDecl->getInstanceMethod((*I)->getSelector()); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1092 | ObjCMethodDecl *MethodDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1093 | CDecl->getInstanceMethod((*I)->getSelector()); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1094 | assert(MethodDecl && |
| 1095 | "MethodDecl is null in ImplMethodsVsClassMethods"); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1096 | // ImpMethodDecl may be null as in a @dynamic property. |
| 1097 | if (ImpMethodDecl) |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1098 | WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl, |
| 1099 | isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1103 | // Check and see if class methods in class interface have been |
| 1104 | // implemented in the implementation class. If so, their types match. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | for (ObjCInterfaceDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1106 | I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1107 | if (ClsMapSeen.count((*I)->getSelector())) |
| 1108 | continue; |
| 1109 | ClsMapSeen.insert((*I)->getSelector()); |
| 1110 | if (!ClsMap.count((*I)->getSelector())) { |
| 1111 | if (ImmediateClass) |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1112 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, |
| 1113 | diag::note_undef_method_impl); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1114 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1115 | ObjCMethodDecl *ImpMethodDecl = |
| 1116 | IMPDecl->getClassMethod((*I)->getSelector()); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1117 | ObjCMethodDecl *MethodDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1118 | CDecl->getClassMethod((*I)->getSelector()); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1119 | WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl, |
| 1120 | isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1121 | } |
| 1122 | } |
Fariborz Jahanian | f54e3ae | 2010-10-08 22:59:25 +0000 | [diff] [blame] | 1123 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1124 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | f54e3ae | 2010-10-08 22:59:25 +0000 | [diff] [blame] | 1125 | // Also methods in class extensions need be looked at next. |
| 1126 | for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension(); |
| 1127 | ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) |
| 1128 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1129 | IMPDecl, |
| 1130 | const_cast<ObjCCategoryDecl *>(ClsExtDecl), |
| 1131 | IncompleteImpl, false); |
| 1132 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1133 | // Check for any implementation of a methods declared in protocol. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1134 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1135 | PI = I->all_referenced_protocol_begin(), |
| 1136 | E = I->all_referenced_protocol_end(); PI != E; ++PI) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1138 | IMPDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1139 | (*PI), IncompleteImpl, false); |
| 1140 | if (I->getSuperClass()) |
| 1141 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | IMPDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1143 | I->getSuperClass(), IncompleteImpl, false); |
| 1144 | } |
| 1145 | } |
| 1146 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1147 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | ObjCContainerDecl* CDecl, |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1149 | bool IncompleteImpl) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1150 | llvm::DenseSet<Selector> InsMap; |
| 1151 | // Check and see if instance methods in class interface have been |
| 1152 | // implemented in the implementation class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1154 | I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1155 | InsMap.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 1157 | // Check and see if properties declared in the interface have either 1) |
| 1158 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 1159 | // of the property in the @implementation. |
Ted Kremenek | c32647d | 2010-12-23 21:35:43 +0000 | [diff] [blame] | 1160 | if (isa<ObjCInterfaceDecl>(CDecl) && |
| 1161 | !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2)) |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1162 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap); |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1164 | llvm::DenseSet<Selector> ClsMap; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1166 | I = IMPDecl->classmeth_begin(), |
| 1167 | E = IMPDecl->classmeth_end(); I != E; ++I) |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1168 | ClsMap.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1170 | // Check for type conflict of methods declared in a class/protocol and |
| 1171 | // its implementation; if any. |
| 1172 | llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1174 | IMPDecl, CDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1175 | IncompleteImpl, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1177 | // Check the protocol list for unimplemented methods in the @implementation |
| 1178 | // class. |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1179 | // Check and see if class methods in class interface have been |
| 1180 | // implemented in the implementation class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1182 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1183 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1184 | PI = I->all_referenced_protocol_begin(), |
| 1185 | E = I->all_referenced_protocol_end(); PI != E; ++PI) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1187 | InsMap, ClsMap, I); |
| 1188 | // Check class extensions (unnamed categories) |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1189 | for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension(); |
| 1190 | Categories; Categories = Categories->getNextClassExtension()) |
| 1191 | ImplMethodsVsClassMethods(S, IMPDecl, |
| 1192 | const_cast<ObjCCategoryDecl*>(Categories), |
| 1193 | IncompleteImpl); |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1194 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1195 | // For extended class, unimplemented methods in its protocols will |
| 1196 | // be reported in the primary class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1197 | if (!C->IsClassExtension()) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1198 | for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), |
| 1199 | E = C->protocol_end(); PI != E; ++PI) |
| 1200 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Fariborz Jahanian | f283859 | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 1201 | InsMap, ClsMap, CDecl); |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1202 | // Report unimplemented properties in the category as well. |
| 1203 | // When reporting on missing setter/getters, do not report when |
| 1204 | // setter/getter is implemented in category's primary class |
| 1205 | // implementation. |
| 1206 | if (ObjCInterfaceDecl *ID = C->getClassInterface()) |
| 1207 | if (ObjCImplDecl *IMP = ID->getImplementation()) { |
| 1208 | for (ObjCImplementationDecl::instmeth_iterator |
| 1209 | I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I) |
| 1210 | InsMap.insert((*I)->getSelector()); |
| 1211 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1212 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap); |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1213 | } |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1214 | } else |
| 1215 | assert(false && "invalid ObjCContainerDecl type."); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | /// ActOnForwardClassDeclaration - |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1219 | Decl * |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1220 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1221 | IdentifierInfo **IdentList, |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 1222 | SourceLocation *IdentLocs, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1223 | unsigned NumElts) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1224 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1226 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1227 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1228 | NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1229 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1230 | LookupOrdinaryName, ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1231 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1232 | // Maybe we will complain about the shadowed template parameter. |
| 1233 | DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); |
| 1234 | // Just pretend that we didn't see the previous declaration. |
| 1235 | PrevDecl = 0; |
| 1236 | } |
| 1237 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1238 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | c733388 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1239 | // GCC apparently allows the following idiom: |
| 1240 | // |
| 1241 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 1242 | // @class XCElementToggler; |
| 1243 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | // FIXME: Make an extension? |
Steve Naroff | c733388 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1245 | TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1246 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1247 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1248 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1249 | } else { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1250 | // a forward class declaration matching a typedef name of a class refers |
| 1251 | // to the underlying class. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1252 | if (const ObjCObjectType *OI = |
| 1253 | TDD->getUnderlyingType()->getAs<ObjCObjectType>()) |
| 1254 | PrevDecl = OI->getInterface(); |
Fariborz Jahanian | cae27c5 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 1255 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1256 | } |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1257 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 1258 | if (!IDecl) { // Not already seen? Make a forward decl. |
| 1259 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
| 1260 | IdentList[i], IdentLocs[i], true); |
| 1261 | |
| 1262 | // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to |
| 1263 | // the current DeclContext. This prevents clients that walk DeclContext |
| 1264 | // from seeing the imaginary ObjCInterfaceDecl until it is actually |
| 1265 | // declared later (if at all). We also take care to explicitly make |
| 1266 | // sure this declaration is visible for name lookup. |
| 1267 | PushOnScopeChains(IDecl, TUScope, false); |
| 1268 | CurContext->makeDeclVisibleInContext(IDecl, true); |
| 1269 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1270 | |
| 1271 | Interfaces.push_back(IDecl); |
| 1272 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1274 | assert(Interfaces.size() == NumElts); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1275 | ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1276 | Interfaces.data(), IdentLocs, |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1277 | Interfaces.size()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1278 | CurContext->addDecl(CDecl); |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1279 | CheckObjCDeclScope(CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1280 | return CDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 1285 | /// returns true, or false, accordingly. |
| 1286 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1288 | const ObjCMethodDecl *PrevMethod, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1289 | bool matchBasedOnSizeAndAlignment, |
| 1290 | bool matchBasedOnStrictEqulity) { |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1291 | QualType T1 = Context.getCanonicalType(Method->getResultType()); |
| 1292 | QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1294 | if (T1 != T2) { |
| 1295 | // The result types are different. |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1296 | if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1297 | return false; |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1298 | // Incomplete types don't have a size and alignment. |
| 1299 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1300 | return false; |
| 1301 | // Check is based on size and alignment. |
| 1302 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1303 | return false; |
| 1304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1306 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1307 | E = Method->param_end(); |
| 1308 | ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1310 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1311 | assert(PrevI != PrevMethod->param_end() && "Param mismatch"); |
| 1312 | T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1313 | T2 = Context.getCanonicalType((*PrevI)->getType()); |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1314 | if (T1 != T2) { |
| 1315 | // The result types are different. |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1316 | if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity) |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1317 | return false; |
| 1318 | // Incomplete types don't have a size and alignment. |
| 1319 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1320 | return false; |
| 1321 | // Check is based on size and alignment. |
| 1322 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1323 | return false; |
| 1324 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1325 | } |
| 1326 | return true; |
| 1327 | } |
| 1328 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1329 | /// \brief Read the contents of the method pool for a given selector from |
| 1330 | /// external storage. |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1331 | /// |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1332 | /// This routine should only be called once, when the method pool has no entry |
| 1333 | /// for this selector. |
| 1334 | Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1335 | assert(ExternalSource && "We need an external AST source"); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1336 | assert(MethodPool.find(Sel) == MethodPool.end() && |
| 1337 | "Selector data already loaded into the method pool"); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1338 | |
| 1339 | // Read the method list from the external source. |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1340 | GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1342 | return MethodPool.insert(std::make_pair(Sel, Methods)).first; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1345 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
| 1346 | bool instance) { |
| 1347 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
| 1348 | if (Pos == MethodPool.end()) { |
| 1349 | if (ExternalSource) |
| 1350 | Pos = ReadMethodPool(Method->getSelector()); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1351 | else |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1352 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 1353 | GlobalMethods())).first; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1354 | } |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1355 | Method->setDefined(impl); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1356 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1357 | if (Entry.Method == 0) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1358 | // Haven't seen a method with this selector name yet - add it. |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1359 | Entry.Method = Method; |
| 1360 | Entry.Next = 0; |
| 1361 | return; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1364 | // We've seen a method with this name, see if we have already seen this type |
| 1365 | // signature. |
| 1366 | for (ObjCMethodList *List = &Entry; List; List = List->Next) |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1367 | if (MatchTwoMethodDeclarations(Method, List->Method)) { |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 1368 | ObjCMethodDecl *PrevObjCMethod = List->Method; |
| 1369 | PrevObjCMethod->setDefined(impl); |
| 1370 | // If a method is deprecated, push it in the global pool. |
| 1371 | // This is used for better diagnostics. |
| 1372 | if (Method->getAttr<DeprecatedAttr>()) { |
| 1373 | if (!PrevObjCMethod->getAttr<DeprecatedAttr>()) |
| 1374 | List->Method = Method; |
| 1375 | } |
| 1376 | // If new method is unavailable, push it into global pool |
| 1377 | // unless previous one is deprecated. |
| 1378 | if (Method->getAttr<UnavailableAttr>()) { |
| 1379 | if (!PrevObjCMethod->getAttr<UnavailableAttr>() && |
| 1380 | !PrevObjCMethod->getAttr<DeprecatedAttr>()) |
| 1381 | List->Method = Method; |
| 1382 | } |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1383 | return; |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1384 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1386 | // We have a new signature for an existing method - add it. |
| 1387 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | 298ed87 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 1388 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
| 1389 | Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1392 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1393 | bool receiverIdOrClass, |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1394 | bool warn, bool instance) { |
| 1395 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 1396 | if (Pos == MethodPool.end()) { |
| 1397 | if (ExternalSource) |
| 1398 | Pos = ReadMethodPool(Sel); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1399 | else |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1403 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1405 | bool strictSelectorMatch = receiverIdOrClass && warn && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1406 | (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl, |
| 1407 | R.getBegin()) != |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1408 | Diagnostic::Ignored); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1409 | if (warn && MethList.Method && MethList.Next) { |
| 1410 | bool issueWarning = false; |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1411 | if (strictSelectorMatch) |
| 1412 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) { |
| 1413 | // This checks if the methods differ in type mismatch. |
| 1414 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true)) |
| 1415 | issueWarning = true; |
| 1416 | } |
| 1417 | |
| 1418 | if (!issueWarning) |
| 1419 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) { |
| 1420 | // This checks if the methods differ by size & alignment. |
| 1421 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
| 1422 | issueWarning = true; |
| 1423 | } |
| 1424 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1425 | if (issueWarning) { |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1426 | if (strictSelectorMatch) |
| 1427 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 1428 | else |
| 1429 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1430 | Diag(MethList.Method->getLocStart(), diag::note_using) |
| 1431 | << MethList.Method->getSourceRange(); |
| 1432 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1433 | Diag(Next->Method->getLocStart(), diag::note_also_found) |
| 1434 | << Next->Method->getSourceRange(); |
| 1435 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1436 | } |
| 1437 | return MethList.Method; |
| 1438 | } |
| 1439 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1440 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1441 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 1442 | if (Pos == MethodPool.end()) |
| 1443 | return 0; |
| 1444 | |
| 1445 | GlobalMethods &Methods = Pos->second; |
| 1446 | |
| 1447 | if (Methods.first.Method && Methods.first.Method->isDefined()) |
| 1448 | return Methods.first.Method; |
| 1449 | if (Methods.second.Method && Methods.second.Method->isDefined()) |
| 1450 | return Methods.second.Method; |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1451 | return 0; |
| 1452 | } |
| 1453 | |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1454 | /// CompareMethodParamsInBaseAndSuper - This routine compares methods with |
| 1455 | /// identical selector names in current and its super classes and issues |
| 1456 | /// a warning if any of their argument types are incompatible. |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1457 | void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl, |
| 1458 | ObjCMethodDecl *Method, |
| 1459 | bool IsInstance) { |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1460 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 1461 | if (ID == 0) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1463 | while (ObjCInterfaceDecl *SD = ID->getSuperClass()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | ObjCMethodDecl *SuperMethodDecl = |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1465 | SD->lookupMethod(Method->getSelector(), IsInstance); |
| 1466 | if (SuperMethodDecl == 0) { |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1467 | ID = SD; |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1468 | continue; |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1469 | } |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1470 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1471 | E = Method->param_end(); |
| 1472 | ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin(); |
| 1473 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1474 | // Number of parameters are the same and is guaranteed by selector match. |
| 1475 | assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch"); |
| 1476 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1477 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 1478 | // If type of arguement of method in this class does not match its |
| 1479 | // respective argument type in the super class method, issue warning; |
| 1480 | if (!Context.typesAreCompatible(T1, T2)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1482 | << T1 << T2; |
| 1483 | Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration); |
| 1484 | return; |
| 1485 | } |
| 1486 | } |
| 1487 | ID = SD; |
| 1488 | } |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 1491 | /// DiagnoseDuplicateIvars - |
| 1492 | /// Check for duplicate ivars in the entire class at the start of |
| 1493 | /// @implementation. This becomes necesssary because class extension can |
| 1494 | /// add ivars to a class in random order which will not be known until |
| 1495 | /// class's @implementation is seen. |
| 1496 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
| 1497 | ObjCInterfaceDecl *SID) { |
| 1498 | for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(), |
| 1499 | IVE = ID->ivar_end(); IVI != IVE; ++IVI) { |
| 1500 | ObjCIvarDecl* Ivar = (*IVI); |
| 1501 | if (Ivar->isInvalidDecl()) |
| 1502 | continue; |
| 1503 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 1504 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 1505 | if (prevIvar) { |
| 1506 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 1507 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 1508 | Ivar->setInvalidDecl(); |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1514 | // Note: For class/category implemenations, allMethods/allProperties is |
| 1515 | // always null. |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1516 | void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1517 | Decl *ClassDecl, |
| 1518 | Decl **allMethods, unsigned allNum, |
| 1519 | Decl **allProperties, unsigned pNum, |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1520 | DeclGroupPtrTy *allTUVars, unsigned tuvNum) { |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1521 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 1522 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1523 | // should be true. |
| 1524 | if (!ClassDecl) |
| 1525 | return; |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1526 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | bool isInterfaceDeclKind = |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1528 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 1529 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1530 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1531 | |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1532 | if (!isInterfaceDeclKind && AtEnd.isInvalid()) { |
| 1533 | // FIXME: This is wrong. We shouldn't be pretending that there is |
| 1534 | // an '@end' in the declaration. |
| 1535 | SourceLocation L = ClassDecl->getLocation(); |
| 1536 | AtEnd.setBegin(L); |
| 1537 | AtEnd.setEnd(L); |
| 1538 | Diag(L, diag::warn_missing_atend); |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1541 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 1542 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 1543 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 1544 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1545 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1546 | ObjCMethodDecl *Method = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1547 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1548 | |
| 1549 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1550 | if (Method->isInstanceMethod()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1551 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1552 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1554 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1556 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1557 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1558 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1559 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 1560 | Method->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1561 | } else { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1562 | InsMap[Method->getSelector()] = Method; |
| 1563 | /// The following allows us to typecheck messages to "id". |
| 1564 | AddInstanceMethodToGlobalPool(Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | // verify that the instance method conforms to the same definition of |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1566 | // parent methods if it shadows one. |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1567 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1568 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1569 | } else { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1570 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1571 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1573 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1575 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1576 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1577 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1578 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 1579 | Method->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1580 | } else { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1581 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1582 | /// The following allows us to typecheck messages to "Class". |
| 1583 | AddFactoryMethodToGlobalPool(Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | // verify that the class method conforms to the same definition of |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1585 | // parent methods if it shadows one. |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1586 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1590 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | // Compares properties declared in this class to those of its |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 1592 | // super class. |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 1593 | ComparePropertiesInBaseAndSuper(I); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1594 | CompareProperties(I, I); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1595 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1596 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | // By the same token, they are also used to add new properties. No |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1598 | // need to compare the added property to those in the class. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1599 | |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1600 | // Compare protocol properties with those in category |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1601 | CompareProperties(C, C); |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 1602 | if (C->IsClassExtension()) { |
| 1603 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 1604 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 1605 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1606 | } |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1607 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1608 | if (CDecl->getIdentifier()) |
| 1609 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 1610 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 1611 | // and adds them to the DeclContext and global method pools. |
| 1612 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 1613 | E = CDecl->prop_end(); |
| 1614 | I != E; ++I) |
| 1615 | ProcessPropertyDecl(*I, CDecl); |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1616 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1617 | } |
| 1618 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1619 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1620 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 1621 | // Any property declared in a class extension might have user |
| 1622 | // declared setter or getter in current class extension or one |
| 1623 | // of the other class extensions. Mark them as synthesized as |
| 1624 | // property will be synthesized when property with same name is |
| 1625 | // seen in the @implementation. |
| 1626 | for (const ObjCCategoryDecl *ClsExtDecl = |
| 1627 | IDecl->getFirstClassExtension(); |
| 1628 | ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) { |
| 1629 | for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(), |
| 1630 | E = ClsExtDecl->prop_end(); I != E; ++I) { |
| 1631 | ObjCPropertyDecl *Property = (*I); |
| 1632 | // Skip over properties declared @dynamic |
| 1633 | if (const ObjCPropertyImplDecl *PIDecl |
| 1634 | = IC->FindPropertyImplDecl(Property->getIdentifier())) |
| 1635 | if (PIDecl->getPropertyImplementation() |
| 1636 | == ObjCPropertyImplDecl::Dynamic) |
| 1637 | continue; |
| 1638 | |
| 1639 | for (const ObjCCategoryDecl *CExtDecl = |
| 1640 | IDecl->getFirstClassExtension(); |
| 1641 | CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) { |
| 1642 | if (ObjCMethodDecl *GetterMethod = |
| 1643 | CExtDecl->getInstanceMethod(Property->getGetterName())) |
| 1644 | GetterMethod->setSynthesized(true); |
| 1645 | if (!Property->isReadOnly()) |
| 1646 | if (ObjCMethodDecl *SetterMethod = |
| 1647 | CExtDecl->getInstanceMethod(Property->getSetterName())) |
| 1648 | SetterMethod->setSynthesized(true); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
Ted Kremenek | c32647d | 2010-12-23 21:35:43 +0000 | [diff] [blame] | 1653 | if (LangOpts.ObjCDefaultSynthProperties && |
| 1654 | LangOpts.ObjCNonFragileABI2) |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1655 | DefaultSynthesizeProperties(S, IC, IDecl); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1656 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1657 | AtomicPropertySetterGetterRules(IC, IDecl); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1658 | |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 1659 | if (LangOpts.ObjCNonFragileABI2) |
| 1660 | while (IDecl->getSuperClass()) { |
| 1661 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 1662 | IDecl = IDecl->getSuperClass(); |
| 1663 | } |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1664 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 1665 | SetIvarInitializers(IC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1667 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1668 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1670 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1671 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 97a5887 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1672 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1673 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1674 | Categories; Categories = Categories->getNextClassCategory()) { |
| 1675 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1676 | ImplMethodsVsClassMethods(S, CatImplClass, Categories); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1677 | break; |
| 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1682 | if (isInterfaceDeclKind) { |
| 1683 | // Reject invalid vardecls. |
| 1684 | for (unsigned i = 0; i != tuvNum; i++) { |
| 1685 | DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); |
| 1686 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 1687 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 1688 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 8745416 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 1689 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | b31cb7f | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1690 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1691 | } |
Fariborz Jahanian | 38e24c7 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1692 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | |
| 1696 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 1697 | /// objective-c's type qualifier from the parser version of the same info. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | static Decl::ObjCDeclQualifier |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1699 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 1700 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 1701 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 1702 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 1703 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 1704 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 1705 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 1706 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 1707 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 1708 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 1709 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 1710 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 1711 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 1712 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1713 | |
| 1714 | return ret; |
| 1715 | } |
| 1716 | |
Ted Kremenek | 422bae7 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 1717 | static inline |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1718 | bool containsInvalidMethodImplAttribute(const AttrVec &A) { |
Ted Kremenek | 422bae7 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 1719 | // The 'ibaction' attribute is allowed on method definitions because of |
| 1720 | // how the IBAction macro is used on both method declarations and definitions. |
| 1721 | // If the method definitions contains any other attributes, return true. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1722 | for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) |
| 1723 | if ((*i)->getKind() != attr::IBAction) |
| 1724 | return true; |
| 1725 | return false; |
Ted Kremenek | 422bae7 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1728 | Decl *Sema::ActOnMethodDeclaration( |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1729 | Scope *S, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1730 | SourceLocation MethodLoc, SourceLocation EndLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1731 | tok::TokenKind MethodType, Decl *ClassDecl, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1732 | ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1733 | Selector Sel, |
| 1734 | // optional arguments. The number of types/arguments is obtained |
| 1735 | // from the Sel.getNumArgs(). |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1736 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1737 | DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1738 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 1739 | bool isVariadic) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1740 | // Make sure we can establish a context for the method. |
| 1741 | if (!ClassDecl) { |
| 1742 | Diag(MethodLoc, diag::error_missing_method_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1743 | return 0; |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1744 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1745 | QualType resultDeclType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 1747 | TypeSourceInfo *ResultTInfo = 0; |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1748 | if (ReturnType) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 1749 | resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1751 | // Methods cannot return interface types. All ObjC objects are |
| 1752 | // passed by reference. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1753 | if (resultDeclType->isObjCObjectType()) { |
Chris Lattner | 2dd979f | 2009-04-11 19:08:56 +0000 | [diff] [blame] | 1754 | Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) |
| 1755 | << 0 << resultDeclType; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1756 | return 0; |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1757 | } |
| 1758 | } else // get the type for "id". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1759 | resultDeclType = Context.getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | |
| 1761 | ObjCMethodDecl* ObjCMethod = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1762 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 1763 | ResultTInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | cast<DeclContext>(ClassDecl), |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1765 | MethodType == tok::minus, isVariadic, |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1766 | false, false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | MethodDeclKind == tok::objc_optional ? |
| 1768 | ObjCMethodDecl::Optional : |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1769 | ObjCMethodDecl::Required); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1771 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | |
Chris Lattner | 7db638d | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 1773 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1774 | QualType ArgType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1775 | TypeSourceInfo *DI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1777 | if (ArgInfo[i].Type == 0) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1778 | ArgType = Context.getObjCIdType(); |
| 1779 | DI = 0; |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1780 | } else { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1781 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Steve Naroff | 6082c62 | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1782 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1783 | ArgType = adjustParameterType(ArgType); |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1784 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1786 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
| 1787 | LookupOrdinaryName, ForRedeclaration); |
| 1788 | LookupName(R, S); |
| 1789 | if (R.isSingleResult()) { |
| 1790 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 1791 | if (S->isDeclScope(PrevDecl)) { |
| 1792 | // FIXME. This should be an error; but will break projects. |
| 1793 | Diag(ArgInfo[i].NameLoc, diag::warn_method_param_redefinition) |
| 1794 | << ArgInfo[i].Name; |
| 1795 | Diag(PrevDecl->getLocation(), |
| 1796 | diag::note_previous_declaration); |
| 1797 | } |
| 1798 | } |
| 1799 | |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1800 | ParmVarDecl* Param |
| 1801 | = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, |
| 1802 | ArgInfo[i].Name, ArgType, DI, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1803 | SC_None, SC_None, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1805 | if (ArgType->isObjCObjectType()) { |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1806 | Diag(ArgInfo[i].NameLoc, |
| 1807 | diag::err_object_cannot_be_passed_returned_by_value) |
| 1808 | << 1 << ArgType; |
| 1809 | Param->setInvalidDecl(); |
| 1810 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1812 | Param->setObjCDeclQualifier( |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1813 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1815 | // Apply the attributes to the parameter. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1816 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1818 | S->AddDecl(Param); |
| 1819 | IdResolver.AddDecl(Param); |
| 1820 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1821 | Params.push_back(Param); |
| 1822 | } |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1823 | |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1824 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1825 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1826 | QualType ArgType = Param->getType(); |
| 1827 | if (ArgType.isNull()) |
| 1828 | ArgType = Context.getObjCIdType(); |
| 1829 | else |
| 1830 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
| 1831 | ArgType = adjustParameterType(ArgType); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1832 | if (ArgType->isObjCObjectType()) { |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1833 | Diag(Param->getLocation(), |
| 1834 | diag::err_object_cannot_be_passed_returned_by_value) |
| 1835 | << 1 << ArgType; |
| 1836 | Param->setInvalidDecl(); |
| 1837 | } |
| 1838 | Param->setDeclContext(ObjCMethod); |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 1839 | |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1840 | Params.push_back(Param); |
| 1841 | } |
| 1842 | |
Fariborz Jahanian | 4ecb25f | 2010-04-09 15:40:42 +0000 | [diff] [blame] | 1843 | ObjCMethod->setMethodParams(Context, Params.data(), Params.size(), |
| 1844 | Sel.getNumArgs()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1845 | ObjCMethod->setObjCDeclQualifier( |
| 1846 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 1847 | const ObjCMethodDecl *PrevMethod = 0; |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1848 | |
| 1849 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1850 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1852 | const ObjCMethodDecl *InterfaceMD = 0; |
| 1853 | |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 1854 | // Add the method now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1856 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1857 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1858 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 1859 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1860 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1861 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 1862 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1863 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1864 | InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel, |
| 1865 | MethodType == tok::minus); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1866 | if (ObjCMethod->hasAttrs() && |
| 1867 | containsInvalidMethodImplAttribute(ObjCMethod->getAttrs())) |
Fariborz Jahanian | 5d36ac2 | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 1868 | Diag(EndLoc, diag::warn_attribute_method_def); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | } else if (ObjCCategoryImplDecl *CatImpDecl = |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1870 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1871 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1872 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
| 1873 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1874 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1875 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
| 1876 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1877 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1878 | if (ObjCMethod->hasAttrs() && |
| 1879 | containsInvalidMethodImplAttribute(ObjCMethod->getAttrs())) |
Fariborz Jahanian | 5d36ac2 | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 1880 | Diag(EndLoc, diag::warn_attribute_method_def); |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 1881 | } else { |
| 1882 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1883 | } |
| 1884 | if (PrevMethod) { |
| 1885 | // You can never have two method definitions with the same name. |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1886 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1887 | << ObjCMethod->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1888 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1890 | |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 1891 | // Merge information down from the interface declaration if we have one. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1892 | if (InterfaceMD) |
John McCall | eca5d22 | 2011-03-02 04:00:57 +0000 | [diff] [blame] | 1893 | mergeObjCMethodDecls(ObjCMethod, InterfaceMD); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1894 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1895 | return ObjCMethod; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1898 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1899 | if (isa<TranslationUnitDecl>(CurContext->getRedeclContext())) |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1900 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1902 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 1903 | D->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1905 | return true; |
| 1906 | } |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1907 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1908 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 1909 | /// instance variables of ClassName into Decls. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1910 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1911 | IdentifierInfo *ClassName, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1912 | llvm::SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1913 | // Check that ClassName is a valid class |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1914 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1915 | if (!Class) { |
| 1916 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 1917 | return; |
| 1918 | } |
Fariborz Jahanian | 0468fb9 | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 1919 | if (LangOpts.ObjCNonFragileABI) { |
| 1920 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 1921 | return; |
| 1922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1924 | // Collect the instance variables |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1925 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
| 1926 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 1927 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1928 | for (unsigned i = 0; i < Ivars.size(); i++) { |
| 1929 | FieldDecl* ID = cast<FieldDecl>(Ivars[i]); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1930 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 1931 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(), |
| 1932 | ID->getIdentifier(), ID->getType(), |
| 1933 | ID->getBitWidth()); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1934 | Decls.push_back(FD); |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 1935 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1937 | // Introduce all of these fields into the appropriate scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1938 | for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1939 | D != Decls.end(); ++D) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1940 | FieldDecl *FD = cast<FieldDecl>(*D); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1941 | if (getLangOptions().CPlusPlus) |
| 1942 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1943 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1944 | Record->addDecl(FD); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1945 | } |
| 1946 | } |
| 1947 | |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1948 | /// \brief Build a type-check a new Objective-C exception variable declaration. |
| 1949 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, |
| 1950 | QualType T, |
| 1951 | IdentifierInfo *Name, |
| 1952 | SourceLocation NameLoc, |
| 1953 | bool Invalid) { |
| 1954 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 1955 | // duration shall not be qualified by an address-space qualifier." |
| 1956 | // Since all parameters have automatic store duration, they can not have |
| 1957 | // an address space. |
| 1958 | if (T.getAddressSpace() != 0) { |
| 1959 | Diag(NameLoc, diag::err_arg_with_address_space); |
| 1960 | Invalid = true; |
| 1961 | } |
| 1962 | |
| 1963 | // An @catch parameter must be an unqualified object pointer type; |
| 1964 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 1965 | if (Invalid) { |
| 1966 | // Don't do any further checking. |
Douglas Gregor | be270a0 | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 1967 | } else if (T->isDependentType()) { |
| 1968 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1969 | } else if (!T->isObjCObjectPointerType()) { |
| 1970 | Invalid = true; |
| 1971 | Diag(NameLoc ,diag::err_catch_param_not_objc_type); |
| 1972 | } else if (T->isObjCQualifiedIdType()) { |
| 1973 | Invalid = true; |
| 1974 | Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm); |
| 1975 | } |
| 1976 | |
| 1977 | VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1978 | SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 1979 | New->setExceptionVariable(true); |
| 1980 | |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1981 | if (Invalid) |
| 1982 | New->setInvalidDecl(); |
| 1983 | return New; |
| 1984 | } |
| 1985 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1986 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 1987 | const DeclSpec &DS = D.getDeclSpec(); |
| 1988 | |
| 1989 | // We allow the "register" storage class on exception variables because |
| 1990 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 1991 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 1992 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 1993 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
| 1994 | } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) { |
| 1995 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
| 1996 | << DS.getStorageClassSpec(); |
| 1997 | } |
| 1998 | if (D.getDeclSpec().isThreadSpecified()) |
| 1999 | Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread); |
| 2000 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 2001 | |
| 2002 | DiagnoseFunctionSpecifiers(D); |
| 2003 | |
| 2004 | // Check that there are no default arguments inside the type of this |
| 2005 | // exception object (C++ only). |
| 2006 | if (getLangOptions().CPlusPlus) |
| 2007 | CheckExtraCXXDefaultArguments(D); |
| 2008 | |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 2009 | TagDecl *OwnedDecl = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 2010 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl); |
| 2011 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 2012 | |
| 2013 | if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) { |
| 2014 | // Objective-C++: Types shall not be defined in exception types. |
| 2015 | Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type) |
| 2016 | << Context.getTypeDeclType(OwnedDecl); |
| 2017 | } |
| 2018 | |
| 2019 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(), |
| 2020 | D.getIdentifierLoc(), |
| 2021 | D.isInvalidType()); |
| 2022 | |
| 2023 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 2024 | if (D.getCXXScopeSpec().isSet()) { |
| 2025 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 2026 | << D.getCXXScopeSpec().getRange(); |
| 2027 | New->setInvalidDecl(); |
| 2028 | } |
| 2029 | |
| 2030 | // Add the parameter declaration into this scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2031 | S->AddDecl(New); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 2032 | if (D.getIdentifier()) |
| 2033 | IdResolver.AddDecl(New); |
| 2034 | |
| 2035 | ProcessDeclAttributes(S, New, D); |
| 2036 | |
| 2037 | if (New->hasAttr<BlocksAttr>()) |
| 2038 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2039 | return New; |
Douglas Gregor | 4e6c0d1 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 2040 | } |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 2041 | |
| 2042 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2043 | /// initialization. |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2044 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2045 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2046 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
| 2047 | Iv= Iv->getNextIvar()) { |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 2048 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 2049 | if (QT->isRecordType()) |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2050 | Ivars.push_back(Iv); |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2053 | |
| 2054 | void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2055 | CXXCtorInitializer ** initializers, |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2056 | unsigned numInitializers) { |
| 2057 | if (numInitializers > 0) { |
| 2058 | NumIvarInitializers = numInitializers; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2059 | CXXCtorInitializer **ivarInitializers = |
| 2060 | new (C) CXXCtorInitializer*[NumIvarInitializers]; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2061 | memcpy(ivarInitializers, initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2062 | numInitializers * sizeof(CXXCtorInitializer*)); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2063 | IvarInitializers = ivarInitializers; |
| 2064 | } |
| 2065 | } |
| 2066 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2067 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
Fariborz Jahanian | 8b78913 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 2068 | // Warning will be issued only when selector table is |
| 2069 | // generated (which means there is at lease one implementation |
| 2070 | // in the TU). This is to match gcc's behavior. |
| 2071 | if (ReferencedSelectors.empty() || |
| 2072 | !Context.AnyObjCImplementation()) |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2073 | return; |
| 2074 | for (llvm::DenseMap<Selector, SourceLocation>::iterator S = |
| 2075 | ReferencedSelectors.begin(), |
| 2076 | E = ReferencedSelectors.end(); S != E; ++S) { |
| 2077 | Selector Sel = (*S).first; |
| 2078 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
| 2079 | Diag((*S).second, diag::warn_unimplemented_selector) << Sel; |
| 2080 | } |
| 2081 | return; |
| 2082 | } |