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