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