| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // | 
|  | 9 | //  This file implements semantic analysis for C++ templates. | 
| Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 |  | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 12 | #include "TreeTransform.h" | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" | 
| John McCall | bbbbe4e | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclFriend.h" | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" | 
|  | 17 | #include "clang/AST/ExprCXX.h" | 
| John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecursiveASTVisitor.h" | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeVisitor.h" | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 20 | #include "clang/Basic/LangOptions.h" | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" | 
|  | 23 | #include "clang/Sema/Lookup.h" | 
|  | 24 | #include "clang/Sema/ParsedTemplate.h" | 
|  | 25 | #include "clang/Sema/Scope.h" | 
|  | 26 | #include "clang/Sema/SemaInternal.h" | 
|  | 27 | #include "clang/Sema/Template.h" | 
|  | 28 | #include "clang/Sema/TemplateDeduction.h" | 
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallBitVector.h" | 
| Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | using namespace clang; | 
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 33 | using namespace sema; | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 34 |  | 
| John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 35 | // Exported for use by Parser. | 
|  | 36 | SourceRange | 
|  | 37 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, | 
|  | 38 | unsigned N) { | 
|  | 39 | if (!N) return SourceRange(); | 
|  | 40 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); | 
|  | 41 | } | 
|  | 42 |  | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 43 | /// \brief Determine whether the declaration found is acceptable as the name | 
|  | 44 | /// of a template and, if so, return that template declaration. Otherwise, | 
|  | 45 | /// returns NULL. | 
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 46 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 47 | NamedDecl *Orig, | 
|  | 48 | bool AllowFunctionTemplates) { | 
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | NamedDecl *D = Orig->getUnderlyingDecl(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 |  | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 51 | if (isa<TemplateDecl>(D)) { | 
|  | 52 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) | 
|  | 53 | return 0; | 
|  | 54 |  | 
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 55 | return Orig; | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 56 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 |  | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 58 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { | 
|  | 59 | // C++ [temp.local]p1: | 
|  | 60 | //   Like normal (non-template) classes, class templates have an | 
|  | 61 | //   injected-class-name (Clause 9). The injected-class-name | 
|  | 62 | //   can be used with or without a template-argument-list. When | 
|  | 63 | //   it is used without a template-argument-list, it is | 
|  | 64 | //   equivalent to the injected-class-name followed by the | 
|  | 65 | //   template-parameters of the class template enclosed in | 
|  | 66 | //   <>. When it is used with a template-argument-list, it | 
|  | 67 | //   refers to the specified class template specialization, | 
|  | 68 | //   which could be the current specialization or another | 
|  | 69 | //   specialization. | 
|  | 70 | if (Record->isInjectedClassName()) { | 
| Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 71 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 72 | if (Record->getDescribedClassTemplate()) | 
|  | 73 | return Record->getDescribedClassTemplate(); | 
|  | 74 |  | 
|  | 75 | if (ClassTemplateSpecializationDecl *Spec | 
|  | 76 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) | 
|  | 77 | return Spec->getSpecializedTemplate(); | 
|  | 78 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 |  | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 80 | return 0; | 
|  | 81 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 |  | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | return 0; | 
|  | 84 | } | 
|  | 85 |  | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 86 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, | 
|  | 87 | bool AllowFunctionTemplates) { | 
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 88 | // The set of class templates we've already seen. | 
|  | 89 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 90 | LookupResult::Filter filter = R.makeFilter(); | 
|  | 91 | while (filter.hasNext()) { | 
|  | 92 | NamedDecl *Orig = filter.next(); | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 93 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, | 
|  | 94 | AllowFunctionTemplates); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 95 | if (!Repl) | 
|  | 96 | filter.erase(); | 
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 97 | else if (Repl != Orig) { | 
|  | 98 |  | 
|  | 99 | // C++ [temp.local]p3: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 100 | //   A lookup that finds an injected-class-name (10.2) can result in an | 
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 101 | //   ambiguity in certain cases (for example, if it is found in more than | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 102 | //   one base class). If all of the injected-class-names that are found | 
|  | 103 | //   refer to specializations of the same class template, and if the name | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 104 | //   is used as a template-name, the reference refers to the class | 
|  | 105 | //   template itself and not a specialization thereof, and is not | 
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 106 | //   ambiguous. | 
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 107 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) | 
|  | 108 | if (!ClassTemplates.insert(ClassTmpl)) { | 
|  | 109 | filter.erase(); | 
|  | 110 | continue; | 
|  | 111 | } | 
| John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 112 |  | 
|  | 113 | // FIXME: we promote access to public here as a workaround to | 
|  | 114 | // the fact that LookupResult doesn't let us remember that we | 
|  | 115 | // found this template through a particular injected class name, | 
|  | 116 | // which means we end up doing nasty things to the invariants. | 
|  | 117 | // Pretending that access is public is *much* safer. | 
|  | 118 | filter.replace(Repl, AS_public); | 
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 119 | } | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 120 | } | 
|  | 121 | filter.done(); | 
|  | 122 | } | 
|  | 123 |  | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 124 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, | 
|  | 125 | bool AllowFunctionTemplates) { | 
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 126 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 127 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) | 
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 128 | return true; | 
|  | 129 |  | 
| Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 130 | return false; | 
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 133 | TemplateNameKind Sema::isTemplateName(Scope *S, | 
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 134 | CXXScopeSpec &SS, | 
| Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 135 | bool hasTemplateKeyword, | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 136 | UnqualifiedId &Name, | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 137 | ParsedType ObjectTypePtr, | 
| Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 138 | bool EnteringContext, | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 139 | TemplateTy &TemplateResult, | 
|  | 140 | bool &MemberOfUnknownSpecialization) { | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 141 | assert(getLangOpts().CPlusPlus && "No template names in C!"); | 
| Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 142 |  | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 143 | DeclarationName TName; | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 144 | MemberOfUnknownSpecialization = false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 145 |  | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 146 | switch (Name.getKind()) { | 
|  | 147 | case UnqualifiedId::IK_Identifier: | 
|  | 148 | TName = DeclarationName(Name.Identifier); | 
|  | 149 | break; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 |  | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 151 | case UnqualifiedId::IK_OperatorFunctionId: | 
|  | 152 | TName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 153 | Name.OperatorFunctionId.Operator); | 
|  | 154 | break; | 
|  | 155 |  | 
| Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 156 | case UnqualifiedId::IK_LiteralOperatorId: | 
| Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 157 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); | 
|  | 158 | break; | 
| Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 159 |  | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 160 | default: | 
|  | 161 | return TNK_Non_template; | 
|  | 162 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 |  | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 164 | QualType ObjectType = ObjectTypePtr.get(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 |  | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 166 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 167 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, | 
|  | 168 | MemberOfUnknownSpecialization); | 
| John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 169 | if (R.empty()) return TNK_Non_template; | 
|  | 170 | if (R.isAmbiguous()) { | 
|  | 171 | // Suppress diagnostics;  we'll redo this lookup later. | 
| John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 172 | R.suppressDiagnostics(); | 
| John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 173 |  | 
|  | 174 | // FIXME: we might have ambiguous templates, in which case we | 
|  | 175 | // should at least parse them properly! | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 176 | return TNK_Non_template; | 
| John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 177 | } | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 178 |  | 
| John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 179 | TemplateName Template; | 
|  | 180 | TemplateNameKind TemplateKind; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 |  | 
| John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 182 | unsigned ResultCount = R.end() - R.begin(); | 
|  | 183 | if (ResultCount > 1) { | 
|  | 184 | // We assume that we'll preserve the qualifier from a function | 
|  | 185 | // template name in other ways. | 
|  | 186 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); | 
|  | 187 | TemplateKind = TNK_Function_template; | 
| John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 188 |  | 
|  | 189 | // We'll do this lookup again later. | 
|  | 190 | R.suppressDiagnostics(); | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 191 | } else { | 
| John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 192 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); | 
|  | 193 |  | 
|  | 194 | if (SS.isSet() && !SS.isInvalid()) { | 
|  | 195 | NestedNameSpecifier *Qualifier | 
|  | 196 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); | 
| Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 197 | Template = Context.getQualifiedTemplateName(Qualifier, | 
|  | 198 | hasTemplateKeyword, TD); | 
| John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 199 | } else { | 
|  | 200 | Template = TemplateName(TD); | 
|  | 201 | } | 
|  | 202 |  | 
| John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 203 | if (isa<FunctionTemplateDecl>(TD)) { | 
| John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 204 | TemplateKind = TNK_Function_template; | 
| John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 205 |  | 
|  | 206 | // We'll do this lookup again later. | 
|  | 207 | R.suppressDiagnostics(); | 
|  | 208 | } else { | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 209 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || | 
|  | 210 | isa<TypeAliasTemplateDecl>(TD)); | 
| John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 211 | TemplateKind = TNK_Type_template; | 
|  | 212 | } | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 213 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 |  | 
| John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 215 | TemplateResult = TemplateTy::make(Template); | 
|  | 216 | return TemplateKind; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 219 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, | 
| Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 220 | SourceLocation IILoc, | 
|  | 221 | Scope *S, | 
|  | 222 | const CXXScopeSpec *SS, | 
|  | 223 | TemplateTy &SuggestedTemplate, | 
|  | 224 | TemplateNameKind &SuggestedKind) { | 
|  | 225 | // We can't recover unless there's a dependent scope specifier preceding the | 
|  | 226 | // template name. | 
| Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 227 | // FIXME: Typo correction? | 
| Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 228 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || | 
|  | 229 | computeDeclContext(*SS)) | 
|  | 230 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 231 |  | 
| Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 232 | // The code is missing a 'template' keyword prior to the dependent template | 
|  | 233 | // name. | 
|  | 234 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); | 
|  | 235 | Diag(IILoc, diag::err_template_kw_missing) | 
|  | 236 | << Qualifier << II.getName() | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 237 | << FixItHint::CreateInsertion(IILoc, "template "); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 238 | SuggestedTemplate | 
| Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 239 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); | 
|  | 240 | SuggestedKind = TNK_Dependent_template_name; | 
|  | 241 | return true; | 
|  | 242 | } | 
|  | 243 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 244 | void Sema::LookupTemplateName(LookupResult &Found, | 
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 245 | Scope *S, CXXScopeSpec &SS, | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 246 | QualType ObjectType, | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 247 | bool EnteringContext, | 
|  | 248 | bool &MemberOfUnknownSpecialization) { | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 249 | // Determine where to perform name lookup | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 250 | MemberOfUnknownSpecialization = false; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | DeclContext *LookupCtx = 0; | 
|  | 252 | bool isDependent = false; | 
|  | 253 | if (!ObjectType.isNull()) { | 
|  | 254 | // This nested-name-specifier occurs in a member access expression, e.g., | 
|  | 255 | // x->B::f, and we are looking into the type of the object. | 
|  | 256 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); | 
|  | 257 | LookupCtx = computeDeclContext(ObjectType); | 
|  | 258 | isDependent = ObjectType->isDependentType(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 259 | assert((isDependent || !ObjectType->isIncompleteType()) && | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | "Caller should have completed object type"); | 
| Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 261 |  | 
|  | 262 | // Template names cannot appear inside an Objective-C class or object type. | 
|  | 263 | if (ObjectType->isObjCObjectOrInterfaceType()) { | 
|  | 264 | Found.clear(); | 
|  | 265 | return; | 
|  | 266 | } | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 267 | } else if (SS.isSet()) { | 
|  | 268 | // This nested-name-specifier occurs after another nested-name-specifier, | 
|  | 269 | // so long into the context associated with the prior nested-name-specifier. | 
|  | 270 | LookupCtx = computeDeclContext(SS, EnteringContext); | 
|  | 271 | isDependent = isDependentScopeSpecifier(SS); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 272 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 273 | // The declaration context must be complete. | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 274 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 275 | return; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | bool ObjectTypeSearchedInScope = false; | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 279 | bool AllowFunctionTemplatesInLookup = true; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 280 | if (LookupCtx) { | 
|  | 281 | // Perform "qualified" name lookup into the declaration context we | 
|  | 282 | // computed, which is either the type of the base of a member access | 
|  | 283 | // expression or the declaration context associated with a prior | 
|  | 284 | // nested-name-specifier. | 
|  | 285 | LookupQualifiedName(Found, LookupCtx); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 286 | if (!ObjectType.isNull() && Found.empty()) { | 
|  | 287 | // C++ [basic.lookup.classref]p1: | 
|  | 288 | //   In a class member access expression (5.2.5), if the . or -> token is | 
|  | 289 | //   immediately followed by an identifier followed by a <, the | 
|  | 290 | //   identifier must be looked up to determine whether the < is the | 
|  | 291 | //   beginning of a template argument list (14.2) or a less-than operator. | 
|  | 292 | //   The identifier is first looked up in the class of the object | 
|  | 293 | //   expression. If the identifier is not found, it is then looked up in | 
|  | 294 | //   the context of the entire postfix-expression and shall name a class | 
|  | 295 | //   or function template. | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 296 | if (S) LookupName(Found, S); | 
|  | 297 | ObjectTypeSearchedInScope = true; | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 298 | AllowFunctionTemplatesInLookup = false; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 299 | } | 
| Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 300 | } else if (isDependent && (!S || ObjectType.isNull())) { | 
| Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 301 | // We cannot look into a dependent object type or nested nme | 
|  | 302 | // specifier. | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 303 | MemberOfUnknownSpecialization = true; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 304 | return; | 
|  | 305 | } else { | 
|  | 306 | // Perform unqualified name lookup in the current scope. | 
|  | 307 | LookupName(Found, S); | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 308 |  | 
|  | 309 | if (!ObjectType.isNull()) | 
|  | 310 | AllowFunctionTemplatesInLookup = false; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
| Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 313 | if (Found.empty() && !isDependent) { | 
| Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 314 | // If we did not find any names, attempt to correct any typos. | 
|  | 315 | DeclarationName Name = Found.getLookupName(); | 
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 316 | Found.clear(); | 
| Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 317 | // Simple filter callback that, for keywords, only accepts the C++ *_cast | 
|  | 318 | CorrectionCandidateCallback FilterCCC; | 
|  | 319 | FilterCCC.WantTypeSpecifiers = false; | 
|  | 320 | FilterCCC.WantExpressionKeywords = false; | 
|  | 321 | FilterCCC.WantRemainingKeywords = false; | 
|  | 322 | FilterCCC.WantCXXNamedCasts = true; | 
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 323 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), | 
|  | 324 | Found.getLookupKind(), S, &SS, | 
| Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 325 | FilterCCC, LookupCtx)) { | 
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 326 | Found.setLookupName(Corrected.getCorrection()); | 
|  | 327 | if (Corrected.getCorrectionDecl()) | 
|  | 328 | Found.addDecl(Corrected.getCorrectionDecl()); | 
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 329 | FilterAcceptableTemplateNames(Found); | 
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 330 | if (!Found.empty()) { | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 331 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); | 
|  | 332 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); | 
| Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 333 | if (LookupCtx) | 
|  | 334 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) | 
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 335 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() | 
| David Blaikie | 04ea41c | 2012-10-12 20:00:44 +0000 | [diff] [blame] | 336 | << FixItHint::CreateReplacement(Corrected.getCorrectionRange(), | 
|  | 337 | CorrectedStr); | 
| Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 338 | else | 
|  | 339 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) | 
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 340 | << Name << CorrectedQuotedStr | 
|  | 341 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); | 
| Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 342 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) | 
|  | 343 | Diag(Template->getLocation(), diag::note_previous_decl) | 
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 344 | << CorrectedQuotedStr; | 
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 345 | } | 
| Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 346 | } else { | 
| Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 347 | Found.setLookupName(Name); | 
| Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 351 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); | 
| Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 352 | if (Found.empty()) { | 
|  | 353 | if (isDependent) | 
|  | 354 | MemberOfUnknownSpecialization = true; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 355 | return; | 
| Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 356 | } | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 357 |  | 
| Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 358 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 359 | !(getLangOpts().CPlusPlus11 && !Found.empty())) { | 
| Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 360 | // C++03 [basic.lookup.classref]p1: | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 361 | //   [...] If the lookup in the class of the object expression finds a | 
|  | 362 | //   template, the name is also looked up in the context of the entire | 
|  | 363 | //   postfix-expression and [...] | 
|  | 364 | // | 
| Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 365 | // Note: C++11 does not perform this second lookup. | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 366 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), | 
|  | 367 | LookupOrdinaryName); | 
|  | 368 | LookupName(FoundOuter, S); | 
| Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 369 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 370 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 371 | if (FoundOuter.empty()) { | 
|  | 372 | //   - if the name is not found, the name found in the class of the | 
|  | 373 | //     object expression is used, otherwise | 
| Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 374 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || | 
|  | 375 | FoundOuter.isAmbiguous()) { | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 376 | //   - if the name is found in the context of the entire | 
|  | 377 | //     postfix-expression and does not name a class template, the name | 
|  | 378 | //     found in the class of the object expression is used, otherwise | 
| Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 379 | FoundOuter.clear(); | 
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 380 | } else if (!Found.isSuppressingDiagnostics()) { | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 381 | //   - if the name found is a class template, it must refer to the same | 
|  | 382 | //     entity as the one found in the class of the object expression, | 
|  | 383 | //     otherwise the program is ill-formed. | 
|  | 384 | if (!Found.isSingleResult() || | 
|  | 385 | Found.getFoundDecl()->getCanonicalDecl() | 
|  | 386 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 387 | Diag(Found.getNameLoc(), | 
| Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 388 | diag::ext_nested_name_member_ref_lookup_ambiguous) | 
|  | 389 | << Found.getLookupName() | 
|  | 390 | << ObjectType; | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 391 | Diag(Found.getRepresentativeDecl()->getLocation(), | 
|  | 392 | diag::note_ambig_member_ref_object_type) | 
|  | 393 | << ObjectType; | 
|  | 394 | Diag(FoundOuter.getFoundDecl()->getLocation(), | 
|  | 395 | diag::note_ambig_member_ref_scope); | 
|  | 396 |  | 
|  | 397 | // Recover by taking the template that we found in the object | 
|  | 398 | // expression's type. | 
|  | 399 | } | 
|  | 400 | } | 
|  | 401 | } | 
|  | 402 | } | 
|  | 403 |  | 
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 404 | /// ActOnDependentIdExpression - Handle a dependent id-expression that | 
|  | 405 | /// was just parsed.  This is only possible with an explicit scope | 
|  | 406 | /// specifier naming a dependent type. | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 407 | ExprResult | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 408 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 409 | SourceLocation TemplateKWLoc, | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 410 | const DeclarationNameInfo &NameInfo, | 
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 411 | bool isAddressOfOperand, | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 412 | const TemplateArgumentListInfo *TemplateArgs) { | 
| John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 413 | DeclContext *DC = getFunctionLevelDeclContext(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 414 |  | 
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 415 | if (!isAddressOfOperand && | 
| John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 416 | isa<CXXMethodDecl>(DC) && | 
|  | 417 | cast<CXXMethodDecl>(DC)->isInstance()) { | 
|  | 418 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 419 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 420 | // Since the 'this' expression is synthesized, we don't need to | 
|  | 421 | // perform the double-lookup check. | 
|  | 422 | NamedDecl *FirstQualifierInScope = 0; | 
|  | 423 |  | 
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 424 | return Owned(CXXDependentScopeMemberExpr::Create(Context, | 
|  | 425 | /*This*/ 0, ThisType, | 
|  | 426 | /*IsArrow*/ true, | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 427 | /*Op*/ SourceLocation(), | 
| Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 428 | SS.getWithLocInContext(Context), | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 429 | TemplateKWLoc, | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 430 | FirstQualifierInScope, | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 431 | NameInfo, | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 432 | TemplateArgs)); | 
|  | 433 | } | 
|  | 434 |  | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 435 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 436 | } | 
|  | 437 |  | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 438 | ExprResult | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 439 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 440 | SourceLocation TemplateKWLoc, | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 441 | const DeclarationNameInfo &NameInfo, | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 442 | const TemplateArgumentListInfo *TemplateArgs) { | 
|  | 443 | return Owned(DependentScopeDeclRefExpr::Create(Context, | 
| Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 444 | SS.getWithLocInContext(Context), | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 445 | TemplateKWLoc, | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 446 | NameInfo, | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 447 | TemplateArgs)); | 
| Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 448 | } | 
|  | 449 |  | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 450 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining | 
|  | 451 | /// that the template parameter 'PrevDecl' is being shadowed by a new | 
|  | 452 | /// declaration at location Loc. Returns true to indicate that this is | 
|  | 453 | /// an error, and false otherwise. | 
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 454 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { | 
| Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 455 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 456 |  | 
|  | 457 | // Microsoft Visual C++ permits template parameters to be shadowed. | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 458 | if (getLangOpts().MicrosoftExt) | 
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 459 | return; | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 460 |  | 
|  | 461 | // C++ [temp.local]p4: | 
|  | 462 | //   A template-parameter shall not be redeclared within its | 
|  | 463 | //   scope (including nested scopes). | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | Diag(Loc, diag::err_template_param_shadow) | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 465 | << cast<NamedDecl>(PrevDecl)->getDeclName(); | 
|  | 466 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); | 
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 467 | return; | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 468 | } | 
|  | 469 |  | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 470 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 471 | /// the parameter D to reference the templated declaration and return a pointer | 
|  | 472 | /// to the template declaration. Otherwise, do nothing to D and return null. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 473 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { | 
|  | 474 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { | 
|  | 475 | D = Temp->getTemplatedDecl(); | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 476 | return Temp; | 
|  | 477 | } | 
|  | 478 | return 0; | 
|  | 479 | } | 
|  | 480 |  | 
| Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 481 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( | 
|  | 482 | SourceLocation EllipsisLoc) const { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 483 | assert(Kind == Template && | 
| Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 484 | "Only template template arguments can be pack expansions here"); | 
|  | 485 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && | 
|  | 486 | "Template template argument pack expansion without packs"); | 
|  | 487 | ParsedTemplateArgument Result(*this); | 
|  | 488 | Result.EllipsisLoc = EllipsisLoc; | 
|  | 489 | return Result; | 
|  | 490 | } | 
|  | 491 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 492 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, | 
|  | 493 | const ParsedTemplateArgument &Arg) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 494 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 495 | switch (Arg.getKind()) { | 
|  | 496 | case ParsedTemplateArgument::Type: { | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 497 | TypeSourceInfo *DI; | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 498 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 499 | if (!DI) | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 500 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 501 | return TemplateArgumentLoc(TemplateArgument(T), DI); | 
|  | 502 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 503 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 504 | case ParsedTemplateArgument::NonType: { | 
|  | 505 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); | 
|  | 506 | return TemplateArgumentLoc(TemplateArgument(E), E); | 
|  | 507 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 508 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 509 | case ParsedTemplateArgument::Template: { | 
| John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 510 | TemplateName Template = Arg.getAsTemplate().get(); | 
| Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 511 | TemplateArgument TArg; | 
|  | 512 | if (Arg.getEllipsisLoc().isValid()) | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 513 | TArg = TemplateArgument(Template, Optional<unsigned int>()); | 
| Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 514 | else | 
|  | 515 | TArg = Template; | 
|  | 516 | return TemplateArgumentLoc(TArg, | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 517 | Arg.getScopeSpec().getWithLocInContext( | 
|  | 518 | SemaRef.Context), | 
| Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 519 | Arg.getLocation(), | 
|  | 520 | Arg.getEllipsisLoc()); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 521 | } | 
|  | 522 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 523 |  | 
| Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 524 | llvm_unreachable("Unhandled parsed template argument"); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 525 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 526 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 527 | /// \brief Translates template arguments as provided by the parser | 
|  | 528 | /// into template arguments used by semantic analysis. | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 529 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, | 
|  | 530 | TemplateArgumentListInfo &TemplateArgs) { | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 531 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 532 | TemplateArgs.addArgument(translateTemplateArgument(*this, | 
|  | 533 | TemplateArgsIn[I])); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 534 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 535 |  | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 536 | /// ActOnTypeParameter - Called when a C++ template type parameter | 
|  | 537 | /// (e.g., "typename T") has been parsed. Typename specifies whether | 
|  | 538 | /// the keyword "typename" was used to declare the type parameter | 
|  | 539 | /// (otherwise, "class" was used), and KeyLoc is the location of the | 
|  | 540 | /// "class" or "typename" keyword. ParamName is the name of the | 
|  | 541 | /// parameter (NULL indicates an unnamed template parameter) and | 
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 542 | /// ParamNameLoc is the location of the parameter name (if any). | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 543 | /// If the type parameter has a default argument, it will be added | 
|  | 544 | /// later via ActOnTypeParameterDefault. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 545 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, | 
|  | 546 | SourceLocation EllipsisLoc, | 
|  | 547 | SourceLocation KeyLoc, | 
|  | 548 | IdentifierInfo *ParamName, | 
|  | 549 | SourceLocation ParamNameLoc, | 
|  | 550 | unsigned Depth, unsigned Position, | 
|  | 551 | SourceLocation EqualLoc, | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 552 | ParsedType DefaultArg) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | assert(S->isTemplateParamScope() && | 
|  | 554 | "Template type parameter not in template parameter scope!"); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 555 | bool Invalid = false; | 
|  | 556 |  | 
|  | 557 | if (ParamName) { | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 558 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, | 
| Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 559 | LookupOrdinaryName, | 
|  | 560 | ForRedeclaration); | 
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 561 | if (PrevDecl && PrevDecl->isTemplateParameter()) { | 
|  | 562 | DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl); | 
|  | 563 | PrevDecl = 0; | 
|  | 564 | } | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 565 | } | 
|  | 566 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 567 | SourceLocation Loc = ParamNameLoc; | 
|  | 568 | if (!ParamName) | 
|  | 569 | Loc = KeyLoc; | 
|  | 570 |  | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 571 | TemplateTypeParmDecl *Param | 
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 572 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), | 
| Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 573 | KeyLoc, Loc, Depth, Position, ParamName, | 
|  | 574 | Typename, Ellipsis); | 
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 575 | Param->setAccess(AS_public); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 576 | if (Invalid) | 
|  | 577 | Param->setInvalidDecl(); | 
|  | 578 |  | 
|  | 579 | if (ParamName) { | 
|  | 580 | // Add the template parameter into the current scope. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 581 | S->AddDecl(Param); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 582 | IdResolver.AddDecl(Param); | 
|  | 583 | } | 
|  | 584 |  | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 585 | // C++0x [temp.param]p9: | 
|  | 586 | //   A default template-argument may be specified for any kind of | 
|  | 587 | //   template-parameter that is not a template parameter pack. | 
|  | 588 | if (DefaultArg && Ellipsis) { | 
|  | 589 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); | 
|  | 590 | DefaultArg = ParsedType(); | 
|  | 591 | } | 
|  | 592 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 593 | // Handle the default argument, if provided. | 
|  | 594 | if (DefaultArg) { | 
|  | 595 | TypeSourceInfo *DefaultTInfo; | 
|  | 596 | GetTypeFromParser(DefaultArg, &DefaultTInfo); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 597 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 598 | assert(DefaultTInfo && "expected source information for type"); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 |  | 
| Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 600 | // Check for unexpanded parameter packs. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 601 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, | 
| Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 602 | UPPC_DefaultArgument)) | 
|  | 603 | return Param; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 604 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 605 | // Check the template argument itself. | 
|  | 606 | if (CheckTemplateArgument(Param, DefaultTInfo)) { | 
|  | 607 | Param->setInvalidDecl(); | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 608 | return Param; | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 609 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 611 | Param->setDefaultArgument(DefaultTInfo, false); | 
|  | 612 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 613 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 614 | return Param; | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 615 | } | 
|  | 616 |  | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 617 | /// \brief Check that the type of a non-type template parameter is | 
|  | 618 | /// well-formed. | 
|  | 619 | /// | 
|  | 620 | /// \returns the (possibly-promoted) parameter type if valid; | 
|  | 621 | /// otherwise, produces a diagnostic and returns a NULL type. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | QualType | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 623 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { | 
| Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 624 | // We don't allow variably-modified types as the type of non-type template | 
|  | 625 | // parameters. | 
|  | 626 | if (T->isVariablyModifiedType()) { | 
|  | 627 | Diag(Loc, diag::err_variably_modified_nontype_template_param) | 
|  | 628 | << T; | 
|  | 629 | return QualType(); | 
|  | 630 | } | 
|  | 631 |  | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 632 | // C++ [temp.param]p4: | 
|  | 633 | // | 
|  | 634 | // A non-type template-parameter shall have one of the following | 
|  | 635 | // (optionally cv-qualified) types: | 
|  | 636 | // | 
|  | 637 | //       -- integral or enumeration type, | 
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 638 | if (T->isIntegralOrEnumerationType() || | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | //   -- pointer to object or pointer to function, | 
| Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 640 | T->isPointerType() || | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | //   -- reference to object or reference to function, | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 642 | T->isReferenceType() || | 
| Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 643 | //   -- pointer to member, | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 644 | T->isMemberPointerType() || | 
| Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 645 | //   -- std::nullptr_t. | 
|  | 646 | T->isNullPtrType() || | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 647 | // If T is a dependent type, we can't do the check now, so we | 
|  | 648 | // assume that it is well-formed. | 
| Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 649 | T->isDependentType()) { | 
|  | 650 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter | 
|  | 651 | // are ignored when determining its type. | 
|  | 652 | return T.getUnqualifiedType(); | 
|  | 653 | } | 
|  | 654 |  | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 655 | // C++ [temp.param]p8: | 
|  | 656 | // | 
|  | 657 | //   A non-type template-parameter of type "array of T" or | 
|  | 658 | //   "function returning T" is adjusted to be of type "pointer to | 
|  | 659 | //   T" or "pointer to function returning T", respectively. | 
|  | 660 | else if (T->isArrayType()) | 
|  | 661 | // FIXME: Keep the type prior to promotion? | 
|  | 662 | return Context.getArrayDecayedType(T); | 
|  | 663 | else if (T->isFunctionType()) | 
|  | 664 | // FIXME: Keep the type prior to promotion? | 
|  | 665 | return Context.getPointerType(T); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 666 |  | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 667 | Diag(Loc, diag::err_template_nontype_parm_bad_type) | 
|  | 668 | << T; | 
|  | 669 |  | 
|  | 670 | return QualType(); | 
|  | 671 | } | 
|  | 672 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 673 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, | 
|  | 674 | unsigned Depth, | 
|  | 675 | unsigned Position, | 
|  | 676 | SourceLocation EqualLoc, | 
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 677 | Expr *Default) { | 
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 678 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); | 
|  | 679 | QualType T = TInfo->getType(); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 680 |  | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 681 | assert(S->isTemplateParamScope() && | 
|  | 682 | "Non-type template parameter not in template parameter scope!"); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 683 | bool Invalid = false; | 
|  | 684 |  | 
|  | 685 | IdentifierInfo *ParamName = D.getIdentifier(); | 
|  | 686 | if (ParamName) { | 
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 687 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), | 
| Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 688 | LookupOrdinaryName, | 
|  | 689 | ForRedeclaration); | 
| Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 690 | if (PrevDecl && PrevDecl->isTemplateParameter()) { | 
|  | 691 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); | 
|  | 692 | PrevDecl = 0; | 
|  | 693 | } | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 694 | } | 
|  | 695 |  | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 696 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); | 
|  | 697 | if (T.isNull()) { | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 698 | T = Context.IntTy; // Recover with an 'int' type. | 
| Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 699 | Invalid = true; | 
|  | 700 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 701 |  | 
| Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 702 | bool IsParameterPack = D.hasEllipsis(); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 703 | NonTypeTemplateParmDecl *Param | 
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 704 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 705 | D.getLocStart(), | 
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 706 | D.getIdentifierLoc(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 707 | Depth, Position, ParamName, T, | 
| Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 708 | IsParameterPack, TInfo); | 
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 709 | Param->setAccess(AS_public); | 
|  | 710 |  | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 711 | if (Invalid) | 
|  | 712 | Param->setInvalidDecl(); | 
|  | 713 |  | 
|  | 714 | if (D.getIdentifier()) { | 
|  | 715 | // Add the template parameter into the current scope. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 716 | S->AddDecl(Param); | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 717 | IdResolver.AddDecl(Param); | 
|  | 718 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 719 |  | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 720 | // C++0x [temp.param]p9: | 
|  | 721 | //   A default template-argument may be specified for any kind of | 
|  | 722 | //   template-parameter that is not a template parameter pack. | 
|  | 723 | if (Default && IsParameterPack) { | 
|  | 724 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); | 
|  | 725 | Default = 0; | 
|  | 726 | } | 
|  | 727 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 728 | // Check the well-formedness of the default template argument, if provided. | 
| Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 729 | if (Default) { | 
| Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 730 | // Check for unexpanded parameter packs. | 
|  | 731 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) | 
|  | 732 | return Param; | 
|  | 733 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 734 | TemplateArgument Converted; | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 735 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); | 
|  | 736 | if (DefaultRes.isInvalid()) { | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 737 | Param->setInvalidDecl(); | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 738 | return Param; | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 739 | } | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 740 | Default = DefaultRes.take(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 741 |  | 
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 742 | Param->setDefaultArgument(Default, false); | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 743 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 744 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 745 | return Param; | 
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 746 | } | 
| Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 747 |  | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 748 | /// ActOnTemplateTemplateParameter - Called when a C++ template template | 
| James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 749 | /// parameter (e.g. T in template <template \<typename> class T> class array) | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 750 | /// has been parsed. S is the current scope. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 751 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, | 
|  | 752 | SourceLocation TmpLoc, | 
| Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 753 | TemplateParameterList *Params, | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 754 | SourceLocation EllipsisLoc, | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 755 | IdentifierInfo *Name, | 
|  | 756 | SourceLocation NameLoc, | 
|  | 757 | unsigned Depth, | 
|  | 758 | unsigned Position, | 
|  | 759 | SourceLocation EqualLoc, | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 760 | ParsedTemplateArgument Default) { | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 761 | assert(S->isTemplateParamScope() && | 
|  | 762 | "Template template parameter not in template parameter scope!"); | 
|  | 763 |  | 
|  | 764 | // Construct the parameter object. | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 765 | bool IsParameterPack = EllipsisLoc.isValid(); | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 766 | TemplateTemplateParmDecl *Param = | 
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 767 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 768 | NameLoc.isInvalid()? TmpLoc : NameLoc, | 
|  | 769 | Depth, Position, IsParameterPack, | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 770 | Name, Params); | 
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 771 | Param->setAccess(AS_public); | 
|  | 772 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | // If the template template parameter has a name, then link the identifier | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 774 | // into the scope and lookup mechanisms. | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 775 | if (Name) { | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 776 | S->AddDecl(Param); | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 777 | IdResolver.AddDecl(Param); | 
|  | 778 | } | 
|  | 779 |  | 
| Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 780 | if (Params->size() == 0) { | 
|  | 781 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) | 
|  | 782 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); | 
|  | 783 | Param->setInvalidDecl(); | 
|  | 784 | } | 
|  | 785 |  | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 786 | // C++0x [temp.param]p9: | 
|  | 787 | //   A default template-argument may be specified for any kind of | 
|  | 788 | //   template-parameter that is not a template parameter pack. | 
|  | 789 | if (IsParameterPack && !Default.isInvalid()) { | 
|  | 790 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); | 
|  | 791 | Default = ParsedTemplateArgument(); | 
|  | 792 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 793 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 794 | if (!Default.isInvalid()) { | 
|  | 795 | // Check only that we have a template template argument. We don't want to | 
|  | 796 | // try to check well-formedness now, because our template template parameter | 
|  | 797 | // might have dependent types in its template parameters, which we wouldn't | 
|  | 798 | // be able to match now. | 
|  | 799 | // | 
|  | 800 | // If none of the template template parameter's template arguments mention | 
|  | 801 | // other template parameters, we could actually perform more checking here. | 
|  | 802 | // However, it isn't worth doing. | 
|  | 803 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); | 
|  | 804 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { | 
|  | 805 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) | 
|  | 806 | << DefaultArg.getSourceRange(); | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 807 | return Param; | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 808 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 809 |  | 
| Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 810 | // Check for unexpanded parameter packs. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 811 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), | 
| Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 812 | DefaultArg.getArgument().getAsTemplate(), | 
|  | 813 | UPPC_DefaultArgument)) | 
|  | 814 | return Param; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 815 |  | 
| Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 816 | Param->setDefaultArgument(DefaultArg, false); | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 817 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 818 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 819 | return Param; | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 820 | } | 
|  | 821 |  | 
| Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 822 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that | 
|  | 823 | /// contains the template parameters in Params/NumParams. | 
| Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 824 | TemplateParameterList * | 
| Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 825 | Sema::ActOnTemplateParameterList(unsigned Depth, | 
|  | 826 | SourceLocation ExportLoc, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | SourceLocation TemplateLoc, | 
| Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 828 | SourceLocation LAngleLoc, | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 829 | Decl **Params, unsigned NumParams, | 
| Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 830 | SourceLocation RAngleLoc) { | 
|  | 831 | if (ExportLoc.isValid()) | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 832 | Diag(ExportLoc, diag::warn_template_export_unsupported); | 
| Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 833 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 834 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 835 | (NamedDecl**)Params, NumParams, | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 836 | RAngleLoc); | 
| Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 837 | } | 
| Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 838 |  | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 839 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { | 
|  | 840 | if (SS.isSet()) | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 841 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 842 | } | 
|  | 843 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 844 | DeclResult | 
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 845 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, | 
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 846 | SourceLocation KWLoc, CXXScopeSpec &SS, | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 847 | IdentifierInfo *Name, SourceLocation NameLoc, | 
|  | 848 | AttributeList *Attr, | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 849 | TemplateParameterList *TemplateParams, | 
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 850 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, | 
| Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 851 | unsigned NumOuterTemplateParamLists, | 
|  | 852 | TemplateParameterList** OuterTemplateParamLists) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | assert(TemplateParams && TemplateParams->size() > 0 && | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 854 | "No template parameters"); | 
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 855 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 856 | bool Invalid = false; | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 857 |  | 
|  | 858 | // Check that we can declare a template here. | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 859 | if (CheckTemplateDeclScope(S, TemplateParams)) | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 860 | return true; | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 861 |  | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 862 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | 
|  | 863 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 864 |  | 
|  | 865 | // There is no such thing as an unnamed class template. | 
|  | 866 | if (!Name) { | 
|  | 867 | Diag(KWLoc, diag::err_template_unnamed_class); | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 868 | return true; | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 869 | } | 
|  | 870 |  | 
| Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 871 | // Find any previous declaration with this name. For a friend with no | 
|  | 872 | // scope explicitly specified, we only look for tag declarations (per | 
|  | 873 | // C++11 [basic.lookup.elab]p2). | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 874 | DeclContext *SemanticContext; | 
| Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 875 | LookupResult Previous(*this, Name, NameLoc, | 
|  | 876 | (SS.isEmpty() && TUK == TUK_Friend) | 
|  | 877 | ? LookupTagName : LookupOrdinaryName, | 
| John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 878 | ForRedeclaration); | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 879 | if (SS.isNotEmpty() && !SS.isInvalid()) { | 
|  | 880 | SemanticContext = computeDeclContext(SS, true); | 
|  | 881 | if (!SemanticContext) { | 
| Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 882 | // FIXME: Horrible, horrible hack! We can't currently represent this | 
|  | 883 | // in the AST, and historically we have just ignored such friend | 
|  | 884 | // class templates, so don't complain here. | 
|  | 885 | if (TUK != TUK_Friend) | 
|  | 886 | Diag(NameLoc, diag::err_template_qualified_declarator_no_match) | 
|  | 887 | << SS.getScopeRep() << SS.getRange(); | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 888 | return true; | 
|  | 889 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 |  | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 891 | if (RequireCompleteDeclContext(SS, SemanticContext)) | 
|  | 892 | return true; | 
|  | 893 |  | 
| Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 894 | // If we're adding a template to a dependent context, we may need to | 
|  | 895 | // rebuilding some of the types used within the template parameter list, | 
|  | 896 | // now that we know what the current instantiation is. | 
|  | 897 | if (SemanticContext->isDependentContext()) { | 
|  | 898 | ContextRAII SavedContext(*this, SemanticContext); | 
|  | 899 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) | 
|  | 900 | Invalid = true; | 
| Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 901 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) | 
|  | 902 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); | 
| Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 903 |  | 
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 904 | LookupQualifiedName(Previous, SemanticContext); | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 905 | } else { | 
|  | 906 | SemanticContext = CurContext; | 
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 907 | LookupName(Previous, S); | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 908 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 |  | 
| Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 910 | if (Previous.isAmbiguous()) | 
|  | 911 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 912 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 913 | NamedDecl *PrevDecl = 0; | 
|  | 914 | if (Previous.begin() != Previous.end()) | 
| Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 915 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 917 | // If there is a previous declaration with the same name, check | 
|  | 918 | // whether this is a valid redeclaration. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | ClassTemplateDecl *PrevClassTemplate | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 920 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); | 
| Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 921 |  | 
|  | 922 | // We may have found the injected-class-name of a class template, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 923 | // class template partial specialization, or class template specialization. | 
| Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 924 | // In these cases, grab the template that is being defined or specialized. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 925 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && | 
| Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 926 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { | 
|  | 927 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 928 | PrevClassTemplate | 
| Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 929 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); | 
|  | 930 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { | 
|  | 931 | PrevClassTemplate | 
|  | 932 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) | 
|  | 933 | ->getSpecializedTemplate(); | 
|  | 934 | } | 
|  | 935 | } | 
|  | 936 |  | 
| John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 937 | if (TUK == TUK_Friend) { | 
| John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 938 | // C++ [namespace.memdef]p3: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 939 | //   [...] When looking for a prior declaration of a class or a function | 
|  | 940 | //   declared as a friend, and when the name of the friend class or | 
| John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 941 | //   function is neither a qualified name nor a template-id, scopes outside | 
|  | 942 | //   the innermost enclosing namespace scope are not considered. | 
| Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 943 | if (!SS.isSet()) { | 
|  | 944 | DeclContext *OutermostContext = CurContext; | 
|  | 945 | while (!OutermostContext->isFileContext()) | 
|  | 946 | OutermostContext = OutermostContext->getLookupParent(); | 
| John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 947 |  | 
| Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 948 | if (PrevDecl && | 
| Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 949 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || | 
|  | 950 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { | 
|  | 951 | SemanticContext = PrevDecl->getDeclContext(); | 
|  | 952 | } else { | 
|  | 953 | // Declarations in outer scopes don't matter. However, the outermost | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | // context we computed is the semantic context for our new | 
| Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 955 | // declaration. | 
|  | 956 | PrevDecl = PrevClassTemplate = 0; | 
|  | 957 | SemanticContext = OutermostContext; | 
| Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 958 |  | 
|  | 959 | // Check that the chosen semantic context doesn't already contain a | 
|  | 960 | // declaration of this name as a non-tag type. | 
|  | 961 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, | 
|  | 962 | ForRedeclaration); | 
|  | 963 | DeclContext *LookupContext = SemanticContext; | 
|  | 964 | while (LookupContext->isTransparentContext()) | 
|  | 965 | LookupContext = LookupContext->getLookupParent(); | 
|  | 966 | LookupQualifiedName(Previous, LookupContext); | 
|  | 967 |  | 
|  | 968 | if (Previous.isAmbiguous()) | 
|  | 969 | return true; | 
|  | 970 |  | 
|  | 971 | if (Previous.begin() != Previous.end()) | 
|  | 972 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); | 
| Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 973 | } | 
| John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 974 | } | 
| John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 975 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) | 
|  | 976 | PrevDecl = PrevClassTemplate = 0; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 977 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 978 | if (PrevClassTemplate) { | 
| Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 979 | // Ensure that the template parameter lists are compatible. Skip this check | 
|  | 980 | // for a friend in a dependent context: the template parameter list itself | 
|  | 981 | // could be dependent. | 
|  | 982 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && | 
|  | 983 | !TemplateParameterListsAreEqual(TemplateParams, | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 984 | PrevClassTemplate->getTemplateParameters(), | 
| Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 985 | /*Complain=*/true, | 
|  | 986 | TPL_TemplateMatch)) | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 987 | return true; | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 988 |  | 
|  | 989 | // C++ [temp.class]p4: | 
|  | 990 | //   In a redeclaration, partial specialization, explicit | 
|  | 991 | //   specialization or explicit instantiation of a class template, | 
|  | 992 | //   the class-key shall agree in kind with the original class | 
|  | 993 | //   template declaration (7.1.5.3). | 
|  | 994 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); | 
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 995 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, | 
|  | 996 | TUK == TUK_Definition,  KWLoc, *Name)) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | Diag(KWLoc, diag::err_use_with_wrong_tag) | 
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 998 | << Name | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 999 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1000 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); | 
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1001 | Kind = PrevRecordDecl->getTagKind(); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1002 | } | 
|  | 1003 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1004 | // Check for redefinition of this class template. | 
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1005 | if (TUK == TUK_Definition) { | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1006 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1007 | Diag(NameLoc, diag::err_redefinition) << Name; | 
|  | 1008 | Diag(Def->getLocation(), diag::note_previous_definition); | 
|  | 1009 | // FIXME: Would it make sense to try to "forget" the previous | 
|  | 1010 | // definition, as part of error recovery? | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1011 | return true; | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1012 | } | 
| Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1013 | } | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1014 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { | 
|  | 1015 | // Maybe we will complain about the shadowed template parameter. | 
|  | 1016 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); | 
|  | 1017 | // Just pretend that we didn't see the previous declaration. | 
|  | 1018 | PrevDecl = 0; | 
|  | 1019 | } else if (PrevDecl) { | 
|  | 1020 | // C++ [temp]p5: | 
|  | 1021 | //   A class template shall not have the same name as any other | 
|  | 1022 | //   template, class, function, object, enumeration, enumerator, | 
|  | 1023 | //   namespace, or type in the same scope (3.3), except as specified | 
|  | 1024 | //   in (14.5.4). | 
|  | 1025 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; | 
|  | 1026 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1027 | return true; | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1028 | } | 
|  | 1029 |  | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1030 | // Check the template parameter list of this declaration, possibly | 
|  | 1031 | // merging in the template parameter list from the previous class | 
| Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1032 | // template declaration. Skip this check for a friend in a dependent | 
|  | 1033 | // context, because the template parameter list might be dependent. | 
|  | 1034 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && | 
|  | 1035 | CheckTemplateParameterList(TemplateParams, | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1036 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, | 
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1037 | (SS.isSet() && SemanticContext && | 
| Douglas Gregor | 4d5c297 | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 1038 | SemanticContext->isRecord() && | 
|  | 1039 | SemanticContext->isDependentContext()) | 
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1040 | ? TPC_ClassTemplateMember | 
|  | 1041 | : TPC_ClassTemplate)) | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1042 | Invalid = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 |  | 
| Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1044 | if (SS.isSet()) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1045 | // If the name of the template was qualified, we must be defining the | 
| Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1046 | // template out-of-line. | 
| Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1047 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { | 
|  | 1048 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match | 
|  | 1049 | : diag::err_member_def_does_not_match) | 
| Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1050 | << Name << SemanticContext << SS.getRange(); | 
| Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1051 | Invalid = true; | 
|  | 1052 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1053 | } | 
|  | 1054 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | CXXRecordDecl *NewClass = | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1056 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | PrevClassTemplate? | 
| Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1058 | PrevClassTemplate->getTemplatedDecl() : 0, | 
|  | 1059 | /*DelayTypeCreation=*/true); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1060 | SetNestedNameSpecifier(NewClass, SS); | 
| Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1061 | if (NumOuterTemplateParamLists > 0) | 
|  | 1062 | NewClass->setTemplateParameterListsInfo(Context, | 
|  | 1063 | NumOuterTemplateParamLists, | 
|  | 1064 | OuterTemplateParamLists); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1065 |  | 
| Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1066 | // Add alignment attributes if necessary; these attributes are checked when | 
|  | 1067 | // the ASTContext lays out the structure. | 
| Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1068 | if (TUK == TUK_Definition) { | 
|  | 1069 | AddAlignmentAttributesForRecord(NewClass); | 
|  | 1070 | AddMsStructLayoutForRecord(NewClass); | 
|  | 1071 | } | 
| Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1072 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1073 | ClassTemplateDecl *NewTemplate | 
|  | 1074 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, | 
|  | 1075 | DeclarationName(Name), TemplateParams, | 
| Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1076 | NewClass, PrevClassTemplate); | 
| Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1077 | NewClass->setDescribedClassTemplate(NewTemplate); | 
| Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1078 |  | 
| Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1079 | if (ModulePrivateLoc.isValid()) | 
| Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1080 | NewTemplate->setModulePrivate(); | 
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1081 |  | 
| Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1082 | // Build the type for the class template declaration now. | 
| Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1083 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1084 | T = Context.getInjectedClassNameType(NewClass, T); | 
| Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1085 | assert(T->isDependentType() && "Class template type is not dependent?"); | 
|  | 1086 | (void)T; | 
|  | 1087 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | // If we are providing an explicit specialization of a member that is a | 
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1089 | // class template, make a note of that. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1090 | if (PrevClassTemplate && | 
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1091 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) | 
|  | 1092 | PrevClassTemplate->setMemberSpecialization(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1093 |  | 
| Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1094 | // Set the access specifier. | 
| Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1095 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1096 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1098 | // Set the lexical context of these templates | 
|  | 1099 | NewClass->setLexicalDeclContext(CurContext); | 
|  | 1100 | NewTemplate->setLexicalDeclContext(CurContext); | 
|  | 1101 |  | 
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1102 | if (TUK == TUK_Definition) | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1103 | NewClass->startDefinition(); | 
|  | 1104 |  | 
|  | 1105 | if (Attr) | 
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1106 | ProcessDeclAttributeList(S, NewClass, Attr); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1107 |  | 
| Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1108 | if (PrevClassTemplate) | 
|  | 1109 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); | 
|  | 1110 |  | 
| Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1111 | AddPushedVisibilityAttribute(NewClass); | 
|  | 1112 |  | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1113 | if (TUK != TUK_Friend) | 
|  | 1114 | PushOnScopeChains(NewTemplate, S); | 
|  | 1115 | else { | 
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1116 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1117 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); | 
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1118 | NewClass->setAccess(PrevClassTemplate->getAccess()); | 
|  | 1119 | } | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1120 |  | 
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1121 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ | 
|  | 1122 | PrevClassTemplate != NULL); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 |  | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1124 | // Friend templates are visible in fairly strange ways. | 
|  | 1125 | if (!CurContext->isDependentContext()) { | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1126 | DeclContext *DC = SemanticContext->getRedeclContext(); | 
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1127 | DC->makeDeclVisibleInContext(NewTemplate); | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1128 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) | 
|  | 1129 | PushOnScopeChains(NewTemplate, EnclosingScope, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1130 | /* AddToContext = */ false); | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1131 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1132 |  | 
| Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1133 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, | 
|  | 1134 | NewClass->getLocation(), | 
|  | 1135 | NewTemplate, | 
|  | 1136 | /*FIXME:*/NewClass->getLocation()); | 
|  | 1137 | Friend->setAccess(AS_public); | 
|  | 1138 | CurContext->addDecl(Friend); | 
| John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1139 | } | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1140 |  | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1141 | if (Invalid) { | 
|  | 1142 | NewTemplate->setInvalidDecl(); | 
|  | 1143 | NewClass->setInvalidDecl(); | 
|  | 1144 | } | 
| Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1145 |  | 
| Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1146 | ActOnDocumentableDecl(NewTemplate); | 
|  | 1147 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1148 | return NewTemplate; | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1149 | } | 
|  | 1150 |  | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1151 | /// \brief Diagnose the presence of a default template argument on a | 
|  | 1152 | /// template parameter, which is ill-formed in certain contexts. | 
|  | 1153 | /// | 
|  | 1154 | /// \returns true if the default template argument should be dropped. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 | static bool DiagnoseDefaultTemplateArgument(Sema &S, | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1156 | Sema::TemplateParamListContext TPC, | 
|  | 1157 | SourceLocation ParamLoc, | 
|  | 1158 | SourceRange DefArgRange) { | 
|  | 1159 | switch (TPC) { | 
|  | 1160 | case Sema::TPC_ClassTemplate: | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1161 | case Sema::TPC_TypeAliasTemplate: | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1162 | return false; | 
|  | 1163 |  | 
|  | 1164 | case Sema::TPC_FunctionTemplate: | 
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1165 | case Sema::TPC_FriendFunctionTemplateDefinition: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1166 | // C++ [temp.param]p9: | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1167 | //   A default template-argument shall not be specified in a | 
|  | 1168 | //   function template declaration or a function template | 
|  | 1169 | //   definition [...] | 
| Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1170 | //   If a friend function template declaration specifies a default | 
|  | 1171 | //   template-argument, that declaration shall be a definition and shall be | 
|  | 1172 | //   the only declaration of the function template in the translation unit. | 
|  | 1173 | // (C++98/03 doesn't have this wording; see DR226). | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1174 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1175 | diag::warn_cxx98_compat_template_parameter_default_in_function_template | 
|  | 1176 | : diag::ext_template_parameter_default_in_function_template) | 
|  | 1177 | << DefArgRange; | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1178 | return false; | 
|  | 1179 |  | 
|  | 1180 | case Sema::TPC_ClassTemplateMember: | 
|  | 1181 | // C++0x [temp.param]p9: | 
|  | 1182 | //   A default template-argument shall not be specified in the | 
|  | 1183 | //   template-parameter-lists of the definition of a member of a | 
|  | 1184 | //   class template that appears outside of the member's class. | 
|  | 1185 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) | 
|  | 1186 | << DefArgRange; | 
|  | 1187 | return true; | 
|  | 1188 |  | 
|  | 1189 | case Sema::TPC_FriendFunctionTemplate: | 
|  | 1190 | // C++ [temp.param]p9: | 
|  | 1191 | //   A default template-argument shall not be specified in a | 
|  | 1192 | //   friend template declaration. | 
|  | 1193 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) | 
|  | 1194 | << DefArgRange; | 
|  | 1195 | return true; | 
|  | 1196 |  | 
|  | 1197 | // FIXME: C++0x [temp.param]p9 allows default template-arguments | 
|  | 1198 | // for friend function templates if there is only a single | 
|  | 1199 | // declaration (and it is a definition). Strange! | 
|  | 1200 | } | 
|  | 1201 |  | 
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1202 | llvm_unreachable("Invalid TemplateParamListContext!"); | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1203 | } | 
|  | 1204 |  | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1205 | /// \brief Check for unexpanded parameter packs within the template parameters | 
|  | 1206 | /// of a template template parameter, recursively. | 
| Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1207 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, | 
|  | 1208 | TemplateTemplateParmDecl *TTP) { | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1209 | // A template template parameter which is a parameter pack is also a pack | 
|  | 1210 | // expansion. | 
|  | 1211 | if (TTP->isParameterPack()) | 
|  | 1212 | return false; | 
|  | 1213 |  | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1214 | TemplateParameterList *Params = TTP->getTemplateParameters(); | 
|  | 1215 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { | 
|  | 1216 | NamedDecl *P = Params->getParam(I); | 
|  | 1217 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1218 | if (!NTTP->isParameterPack() && | 
|  | 1219 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1220 | NTTP->getTypeSourceInfo(), | 
|  | 1221 | Sema::UPPC_NonTypeTemplateParameterType)) | 
|  | 1222 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1223 |  | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1224 | continue; | 
|  | 1225 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1226 |  | 
|  | 1227 | if (TemplateTemplateParmDecl *InnerTTP | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1228 | = dyn_cast<TemplateTemplateParmDecl>(P)) | 
|  | 1229 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) | 
|  | 1230 | return true; | 
|  | 1231 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1232 |  | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1233 | return false; | 
|  | 1234 | } | 
|  | 1235 |  | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1236 | /// \brief Checks the validity of a template parameter list, possibly | 
|  | 1237 | /// considering the template parameter list from a previous | 
|  | 1238 | /// declaration. | 
|  | 1239 | /// | 
|  | 1240 | /// If an "old" template parameter list is provided, it must be | 
|  | 1241 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" | 
|  | 1242 | /// template parameter list. | 
|  | 1243 | /// | 
|  | 1244 | /// \param NewParams Template parameter list for a new template | 
|  | 1245 | /// declaration. This template parameter list will be updated with any | 
|  | 1246 | /// default arguments that are carried through from the previous | 
|  | 1247 | /// template parameter list. | 
|  | 1248 | /// | 
|  | 1249 | /// \param OldParams If provided, template parameter list from a | 
|  | 1250 | /// previous declaration of the same template. Default template | 
|  | 1251 | /// arguments will be merged from the old template parameter list to | 
|  | 1252 | /// the new template parameter list. | 
|  | 1253 | /// | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1254 | /// \param TPC Describes the context in which we are checking the given | 
|  | 1255 | /// template parameter list. | 
|  | 1256 | /// | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1257 | /// \returns true if an error occurred, false otherwise. | 
|  | 1258 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1259 | TemplateParameterList *OldParams, | 
|  | 1260 | TemplateParamListContext TPC) { | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1261 | bool Invalid = false; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 |  | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1263 | // C++ [temp.param]p10: | 
|  | 1264 | //   The set of default template-arguments available for use with a | 
|  | 1265 | //   template declaration or definition is obtained by merging the | 
|  | 1266 | //   default arguments from the definition (if in scope) and all | 
|  | 1267 | //   declarations in scope in the same way default function | 
|  | 1268 | //   arguments are (8.3.6). | 
|  | 1269 | bool SawDefaultArgument = false; | 
|  | 1270 | SourceLocation PreviousDefaultArgLoc; | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1271 |  | 
| Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1272 | // Dummy initialization to avoid warnings. | 
| Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1273 | TemplateParameterList::iterator OldParam = NewParams->end(); | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1274 | if (OldParams) | 
|  | 1275 | OldParam = OldParams->begin(); | 
|  | 1276 |  | 
| Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1277 | bool RemoveDefaultArguments = false; | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1278 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), | 
|  | 1279 | NewParamEnd = NewParams->end(); | 
|  | 1280 | NewParam != NewParamEnd; ++NewParam) { | 
|  | 1281 | // Variables used to diagnose redundant default arguments | 
|  | 1282 | bool RedundantDefaultArg = false; | 
|  | 1283 | SourceLocation OldDefaultLoc; | 
|  | 1284 | SourceLocation NewDefaultLoc; | 
|  | 1285 |  | 
| David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1286 | // Variable used to diagnose missing default arguments | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1287 | bool MissingDefaultArg = false; | 
|  | 1288 |  | 
| David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1289 | // Variable used to diagnose non-final parameter packs | 
|  | 1290 | bool SawParameterPack = false; | 
| Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1291 |  | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1292 | if (TemplateTypeParmDecl *NewTypeParm | 
|  | 1293 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1294 | // Check the presence of a default argument here. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1295 | if (NewTypeParm->hasDefaultArgument() && | 
|  | 1296 | DiagnoseDefaultTemplateArgument(*this, TPC, | 
|  | 1297 | NewTypeParm->getLocation(), | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1298 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() | 
| Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1299 | .getSourceRange())) | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1300 | NewTypeParm->removeDefaultArgument(); | 
|  | 1301 |  | 
|  | 1302 | // Merge default arguments for template type parameters. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | TemplateTypeParmDecl *OldTypeParm | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1304 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 |  | 
| Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1306 | if (NewTypeParm->isParameterPack()) { | 
|  | 1307 | assert(!NewTypeParm->hasDefaultArgument() && | 
|  | 1308 | "Parameter packs can't have a default argument!"); | 
|  | 1309 | SawParameterPack = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1311 | NewTypeParm->hasDefaultArgument()) { | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1312 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); | 
|  | 1313 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); | 
|  | 1314 | SawDefaultArgument = true; | 
|  | 1315 | RedundantDefaultArg = true; | 
|  | 1316 | PreviousDefaultArgLoc = NewDefaultLoc; | 
|  | 1317 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { | 
|  | 1318 | // Merge the default argument from the old declaration to the | 
|  | 1319 | // new declaration. | 
|  | 1320 | SawDefaultArgument = true; | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1321 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1322 | true); | 
|  | 1323 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); | 
|  | 1324 | } else if (NewTypeParm->hasDefaultArgument()) { | 
|  | 1325 | SawDefaultArgument = true; | 
|  | 1326 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); | 
|  | 1327 | } else if (SawDefaultArgument) | 
|  | 1328 | MissingDefaultArg = true; | 
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1329 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1330 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1331 | // Check for unexpanded parameter packs. | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1332 | if (!NewNonTypeParm->isParameterPack() && | 
|  | 1333 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1334 | NewNonTypeParm->getTypeSourceInfo(), | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1335 | UPPC_NonTypeTemplateParameterType)) { | 
|  | 1336 | Invalid = true; | 
|  | 1337 | continue; | 
|  | 1338 | } | 
|  | 1339 |  | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1340 | // Check the presence of a default argument here. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1341 | if (NewNonTypeParm->hasDefaultArgument() && | 
|  | 1342 | DiagnoseDefaultTemplateArgument(*this, TPC, | 
|  | 1343 | NewNonTypeParm->getLocation(), | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1344 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { | 
| Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1345 | NewNonTypeParm->removeDefaultArgument(); | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1346 | } | 
|  | 1347 |  | 
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1348 | // Merge default arguments for non-type template parameters | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1349 | NonTypeTemplateParmDecl *OldNonTypeParm | 
|  | 1350 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; | 
| Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1351 | if (NewNonTypeParm->isParameterPack()) { | 
|  | 1352 | assert(!NewNonTypeParm->hasDefaultArgument() && | 
|  | 1353 | "Parameter packs can't have a default argument!"); | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1354 | if (!NewNonTypeParm->isPackExpansion()) | 
|  | 1355 | SawParameterPack = true; | 
| Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1356 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1357 | NewNonTypeParm->hasDefaultArgument()) { | 
|  | 1358 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); | 
|  | 1359 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); | 
|  | 1360 | SawDefaultArgument = true; | 
|  | 1361 | RedundantDefaultArg = true; | 
|  | 1362 | PreviousDefaultArgLoc = NewDefaultLoc; | 
|  | 1363 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { | 
|  | 1364 | // Merge the default argument from the old declaration to the | 
|  | 1365 | // new declaration. | 
|  | 1366 | SawDefaultArgument = true; | 
|  | 1367 | // FIXME: We need to create a new kind of "default argument" | 
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1368 | // expression that points to a previous non-type template | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1369 | // parameter. | 
|  | 1370 | NewNonTypeParm->setDefaultArgument( | 
| Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1371 | OldNonTypeParm->getDefaultArgument(), | 
|  | 1372 | /*Inherited=*/ true); | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1373 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); | 
|  | 1374 | } else if (NewNonTypeParm->hasDefaultArgument()) { | 
|  | 1375 | SawDefaultArgument = true; | 
|  | 1376 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); | 
|  | 1377 | } else if (SawDefaultArgument) | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | MissingDefaultArg = true; | 
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1379 | } else { | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1380 | TemplateTemplateParmDecl *NewTemplateParm | 
|  | 1381 | = cast<TemplateTemplateParmDecl>(*NewParam); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1382 |  | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1383 | // Check for unexpanded parameter packs, recursively. | 
| Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1384 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { | 
| Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1385 | Invalid = true; | 
|  | 1386 | continue; | 
|  | 1387 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1388 |  | 
| David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1389 | // Check the presence of a default argument here. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1390 | if (NewTemplateParm->hasDefaultArgument() && | 
|  | 1391 | DiagnoseDefaultTemplateArgument(*this, TPC, | 
|  | 1392 | NewTemplateParm->getLocation(), | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1393 | NewTemplateParm->getDefaultArgument().getSourceRange())) | 
| Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1394 | NewTemplateParm->removeDefaultArgument(); | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1395 |  | 
|  | 1396 | // Merge default arguments for template template parameters | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1397 | TemplateTemplateParmDecl *OldTemplateParm | 
|  | 1398 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; | 
| Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1399 | if (NewTemplateParm->isParameterPack()) { | 
|  | 1400 | assert(!NewTemplateParm->hasDefaultArgument() && | 
|  | 1401 | "Parameter packs can't have a default argument!"); | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1402 | if (!NewTemplateParm->isPackExpansion()) | 
|  | 1403 | SawParameterPack = true; | 
| Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1404 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1405 | NewTemplateParm->hasDefaultArgument()) { | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1406 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); | 
|  | 1407 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1408 | SawDefaultArgument = true; | 
|  | 1409 | RedundantDefaultArg = true; | 
|  | 1410 | PreviousDefaultArgLoc = NewDefaultLoc; | 
|  | 1411 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { | 
|  | 1412 | // Merge the default argument from the old declaration to the | 
|  | 1413 | // new declaration. | 
|  | 1414 | SawDefaultArgument = true; | 
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1415 | // FIXME: We need to create a new kind of "default argument" expression | 
|  | 1416 | // that points to a previous template template parameter. | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1417 | NewTemplateParm->setDefaultArgument( | 
| Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1418 | OldTemplateParm->getDefaultArgument(), | 
|  | 1419 | /*Inherited=*/ true); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1420 | PreviousDefaultArgLoc | 
|  | 1421 | = OldTemplateParm->getDefaultArgument().getLocation(); | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1422 | } else if (NewTemplateParm->hasDefaultArgument()) { | 
|  | 1423 | SawDefaultArgument = true; | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1424 | PreviousDefaultArgLoc | 
|  | 1425 | = NewTemplateParm->getDefaultArgument().getLocation(); | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1426 | } else if (SawDefaultArgument) | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1427 | MissingDefaultArg = true; | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1428 | } | 
|  | 1429 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1430 | // C++11 [temp.param]p11: | 
| David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1431 | //   If a template parameter of a primary class template or alias template | 
|  | 1432 | //   is a template parameter pack, it shall be the last template parameter. | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1433 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && | 
| David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1434 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { | 
|  | 1435 | Diag((*NewParam)->getLocation(), | 
|  | 1436 | diag::err_template_param_pack_must_be_last_template_parameter); | 
|  | 1437 | Invalid = true; | 
|  | 1438 | } | 
|  | 1439 |  | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1440 | if (RedundantDefaultArg) { | 
|  | 1441 | // C++ [temp.param]p12: | 
|  | 1442 | //   A template-parameter shall not be given default arguments | 
|  | 1443 | //   by two different declarations in the same scope. | 
|  | 1444 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); | 
|  | 1445 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); | 
|  | 1446 | Invalid = true; | 
| Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1447 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1448 | // C++ [temp.param]p11: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | //   If a template-parameter of a class template has a default | 
|  | 1450 | //   template-argument, each subsequent template-parameter shall either | 
| Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1451 | //   have a default template-argument supplied or be a template parameter | 
|  | 1452 | //   pack. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | Diag((*NewParam)->getLocation(), | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1454 | diag::err_template_param_default_arg_missing); | 
|  | 1455 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); | 
|  | 1456 | Invalid = true; | 
| Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1457 | RemoveDefaultArguments = true; | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1458 | } | 
|  | 1459 |  | 
|  | 1460 | // If we have an old template parameter list that we're merging | 
|  | 1461 | // in, move on to the next parameter. | 
|  | 1462 | if (OldParams) | 
|  | 1463 | ++OldParam; | 
|  | 1464 | } | 
|  | 1465 |  | 
| Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1466 | // We were missing some default arguments at the end of the list, so remove | 
|  | 1467 | // all of the default arguments. | 
|  | 1468 | if (RemoveDefaultArguments) { | 
|  | 1469 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), | 
|  | 1470 | NewParamEnd = NewParams->end(); | 
|  | 1471 | NewParam != NewParamEnd; ++NewParam) { | 
|  | 1472 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) | 
|  | 1473 | TTP->removeDefaultArgument(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1474 | else if (NonTypeTemplateParmDecl *NTTP | 
| Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1475 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) | 
|  | 1476 | NTTP->removeDefaultArgument(); | 
|  | 1477 | else | 
|  | 1478 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); | 
|  | 1479 | } | 
|  | 1480 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1481 |  | 
| Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1482 | return Invalid; | 
|  | 1483 | } | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1484 |  | 
| John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1485 | namespace { | 
|  | 1486 |  | 
|  | 1487 | /// A class which looks for a use of a certain level of template | 
|  | 1488 | /// parameter. | 
|  | 1489 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { | 
|  | 1490 | typedef RecursiveASTVisitor<DependencyChecker> super; | 
|  | 1491 |  | 
|  | 1492 | unsigned Depth; | 
|  | 1493 | bool Match; | 
|  | 1494 |  | 
|  | 1495 | DependencyChecker(TemplateParameterList *Params) : Match(false) { | 
|  | 1496 | NamedDecl *ND = Params->getParam(0); | 
|  | 1497 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { | 
|  | 1498 | Depth = PD->getDepth(); | 
|  | 1499 | } else if (NonTypeTemplateParmDecl *PD = | 
|  | 1500 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { | 
|  | 1501 | Depth = PD->getDepth(); | 
|  | 1502 | } else { | 
|  | 1503 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); | 
|  | 1504 | } | 
|  | 1505 | } | 
|  | 1506 |  | 
|  | 1507 | bool Matches(unsigned ParmDepth) { | 
|  | 1508 | if (ParmDepth >= Depth) { | 
|  | 1509 | Match = true; | 
|  | 1510 | return true; | 
|  | 1511 | } | 
|  | 1512 | return false; | 
|  | 1513 | } | 
|  | 1514 |  | 
|  | 1515 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { | 
|  | 1516 | return !Matches(T->getDepth()); | 
|  | 1517 | } | 
|  | 1518 |  | 
|  | 1519 | bool TraverseTemplateName(TemplateName N) { | 
|  | 1520 | if (TemplateTemplateParmDecl *PD = | 
|  | 1521 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) | 
|  | 1522 | if (Matches(PD->getDepth())) return false; | 
|  | 1523 | return super::TraverseTemplateName(N); | 
|  | 1524 | } | 
|  | 1525 |  | 
|  | 1526 | bool VisitDeclRefExpr(DeclRefExpr *E) { | 
|  | 1527 | if (NonTypeTemplateParmDecl *PD = | 
|  | 1528 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { | 
|  | 1529 | if (PD->getDepth() == Depth) { | 
|  | 1530 | Match = true; | 
|  | 1531 | return false; | 
|  | 1532 | } | 
|  | 1533 | } | 
|  | 1534 | return super::VisitDeclRefExpr(E); | 
|  | 1535 | } | 
| Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1536 |  | 
|  | 1537 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { | 
|  | 1538 | return TraverseType(T->getInjectedSpecializationType()); | 
|  | 1539 | } | 
| John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1540 | }; | 
|  | 1541 | } | 
|  | 1542 |  | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1543 | /// Determines whether a given type depends on the given parameter | 
| John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1544 | /// list. | 
|  | 1545 | static bool | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1546 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { | 
| John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1547 | DependencyChecker Checker(Params); | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1548 | Checker.TraverseType(T); | 
| John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1549 | return Checker.Match; | 
|  | 1550 | } | 
|  | 1551 |  | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1552 | // Find the source range corresponding to the named type in the given | 
|  | 1553 | // nested-name-specifier, if any. | 
|  | 1554 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, | 
|  | 1555 | QualType T, | 
|  | 1556 | const CXXScopeSpec &SS) { | 
|  | 1557 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); | 
|  | 1558 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { | 
|  | 1559 | if (const Type *CurType = NNS->getAsType()) { | 
|  | 1560 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) | 
|  | 1561 | return NNSLoc.getTypeLoc().getSourceRange(); | 
|  | 1562 | } else | 
|  | 1563 | break; | 
|  | 1564 |  | 
|  | 1565 | NNSLoc = NNSLoc.getPrefix(); | 
|  | 1566 | } | 
|  | 1567 |  | 
|  | 1568 | return SourceRange(); | 
|  | 1569 | } | 
|  | 1570 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | /// \brief Match the given template parameter lists to the given scope | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1572 | /// specifier, returning the template parameter list that applies to the | 
|  | 1573 | /// name. | 
|  | 1574 | /// | 
|  | 1575 | /// \param DeclStartLoc the start of the declaration that has a scope | 
|  | 1576 | /// specifier or a template parameter list. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | /// | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1578 | /// \param DeclLoc The location of the declaration itself. | 
|  | 1579 | /// | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1580 | /// \param SS the scope specifier that will be matched to the given template | 
|  | 1581 | /// parameter lists. This scope specifier precedes a qualified name that is | 
|  | 1582 | /// being declared. | 
|  | 1583 | /// | 
|  | 1584 | /// \param ParamLists the template parameter lists, from the outermost to the | 
|  | 1585 | /// innermost template parameter lists. | 
|  | 1586 | /// | 
|  | 1587 | /// \param NumParamLists the number of template parameter lists in ParamLists. | 
|  | 1588 | /// | 
| John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1589 | /// \param IsFriend Whether to apply the slightly different rules for | 
|  | 1590 | /// matching template parameters to scope specifiers in friend | 
|  | 1591 | /// declarations. | 
|  | 1592 | /// | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1593 | /// \param IsExplicitSpecialization will be set true if the entity being | 
|  | 1594 | /// declared is an explicit specialization, false otherwise. | 
|  | 1595 | /// | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | /// \returns the template parameter list, if any, that corresponds to the | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1597 | /// name that is preceded by the scope specifier @p SS. This template | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1598 | /// parameter list may have template parameters (if we're declaring a | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | /// template) or may have no template parameters (if we're declaring a | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1600 | /// template specialization), or may be NULL (if what we're declaring isn't | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1601 | /// itself a template). | 
|  | 1602 | TemplateParameterList * | 
|  | 1603 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1604 | SourceLocation DeclLoc, | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1605 | const CXXScopeSpec &SS, | 
|  | 1606 | TemplateParameterList **ParamLists, | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1607 | unsigned NumParamLists, | 
| John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1608 | bool IsFriend, | 
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1609 | bool &IsExplicitSpecialization, | 
|  | 1610 | bool &Invalid) { | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1611 | IsExplicitSpecialization = false; | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1612 | Invalid = false; | 
|  | 1613 |  | 
|  | 1614 | // The sequence of nested types to which we will match up the template | 
|  | 1615 | // parameter lists. We first build this list by starting with the type named | 
|  | 1616 | // by the nested-name-specifier and walking out until we run out of types. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1617 | SmallVector<QualType, 4> NestedTypes; | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1618 | QualType T; | 
| Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1619 | if (SS.getScopeRep()) { | 
|  | 1620 | if (CXXRecordDecl *Record | 
|  | 1621 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) | 
|  | 1622 | T = Context.getTypeDeclType(Record); | 
|  | 1623 | else | 
|  | 1624 | T = QualType(SS.getScopeRep()->getAsType(), 0); | 
|  | 1625 | } | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1626 |  | 
|  | 1627 | // If we found an explicit specialization that prevents us from needing | 
|  | 1628 | // 'template<>' headers, this will be set to the location of that | 
|  | 1629 | // explicit specialization. | 
|  | 1630 | SourceLocation ExplicitSpecLoc; | 
|  | 1631 |  | 
|  | 1632 | while (!T.isNull()) { | 
|  | 1633 | NestedTypes.push_back(T); | 
|  | 1634 |  | 
|  | 1635 | // Retrieve the parent of a record type. | 
|  | 1636 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { | 
|  | 1637 | // If this type is an explicit specialization, we're done. | 
|  | 1638 | if (ClassTemplateSpecializationDecl *Spec | 
|  | 1639 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { | 
|  | 1640 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && | 
|  | 1641 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { | 
|  | 1642 | ExplicitSpecLoc = Spec->getLocation(); | 
|  | 1643 | break; | 
| Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1644 | } | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1645 | } else if (Record->getTemplateSpecializationKind() | 
|  | 1646 | == TSK_ExplicitSpecialization) { | 
|  | 1647 | ExplicitSpecLoc = Record->getLocation(); | 
| John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1648 | break; | 
|  | 1649 | } | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1650 |  | 
|  | 1651 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) | 
|  | 1652 | T = Context.getTypeDeclType(Parent); | 
|  | 1653 | else | 
|  | 1654 | T = QualType(); | 
|  | 1655 | continue; | 
|  | 1656 | } | 
|  | 1657 |  | 
|  | 1658 | if (const TemplateSpecializationType *TST | 
|  | 1659 | = T->getAs<TemplateSpecializationType>()) { | 
|  | 1660 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { | 
|  | 1661 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) | 
|  | 1662 | T = Context.getTypeDeclType(Parent); | 
|  | 1663 | else | 
|  | 1664 | T = QualType(); | 
|  | 1665 | continue; | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1666 | } | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1667 | } | 
|  | 1668 |  | 
|  | 1669 | // Look one step prior in a dependent template specialization type. | 
|  | 1670 | if (const DependentTemplateSpecializationType *DependentTST | 
|  | 1671 | = T->getAs<DependentTemplateSpecializationType>()) { | 
|  | 1672 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) | 
|  | 1673 | T = QualType(NNS->getAsType(), 0); | 
|  | 1674 | else | 
|  | 1675 | T = QualType(); | 
|  | 1676 | continue; | 
|  | 1677 | } | 
|  | 1678 |  | 
|  | 1679 | // Look one step prior in a dependent name type. | 
|  | 1680 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ | 
|  | 1681 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) | 
|  | 1682 | T = QualType(NNS->getAsType(), 0); | 
|  | 1683 | else | 
|  | 1684 | T = QualType(); | 
|  | 1685 | continue; | 
|  | 1686 | } | 
|  | 1687 |  | 
|  | 1688 | // Retrieve the parent of an enumeration type. | 
|  | 1689 | if (const EnumType *EnumT = T->getAs<EnumType>()) { | 
|  | 1690 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization | 
|  | 1691 | // check here. | 
|  | 1692 | EnumDecl *Enum = EnumT->getDecl(); | 
|  | 1693 |  | 
|  | 1694 | // Get to the parent type. | 
|  | 1695 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) | 
|  | 1696 | T = Context.getTypeDeclType(Parent); | 
|  | 1697 | else | 
|  | 1698 | T = QualType(); | 
|  | 1699 | continue; | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1700 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 |  | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1702 | T = QualType(); | 
|  | 1703 | } | 
|  | 1704 | // Reverse the nested types list, since we want to traverse from the outermost | 
|  | 1705 | // to the innermost while checking template-parameter-lists. | 
|  | 1706 | std::reverse(NestedTypes.begin(), NestedTypes.end()); | 
| Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1707 |  | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1708 | // C++0x [temp.expl.spec]p17: | 
|  | 1709 | //   A member or a member template may be nested within many | 
|  | 1710 | //   enclosing class templates. In an explicit specialization for | 
|  | 1711 | //   such a member, the member declaration shall be preceded by a | 
|  | 1712 | //   template<> for each enclosing class template that is | 
|  | 1713 | //   explicitly specialized. | 
| Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1714 | bool SawNonEmptyTemplateParameterList = false; | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1715 | unsigned ParamIdx = 0; | 
|  | 1716 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; | 
|  | 1717 | ++TypeIdx) { | 
|  | 1718 | T = NestedTypes[TypeIdx]; | 
|  | 1719 |  | 
|  | 1720 | // Whether we expect a 'template<>' header. | 
|  | 1721 | bool NeedEmptyTemplateHeader = false; | 
|  | 1722 |  | 
|  | 1723 | // Whether we expect a template header with parameters. | 
|  | 1724 | bool NeedNonemptyTemplateHeader = false; | 
|  | 1725 |  | 
|  | 1726 | // For a dependent type, the set of template parameters that we | 
|  | 1727 | // expect to see. | 
|  | 1728 | TemplateParameterList *ExpectedTemplateParams = 0; | 
|  | 1729 |  | 
| Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1730 | // C++0x [temp.expl.spec]p15: | 
|  | 1731 | //   A member or a member template may be nested within many enclosing | 
|  | 1732 | //   class templates. In an explicit specialization for such a member, the | 
|  | 1733 | //   member declaration shall be preceded by a template<> for each | 
|  | 1734 | //   enclosing class template that is explicitly specialized. | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1735 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { | 
|  | 1736 | if (ClassTemplatePartialSpecializationDecl *Partial | 
|  | 1737 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { | 
|  | 1738 | ExpectedTemplateParams = Partial->getTemplateParameters(); | 
|  | 1739 | NeedNonemptyTemplateHeader = true; | 
|  | 1740 | } else if (Record->isDependentType()) { | 
|  | 1741 | if (Record->getDescribedClassTemplate()) { | 
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1742 | ExpectedTemplateParams = Record->getDescribedClassTemplate() | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1743 | ->getTemplateParameters(); | 
|  | 1744 | NeedNonemptyTemplateHeader = true; | 
|  | 1745 | } | 
|  | 1746 | } else if (ClassTemplateSpecializationDecl *Spec | 
|  | 1747 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { | 
|  | 1748 | // C++0x [temp.expl.spec]p4: | 
|  | 1749 | //   Members of an explicitly specialized class template are defined | 
|  | 1750 | //   in the same manner as members of normal classes, and not using | 
|  | 1751 | //   the template<> syntax. | 
|  | 1752 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) | 
|  | 1753 | NeedEmptyTemplateHeader = true; | 
|  | 1754 | else | 
| Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1755 | continue; | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1756 | } else if (Record->getTemplateSpecializationKind()) { | 
|  | 1757 | if (Record->getTemplateSpecializationKind() | 
| Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1758 | != TSK_ExplicitSpecialization && | 
|  | 1759 | TypeIdx == NumTypes - 1) | 
|  | 1760 | IsExplicitSpecialization = true; | 
|  | 1761 |  | 
|  | 1762 | continue; | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1763 | } | 
|  | 1764 | } else if (const TemplateSpecializationType *TST | 
|  | 1765 | = T->getAs<TemplateSpecializationType>()) { | 
|  | 1766 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { | 
|  | 1767 | ExpectedTemplateParams = Template->getTemplateParameters(); | 
|  | 1768 | NeedNonemptyTemplateHeader = true; | 
|  | 1769 | } | 
|  | 1770 | } else if (T->getAs<DependentTemplateSpecializationType>()) { | 
|  | 1771 | // FIXME:  We actually could/should check the template arguments here | 
|  | 1772 | // against the corresponding template parameter list. | 
|  | 1773 | NeedNonemptyTemplateHeader = false; | 
|  | 1774 | } | 
|  | 1775 |  | 
| Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1776 | // C++ [temp.expl.spec]p16: | 
|  | 1777 | //   In an explicit specialization declaration for a member of a class | 
|  | 1778 | //   template or a member template that ap- pears in namespace scope, the | 
|  | 1779 | //   member template and some of its enclosing class templates may remain | 
|  | 1780 | //   unspecialized, except that the declaration shall not explicitly | 
|  | 1781 | //   specialize a class member template if its en- closing class templates | 
|  | 1782 | //   are not explicitly specialized as well. | 
|  | 1783 | if (ParamIdx < NumParamLists) { | 
|  | 1784 | if (ParamLists[ParamIdx]->size() == 0) { | 
|  | 1785 | if (SawNonEmptyTemplateParameterList) { | 
|  | 1786 | Diag(DeclLoc, diag::err_specialize_member_of_template) | 
|  | 1787 | << ParamLists[ParamIdx]->getSourceRange(); | 
|  | 1788 | Invalid = true; | 
|  | 1789 | IsExplicitSpecialization = false; | 
|  | 1790 | return 0; | 
|  | 1791 | } | 
|  | 1792 | } else | 
|  | 1793 | SawNonEmptyTemplateParameterList = true; | 
|  | 1794 | } | 
|  | 1795 |  | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1796 | if (NeedEmptyTemplateHeader) { | 
|  | 1797 | // If we're on the last of the types, and we need a 'template<>' header | 
|  | 1798 | // here, then it's an explicit specialization. | 
|  | 1799 | if (TypeIdx == NumTypes - 1) | 
|  | 1800 | IsExplicitSpecialization = true; | 
|  | 1801 |  | 
|  | 1802 | if (ParamIdx < NumParamLists) { | 
|  | 1803 | if (ParamLists[ParamIdx]->size() > 0) { | 
|  | 1804 | // The header has template parameters when it shouldn't. Complain. | 
|  | 1805 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), | 
|  | 1806 | diag::err_template_param_list_matches_nontemplate) | 
|  | 1807 | << T | 
|  | 1808 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), | 
|  | 1809 | ParamLists[ParamIdx]->getRAngleLoc()) | 
|  | 1810 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); | 
|  | 1811 | Invalid = true; | 
|  | 1812 | return 0; | 
|  | 1813 | } | 
|  | 1814 |  | 
|  | 1815 | // Consume this template header. | 
|  | 1816 | ++ParamIdx; | 
|  | 1817 | continue; | 
|  | 1818 | } | 
|  | 1819 |  | 
|  | 1820 | if (!IsFriend) { | 
|  | 1821 | // We don't have a template header, but we should. | 
|  | 1822 | SourceLocation ExpectedTemplateLoc; | 
|  | 1823 | if (NumParamLists > 0) | 
|  | 1824 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); | 
|  | 1825 | else | 
|  | 1826 | ExpectedTemplateLoc = DeclStartLoc; | 
|  | 1827 |  | 
|  | 1828 | Diag(DeclLoc, diag::err_template_spec_needs_header) | 
|  | 1829 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) | 
|  | 1830 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); | 
|  | 1831 | } | 
|  | 1832 |  | 
|  | 1833 | continue; | 
|  | 1834 | } | 
|  | 1835 |  | 
|  | 1836 | if (NeedNonemptyTemplateHeader) { | 
|  | 1837 | // In friend declarations we can have template-ids which don't | 
|  | 1838 | // depend on the corresponding template parameter lists.  But | 
|  | 1839 | // assume that empty parameter lists are supposed to match this | 
|  | 1840 | // template-id. | 
|  | 1841 | if (IsFriend && T->isDependentType()) { | 
|  | 1842 | if (ParamIdx < NumParamLists && | 
|  | 1843 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) | 
|  | 1844 | ExpectedTemplateParams = 0; | 
|  | 1845 | else | 
|  | 1846 | continue; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | } | 
| Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1848 |  | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1849 | if (ParamIdx < NumParamLists) { | 
|  | 1850 | // Check the template parameter list, if we can. | 
|  | 1851 | if (ExpectedTemplateParams && | 
|  | 1852 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], | 
|  | 1853 | ExpectedTemplateParams, | 
|  | 1854 | true, TPL_TemplateMatch)) | 
|  | 1855 | Invalid = true; | 
|  | 1856 |  | 
|  | 1857 | if (!Invalid && | 
|  | 1858 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, | 
|  | 1859 | TPC_ClassTemplateMember)) | 
|  | 1860 | Invalid = true; | 
|  | 1861 |  | 
|  | 1862 | ++ParamIdx; | 
|  | 1863 | continue; | 
|  | 1864 | } | 
|  | 1865 |  | 
|  | 1866 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) | 
|  | 1867 | << T | 
|  | 1868 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); | 
|  | 1869 | Invalid = true; | 
|  | 1870 | continue; | 
|  | 1871 | } | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1872 | } | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1873 |  | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1874 | // If there were at least as many template-ids as there were template | 
|  | 1875 | // parameter lists, then there are no template parameter lists remaining for | 
|  | 1876 | // the declaration itself. | 
| John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1877 | if (ParamIdx >= NumParamLists) | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1878 | return 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 |  | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1880 | // If there were too many template parameter lists, complain about that now. | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1881 | if (ParamIdx < NumParamLists - 1) { | 
|  | 1882 | bool HasAnyExplicitSpecHeader = false; | 
|  | 1883 | bool AllExplicitSpecHeaders = true; | 
|  | 1884 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { | 
|  | 1885 | if (ParamLists[I]->size() == 0) | 
|  | 1886 | HasAnyExplicitSpecHeader = true; | 
|  | 1887 | else | 
|  | 1888 | AllExplicitSpecHeaders = false; | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1889 | } | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1890 |  | 
|  | 1891 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), | 
|  | 1892 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers | 
|  | 1893 | : diag::err_template_spec_extra_headers) | 
|  | 1894 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), | 
|  | 1895 | ParamLists[NumParamLists - 2]->getRAngleLoc()); | 
|  | 1896 |  | 
|  | 1897 | // If there was a specialization somewhere, such that 'template<>' is | 
|  | 1898 | // not required, and there were any 'template<>' headers, note where the | 
|  | 1899 | // specialization occurred. | 
|  | 1900 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) | 
|  | 1901 | Diag(ExplicitSpecLoc, | 
|  | 1902 | diag::note_explicit_template_spec_does_not_need_header) | 
|  | 1903 | << NestedTypes.back(); | 
|  | 1904 |  | 
|  | 1905 | // We have a template parameter list with no corresponding scope, which | 
|  | 1906 | // means that the resulting template declaration can't be instantiated | 
|  | 1907 | // properly (we'll end up with dependent nodes when we shouldn't). | 
|  | 1908 | if (!AllExplicitSpecHeaders) | 
|  | 1909 | Invalid = true; | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1910 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 |  | 
| Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1912 | // C++ [temp.expl.spec]p16: | 
|  | 1913 | //   In an explicit specialization declaration for a member of a class | 
|  | 1914 | //   template or a member template that ap- pears in namespace scope, the | 
|  | 1915 | //   member template and some of its enclosing class templates may remain | 
|  | 1916 | //   unspecialized, except that the declaration shall not explicitly | 
|  | 1917 | //   specialize a class member template if its en- closing class templates | 
|  | 1918 | //   are not explicitly specialized as well. | 
|  | 1919 | if (ParamLists[NumParamLists - 1]->size() == 0 && | 
|  | 1920 | SawNonEmptyTemplateParameterList) { | 
|  | 1921 | Diag(DeclLoc, diag::err_specialize_member_of_template) | 
|  | 1922 | << ParamLists[ParamIdx]->getSourceRange(); | 
|  | 1923 | Invalid = true; | 
|  | 1924 | IsExplicitSpecialization = false; | 
|  | 1925 | return 0; | 
|  | 1926 | } | 
|  | 1927 |  | 
| Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1928 | // Return the last template parameter list, which corresponds to the | 
|  | 1929 | // entity being declared. | 
|  | 1930 | return ParamLists[NumParamLists - 1]; | 
|  | 1931 | } | 
|  | 1932 |  | 
| Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1933 | void Sema::NoteAllFoundTemplates(TemplateName Name) { | 
|  | 1934 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { | 
|  | 1935 | Diag(Template->getLocation(), diag::note_template_declared_here) | 
|  | 1936 | << (isa<FunctionTemplateDecl>(Template)? 0 | 
|  | 1937 | : isa<ClassTemplateDecl>(Template)? 1 | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1938 | : isa<TypeAliasTemplateDecl>(Template)? 2 | 
|  | 1939 | : 3) | 
| Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1940 | << Template->getDeclName(); | 
|  | 1941 | return; | 
|  | 1942 | } | 
|  | 1943 |  | 
|  | 1944 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { | 
|  | 1945 | for (OverloadedTemplateStorage::iterator I = OST->begin(), | 
|  | 1946 | IEnd = OST->end(); | 
|  | 1947 | I != IEnd; ++I) | 
|  | 1948 | Diag((*I)->getLocation(), diag::note_template_declared_here) | 
|  | 1949 | << 0 << (*I)->getDeclName(); | 
|  | 1950 |  | 
|  | 1951 | return; | 
|  | 1952 | } | 
|  | 1953 | } | 
|  | 1954 |  | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1955 | QualType Sema::CheckTemplateIdType(TemplateName Name, | 
|  | 1956 | SourceLocation TemplateLoc, | 
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1957 | TemplateArgumentListInfo &TemplateArgs) { | 
| John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1958 | DependentTemplateName *DTN | 
|  | 1959 | = Name.getUnderlying().getAsDependentTemplateName(); | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1960 | if (DTN && DTN->isIdentifier()) | 
|  | 1961 | // When building a template-id where the template-name is dependent, | 
|  | 1962 | // assume the template is a type template. Either our assumption is | 
|  | 1963 | // correct, or the code is ill-formed and will be diagnosed when the | 
|  | 1964 | // dependent name is substituted. | 
|  | 1965 | return Context.getDependentTemplateSpecializationType(ETK_None, | 
|  | 1966 | DTN->getQualifier(), | 
|  | 1967 | DTN->getIdentifier(), | 
|  | 1968 | TemplateArgs); | 
|  | 1969 |  | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1970 | TemplateDecl *Template = Name.getAsTemplateDecl(); | 
| Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1971 | if (!Template || isa<FunctionTemplateDecl>(Template)) { | 
|  | 1972 | // We might have a substituted template template parameter pack. If so, | 
|  | 1973 | // build a template specialization type for it. | 
|  | 1974 | if (Name.getAsSubstTemplateTemplateParmPack()) | 
|  | 1975 | return Context.getTemplateSpecializationType(Name, TemplateArgs); | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1976 |  | 
| Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1977 | Diag(TemplateLoc, diag::err_template_id_not_a_type) | 
|  | 1978 | << Name; | 
|  | 1979 | NoteAllFoundTemplates(Name); | 
|  | 1980 | return QualType(); | 
| Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1981 | } | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1982 |  | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1983 | // Check that the template argument list is well-formed for this | 
|  | 1984 | // template. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1985 | SmallVector<TemplateArgument, 4> Converted; | 
| Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1986 | bool ExpansionIntoFixedList = false; | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1987 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, | 
| Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1988 | false, Converted, &ExpansionIntoFixedList)) | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1989 | return QualType(); | 
|  | 1990 |  | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1991 | QualType CanonType; | 
|  | 1992 |  | 
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1993 | bool InstantiationDependent = false; | 
| Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1994 | TypeAliasTemplateDecl *AliasTemplate = 0; | 
|  | 1995 | if (!ExpansionIntoFixedList && | 
|  | 1996 | (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) { | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1997 | // Find the canonical type for this type alias template specialization. | 
|  | 1998 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); | 
|  | 1999 | if (Pattern->isInvalidDecl()) | 
|  | 2000 | return QualType(); | 
|  | 2001 |  | 
|  | 2002 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, | 
|  | 2003 | Converted.data(), Converted.size()); | 
|  | 2004 |  | 
|  | 2005 | // Only substitute for the innermost template argument list. | 
|  | 2006 | MultiLevelTemplateArgumentList TemplateArgLists; | 
| Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2007 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); | 
| Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2008 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); | 
|  | 2009 | for (unsigned I = 0; I < Depth; ++I) | 
|  | 2010 | TemplateArgLists.addOuterTemplateArguments(0, 0); | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2011 |  | 
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2012 | LocalInstantiationScope Scope(*this); | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2013 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); | 
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2014 | if (Inst) | 
|  | 2015 | return QualType(); | 
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2016 |  | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2017 | CanonType = SubstType(Pattern->getUnderlyingType(), | 
|  | 2018 | TemplateArgLists, AliasTemplate->getLocation(), | 
|  | 2019 | AliasTemplate->getDeclName()); | 
|  | 2020 | if (CanonType.isNull()) | 
|  | 2021 | return QualType(); | 
|  | 2022 | } else if (Name.isDependent() || | 
|  | 2023 | TemplateSpecializationType::anyDependentTemplateArguments( | 
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2024 | TemplateArgs, InstantiationDependent)) { | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2025 | // This class template specialization is a dependent | 
|  | 2026 | // type. Therefore, its canonical type is another class template | 
|  | 2027 | // specialization type that contains all of the converted | 
|  | 2028 | // arguments in canonical form. This ensures that, e.g., A<T> and | 
|  | 2029 | // A<T, T> have identical types when A is declared as: | 
|  | 2030 | // | 
|  | 2031 | //   template<typename T, typename U = T> struct A; | 
| Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2032 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | CanonType = Context.getTemplateSpecializationType(CanonName, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2034 | Converted.data(), | 
|  | 2035 | Converted.size()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 |  | 
| Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2037 | // FIXME: CanonType is not actually the canonical type, and unfortunately | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2038 | // it is a TemplateSpecializationType that we will never use again. | 
| Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2039 | // In the future, we need to teach getTemplateSpecializationType to only | 
|  | 2040 | // build the canonical type and return that to us. | 
|  | 2041 | CanonType = Context.getCanonicalType(CanonType); | 
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2042 |  | 
|  | 2043 | // This might work out to be a current instantiation, in which | 
|  | 2044 | // case the canonical type needs to be the InjectedClassNameType. | 
|  | 2045 | // | 
|  | 2046 | // TODO: in theory this could be a simple hashtable lookup; most | 
|  | 2047 | // changes to CurContext don't change the set of current | 
|  | 2048 | // instantiations. | 
|  | 2049 | if (isa<ClassTemplateDecl>(Template)) { | 
|  | 2050 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { | 
|  | 2051 | // If we get out to a namespace, we're done. | 
|  | 2052 | if (Ctx->isFileContext()) break; | 
|  | 2053 |  | 
|  | 2054 | // If this isn't a record, keep looking. | 
|  | 2055 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); | 
|  | 2056 | if (!Record) continue; | 
|  | 2057 |  | 
|  | 2058 | // Look for one of the two cases with InjectedClassNameTypes | 
|  | 2059 | // and check whether it's the same template. | 
|  | 2060 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && | 
|  | 2061 | !Record->getDescribedClassTemplate()) | 
|  | 2062 | continue; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2063 |  | 
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2064 | // Fetch the injected class name type and check whether its | 
|  | 2065 | // injected type is equal to the type we just built. | 
|  | 2066 | QualType ICNT = Context.getTypeDeclType(Record); | 
|  | 2067 | QualType Injected = cast<InjectedClassNameType>(ICNT) | 
|  | 2068 | ->getInjectedSpecializationType(); | 
|  | 2069 |  | 
|  | 2070 | if (CanonType != Injected->getCanonicalTypeInternal()) | 
|  | 2071 | continue; | 
|  | 2072 |  | 
|  | 2073 | // If so, the canonical type of this TST is the injected | 
|  | 2074 | // class name type of the record we just found. | 
|  | 2075 | assert(ICNT.isCanonical()); | 
|  | 2076 | CanonType = ICNT; | 
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2077 | break; | 
|  | 2078 | } | 
|  | 2079 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | } else if (ClassTemplateDecl *ClassTemplate | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2081 | = dyn_cast<ClassTemplateDecl>(Template)) { | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2082 | // Find the class template specialization declaration that | 
|  | 2083 | // corresponds to these arguments. | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2084 | void *InsertPos = 0; | 
|  | 2085 | ClassTemplateSpecializationDecl *Decl | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2087 | InsertPos); | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2088 | if (!Decl) { | 
|  | 2089 | // This is the first time we have referenced this class template | 
|  | 2090 | // specialization. Create the canonical declaration and add it to | 
|  | 2091 | // the set of specializations. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | Decl = ClassTemplateSpecializationDecl::Create(Context, | 
| Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2093 | ClassTemplate->getTemplatedDecl()->getTagKind(), | 
|  | 2094 | ClassTemplate->getDeclContext(), | 
| Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2095 | ClassTemplate->getTemplatedDecl()->getLocStart(), | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2096 | ClassTemplate->getLocation(), | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2097 | ClassTemplate, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2098 | Converted.data(), | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2099 | Converted.size(), 0); | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2100 | ClassTemplate->AddSpecialization(Decl, InsertPos); | 
| Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2101 | if (ClassTemplate->isOutOfLine()) | 
|  | 2102 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2103 | } | 
|  | 2104 |  | 
|  | 2105 | CanonType = Context.getTypeDeclType(Decl); | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2106 | assert(isa<RecordType>(CanonType) && | 
|  | 2107 | "type of non-dependent specialization is not a RecordType"); | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2108 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 |  | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2110 | // Build the fully-sugared type for this class template | 
|  | 2111 | // specialization, which refers back to the class template | 
|  | 2112 | // specialization we created or found. | 
| John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2113 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2114 | } | 
|  | 2115 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2116 | TypeResult | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2117 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2118 | TemplateTy TemplateD, SourceLocation TemplateLoc, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | SourceLocation LAngleLoc, | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2120 | ASTTemplateArgsPtr TemplateArgsIn, | 
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2121 | SourceLocation RAngleLoc, | 
|  | 2122 | bool IsCtorOrDtorName) { | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2123 | if (SS.isInvalid()) | 
|  | 2124 | return true; | 
|  | 2125 |  | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2126 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); | 
| Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2127 |  | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2128 | // Translate the parser's template argument list in our AST format. | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2129 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | 
| Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2130 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2131 |  | 
| Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2132 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { | 
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2133 | QualType T | 
|  | 2134 | = Context.getDependentTemplateSpecializationType(ETK_None, | 
|  | 2135 | DTN->getQualifier(), | 
|  | 2136 | DTN->getIdentifier(), | 
|  | 2137 | TemplateArgs); | 
|  | 2138 | // Build type-source information. | 
| Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2139 | TypeLocBuilder TLB; | 
|  | 2140 | DependentTemplateSpecializationTypeLoc SpecTL | 
|  | 2141 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2142 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); | 
|  | 2143 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); | 
| Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2144 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2145 | SpecTL.setTemplateNameLoc(TemplateLoc); | 
| Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2146 | SpecTL.setLAngleLoc(LAngleLoc); | 
|  | 2147 | SpecTL.setRAngleLoc(RAngleLoc); | 
| Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2148 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) | 
|  | 2149 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | 
|  | 2150 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); | 
|  | 2151 | } | 
|  | 2152 |  | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2153 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); | 
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2154 |  | 
|  | 2155 | if (Result.isNull()) | 
|  | 2156 | return true; | 
|  | 2157 |  | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2158 | // Build type-source information. | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2159 | TypeLocBuilder TLB; | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2160 | TemplateSpecializationTypeLoc SpecTL | 
|  | 2161 | = TLB.push<TemplateSpecializationTypeLoc>(Result); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2162 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2163 | SpecTL.setTemplateNameLoc(TemplateLoc); | 
|  | 2164 | SpecTL.setLAngleLoc(LAngleLoc); | 
|  | 2165 | SpecTL.setRAngleLoc(RAngleLoc); | 
|  | 2166 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) | 
|  | 2167 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2168 |  | 
| Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2169 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a | 
|  | 2170 | // constructor or destructor name (in such a case, the scope specifier | 
|  | 2171 | // will be attached to the enclosing Decl or Expr node). | 
|  | 2172 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2173 | // Create an elaborated-type-specifier containing the nested-name-specifier. | 
|  | 2174 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); | 
|  | 2175 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); | 
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2176 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2177 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); | 
|  | 2178 | } | 
|  | 2179 |  | 
|  | 2180 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); | 
| John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2181 | } | 
| John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2182 |  | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2183 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2184 | TypeSpecifierType TagSpec, | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2185 | SourceLocation TagLoc, | 
|  | 2186 | CXXScopeSpec &SS, | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2187 | SourceLocation TemplateKWLoc, | 
|  | 2188 | TemplateTy TemplateD, | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2189 | SourceLocation TemplateLoc, | 
|  | 2190 | SourceLocation LAngleLoc, | 
|  | 2191 | ASTTemplateArgsPtr TemplateArgsIn, | 
|  | 2192 | SourceLocation RAngleLoc) { | 
|  | 2193 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); | 
|  | 2194 |  | 
|  | 2195 | // Translate the parser's template argument list in our AST format. | 
|  | 2196 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | 
|  | 2197 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | 
|  | 2198 |  | 
|  | 2199 | // Determine the tag kind | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2200 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2201 | ElaboratedTypeKeyword Keyword | 
|  | 2202 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 |  | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2204 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { | 
|  | 2205 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, | 
|  | 2206 | DTN->getQualifier(), | 
|  | 2207 | DTN->getIdentifier(), | 
|  | 2208 | TemplateArgs); | 
|  | 2209 |  | 
|  | 2210 | // Build type-source information. | 
|  | 2211 | TypeLocBuilder TLB; | 
|  | 2212 | DependentTemplateSpecializationTypeLoc SpecTL | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2213 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); | 
|  | 2214 | SpecTL.setElaboratedKeywordLoc(TagLoc); | 
|  | 2215 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); | 
| Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2216 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2217 | SpecTL.setTemplateNameLoc(TemplateLoc); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2218 | SpecTL.setLAngleLoc(LAngleLoc); | 
|  | 2219 | SpecTL.setRAngleLoc(RAngleLoc); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2220 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) | 
|  | 2221 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | 
|  | 2222 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); | 
|  | 2223 | } | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2224 |  | 
|  | 2225 | if (TypeAliasTemplateDecl *TAT = | 
|  | 2226 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { | 
|  | 2227 | // C++0x [dcl.type.elab]p2: | 
|  | 2228 | //   If the identifier resolves to a typedef-name or the simple-template-id | 
|  | 2229 | //   resolves to an alias template specialization, the | 
|  | 2230 | //   elaborated-type-specifier is ill-formed. | 
|  | 2231 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; | 
|  | 2232 | Diag(TAT->getLocation(), diag::note_declared_at); | 
|  | 2233 | } | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2234 |  | 
|  | 2235 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); | 
|  | 2236 | if (Result.isNull()) | 
| Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2237 | return TypeResult(true); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2238 |  | 
|  | 2239 | // Check the tag kind | 
|  | 2240 | if (const RecordType *RT = Result->getAs<RecordType>()) { | 
| John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2241 | RecordDecl *D = RT->getDecl(); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2242 |  | 
| John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2243 | IdentifierInfo *Id = D->getIdentifier(); | 
|  | 2244 | assert(Id && "templated class must have an identifier"); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2245 |  | 
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2246 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, | 
|  | 2247 | TagLoc, *Id)) { | 
| John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2248 | Diag(TagLoc, diag::err_use_with_wrong_tag) | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2249 | << Result | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2250 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); | 
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2251 | Diag(D->getLocation(), diag::note_previous_use); | 
| John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2252 | } | 
|  | 2253 | } | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2254 |  | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2255 | // Provide source-location information for the template specialization. | 
|  | 2256 | TypeLocBuilder TLB; | 
|  | 2257 | TemplateSpecializationTypeLoc SpecTL | 
|  | 2258 | = TLB.push<TemplateSpecializationTypeLoc>(Result); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2259 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2260 | SpecTL.setTemplateNameLoc(TemplateLoc); | 
|  | 2261 | SpecTL.setLAngleLoc(LAngleLoc); | 
|  | 2262 | SpecTL.setRAngleLoc(RAngleLoc); | 
|  | 2263 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) | 
|  | 2264 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); | 
| John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2265 |  | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2266 | // Construct an elaborated type containing the nested-name-specifier (if any) | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2267 | // and tag keyword. | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2268 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); | 
|  | 2269 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); | 
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2270 | ElabTL.setElaboratedKeywordLoc(TagLoc); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2271 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); | 
|  | 2272 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); | 
| Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2273 | } | 
|  | 2274 |  | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2275 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2276 | SourceLocation TemplateKWLoc, | 
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2277 | LookupResult &R, | 
|  | 2278 | bool RequiresADL, | 
| Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2279 | const TemplateArgumentListInfo *TemplateArgs) { | 
| Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2280 | // FIXME: Can we do any checking at this point? I guess we could check the | 
|  | 2281 | // template arguments that we have against the template name, if the template | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | // name refers to a single template. That's not a terribly common case, | 
| Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2283 | // though. | 
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2284 | // foo<int> could identify a single function unambiguously | 
|  | 2285 | // This approach does NOT work, since f<int>(1); | 
|  | 2286 | // gets resolved prior to resorting to overload resolution | 
|  | 2287 | // i.e., template<class T> void f(double); | 
|  | 2288 | //       vs template<class T, class U> void f(U); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2289 |  | 
|  | 2290 | // These should be filtered out by our callers. | 
|  | 2291 | assert(!R.empty() && "empty lookup results when building templateid"); | 
|  | 2292 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); | 
|  | 2293 |  | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2294 | // We don't want lookup warnings at this point. | 
|  | 2295 | R.suppressDiagnostics(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2296 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2297 | UnresolvedLookupExpr *ULE | 
| Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2298 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), | 
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2299 | SS.getWithLocInContext(Context), | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2300 | TemplateKWLoc, | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2301 | R.getLookupNameInfo(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2302 | RequiresADL, TemplateArgs, | 
| Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2303 | R.begin(), R.end()); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2304 |  | 
|  | 2305 | return Owned(ULE); | 
| Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2306 | } | 
|  | 2307 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2308 | // We actually only call this from template instantiation. | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2309 | ExprResult | 
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2310 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2311 | SourceLocation TemplateKWLoc, | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2312 | const DeclarationNameInfo &NameInfo, | 
| Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2313 | const TemplateArgumentListInfo *TemplateArgs) { | 
|  | 2314 | assert(TemplateArgs || TemplateKWLoc.isValid()); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2315 | DeclContext *DC; | 
|  | 2316 | if (!(DC = computeDeclContext(SS, false)) || | 
|  | 2317 | DC->isDependentContext() || | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2318 | RequireCompleteDeclContext(SS, DC)) | 
| Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2319 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 |  | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2321 | bool MemberOfUnknownSpecialization; | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2322 | LookupResult R(*this, NameInfo, LookupOrdinaryName); | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2323 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, | 
|  | 2324 | MemberOfUnknownSpecialization); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2326 | if (R.isAmbiguous()) | 
|  | 2327 | return ExprError(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2328 |  | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2329 | if (R.empty()) { | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2330 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) | 
|  | 2331 | << NameInfo.getName() << SS.getRange(); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2332 | return ExprError(); | 
|  | 2333 | } | 
|  | 2334 |  | 
|  | 2335 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2336 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) | 
|  | 2337 | << (NestedNameSpecifier*) SS.getScopeRep() | 
|  | 2338 | << NameInfo.getName() << SS.getRange(); | 
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2339 | Diag(Temp->getLocation(), diag::note_referenced_class_template); | 
|  | 2340 | return ExprError(); | 
|  | 2341 | } | 
|  | 2342 |  | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2343 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); | 
| Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2344 | } | 
|  | 2345 |  | 
| Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2346 | /// \brief Form a dependent template name. | 
|  | 2347 | /// | 
|  | 2348 | /// This action forms a dependent template name given the template | 
|  | 2349 | /// name and its (presumably dependent) scope specifier. For | 
|  | 2350 | /// example, given "MetaFun::template apply", the scope specifier \p | 
|  | 2351 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location | 
|  | 2352 | /// of the "template" keyword, and "apply" is the \p Name. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2353 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2354 | CXXScopeSpec &SS, | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2355 | SourceLocation TemplateKWLoc, | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2356 | UnqualifiedId &Name, | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2357 | ParsedType ObjectType, | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2358 | bool EnteringContext, | 
|  | 2359 | TemplateTy &Result) { | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2360 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) | 
|  | 2361 | Diag(TemplateKWLoc, | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2362 | getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2363 | diag::warn_cxx98_compat_template_outside_of_template : | 
|  | 2364 | diag::ext_template_outside_of_template) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2365 | << FixItHint::CreateRemoval(TemplateKWLoc); | 
|  | 2366 |  | 
| Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2367 | DeclContext *LookupCtx = 0; | 
|  | 2368 | if (SS.isSet()) | 
|  | 2369 | LookupCtx = computeDeclContext(SS, EnteringContext); | 
|  | 2370 | if (!LookupCtx && ObjectType) | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2371 | LookupCtx = computeDeclContext(ObjectType.get()); | 
| Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2372 | if (LookupCtx) { | 
| Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2373 | // C++0x [temp.names]p5: | 
|  | 2374 | //   If a name prefixed by the keyword template is not the name of | 
|  | 2375 | //   a template, the program is ill-formed. [Note: the keyword | 
|  | 2376 | //   template may not be applied to non-template members of class | 
|  | 2377 | //   templates. -end note ] [ Note: as is the case with the | 
|  | 2378 | //   typename prefix, the template prefix is allowed in cases | 
|  | 2379 | //   where it is not strictly necessary; i.e., when the | 
|  | 2380 | //   nested-name-specifier or the expression on the left of the -> | 
|  | 2381 | //   or . is not dependent on a template-parameter, or the use | 
|  | 2382 | //   does not appear in the scope of a template. -end note] | 
|  | 2383 | // | 
|  | 2384 | // Note: C++03 was more strict here, because it banned the use of | 
|  | 2385 | // the "template" keyword prior to a template-name that was not a | 
|  | 2386 | // dependent name. C++ DR468 relaxed this requirement (the | 
|  | 2387 | // "template" keyword is now permitted). We follow the C++0x | 
| Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2388 | // rules, even in C++03 mode with a warning, retroactively applying the DR. | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2389 | bool MemberOfUnknownSpecialization; | 
| Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 2390 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, | 
| Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2391 | ObjectType, EnteringContext, Result, | 
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2392 | MemberOfUnknownSpecialization); | 
| Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2393 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && | 
|  | 2394 | isa<CXXRecordDecl>(LookupCtx) && | 
| Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2395 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || | 
|  | 2396 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2397 | // This is a dependent template. Handle it below. | 
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2398 | } else if (TNK == TNK_Non_template) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2399 | Diag(Name.getLocStart(), | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2400 | diag::err_template_kw_refers_to_non_template) | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2401 | << GetNameFromUnqualifiedId(Name).getName() | 
| Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2402 | << Name.getSourceRange() | 
|  | 2403 | << TemplateKWLoc; | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2404 | return TNK_Non_template; | 
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2405 | } else { | 
|  | 2406 | // We found something; return it. | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2407 | return TNK; | 
| Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2408 | } | 
| Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2409 | } | 
|  | 2410 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2411 | NestedNameSpecifier *Qualifier | 
| Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2412 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2413 |  | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2414 | switch (Name.getKind()) { | 
|  | 2415 | case UnqualifiedId::IK_Identifier: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2416 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2417 | Name.Identifier)); | 
|  | 2418 | return TNK_Dependent_template_name; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2419 |  | 
| Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2420 | case UnqualifiedId::IK_OperatorFunctionId: | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2421 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, | 
| Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2422 | Name.OperatorFunctionId.Operator)); | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2423 | return TNK_Dependent_template_name; | 
| Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2424 |  | 
|  | 2425 | case UnqualifiedId::IK_LiteralOperatorId: | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2426 | llvm_unreachable( | 
|  | 2427 | "We don't support these; Parse shouldn't have allowed propagation"); | 
| Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2428 |  | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2429 | default: | 
|  | 2430 | break; | 
|  | 2431 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2432 |  | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2433 | Diag(Name.getLocStart(), | 
| Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2434 | diag::err_template_kw_refers_to_non_template) | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2435 | << GetNameFromUnqualifiedId(Name).getName() | 
| Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2436 | << Name.getSourceRange() | 
|  | 2437 | << TemplateKWLoc; | 
| Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2438 | return TNK_Non_template; | 
| Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2439 | } | 
|  | 2440 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2442 | const TemplateArgumentLoc &AL, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2443 | SmallVectorImpl<TemplateArgument> &Converted) { | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2444 | const TemplateArgument &Arg = AL.getArgument(); | 
|  | 2445 |  | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2446 | // Check template type parameter. | 
| Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2447 | switch(Arg.getKind()) { | 
|  | 2448 | case TemplateArgument::Type: | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2449 | // C++ [temp.arg.type]p1: | 
|  | 2450 | //   A template-argument for a template-parameter which is a | 
|  | 2451 | //   type shall be a type-id. | 
| Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2452 | break; | 
|  | 2453 | case TemplateArgument::Template: { | 
|  | 2454 | // We have a template type parameter but the template argument | 
|  | 2455 | // is a template without any arguments. | 
|  | 2456 | SourceRange SR = AL.getSourceRange(); | 
|  | 2457 | TemplateName Name = Arg.getAsTemplate(); | 
|  | 2458 | Diag(SR.getBegin(), diag::err_template_missing_args) | 
|  | 2459 | << Name << SR; | 
|  | 2460 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) | 
|  | 2461 | Diag(Decl->getLocation(), diag::note_template_decl_here); | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2462 |  | 
| Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2463 | return true; | 
|  | 2464 | } | 
| Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 2465 | case TemplateArgument::Expression: { | 
|  | 2466 | // We have a template type parameter but the template argument is an | 
|  | 2467 | // expression; see if maybe it is missing the "typename" keyword. | 
|  | 2468 | CXXScopeSpec SS; | 
|  | 2469 | DeclarationNameInfo NameInfo; | 
|  | 2470 |  | 
|  | 2471 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { | 
|  | 2472 | SS.Adopt(ArgExpr->getQualifierLoc()); | 
|  | 2473 | NameInfo = ArgExpr->getNameInfo(); | 
|  | 2474 | } else if (DependentScopeDeclRefExpr *ArgExpr = | 
|  | 2475 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { | 
|  | 2476 | SS.Adopt(ArgExpr->getQualifierLoc()); | 
|  | 2477 | NameInfo = ArgExpr->getNameInfo(); | 
|  | 2478 | } else if (CXXDependentScopeMemberExpr *ArgExpr = | 
|  | 2479 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { | 
| Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 2480 | if (ArgExpr->isImplicitAccess()) { | 
|  | 2481 | SS.Adopt(ArgExpr->getQualifierLoc()); | 
|  | 2482 | NameInfo = ArgExpr->getMemberNameInfo(); | 
|  | 2483 | } | 
| Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 2484 | } | 
|  | 2485 |  | 
| Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 2486 | if (NameInfo.getName().isIdentifier()) { | 
| Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 2487 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); | 
|  | 2488 | LookupParsedName(Result, CurScope, &SS); | 
|  | 2489 |  | 
| Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 2490 | if (Result.getAsSingle<TypeDecl>() || | 
|  | 2491 | Result.getResultKind() == | 
|  | 2492 | LookupResult::NotFoundInCurrentInstantiation) { | 
|  | 2493 | // FIXME: Add a FixIt and fix up the template argument for recovery. | 
| Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 2494 | SourceLocation Loc = AL.getSourceRange().getBegin(); | 
|  | 2495 | Diag(Loc, diag::err_template_arg_must_be_type_suggest); | 
|  | 2496 | Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 2497 | return true; | 
|  | 2498 | } | 
|  | 2499 | } | 
|  | 2500 | // fallthrough | 
|  | 2501 | } | 
| Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2502 | default: { | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2503 | // We have a template type parameter but the template argument | 
|  | 2504 | // is not a type. | 
| John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2505 | SourceRange SR = AL.getSourceRange(); | 
|  | 2506 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2507 | Diag(Param->getLocation(), diag::note_template_param_here); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2508 |  | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2509 | return true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | } | 
| Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2511 | } | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2512 |  | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2513 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2514 | return true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 |  | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2516 | // Add the converted template type argument. | 
| Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2517 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); | 
|  | 2518 |  | 
|  | 2519 | // Objective-C ARC: | 
|  | 2520 | //   If an explicitly-specified template argument type is a lifetime type | 
|  | 2521 | //   with no lifetime qualifier, the __strong lifetime qualifier is inferred. | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2522 | if (getLangOpts().ObjCAutoRefCount && | 
| Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2523 | ArgType->isObjCLifetimeType() && | 
|  | 2524 | !ArgType.getObjCLifetime()) { | 
|  | 2525 | Qualifiers Qs; | 
|  | 2526 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); | 
|  | 2527 | ArgType = Context.getQualifiedType(ArgType, Qs); | 
|  | 2528 | } | 
|  | 2529 |  | 
|  | 2530 | Converted.push_back(TemplateArgument(ArgType)); | 
| Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2531 | return false; | 
|  | 2532 | } | 
|  | 2533 |  | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2534 | /// \brief Substitute template arguments into the default template argument for | 
|  | 2535 | /// the given template type parameter. | 
|  | 2536 | /// | 
|  | 2537 | /// \param SemaRef the semantic analysis object for which we are performing | 
|  | 2538 | /// the substitution. | 
|  | 2539 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2540 | /// \param Template the template that we are synthesizing template arguments | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2541 | /// for. | 
|  | 2542 | /// | 
|  | 2543 | /// \param TemplateLoc the location of the template name that started the | 
|  | 2544 | /// template-id we are checking. | 
|  | 2545 | /// | 
|  | 2546 | /// \param RAngleLoc the location of the right angle bracket ('>') that | 
|  | 2547 | /// terminates the template-id. | 
|  | 2548 | /// | 
|  | 2549 | /// \param Param the template template parameter whose default we are | 
|  | 2550 | /// substituting into. | 
|  | 2551 | /// | 
|  | 2552 | /// \param Converted the list of template arguments provided for template | 
|  | 2553 | /// parameters that precede \p Param in the template parameter list. | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2554 | /// \returns the substituted template argument, or NULL if an error occurred. | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2555 | static TypeSourceInfo * | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2556 | SubstDefaultTemplateArgument(Sema &SemaRef, | 
|  | 2557 | TemplateDecl *Template, | 
|  | 2558 | SourceLocation TemplateLoc, | 
|  | 2559 | SourceLocation RAngleLoc, | 
|  | 2560 | TemplateTypeParmDecl *Param, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2561 | SmallVectorImpl<TemplateArgument> &Converted) { | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2562 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2563 |  | 
|  | 2564 | // If the argument type is dependent, instantiate it now based | 
|  | 2565 | // on the previously-computed template arguments. | 
|  | 2566 | if (ArgType->getType()->isDependentType()) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2567 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2568 | Converted.data(), Converted.size()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2569 |  | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2570 | MultiLevelTemplateArgumentList AllTemplateArgs | 
|  | 2571 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); | 
|  | 2572 |  | 
|  | 2573 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, | 
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2574 | Template, Converted, | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2575 | SourceRange(TemplateLoc, RAngleLoc)); | 
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2576 | if (Inst) | 
|  | 2577 | return 0; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2578 |  | 
| Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 2579 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2580 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, | 
|  | 2581 | Param->getDefaultArgumentLoc(), | 
|  | 2582 | Param->getDeclName()); | 
|  | 2583 | } | 
|  | 2584 |  | 
|  | 2585 | return ArgType; | 
|  | 2586 | } | 
|  | 2587 |  | 
|  | 2588 | /// \brief Substitute template arguments into the default template argument for | 
|  | 2589 | /// the given non-type template parameter. | 
|  | 2590 | /// | 
|  | 2591 | /// \param SemaRef the semantic analysis object for which we are performing | 
|  | 2592 | /// the substitution. | 
|  | 2593 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2594 | /// \param Template the template that we are synthesizing template arguments | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2595 | /// for. | 
|  | 2596 | /// | 
|  | 2597 | /// \param TemplateLoc the location of the template name that started the | 
|  | 2598 | /// template-id we are checking. | 
|  | 2599 | /// | 
|  | 2600 | /// \param RAngleLoc the location of the right angle bracket ('>') that | 
|  | 2601 | /// terminates the template-id. | 
|  | 2602 | /// | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2603 | /// \param Param the non-type template parameter whose default we are | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2604 | /// substituting into. | 
|  | 2605 | /// | 
|  | 2606 | /// \param Converted the list of template arguments provided for template | 
|  | 2607 | /// parameters that precede \p Param in the template parameter list. | 
|  | 2608 | /// | 
|  | 2609 | /// \returns the substituted template argument, or NULL if an error occurred. | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2610 | static ExprResult | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2611 | SubstDefaultTemplateArgument(Sema &SemaRef, | 
|  | 2612 | TemplateDecl *Template, | 
|  | 2613 | SourceLocation TemplateLoc, | 
|  | 2614 | SourceLocation RAngleLoc, | 
|  | 2615 | NonTypeTemplateParmDecl *Param, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2616 | SmallVectorImpl<TemplateArgument> &Converted) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2617 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2618 | Converted.data(), Converted.size()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2619 |  | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2620 | MultiLevelTemplateArgumentList AllTemplateArgs | 
|  | 2621 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2622 |  | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2623 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, | 
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2624 | Template, Converted, | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2625 | SourceRange(TemplateLoc, RAngleLoc)); | 
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2626 | if (Inst) | 
|  | 2627 | return ExprError(); | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2628 |  | 
| Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 2629 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); | 
| Eli Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 2630 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); | 
| Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2631 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); | 
|  | 2632 | } | 
|  | 2633 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2634 | /// \brief Substitute template arguments into the default template argument for | 
|  | 2635 | /// the given template template parameter. | 
|  | 2636 | /// | 
|  | 2637 | /// \param SemaRef the semantic analysis object for which we are performing | 
|  | 2638 | /// the substitution. | 
|  | 2639 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2640 | /// \param Template the template that we are synthesizing template arguments | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2641 | /// for. | 
|  | 2642 | /// | 
|  | 2643 | /// \param TemplateLoc the location of the template name that started the | 
|  | 2644 | /// template-id we are checking. | 
|  | 2645 | /// | 
|  | 2646 | /// \param RAngleLoc the location of the right angle bracket ('>') that | 
|  | 2647 | /// terminates the template-id. | 
|  | 2648 | /// | 
|  | 2649 | /// \param Param the template template parameter whose default we are | 
|  | 2650 | /// substituting into. | 
|  | 2651 | /// | 
|  | 2652 | /// \param Converted the list of template arguments provided for template | 
|  | 2653 | /// parameters that precede \p Param in the template parameter list. | 
|  | 2654 | /// | 
| Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2655 | /// \param QualifierLoc Will be set to the nested-name-specifier (with | 
|  | 2656 | /// source-location information) that precedes the template name. | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2657 | /// | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2658 | /// \returns the substituted template argument, or NULL if an error occurred. | 
|  | 2659 | static TemplateName | 
|  | 2660 | SubstDefaultTemplateArgument(Sema &SemaRef, | 
|  | 2661 | TemplateDecl *Template, | 
|  | 2662 | SourceLocation TemplateLoc, | 
|  | 2663 | SourceLocation RAngleLoc, | 
|  | 2664 | TemplateTemplateParmDecl *Param, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2665 | SmallVectorImpl<TemplateArgument> &Converted, | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2666 | NestedNameSpecifierLoc &QualifierLoc) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2667 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2668 | Converted.data(), Converted.size()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2669 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2670 | MultiLevelTemplateArgumentList AllTemplateArgs | 
|  | 2671 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2672 |  | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2673 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, | 
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2674 | Template, Converted, | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2675 | SourceRange(TemplateLoc, RAngleLoc)); | 
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2676 | if (Inst) | 
|  | 2677 | return TemplateName(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2678 |  | 
| Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 2679 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2680 | // Substitute into the nested-name-specifier first, | 
| Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2681 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2682 | if (QualifierLoc) { | 
|  | 2683 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, | 
|  | 2684 | AllTemplateArgs); | 
|  | 2685 | if (!QualifierLoc) | 
|  | 2686 | return TemplateName(); | 
|  | 2687 | } | 
|  | 2688 |  | 
| Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2689 | return SemaRef.SubstTemplateName(QualifierLoc, | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2690 | Param->getDefaultArgument().getArgument().getAsTemplate(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2691 | Param->getDefaultArgument().getTemplateNameLoc(), | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2692 | AllTemplateArgs); | 
|  | 2693 | } | 
|  | 2694 |  | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2695 | /// \brief If the given template parameter has a default template | 
|  | 2696 | /// argument, substitute into that default template argument and | 
|  | 2697 | /// return the corresponding template argument. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2698 | TemplateArgumentLoc | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2699 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, | 
|  | 2700 | SourceLocation TemplateLoc, | 
|  | 2701 | SourceLocation RAngleLoc, | 
|  | 2702 | Decl *Param, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2703 | SmallVectorImpl<TemplateArgument> &Converted) { | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2704 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2705 | if (!TypeParm->hasDefaultArgument()) | 
|  | 2706 | return TemplateArgumentLoc(); | 
|  | 2707 |  | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2708 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2709 | TemplateLoc, | 
|  | 2710 | RAngleLoc, | 
|  | 2711 | TypeParm, | 
|  | 2712 | Converted); | 
|  | 2713 | if (DI) | 
|  | 2714 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); | 
|  | 2715 |  | 
|  | 2716 | return TemplateArgumentLoc(); | 
|  | 2717 | } | 
|  | 2718 |  | 
|  | 2719 | if (NonTypeTemplateParmDecl *NonTypeParm | 
|  | 2720 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { | 
|  | 2721 | if (!NonTypeParm->hasDefaultArgument()) | 
|  | 2722 | return TemplateArgumentLoc(); | 
|  | 2723 |  | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2724 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2725 | TemplateLoc, | 
|  | 2726 | RAngleLoc, | 
|  | 2727 | NonTypeParm, | 
|  | 2728 | Converted); | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2729 | if (Arg.isInvalid()) | 
|  | 2730 | return TemplateArgumentLoc(); | 
|  | 2731 |  | 
|  | 2732 | Expr *ArgE = Arg.takeAs<Expr>(); | 
|  | 2733 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); | 
|  | 2734 | } | 
|  | 2735 |  | 
|  | 2736 | TemplateTemplateParmDecl *TempTempParm | 
|  | 2737 | = cast<TemplateTemplateParmDecl>(Param); | 
|  | 2738 | if (!TempTempParm->hasDefaultArgument()) | 
|  | 2739 | return TemplateArgumentLoc(); | 
|  | 2740 |  | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2741 |  | 
| Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2742 | NestedNameSpecifierLoc QualifierLoc; | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2743 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2744 | TemplateLoc, | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2745 | RAngleLoc, | 
|  | 2746 | TempTempParm, | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2747 | Converted, | 
|  | 2748 | QualifierLoc); | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2749 | if (TName.isNull()) | 
|  | 2750 | return TemplateArgumentLoc(); | 
|  | 2751 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2752 | return TemplateArgumentLoc(TemplateArgument(TName), | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2753 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), | 
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2754 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); | 
|  | 2755 | } | 
|  | 2756 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2757 | /// \brief Check that the given template argument corresponds to the given | 
|  | 2758 | /// template parameter. | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2759 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2760 | /// \param Param The template parameter against which the argument will be | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2761 | /// checked. | 
|  | 2762 | /// | 
|  | 2763 | /// \param Arg The template argument. | 
|  | 2764 | /// | 
|  | 2765 | /// \param Template The template in which the template argument resides. | 
|  | 2766 | /// | 
|  | 2767 | /// \param TemplateLoc The location of the template name for the template | 
|  | 2768 | /// whose argument list we're matching. | 
|  | 2769 | /// | 
|  | 2770 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes | 
|  | 2771 | /// the template argument list. | 
|  | 2772 | /// | 
|  | 2773 | /// \param ArgumentPackIndex The index into the argument pack where this | 
|  | 2774 | /// argument will be placed. Only valid if the parameter is a parameter pack. | 
|  | 2775 | /// | 
|  | 2776 | /// \param Converted The checked, converted argument will be added to the | 
|  | 2777 | /// end of this small vector. | 
|  | 2778 | /// | 
|  | 2779 | /// \param CTAK Describes how we arrived at this particular template argument: | 
|  | 2780 | /// explicitly written, deduced, etc. | 
|  | 2781 | /// | 
|  | 2782 | /// \returns true on error, false otherwise. | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2783 | bool Sema::CheckTemplateArgument(NamedDecl *Param, | 
|  | 2784 | const TemplateArgumentLoc &Arg, | 
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2785 | NamedDecl *Template, | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2786 | SourceLocation TemplateLoc, | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2787 | SourceLocation RAngleLoc, | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2788 | unsigned ArgumentPackIndex, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2789 | SmallVectorImpl<TemplateArgument> &Converted, | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2790 | CheckTemplateArgumentKind CTAK) { | 
| Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2791 | // Check template type parameters. | 
|  | 2792 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2793 | return CheckTemplateTypeArgument(TTP, Arg, Converted); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 |  | 
| Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2795 | // Check non-type template parameters. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2796 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2797 | // Do substitution on the type of the non-type template parameter | 
| Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2798 | // with the template arguments we've seen thus far.  But if the | 
|  | 2799 | // template has a dependent context then we cannot substitute yet. | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2800 | QualType NTTPType = NTTP->getType(); | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2801 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) | 
|  | 2802 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2803 |  | 
| Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2804 | if (NTTPType->isDependentType() && | 
|  | 2805 | !isa<TemplateTemplateParmDecl>(Template) && | 
|  | 2806 | !Template->getDeclContext()->isDependentContext()) { | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2807 | // Do substitution on the type of the non-type template parameter. | 
|  | 2808 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, | 
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2809 | NTTP, Converted, | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2810 | SourceRange(TemplateLoc, RAngleLoc)); | 
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2811 | if (Inst) | 
|  | 2812 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 |  | 
|  | 2814 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2815 | Converted.data(), Converted.size()); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2816 | NTTPType = SubstType(NTTPType, | 
|  | 2817 | MultiLevelTemplateArgumentList(TemplateArgs), | 
|  | 2818 | NTTP->getLocation(), | 
|  | 2819 | NTTP->getDeclName()); | 
|  | 2820 | // If that worked, check the non-type template parameter type | 
|  | 2821 | // for validity. | 
|  | 2822 | if (!NTTPType.isNull()) | 
|  | 2823 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, | 
|  | 2824 | NTTP->getLocation()); | 
|  | 2825 | if (NTTPType.isNull()) | 
|  | 2826 | return true; | 
|  | 2827 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2828 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2829 | switch (Arg.getArgument().getKind()) { | 
|  | 2830 | case TemplateArgument::Null: | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2831 | llvm_unreachable("Should never see a NULL template argument here"); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2832 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2833 | case TemplateArgument::Expression: { | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2834 | TemplateArgument Result; | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2835 | ExprResult Res = | 
|  | 2836 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), | 
|  | 2837 | Result, CTAK); | 
|  | 2838 | if (Res.isInvalid()) | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2839 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2840 |  | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2841 | Converted.push_back(Result); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2842 | break; | 
|  | 2843 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2844 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2845 | case TemplateArgument::Declaration: | 
|  | 2846 | case TemplateArgument::Integral: | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2847 | case TemplateArgument::NullPtr: | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2848 | // We've already checked this template argument, so just copy | 
|  | 2849 | // it to the list of converted arguments. | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2850 | Converted.push_back(Arg.getArgument()); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2851 | break; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2852 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2853 | case TemplateArgument::Template: | 
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2854 | case TemplateArgument::TemplateExpansion: | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2855 | // We were given a template template argument. It may not be ill-formed; | 
|  | 2856 | // see below. | 
|  | 2857 | if (DependentTemplateName *DTN | 
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2858 | = Arg.getArgument().getAsTemplateOrTemplatePattern() | 
|  | 2859 | .getAsDependentTemplateName()) { | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2860 | // We have a template argument such as \c T::template X, which we | 
|  | 2861 | // parsed as a template template argument. However, since we now | 
|  | 2862 | // know that we need a non-type template argument, convert this | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2863 | // template name into an expression. | 
|  | 2864 |  | 
|  | 2865 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), | 
|  | 2866 | Arg.getTemplateNameLoc()); | 
|  | 2867 |  | 
| Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2868 | CXXScopeSpec SS; | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2869 | SS.Adopt(Arg.getTemplateQualifierLoc()); | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2870 | // FIXME: the template-template arg was a DependentTemplateName, | 
|  | 2871 | // so it was provided with a template keyword. However, its source | 
|  | 2872 | // location is not stored in the template argument structure. | 
|  | 2873 | SourceLocation TemplateKWLoc; | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2874 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, | 
| Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2875 | SS.getWithLocInContext(Context), | 
| Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2876 | TemplateKWLoc, | 
|  | 2877 | NameInfo, 0)); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2878 |  | 
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2879 | // If we parsed the template argument as a pack expansion, create a | 
|  | 2880 | // pack expansion expression. | 
|  | 2881 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2882 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); | 
|  | 2883 | if (E.isInvalid()) | 
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2884 | return true; | 
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2885 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2886 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2887 | TemplateArgument Result; | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2888 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); | 
|  | 2889 | if (E.isInvalid()) | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2890 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2891 |  | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2892 | Converted.push_back(Result); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2893 | break; | 
|  | 2894 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2895 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2896 | // We have a template argument that actually does refer to a class | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2897 | // template, alias template, or template template parameter, and | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2898 | // therefore cannot be a non-type template argument. | 
|  | 2899 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) | 
|  | 2900 | << Arg.getSourceRange(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2901 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2902 | Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 2903 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2904 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2905 | case TemplateArgument::Type: { | 
|  | 2906 | // We have a non-type template parameter but the template | 
|  | 2907 | // argument is a type. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2908 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2909 | // C++ [temp.arg]p2: | 
|  | 2910 | //   In a template-argument, an ambiguity between a type-id and | 
|  | 2911 | //   an expression is resolved to a type-id, regardless of the | 
|  | 2912 | //   form of the corresponding template-parameter. | 
|  | 2913 | // | 
|  | 2914 | // We warn specifically about this case, since it can be rather | 
|  | 2915 | // confusing for users. | 
|  | 2916 | QualType T = Arg.getArgument().getAsType(); | 
|  | 2917 | SourceRange SR = Arg.getSourceRange(); | 
|  | 2918 | if (T->isFunctionType()) | 
|  | 2919 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; | 
|  | 2920 | else | 
|  | 2921 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; | 
|  | 2922 | Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 2923 | return true; | 
|  | 2924 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2925 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2926 | case TemplateArgument::Pack: | 
| Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2927 | llvm_unreachable("Caller must expand template argument packs"); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2928 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2930 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2931 | } | 
|  | 2932 |  | 
|  | 2933 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2934 | // Check template template parameters. | 
|  | 2935 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2936 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2937 | // Substitute into the template parameter list of the template | 
|  | 2938 | // template parameter, since previously-supplied template arguments | 
|  | 2939 | // may appear within the template template parameter. | 
|  | 2940 | { | 
|  | 2941 | // Set up a template instantiation context. | 
|  | 2942 | LocalInstantiationScope Scope(*this); | 
|  | 2943 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, | 
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2944 | TempParm, Converted, | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2945 | SourceRange(TemplateLoc, RAngleLoc)); | 
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2946 | if (Inst) | 
|  | 2947 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2948 |  | 
|  | 2949 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2950 | Converted.data(), Converted.size()); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2951 | TempParm = cast_or_null<TemplateTemplateParmDecl>( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2952 | SubstDecl(TempParm, CurContext, | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2953 | MultiLevelTemplateArgumentList(TemplateArgs))); | 
|  | 2954 | if (!TempParm) | 
|  | 2955 | return true; | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2956 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2957 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2958 | switch (Arg.getArgument().getKind()) { | 
|  | 2959 | case TemplateArgument::Null: | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2960 | llvm_unreachable("Should never see a NULL template argument here"); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2961 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2962 | case TemplateArgument::Template: | 
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2963 | case TemplateArgument::TemplateExpansion: | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2964 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2965 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2966 |  | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2967 | Converted.push_back(Arg.getArgument()); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2968 | break; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2969 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2970 | case TemplateArgument::Expression: | 
|  | 2971 | case TemplateArgument::Type: | 
|  | 2972 | // We have a template template parameter but the template | 
|  | 2973 | // argument does not refer to a template. | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2974 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2975 | << getLangOpts().CPlusPlus11; | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2976 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2978 | case TemplateArgument::Declaration: | 
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2979 | llvm_unreachable("Declaration argument with template template parameter"); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2980 | case TemplateArgument::Integral: | 
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2981 | llvm_unreachable("Integral argument with template template parameter"); | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2982 | case TemplateArgument::NullPtr: | 
|  | 2983 | llvm_unreachable("Null pointer argument with template template parameter"); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2984 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2985 | case TemplateArgument::Pack: | 
| Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2986 | llvm_unreachable("Caller must expand template argument packs"); | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2987 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2988 |  | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2989 | return false; | 
|  | 2990 | } | 
|  | 2991 |  | 
| Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2992 | /// \brief Diagnose an arity mismatch in the | 
|  | 2993 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, | 
|  | 2994 | SourceLocation TemplateLoc, | 
|  | 2995 | TemplateArgumentListInfo &TemplateArgs) { | 
|  | 2996 | TemplateParameterList *Params = Template->getTemplateParameters(); | 
|  | 2997 | unsigned NumParams = Params->size(); | 
|  | 2998 | unsigned NumArgs = TemplateArgs.size(); | 
|  | 2999 |  | 
|  | 3000 | SourceRange Range; | 
|  | 3001 | if (NumArgs > NumParams) | 
|  | 3002 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), | 
|  | 3003 | TemplateArgs.getRAngleLoc()); | 
|  | 3004 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) | 
|  | 3005 | << (NumArgs > NumParams) | 
|  | 3006 | << (isa<ClassTemplateDecl>(Template)? 0 : | 
|  | 3007 | isa<FunctionTemplateDecl>(Template)? 1 : | 
|  | 3008 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) | 
|  | 3009 | << Template << Range; | 
|  | 3010 | S.Diag(Template->getLocation(), diag::note_template_decl_here) | 
|  | 3011 | << Params->getSourceRange(); | 
|  | 3012 | return true; | 
|  | 3013 | } | 
|  | 3014 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3015 | /// \brief Check whether the template parameter is a pack expansion, and if so, | 
|  | 3016 | /// determine the number of parameters produced by that expansion. For instance: | 
|  | 3017 | /// | 
|  | 3018 | /// \code | 
|  | 3019 | /// template<typename ...Ts> struct A { | 
|  | 3020 | ///   template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; | 
|  | 3021 | /// }; | 
|  | 3022 | /// \endcode | 
|  | 3023 | /// | 
|  | 3024 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us | 
|  | 3025 | /// is not a pack expansion, so returns an empty Optional. | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3026 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3027 | if (NonTypeTemplateParmDecl *NTTP | 
|  | 3028 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { | 
|  | 3029 | if (NTTP->isExpandedParameterPack()) | 
|  | 3030 | return NTTP->getNumExpansionTypes(); | 
|  | 3031 | } | 
|  | 3032 |  | 
|  | 3033 | if (TemplateTemplateParmDecl *TTP | 
|  | 3034 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { | 
|  | 3035 | if (TTP->isExpandedParameterPack()) | 
|  | 3036 | return TTP->getNumExpansionTemplateParameters(); | 
|  | 3037 | } | 
|  | 3038 |  | 
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3039 | return None; | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3040 | } | 
|  | 3041 |  | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3042 | /// \brief Check that the given template argument list is well-formed | 
|  | 3043 | /// for specializing the given template. | 
|  | 3044 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, | 
|  | 3045 | SourceLocation TemplateLoc, | 
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3046 | TemplateArgumentListInfo &TemplateArgs, | 
| Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3047 | bool PartialTemplateArgs, | 
| Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3048 | SmallVectorImpl<TemplateArgument> &Converted, | 
|  | 3049 | bool *ExpansionIntoFixedList) { | 
|  | 3050 | if (ExpansionIntoFixedList) | 
|  | 3051 | *ExpansionIntoFixedList = false; | 
|  | 3052 |  | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3053 | TemplateParameterList *Params = Template->getTemplateParameters(); | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3054 |  | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3055 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); | 
|  | 3056 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3057 | // C++ [temp.arg]p1: | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3058 | //   [...] The type and form of each template-argument specified in | 
|  | 3059 | //   a template-id shall match the type and form specified for the | 
|  | 3060 | //   corresponding parameter declared by the template in its | 
|  | 3061 | //   template-parameter-list. | 
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3062 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3063 | SmallVector<TemplateArgument, 2> ArgumentPack; | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3064 | unsigned ArgIdx = 0, NumArgs = TemplateArgs.size(); | 
| Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3065 | LocalInstantiationScope InstScope(*this, true); | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3066 | for (TemplateParameterList::iterator Param = Params->begin(), | 
|  | 3067 | ParamEnd = Params->end(); | 
|  | 3068 | Param != ParamEnd; /* increment in loop */) { | 
|  | 3069 | // If we have an expanded parameter pack, make sure we don't have too | 
|  | 3070 | // many arguments. | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3071 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3072 | if (*Expansions == ArgumentPack.size()) { | 
|  | 3073 | // We're done with this parameter pack. Pack up its arguments and add | 
|  | 3074 | // them to the list. | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3075 | Converted.push_back( | 
|  | 3076 | TemplateArgument::CreatePackCopy(Context, | 
|  | 3077 | ArgumentPack.data(), | 
|  | 3078 | ArgumentPack.size())); | 
|  | 3079 | ArgumentPack.clear(); | 
|  | 3080 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3081 | // This argument is assigned to the next parameter. | 
|  | 3082 | ++Param; | 
|  | 3083 | continue; | 
|  | 3084 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { | 
|  | 3085 | // Not enough arguments for this parameter pack. | 
|  | 3086 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) | 
|  | 3087 | << false | 
|  | 3088 | << (isa<ClassTemplateDecl>(Template)? 0 : | 
|  | 3089 | isa<FunctionTemplateDecl>(Template)? 1 : | 
|  | 3090 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) | 
|  | 3091 | << Template; | 
|  | 3092 | Diag(Template->getLocation(), diag::note_template_decl_here) | 
|  | 3093 | << Params->getSourceRange(); | 
|  | 3094 | return true; | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3095 | } | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3096 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3097 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3098 | if (ArgIdx < NumArgs) { | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3099 | // Check the template argument we were given. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3100 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, | 
|  | 3101 | TemplateLoc, RAngleLoc, | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3102 | ArgumentPack.size(), Converted)) | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3103 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3104 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3105 | // We're now done with this argument. | 
|  | 3106 | ++ArgIdx; | 
|  | 3107 |  | 
| Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3108 | if ((*Param)->isTemplateParameterPack()) { | 
|  | 3109 | // The template parameter was a template parameter pack, so take the | 
|  | 3110 | // deduced argument and place it on the argument pack. Note that we | 
|  | 3111 | // stay on the same template parameter so that we can deduce more | 
|  | 3112 | // arguments. | 
|  | 3113 | ArgumentPack.push_back(Converted.back()); | 
|  | 3114 | Converted.pop_back(); | 
|  | 3115 | } else { | 
|  | 3116 | // Move to the next template parameter. | 
|  | 3117 | ++Param; | 
|  | 3118 | } | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3119 |  | 
|  | 3120 | // If we just saw a pack expansion, then directly convert the remaining | 
|  | 3121 | // arguments, because we don't know what parameters they'll match up | 
|  | 3122 | // with. | 
|  | 3123 | if (TemplateArgs[ArgIdx-1].getArgument().isPackExpansion()) { | 
|  | 3124 | bool InFinalParameterPack = Param != ParamEnd && | 
|  | 3125 | Param + 1 == ParamEnd && | 
|  | 3126 | (*Param)->isTemplateParameterPack() && | 
|  | 3127 | !getExpandedPackSize(*Param); | 
|  | 3128 |  | 
|  | 3129 | if (!InFinalParameterPack && !ArgumentPack.empty()) { | 
|  | 3130 | // If we were part way through filling in an expanded parameter pack, | 
|  | 3131 | // fall back to just producing individual arguments. | 
|  | 3132 | Converted.insert(Converted.end(), | 
|  | 3133 | ArgumentPack.begin(), ArgumentPack.end()); | 
|  | 3134 | ArgumentPack.clear(); | 
|  | 3135 | } | 
|  | 3136 |  | 
|  | 3137 | while (ArgIdx < NumArgs) { | 
|  | 3138 | if (InFinalParameterPack) | 
|  | 3139 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); | 
|  | 3140 | else | 
|  | 3141 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); | 
|  | 3142 | ++ArgIdx; | 
|  | 3143 | } | 
|  | 3144 |  | 
|  | 3145 | // Push the argument pack onto the list of converted arguments. | 
|  | 3146 | if (InFinalParameterPack) { | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3147 | Converted.push_back( | 
|  | 3148 | TemplateArgument::CreatePackCopy(Context, | 
|  | 3149 | ArgumentPack.data(), | 
|  | 3150 | ArgumentPack.size())); | 
|  | 3151 | ArgumentPack.clear(); | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3152 | } else if (ExpansionIntoFixedList) { | 
|  | 3153 | // We have expanded a pack into a fixed list. | 
|  | 3154 | *ExpansionIntoFixedList = true; | 
|  | 3155 | } | 
|  | 3156 |  | 
|  | 3157 | return false; | 
| Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3158 | } | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3159 |  | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3160 | continue; | 
| Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3161 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3162 |  | 
| Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3163 | // If we're checking a partial template argument list, we're done. | 
|  | 3164 | if (PartialTemplateArgs) { | 
|  | 3165 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) | 
|  | 3166 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, | 
|  | 3167 | ArgumentPack.data(), | 
|  | 3168 | ArgumentPack.size())); | 
|  | 3169 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3170 | return false; | 
| Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3171 | } | 
|  | 3172 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3173 | // If we have a template parameter pack with no more corresponding | 
| Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3174 | // arguments, just break out now and we'll fill in the argument pack below. | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3175 | if ((*Param)->isTemplateParameterPack()) { | 
|  | 3176 | assert(!getExpandedPackSize(*Param) && | 
|  | 3177 | "Should have dealt with this already"); | 
|  | 3178 |  | 
|  | 3179 | // A non-expanded parameter pack before the end of the parameter list | 
|  | 3180 | // only occurs for an ill-formed template parameter list, unless we've | 
|  | 3181 | // got a partial argument list for a function template, so just bail out. | 
|  | 3182 | if (Param + 1 != ParamEnd) | 
|  | 3183 | return true; | 
|  | 3184 |  | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3185 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, | 
|  | 3186 | ArgumentPack.data(), | 
|  | 3187 | ArgumentPack.size())); | 
|  | 3188 | ArgumentPack.clear(); | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3189 |  | 
|  | 3190 | ++Param; | 
|  | 3191 | continue; | 
|  | 3192 | } | 
|  | 3193 |  | 
| Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3194 | // Check whether we have a default argument. | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3195 | TemplateArgumentLoc Arg; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3196 |  | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3197 | // Retrieve the default template argument from the template | 
|  | 3198 | // parameter. For each kind of template parameter, we substitute the | 
|  | 3199 | // template arguments provided thus far and any "outer" template arguments | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3200 | // (when the template parameter was part of a nested template) into | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3201 | // the default argument. | 
|  | 3202 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { | 
| Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3203 | if (!TTP->hasDefaultArgument()) | 
|  | 3204 | return diagnoseArityMismatch(*this, Template, TemplateLoc, | 
|  | 3205 | TemplateArgs); | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3206 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3208 | Template, | 
|  | 3209 | TemplateLoc, | 
|  | 3210 | RAngleLoc, | 
|  | 3211 | TTP, | 
|  | 3212 | Converted); | 
|  | 3213 | if (!ArgType) | 
|  | 3214 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3215 |  | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3216 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), | 
|  | 3217 | ArgType); | 
|  | 3218 | } else if (NonTypeTemplateParmDecl *NTTP | 
|  | 3219 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { | 
| Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3220 | if (!NTTP->hasDefaultArgument()) | 
|  | 3221 | return diagnoseArityMismatch(*this, Template, TemplateLoc, | 
|  | 3222 | TemplateArgs); | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3223 |  | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3224 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3225 | TemplateLoc, | 
|  | 3226 | RAngleLoc, | 
|  | 3227 | NTTP, | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3228 | Converted); | 
|  | 3229 | if (E.isInvalid()) | 
|  | 3230 | return true; | 
|  | 3231 |  | 
|  | 3232 | Expr *Ex = E.takeAs<Expr>(); | 
|  | 3233 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); | 
|  | 3234 | } else { | 
|  | 3235 | TemplateTemplateParmDecl *TempParm | 
|  | 3236 | = cast<TemplateTemplateParmDecl>(*Param); | 
|  | 3237 |  | 
| Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3238 | if (!TempParm->hasDefaultArgument()) | 
|  | 3239 | return diagnoseArityMismatch(*this, Template, TemplateLoc, | 
|  | 3240 | TemplateArgs); | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3241 |  | 
| Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3242 | NestedNameSpecifierLoc QualifierLoc; | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3243 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3244 | TemplateLoc, | 
|  | 3245 | RAngleLoc, | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3246 | TempParm, | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3247 | Converted, | 
|  | 3248 | QualifierLoc); | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3249 | if (Name.isNull()) | 
|  | 3250 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3251 |  | 
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3252 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, | 
|  | 3253 | TempParm->getDefaultArgument().getTemplateNameLoc()); | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3254 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3255 |  | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3256 | // Introduce an instantiation record that describes where we are using | 
|  | 3257 | // the default template argument. | 
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3258 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, | 
|  | 3259 | *Param, Converted, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | SourceRange(TemplateLoc, RAngleLoc)); | 
| Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3261 | if (Instantiating) | 
|  | 3262 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3263 |  | 
| Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3264 | // Check the default template argument. | 
| Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3265 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3266 | RAngleLoc, 0, Converted)) | 
| Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3267 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3268 |  | 
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3269 | // Core issue 150 (assumed resolution): if this is a template template | 
|  | 3270 | // parameter, keep track of the default template arguments from the | 
|  | 3271 | // template definition. | 
|  | 3272 | if (isTemplateTemplateParameter) | 
|  | 3273 | TemplateArgs.addArgument(Arg); | 
|  | 3274 |  | 
| Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3275 | // Move to the next template parameter and argument. | 
|  | 3276 | ++Param; | 
|  | 3277 | ++ArgIdx; | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3278 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3279 |  | 
| Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3280 | // If we have any leftover arguments, then there were too many arguments. | 
|  | 3281 | // Complain and fail. | 
|  | 3282 | if (ArgIdx < NumArgs) | 
|  | 3283 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3285 | return false; | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3286 | } | 
|  | 3287 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3288 | namespace { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3289 | class UnnamedLocalNoLinkageFinder | 
|  | 3290 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3291 | { | 
|  | 3292 | Sema &S; | 
|  | 3293 | SourceRange SR; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3294 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3295 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3297 | public: | 
|  | 3298 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } | 
|  | 3299 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | bool Visit(QualType T) { | 
|  | 3301 | return inherited::Visit(T.getTypePtr()); | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3302 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3303 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3304 | #define TYPE(Class, Parent) \ | 
|  | 3305 | bool Visit##Class##Type(const Class##Type *); | 
|  | 3306 | #define ABSTRACT_TYPE(Class, Parent) \ | 
|  | 3307 | bool Visit##Class##Type(const Class##Type *) { return false; } | 
|  | 3308 | #define NON_CANONICAL_TYPE(Class, Parent) \ | 
|  | 3309 | bool Visit##Class##Type(const Class##Type *) { return false; } | 
|  | 3310 | #include "clang/AST/TypeNodes.def" | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3311 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3312 | bool VisitTagDecl(const TagDecl *Tag); | 
|  | 3313 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); | 
|  | 3314 | }; | 
|  | 3315 | } | 
|  | 3316 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3317 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3318 | return false; | 
|  | 3319 | } | 
|  | 3320 |  | 
|  | 3321 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { | 
|  | 3322 | return Visit(T->getElementType()); | 
|  | 3323 | } | 
|  | 3324 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3325 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3326 | return Visit(T->getPointeeType()); | 
|  | 3327 | } | 
|  | 3328 |  | 
|  | 3329 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3330 | const BlockPointerType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3331 | return Visit(T->getPointeeType()); | 
|  | 3332 | } | 
|  | 3333 |  | 
|  | 3334 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3335 | const LValueReferenceType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3336 | return Visit(T->getPointeeType()); | 
|  | 3337 | } | 
|  | 3338 |  | 
|  | 3339 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3340 | const RValueReferenceType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3341 | return Visit(T->getPointeeType()); | 
|  | 3342 | } | 
|  | 3343 |  | 
|  | 3344 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3345 | const MemberPointerType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3346 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); | 
|  | 3347 | } | 
|  | 3348 |  | 
|  | 3349 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3350 | const ConstantArrayType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3351 | return Visit(T->getElementType()); | 
|  | 3352 | } | 
|  | 3353 |  | 
|  | 3354 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | const IncompleteArrayType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3356 | return Visit(T->getElementType()); | 
|  | 3357 | } | 
|  | 3358 |  | 
|  | 3359 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3360 | const VariableArrayType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3361 | return Visit(T->getElementType()); | 
|  | 3362 | } | 
|  | 3363 |  | 
|  | 3364 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3365 | const DependentSizedArrayType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3366 | return Visit(T->getElementType()); | 
|  | 3367 | } | 
|  | 3368 |  | 
|  | 3369 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3370 | const DependentSizedExtVectorType* T) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3371 | return Visit(T->getElementType()); | 
|  | 3372 | } | 
|  | 3373 |  | 
|  | 3374 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { | 
|  | 3375 | return Visit(T->getElementType()); | 
|  | 3376 | } | 
|  | 3377 |  | 
|  | 3378 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { | 
|  | 3379 | return Visit(T->getElementType()); | 
|  | 3380 | } | 
|  | 3381 |  | 
|  | 3382 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( | 
|  | 3383 | const FunctionProtoType* T) { | 
|  | 3384 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3385 | AEnd = T->arg_type_end(); | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3386 | A != AEnd; ++A) { | 
|  | 3387 | if (Visit(*A)) | 
|  | 3388 | return true; | 
|  | 3389 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3391 | return Visit(T->getResultType()); | 
|  | 3392 | } | 
|  | 3393 |  | 
|  | 3394 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( | 
|  | 3395 | const FunctionNoProtoType* T) { | 
|  | 3396 | return Visit(T->getResultType()); | 
|  | 3397 | } | 
|  | 3398 |  | 
|  | 3399 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( | 
|  | 3400 | const UnresolvedUsingType*) { | 
|  | 3401 | return false; | 
|  | 3402 | } | 
|  | 3403 |  | 
|  | 3404 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { | 
|  | 3405 | return false; | 
|  | 3406 | } | 
|  | 3407 |  | 
|  | 3408 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { | 
|  | 3409 | return Visit(T->getUnderlyingType()); | 
|  | 3410 | } | 
|  | 3411 |  | 
|  | 3412 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { | 
|  | 3413 | return false; | 
|  | 3414 | } | 
|  | 3415 |  | 
| Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3416 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( | 
|  | 3417 | const UnaryTransformType*) { | 
|  | 3418 | return false; | 
|  | 3419 | } | 
|  | 3420 |  | 
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3421 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { | 
|  | 3422 | return Visit(T->getDeducedType()); | 
|  | 3423 | } | 
|  | 3424 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3425 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { | 
|  | 3426 | return VisitTagDecl(T->getDecl()); | 
|  | 3427 | } | 
|  | 3428 |  | 
|  | 3429 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { | 
|  | 3430 | return VisitTagDecl(T->getDecl()); | 
|  | 3431 | } | 
|  | 3432 |  | 
|  | 3433 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( | 
|  | 3434 | const TemplateTypeParmType*) { | 
|  | 3435 | return false; | 
|  | 3436 | } | 
|  | 3437 |  | 
| Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3438 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( | 
|  | 3439 | const SubstTemplateTypeParmPackType *) { | 
|  | 3440 | return false; | 
|  | 3441 | } | 
|  | 3442 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3443 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( | 
|  | 3444 | const TemplateSpecializationType*) { | 
|  | 3445 | return false; | 
|  | 3446 | } | 
|  | 3447 |  | 
|  | 3448 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( | 
|  | 3449 | const InjectedClassNameType* T) { | 
|  | 3450 | return VisitTagDecl(T->getDecl()); | 
|  | 3451 | } | 
|  | 3452 |  | 
|  | 3453 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( | 
|  | 3454 | const DependentNameType* T) { | 
|  | 3455 | return VisitNestedNameSpecifier(T->getQualifier()); | 
|  | 3456 | } | 
|  | 3457 |  | 
|  | 3458 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( | 
|  | 3459 | const DependentTemplateSpecializationType* T) { | 
|  | 3460 | return VisitNestedNameSpecifier(T->getQualifier()); | 
|  | 3461 | } | 
|  | 3462 |  | 
| Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3463 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( | 
|  | 3464 | const PackExpansionType* T) { | 
|  | 3465 | return Visit(T->getPattern()); | 
|  | 3466 | } | 
|  | 3467 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3468 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { | 
|  | 3469 | return false; | 
|  | 3470 | } | 
|  | 3471 |  | 
|  | 3472 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( | 
|  | 3473 | const ObjCInterfaceType *) { | 
|  | 3474 | return false; | 
|  | 3475 | } | 
|  | 3476 |  | 
|  | 3477 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( | 
|  | 3478 | const ObjCObjectPointerType *) { | 
|  | 3479 | return false; | 
|  | 3480 | } | 
|  | 3481 |  | 
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3482 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { | 
|  | 3483 | return Visit(T->getValueType()); | 
|  | 3484 | } | 
|  | 3485 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3486 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { | 
|  | 3487 | if (Tag->getDeclContext()->isFunctionOrMethod()) { | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3488 | S.Diag(SR.getBegin(), | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3489 | S.getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3490 | diag::warn_cxx98_compat_template_arg_local_type : | 
|  | 3491 | diag::ext_template_arg_local_type) | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3492 | << S.Context.getTypeDeclType(Tag) << SR; | 
|  | 3493 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 | } | 
|  | 3495 |  | 
| John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 3496 | if (!Tag->hasNameForLinkage()) { | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3497 | S.Diag(SR.getBegin(), | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3498 | S.getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3499 | diag::warn_cxx98_compat_template_arg_unnamed_type : | 
|  | 3500 | diag::ext_template_arg_unnamed_type) << SR; | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3501 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); | 
|  | 3502 | return true; | 
|  | 3503 | } | 
|  | 3504 |  | 
|  | 3505 | return false; | 
|  | 3506 | } | 
|  | 3507 |  | 
|  | 3508 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( | 
|  | 3509 | NestedNameSpecifier *NNS) { | 
|  | 3510 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) | 
|  | 3511 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3512 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3513 | switch (NNS->getKind()) { | 
|  | 3514 | case NestedNameSpecifier::Identifier: | 
|  | 3515 | case NestedNameSpecifier::Namespace: | 
| Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3516 | case NestedNameSpecifier::NamespaceAlias: | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3517 | case NestedNameSpecifier::Global: | 
|  | 3518 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3519 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3520 | case NestedNameSpecifier::TypeSpec: | 
|  | 3521 | case NestedNameSpecifier::TypeSpecWithTemplate: | 
|  | 3522 | return Visit(QualType(NNS->getAsType(), 0)); | 
|  | 3523 | } | 
| David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3524 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3525 | } | 
|  | 3526 |  | 
|  | 3527 |  | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3528 | /// \brief Check a template argument against its corresponding | 
|  | 3529 | /// template type parameter. | 
|  | 3530 | /// | 
|  | 3531 | /// This routine implements the semantics of C++ [temp.arg.type]. It | 
|  | 3532 | /// returns true if an error occurred, and false otherwise. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3534 | TypeSourceInfo *ArgInfo) { | 
|  | 3535 | assert(ArgInfo && "invalid TypeSourceInfo"); | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3536 | QualType Arg = ArgInfo->getType(); | 
| Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3537 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); | 
| Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3538 |  | 
|  | 3539 | if (Arg->isVariablyModifiedType()) { | 
|  | 3540 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; | 
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3541 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { | 
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3542 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3543 | } | 
|  | 3544 |  | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3545 | // C++03 [temp.arg.type]p2: | 
|  | 3546 | //   A local type, a type with no linkage, an unnamed type or a type | 
|  | 3547 | //   compounded from any of these types shall not be used as a | 
|  | 3548 | //   template-argument for a template type-parameter. | 
|  | 3549 | // | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3550 | // C++11 allows these, and even in C++03 we allow them as an extension with | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3551 | // a warning. | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3552 | if (LangOpts.CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3553 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, | 
|  | 3554 | SR.getBegin()) != DiagnosticsEngine::Ignored || | 
|  | 3555 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, | 
|  | 3556 | SR.getBegin()) != DiagnosticsEngine::Ignored : | 
|  | 3557 | Arg->hasUnnamedOrLocalType()) { | 
| Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3558 | UnnamedLocalNoLinkageFinder Finder(*this, SR); | 
|  | 3559 | (void)Finder.Visit(Context.getCanonicalType(Arg)); | 
|  | 3560 | } | 
|  | 3561 |  | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3562 | return false; | 
|  | 3563 | } | 
|  | 3564 |  | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3565 | enum NullPointerValueKind { | 
|  | 3566 | NPV_NotNullPointer, | 
|  | 3567 | NPV_NullPointer, | 
|  | 3568 | NPV_Error | 
|  | 3569 | }; | 
|  | 3570 |  | 
|  | 3571 | /// \brief Determine whether the given template argument is a null pointer | 
|  | 3572 | /// value of the appropriate type. | 
|  | 3573 | static NullPointerValueKind | 
|  | 3574 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, | 
|  | 3575 | QualType ParamType, Expr *Arg) { | 
|  | 3576 | if (Arg->isValueDependent() || Arg->isTypeDependent()) | 
|  | 3577 | return NPV_NotNullPointer; | 
|  | 3578 |  | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3579 | if (!S.getLangOpts().CPlusPlus11) | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3580 | return NPV_NotNullPointer; | 
|  | 3581 |  | 
|  | 3582 | // Determine whether we have a constant expression. | 
| Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3583 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); | 
|  | 3584 | if (ArgRV.isInvalid()) | 
|  | 3585 | return NPV_Error; | 
|  | 3586 | Arg = ArgRV.take(); | 
|  | 3587 |  | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3588 | Expr::EvalResult EvalResult; | 
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3589 | SmallVector<PartialDiagnosticAt, 8> Notes; | 
| Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3590 | EvalResult.Diag = &Notes; | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3591 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || | 
| Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3592 | EvalResult.HasSideEffects) { | 
|  | 3593 | SourceLocation DiagLoc = Arg->getExprLoc(); | 
|  | 3594 |  | 
|  | 3595 | // If our only note is the usual "invalid subexpression" note, just point | 
|  | 3596 | // the caret at its location rather than producing an essentially | 
|  | 3597 | // redundant note. | 
|  | 3598 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == | 
|  | 3599 | diag::note_invalid_subexpr_in_const_expr) { | 
|  | 3600 | DiagLoc = Notes[0].first; | 
|  | 3601 | Notes.clear(); | 
|  | 3602 | } | 
|  | 3603 |  | 
|  | 3604 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) | 
|  | 3605 | << Arg->getType() << Arg->getSourceRange(); | 
|  | 3606 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) | 
|  | 3607 | S.Diag(Notes[I].first, Notes[I].second); | 
|  | 3608 |  | 
|  | 3609 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3610 | return NPV_Error; | 
|  | 3611 | } | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3612 |  | 
|  | 3613 | // C++11 [temp.arg.nontype]p1: | 
|  | 3614 | //   - an address constant expression of type std::nullptr_t | 
|  | 3615 | if (Arg->getType()->isNullPtrType()) | 
|  | 3616 | return NPV_NullPointer; | 
|  | 3617 |  | 
|  | 3618 | //   - a constant expression that evaluates to a null pointer value (4.10); or | 
|  | 3619 | //   - a constant expression that evaluates to a null member pointer value | 
|  | 3620 | //     (4.11); or | 
|  | 3621 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || | 
|  | 3622 | (EvalResult.Val.isMemberPointer() && | 
|  | 3623 | !EvalResult.Val.getMemberPointerDecl())) { | 
|  | 3624 | // If our expression has an appropriate type, we've succeeded. | 
|  | 3625 | bool ObjCLifetimeConversion; | 
|  | 3626 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || | 
|  | 3627 | S.IsQualificationConversion(Arg->getType(), ParamType, false, | 
|  | 3628 | ObjCLifetimeConversion)) | 
|  | 3629 | return NPV_NullPointer; | 
|  | 3630 |  | 
|  | 3631 | // The types didn't match, but we know we got a null pointer; complain, | 
|  | 3632 | // then recover as if the types were correct. | 
|  | 3633 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) | 
|  | 3634 | << Arg->getType() << ParamType << Arg->getSourceRange(); | 
|  | 3635 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3636 | return NPV_NullPointer; | 
|  | 3637 | } | 
|  | 3638 |  | 
|  | 3639 | // If we don't have a null pointer value, but we do have a NULL pointer | 
|  | 3640 | // constant, suggest a cast to the appropriate type. | 
|  | 3641 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { | 
|  | 3642 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; | 
|  | 3643 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) | 
|  | 3644 | << ParamType | 
|  | 3645 | << FixItHint::CreateInsertion(Arg->getLocStart(), Code) | 
|  | 3646 | << FixItHint::CreateInsertion(S.PP.getLocForEndOfToken(Arg->getLocEnd()), | 
|  | 3647 | ")"); | 
|  | 3648 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3649 | return NPV_NullPointer; | 
|  | 3650 | } | 
|  | 3651 |  | 
|  | 3652 | // FIXME: If we ever want to support general, address-constant expressions | 
|  | 3653 | // as non-type template arguments, we should return the ExprResult here to | 
|  | 3654 | // be interpreted by the caller. | 
|  | 3655 | return NPV_NotNullPointer; | 
|  | 3656 | } | 
|  | 3657 |  | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3658 | /// \brief Checks whether the given template argument is the address | 
|  | 3659 | /// of an object or function according to C++ [temp.arg.nontype]p1. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3660 | static bool | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3661 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, | 
|  | 3662 | NonTypeTemplateParmDecl *Param, | 
|  | 3663 | QualType ParamType, | 
|  | 3664 | Expr *ArgIn, | 
|  | 3665 | TemplateArgument &Converted) { | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3666 | bool Invalid = false; | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3667 | Expr *Arg = ArgIn; | 
|  | 3668 | QualType ArgType = Arg->getType(); | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3669 |  | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3670 | // If our parameter has pointer type, check for a null template value. | 
|  | 3671 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { | 
|  | 3672 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { | 
|  | 3673 | case NPV_NullPointer: | 
| Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 3674 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3675 | Converted = TemplateArgument(ParamType, /*isNullPtr*/true); | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3676 | return false; | 
|  | 3677 |  | 
|  | 3678 | case NPV_Error: | 
|  | 3679 | return true; | 
|  | 3680 |  | 
|  | 3681 | case NPV_NotNullPointer: | 
|  | 3682 | break; | 
|  | 3683 | } | 
|  | 3684 | } | 
|  | 3685 |  | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3686 | // See through any implicit casts we added to fix the type. | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3687 | Arg = Arg->IgnoreImpCasts(); | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3688 |  | 
|  | 3689 | // C++ [temp.arg.nontype]p1: | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | // | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3691 | //   A template-argument for a non-type, non-template | 
|  | 3692 | //   template-parameter shall be one of: [...] | 
|  | 3693 | // | 
|  | 3694 | //     -- the address of an object or function with external | 
|  | 3695 | //        linkage, including function templates and function | 
|  | 3696 | //        template-ids but excluding non-static class members, | 
|  | 3697 | //        expressed as & id-expression where the & is optional if | 
|  | 3698 | //        the name refers to a function or array, or if the | 
|  | 3699 | //        corresponding template-parameter is a reference; or | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 |  | 
| Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3701 | // In C++98/03 mode, give an extension warning on any extra parentheses. | 
|  | 3702 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 | 
|  | 3703 | bool ExtraParens = false; | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3704 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3705 | if (!Invalid && !ExtraParens) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3706 | S.Diag(Arg->getLocStart(), | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3707 | S.getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3708 | diag::warn_cxx98_compat_template_arg_extra_parens : | 
|  | 3709 | diag::ext_template_arg_extra_parens) | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3710 | << Arg->getSourceRange(); | 
| Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3711 | ExtraParens = true; | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3712 | } | 
|  | 3713 |  | 
|  | 3714 | Arg = Parens->getSubExpr(); | 
|  | 3715 | } | 
|  | 3716 |  | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3717 | while (SubstNonTypeTemplateParmExpr *subst = | 
|  | 3718 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) | 
|  | 3719 | Arg = subst->getReplacement()->IgnoreImpCasts(); | 
|  | 3720 |  | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3721 | bool AddressTaken = false; | 
|  | 3722 | SourceLocation AddrOpLoc; | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3723 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { | 
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3724 | if (UnOp->getOpcode() == UO_AddrOf) { | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3725 | Arg = UnOp->getSubExpr(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3726 | AddressTaken = true; | 
|  | 3727 | AddrOpLoc = UnOp->getOperatorLoc(); | 
|  | 3728 | } | 
| Francois Pichet | c867975 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3729 | } | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3730 |  | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3731 | if (S.getLangOpts().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3732 | Converted = TemplateArgument(ArgIn); | 
|  | 3733 | return false; | 
|  | 3734 | } | 
|  | 3735 |  | 
|  | 3736 | while (SubstNonTypeTemplateParmExpr *subst = | 
|  | 3737 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) | 
|  | 3738 | Arg = subst->getReplacement()->IgnoreImpCasts(); | 
|  | 3739 |  | 
| Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3740 | // Stop checking the precise nature of the argument if it is value dependent, | 
|  | 3741 | // it should be checked when instantiated. | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3742 | if (Arg->isValueDependent()) { | 
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3743 | Converted = TemplateArgument(ArgIn); | 
| Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3744 | return false; | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3745 | } | 
| Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3746 |  | 
|  | 3747 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); | 
|  | 3748 | if (!DRE) { | 
|  | 3749 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) | 
|  | 3750 | << Arg->getSourceRange(); | 
|  | 3751 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3752 | return true; | 
|  | 3753 | } | 
| Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3754 |  | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3755 | if (!isa<ValueDecl>(DRE->getDecl())) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3756 | S.Diag(Arg->getLocStart(), | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3757 | diag::err_template_arg_not_object_or_func_form) | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3758 | << Arg->getSourceRange(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3759 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3760 | return true; | 
|  | 3761 | } | 
|  | 3762 |  | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3763 | ValueDecl *Entity = DRE->getDecl(); | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3764 |  | 
|  | 3765 | // Cannot refer to non-static data members | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3766 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Entity)) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3767 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3768 | << Field << Arg->getSourceRange(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3769 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3770 | return true; | 
|  | 3771 | } | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3772 |  | 
|  | 3773 | // Cannot refer to non-static member functions | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3774 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3775 | if (!Method->isStatic()) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3776 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3777 | << Method << Arg->getSourceRange(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3778 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3779 | return true; | 
|  | 3780 | } | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3781 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 |  | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3783 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); | 
|  | 3784 | VarDecl *Var = dyn_cast<VarDecl>(Entity); | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3785 |  | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3786 | // A non-type template argument must refer to an object or function. | 
|  | 3787 | if (!Func && !Var) { | 
|  | 3788 | // We found something, but we don't know specifically what it is. | 
|  | 3789 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) | 
|  | 3790 | << Arg->getSourceRange(); | 
|  | 3791 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); | 
|  | 3792 | return true; | 
|  | 3793 | } | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3794 |  | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3795 | // Address / reference template args must have external linkage in C++98. | 
|  | 3796 | if (Entity->getLinkage() == InternalLinkage) { | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3797 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3798 | diag::warn_cxx98_compat_template_arg_object_internal : | 
|  | 3799 | diag::ext_template_arg_object_internal) | 
|  | 3800 | << !Func << Entity << Arg->getSourceRange(); | 
|  | 3801 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) | 
|  | 3802 | << !Func; | 
|  | 3803 | } else if (Entity->getLinkage() == NoLinkage) { | 
|  | 3804 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) | 
|  | 3805 | << !Func << Entity << Arg->getSourceRange(); | 
|  | 3806 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) | 
|  | 3807 | << !Func; | 
|  | 3808 | return true; | 
|  | 3809 | } | 
|  | 3810 |  | 
|  | 3811 | if (Func) { | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3812 | // If the template parameter has pointer type, the function decays. | 
|  | 3813 | if (ParamType->isPointerType() && !AddressTaken) | 
|  | 3814 | ArgType = S.Context.getPointerType(Func->getType()); | 
|  | 3815 | else if (AddressTaken && ParamType->isReferenceType()) { | 
|  | 3816 | // If we originally had an address-of operator, but the | 
|  | 3817 | // parameter has reference type, complain and (if things look | 
|  | 3818 | // like they will work) drop the address-of operator. | 
|  | 3819 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), | 
|  | 3820 | ParamType.getNonReferenceType())) { | 
|  | 3821 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | 
|  | 3822 | << ParamType; | 
|  | 3823 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3824 | return true; | 
|  | 3825 | } | 
|  | 3826 |  | 
|  | 3827 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | 
|  | 3828 | << ParamType | 
|  | 3829 | << FixItHint::CreateRemoval(AddrOpLoc); | 
|  | 3830 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3831 |  | 
|  | 3832 | ArgType = Func->getType(); | 
|  | 3833 | } | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3834 | } else { | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3835 | // A value of reference type is not an object. | 
|  | 3836 | if (Var->getType()->isReferenceType()) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3837 | S.Diag(Arg->getLocStart(), | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3838 | diag::err_template_arg_reference_var) | 
|  | 3839 | << Var->getType() << Arg->getSourceRange(); | 
|  | 3840 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3841 | return true; | 
|  | 3842 | } | 
|  | 3843 |  | 
| Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3844 | // A template argument must have static storage duration. | 
|  | 3845 | // FIXME: Ensure this works for thread_local as well as __thread. | 
|  | 3846 | if (Var->isThreadSpecified()) { | 
|  | 3847 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) | 
|  | 3848 | << Arg->getSourceRange(); | 
|  | 3849 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); | 
|  | 3850 | return true; | 
|  | 3851 | } | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3852 |  | 
|  | 3853 | // If the template parameter has pointer type, we must have taken | 
|  | 3854 | // the address of this object. | 
|  | 3855 | if (ParamType->isReferenceType()) { | 
|  | 3856 | if (AddressTaken) { | 
|  | 3857 | // If we originally had an address-of operator, but the | 
|  | 3858 | // parameter has reference type, complain and (if things look | 
|  | 3859 | // like they will work) drop the address-of operator. | 
|  | 3860 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), | 
|  | 3861 | ParamType.getNonReferenceType())) { | 
|  | 3862 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | 
|  | 3863 | << ParamType; | 
|  | 3864 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3865 | return true; | 
|  | 3866 | } | 
|  | 3867 |  | 
|  | 3868 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) | 
|  | 3869 | << ParamType | 
|  | 3870 | << FixItHint::CreateRemoval(AddrOpLoc); | 
|  | 3871 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3872 |  | 
|  | 3873 | ArgType = Var->getType(); | 
|  | 3874 | } | 
|  | 3875 | } else if (!AddressTaken && ParamType->isPointerType()) { | 
|  | 3876 | if (Var->getType()->isArrayType()) { | 
|  | 3877 | // Array-to-pointer decay. | 
|  | 3878 | ArgType = S.Context.getArrayDecayedType(Var->getType()); | 
|  | 3879 | } else { | 
|  | 3880 | // If the template parameter has pointer type but the address of | 
|  | 3881 | // this object was not taken, complain and (possibly) recover by | 
|  | 3882 | // taking the address of the entity. | 
|  | 3883 | ArgType = S.Context.getPointerType(Var->getType()); | 
|  | 3884 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { | 
|  | 3885 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) | 
|  | 3886 | << ParamType; | 
|  | 3887 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3888 | return true; | 
|  | 3889 | } | 
|  | 3890 |  | 
|  | 3891 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) | 
|  | 3892 | << ParamType | 
|  | 3893 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); | 
|  | 3894 |  | 
|  | 3895 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3896 | } | 
|  | 3897 | } | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3898 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 |  | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3900 | bool ObjCLifetimeConversion; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3901 | if (ParamType->isPointerType() && | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3902 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3903 | S.IsQualificationConversion(ArgType, ParamType, false, | 
|  | 3904 | ObjCLifetimeConversion)) { | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3905 | // For pointer-to-object types, qualification conversions are | 
|  | 3906 | // permitted. | 
|  | 3907 | } else { | 
|  | 3908 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { | 
|  | 3909 | if (!ParamRef->getPointeeType()->isFunctionType()) { | 
|  | 3910 | // C++ [temp.arg.nontype]p5b3: | 
|  | 3911 | //   For a non-type template-parameter of type reference to | 
|  | 3912 | //   object, no conversions apply. The type referred to by the | 
|  | 3913 | //   reference may be more cv-qualified than the (otherwise | 
|  | 3914 | //   identical) type of the template- argument. The | 
|  | 3915 | //   template-parameter is bound directly to the | 
|  | 3916 | //   template-argument, which shall be an lvalue. | 
|  | 3917 |  | 
|  | 3918 | // FIXME: Other qualifiers? | 
|  | 3919 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); | 
|  | 3920 | unsigned ArgQuals = ArgType.getCVRQualifiers(); | 
|  | 3921 |  | 
|  | 3922 | if ((ParamQuals | ArgQuals) != ParamQuals) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3923 | S.Diag(Arg->getLocStart(), | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3924 | diag::err_template_arg_ref_bind_ignores_quals) | 
|  | 3925 | << ParamType << Arg->getType() | 
|  | 3926 | << Arg->getSourceRange(); | 
|  | 3927 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3928 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3929 | } | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3930 | } | 
|  | 3931 | } | 
|  | 3932 |  | 
|  | 3933 | // At this point, the template argument refers to an object or | 
|  | 3934 | // function with external linkage. We now need to check whether the | 
|  | 3935 | // argument and parameter types are compatible. | 
|  | 3936 | if (!S.Context.hasSameUnqualifiedType(ArgType, | 
|  | 3937 | ParamType.getNonReferenceType())) { | 
|  | 3938 | // We can't perform this conversion or binding. | 
|  | 3939 | if (ParamType->isReferenceType()) | 
|  | 3940 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3941 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3942 | else | 
|  | 3943 | S.Diag(Arg->getLocStart(),  diag::err_template_arg_not_convertible) | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3944 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3945 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3946 | return true; | 
|  | 3947 | } | 
|  | 3948 | } | 
|  | 3949 |  | 
|  | 3950 | // Create the template argument. | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3951 | Converted = TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), | 
|  | 3952 | ParamType->isReferenceType()); | 
| Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 3953 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3954 | return false; | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3955 | } | 
|  | 3956 |  | 
|  | 3957 | /// \brief Checks whether the given template argument is a pointer to | 
|  | 3958 | /// member constant according to C++ [temp.arg.nontype]p1. | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3959 | static bool CheckTemplateArgumentPointerToMember(Sema &S, | 
|  | 3960 | NonTypeTemplateParmDecl *Param, | 
|  | 3961 | QualType ParamType, | 
|  | 3962 | Expr *&ResultArg, | 
|  | 3963 | TemplateArgument &Converted) { | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3964 | bool Invalid = false; | 
|  | 3965 |  | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3966 | // Check for a null pointer value. | 
|  | 3967 | Expr *Arg = ResultArg; | 
|  | 3968 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { | 
|  | 3969 | case NPV_Error: | 
|  | 3970 | return true; | 
|  | 3971 | case NPV_NullPointer: | 
| Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 3972 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3973 | Converted = TemplateArgument(ParamType, /*isNullPtr*/true); | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3974 | return false; | 
|  | 3975 | case NPV_NotNullPointer: | 
|  | 3976 | break; | 
|  | 3977 | } | 
|  | 3978 |  | 
|  | 3979 | bool ObjCLifetimeConversion; | 
|  | 3980 | if (S.IsQualificationConversion(Arg->getType(), | 
|  | 3981 | ParamType.getNonReferenceType(), | 
|  | 3982 | false, ObjCLifetimeConversion)) { | 
|  | 3983 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, | 
|  | 3984 | Arg->getValueKind()).take(); | 
|  | 3985 | ResultArg = Arg; | 
|  | 3986 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), | 
|  | 3987 | ParamType.getNonReferenceType())) { | 
|  | 3988 | // We can't perform this conversion. | 
|  | 3989 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) | 
|  | 3990 | << Arg->getType() << ParamType << Arg->getSourceRange(); | 
|  | 3991 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 3992 | return true; | 
|  | 3993 | } | 
|  | 3994 |  | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3995 | // See through any implicit casts we added to fix the type. | 
| Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3996 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3997 | Arg = Cast->getSubExpr(); | 
|  | 3998 |  | 
|  | 3999 | // C++ [temp.arg.nontype]p1: | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | // | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4001 | //   A template-argument for a non-type, non-template | 
|  | 4002 | //   template-parameter shall be one of: [...] | 
|  | 4003 | // | 
|  | 4004 | //     -- a pointer to member expressed as described in 5.3.1. | 
| Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4005 | DeclRefExpr *DRE = 0; | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4006 |  | 
| Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4007 | // In C++98/03 mode, give an extension warning on any extra parentheses. | 
|  | 4008 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 | 
|  | 4009 | bool ExtraParens = false; | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4010 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4011 | if (!Invalid && !ExtraParens) { | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4012 | S.Diag(Arg->getLocStart(), | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4013 | S.getLangOpts().CPlusPlus11 ? | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4014 | diag::warn_cxx98_compat_template_arg_extra_parens : | 
|  | 4015 | diag::ext_template_arg_extra_parens) | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4016 | << Arg->getSourceRange(); | 
| Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4017 | ExtraParens = true; | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4018 | } | 
|  | 4019 |  | 
|  | 4020 | Arg = Parens->getSubExpr(); | 
|  | 4021 | } | 
|  | 4022 |  | 
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4023 | while (SubstNonTypeTemplateParmExpr *subst = | 
|  | 4024 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) | 
|  | 4025 | Arg = subst->getReplacement()->IgnoreImpCasts(); | 
|  | 4026 |  | 
| Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4027 | // A pointer-to-member constant written &Class::member. | 
|  | 4028 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { | 
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4029 | if (UnOp->getOpcode() == UO_AddrOf) { | 
| Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4030 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); | 
|  | 4031 | if (DRE && !DRE->getQualifier()) | 
|  | 4032 | DRE = 0; | 
|  | 4033 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4034 | } | 
| Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4035 | // A constant of pointer-to-member type. | 
|  | 4036 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { | 
|  | 4037 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { | 
|  | 4038 | if (VD->getType()->isMemberPointerType()) { | 
|  | 4039 | if (isa<NonTypeTemplateParmDecl>(VD) || | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4040 | (isa<VarDecl>(VD) && | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4041 | S.Context.getCanonicalType(VD->getType()).isConstQualified())) { | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4042 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { | 
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4043 | Converted = TemplateArgument(Arg); | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4044 | } else { | 
|  | 4045 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); | 
|  | 4046 | Converted = TemplateArgument(VD, /*isReferenceParam*/false); | 
|  | 4047 | } | 
| Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4048 | return Invalid; | 
|  | 4049 | } | 
|  | 4050 | } | 
|  | 4051 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4052 |  | 
| Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4053 | DRE = 0; | 
|  | 4054 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4055 |  | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4056 | if (!DRE) | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4057 | return S.Diag(Arg->getLocStart(), | 
|  | 4058 | diag::err_template_arg_not_pointer_to_member_form) | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4059 | << Arg->getSourceRange(); | 
|  | 4060 |  | 
|  | 4061 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { | 
|  | 4062 | assert((isa<FieldDecl>(DRE->getDecl()) || | 
|  | 4063 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && | 
|  | 4064 | "Only non-static member pointers can make it here"); | 
|  | 4065 |  | 
|  | 4066 | // Okay: this is the address of a non-static member, and therefore | 
|  | 4067 | // a member pointer constant. | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4068 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { | 
| John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4069 | Converted = TemplateArgument(Arg); | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4070 | } else { | 
|  | 4071 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); | 
|  | 4072 | Converted = TemplateArgument(D, /*isReferenceParam*/false); | 
|  | 4073 | } | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4074 | return Invalid; | 
|  | 4075 | } | 
|  | 4076 |  | 
|  | 4077 | // We found something else, but we don't know specifically what it is. | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4078 | S.Diag(Arg->getLocStart(), | 
|  | 4079 | diag::err_template_arg_not_pointer_to_member_form) | 
|  | 4080 | << Arg->getSourceRange(); | 
|  | 4081 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); | 
| Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4082 | return true; | 
|  | 4083 | } | 
|  | 4084 |  | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4085 | /// \brief Check a template argument against its corresponding | 
|  | 4086 | /// non-type template parameter. | 
|  | 4087 | /// | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4088 | /// This routine implements the semantics of C++ [temp.arg.nontype]. | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4089 | /// If an error occurred, it returns ExprError(); otherwise, it | 
|  | 4090 | /// returns the converted template argument. \p | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4091 | /// InstantiatedParamType is the type of the non-type template | 
|  | 4092 | /// parameter after it has been instantiated. | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4093 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, | 
|  | 4094 | QualType InstantiatedParamType, Expr *Arg, | 
|  | 4095 | TemplateArgument &Converted, | 
|  | 4096 | CheckTemplateArgumentKind CTAK) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4097 | SourceLocation StartLoc = Arg->getLocStart(); | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4098 |  | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4099 | // If either the parameter has a dependent type or the argument is | 
|  | 4100 | // type-dependent, there's nothing we can check now. | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4101 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { | 
|  | 4102 | // FIXME: Produce a cloned, canonical expression? | 
| Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4103 | Converted = TemplateArgument(Arg); | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4104 | return Owned(Arg); | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4105 | } | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4106 |  | 
|  | 4107 | // C++ [temp.arg.nontype]p5: | 
|  | 4108 | //   The following conversions are performed on each expression used | 
|  | 4109 | //   as a non-type template-argument. If a non-type | 
|  | 4110 | //   template-argument cannot be converted to the type of the | 
|  | 4111 | //   corresponding template-parameter then the program is | 
|  | 4112 | //   ill-formed. | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4113 | QualType ParamType = InstantiatedParamType; | 
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4114 | if (ParamType->isIntegralOrEnumerationType()) { | 
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4115 | // C++11: | 
|  | 4116 | //   -- for a non-type template-parameter of integral or | 
|  | 4117 | //      enumeration type, conversions permitted in a converted | 
|  | 4118 | //      constant expression are applied. | 
|  | 4119 | // | 
|  | 4120 | // C++98: | 
|  | 4121 | //   -- for a non-type template-parameter of integral or | 
|  | 4122 | //      enumeration type, integral promotions (4.5) and integral | 
|  | 4123 | //      conversions (4.7) are applied. | 
|  | 4124 |  | 
|  | 4125 | if (CTAK == CTAK_Deduced && | 
|  | 4126 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { | 
|  | 4127 | // C++ [temp.deduct.type]p17: | 
|  | 4128 | //   If, in the declaration of a function template with a non-type | 
|  | 4129 | //   template-parameter, the non-type template-parameter is used | 
|  | 4130 | //   in an expression in the function parameter-list and, if the | 
|  | 4131 | //   corresponding template-argument is deduced, the | 
|  | 4132 | //   template-argument type shall match the type of the | 
|  | 4133 | //   template-parameter exactly, except that a template-argument | 
|  | 4134 | //   deduced from an array bound may be of any integral type. | 
|  | 4135 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) | 
|  | 4136 | << Arg->getType().getUnqualifiedType() | 
|  | 4137 | << ParamType.getUnqualifiedType(); | 
|  | 4138 | Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 4139 | return ExprError(); | 
|  | 4140 | } | 
|  | 4141 |  | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4142 | if (getLangOpts().CPlusPlus11) { | 
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4143 | // We can't check arbitrary value-dependent arguments. | 
|  | 4144 | // FIXME: If there's no viable conversion to the template parameter type, | 
|  | 4145 | // we should be able to diagnose that prior to instantiation. | 
|  | 4146 | if (Arg->isValueDependent()) { | 
|  | 4147 | Converted = TemplateArgument(Arg); | 
|  | 4148 | return Owned(Arg); | 
|  | 4149 | } | 
|  | 4150 |  | 
|  | 4151 | // C++ [temp.arg.nontype]p1: | 
|  | 4152 | //   A template-argument for a non-type, non-template template-parameter | 
|  | 4153 | //   shall be one of: | 
|  | 4154 | // | 
|  | 4155 | //     -- for a non-type template-parameter of integral or enumeration | 
|  | 4156 | //        type, a converted constant expression of the type of the | 
|  | 4157 | //        template-parameter; or | 
|  | 4158 | llvm::APSInt Value; | 
|  | 4159 | ExprResult ArgResult = | 
|  | 4160 | CheckConvertedConstantExpression(Arg, ParamType, Value, | 
|  | 4161 | CCEK_TemplateArg); | 
|  | 4162 | if (ArgResult.isInvalid()) | 
|  | 4163 | return ExprError(); | 
|  | 4164 |  | 
|  | 4165 | // Widen the argument value to sizeof(parameter type). This is almost | 
|  | 4166 | // always a no-op, except when the parameter type is bool. In | 
|  | 4167 | // that case, this may extend the argument from 1 bit to 8 bits. | 
|  | 4168 | QualType IntegerType = ParamType; | 
|  | 4169 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) | 
|  | 4170 | IntegerType = Enum->getDecl()->getIntegerType(); | 
|  | 4171 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); | 
|  | 4172 |  | 
| Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4173 | Converted = TemplateArgument(Context, Value, | 
|  | 4174 | Context.getCanonicalType(ParamType)); | 
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4175 | return ArgResult; | 
|  | 4176 | } | 
|  | 4177 |  | 
| Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4178 | ExprResult ArgResult = DefaultLvalueConversion(Arg); | 
|  | 4179 | if (ArgResult.isInvalid()) | 
|  | 4180 | return ExprError(); | 
|  | 4181 | Arg = ArgResult.take(); | 
|  | 4182 |  | 
|  | 4183 | QualType ArgType = Arg->getType(); | 
|  | 4184 |  | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4185 | // C++ [temp.arg.nontype]p1: | 
|  | 4186 | //   A template-argument for a non-type, non-template | 
|  | 4187 | //   template-parameter shall be one of: | 
|  | 4188 | // | 
|  | 4189 | //     -- an integral constant-expression of integral or enumeration | 
|  | 4190 | //        type; or | 
|  | 4191 | //     -- the name of a non-type template-parameter; or | 
|  | 4192 | SourceLocation NonConstantLoc; | 
| Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4193 | llvm::APSInt Value; | 
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4194 | if (!ArgType->isIntegralOrEnumerationType()) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4195 | Diag(Arg->getLocStart(), | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4196 | diag::err_template_arg_not_integral_or_enumeral) | 
|  | 4197 | << ArgType << Arg->getSourceRange(); | 
|  | 4198 | Diag(Param->getLocation(), diag::note_template_param_here); | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4199 | return ExprError(); | 
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4200 | } else if (!Arg->isValueDependent()) { | 
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4201 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { | 
|  | 4202 | QualType T; | 
|  | 4203 |  | 
|  | 4204 | public: | 
|  | 4205 | TmplArgICEDiagnoser(QualType T) : T(T) { } | 
|  | 4206 |  | 
|  | 4207 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, | 
|  | 4208 | SourceRange SR) { | 
|  | 4209 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; | 
|  | 4210 | } | 
|  | 4211 | } Diagnoser(ArgType); | 
|  | 4212 |  | 
|  | 4213 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, | 
|  | 4214 | false).take(); | 
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4215 | if (!Arg) | 
|  | 4216 | return ExprError(); | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4217 | } | 
|  | 4218 |  | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4219 | // From here on out, all we care about are the unqualified forms | 
|  | 4220 | // of the parameter and argument types. | 
|  | 4221 | ParamType = ParamType.getUnqualifiedType(); | 
|  | 4222 | ArgType = ArgType.getUnqualifiedType(); | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4223 |  | 
|  | 4224 | // Try to convert the argument to the parameter's type. | 
| Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 4225 | if (Context.hasSameType(ParamType, ArgType)) { | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4226 | // Okay: no conversion necessary | 
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4227 | } else if (ParamType->isBooleanType()) { | 
|  | 4228 | // This is an integral-to-boolean conversion. | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4229 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4230 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || | 
|  | 4231 | !ParamType->isEnumeralType()) { | 
|  | 4232 | // This is an integral promotion or conversion. | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4233 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4234 | } else { | 
|  | 4235 | // We can't perform this conversion. | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4236 | Diag(Arg->getLocStart(), | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4237 | diag::err_template_arg_not_convertible) | 
| Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4238 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4239 | Diag(Param->getLocation(), diag::note_template_param_here); | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4240 | return ExprError(); | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4241 | } | 
|  | 4242 |  | 
| Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4243 | // Add the value of this argument to the list of converted | 
|  | 4244 | // arguments. We use the bitwidth and signedness of the template | 
|  | 4245 | // parameter. | 
|  | 4246 | if (Arg->isValueDependent()) { | 
|  | 4247 | // The argument is value-dependent. Create a new | 
|  | 4248 | // TemplateArgument with the converted expression. | 
|  | 4249 | Converted = TemplateArgument(Arg); | 
|  | 4250 | return Owned(Arg); | 
|  | 4251 | } | 
|  | 4252 |  | 
| Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4253 | QualType IntegerType = Context.getCanonicalType(ParamType); | 
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4254 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) | 
| Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4255 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); | 
| Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4256 |  | 
| Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4257 | if (ParamType->isBooleanType()) { | 
|  | 4258 | // Value must be zero or one. | 
|  | 4259 | Value = Value != 0; | 
|  | 4260 | unsigned AllowedBits = Context.getTypeSize(IntegerType); | 
|  | 4261 | if (Value.getBitWidth() != AllowedBits) | 
|  | 4262 | Value = Value.extOrTrunc(AllowedBits); | 
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4263 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); | 
| Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4264 | } else { | 
| Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4265 | llvm::APSInt OldValue = Value; | 
| Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4266 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | // Coerce the template argument's value to the value it will have | 
| Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4268 | // based on the template parameter's type. | 
| Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4269 | unsigned AllowedBits = Context.getTypeSize(IntegerType); | 
| Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4270 | if (Value.getBitWidth() != AllowedBits) | 
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4271 | Value = Value.extOrTrunc(AllowedBits); | 
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4272 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); | 
| Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4273 |  | 
| Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4274 | // Complain if an unsigned parameter received a negative value. | 
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4275 | if (IntegerType->isUnsignedIntegerOrEnumerationType() | 
| Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4276 | && (OldValue.isSigned() && OldValue.isNegative())) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4277 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) | 
| Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4278 | << OldValue.toString(10) << Value.toString(10) << Param->getType() | 
|  | 4279 | << Arg->getSourceRange(); | 
|  | 4280 | Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 4281 | } | 
| Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4282 |  | 
| Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4283 | // Complain if we overflowed the template parameter's type. | 
|  | 4284 | unsigned RequiredBits; | 
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4285 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) | 
| Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4286 | RequiredBits = OldValue.getActiveBits(); | 
|  | 4287 | else if (OldValue.isUnsigned()) | 
|  | 4288 | RequiredBits = OldValue.getActiveBits() + 1; | 
|  | 4289 | else | 
|  | 4290 | RequiredBits = OldValue.getMinSignedBits(); | 
|  | 4291 | if (RequiredBits > AllowedBits) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4292 | Diag(Arg->getLocStart(), | 
| Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4293 | diag::warn_template_arg_too_large) | 
|  | 4294 | << OldValue.toString(10) << Value.toString(10) << Param->getType() | 
|  | 4295 | << Arg->getSourceRange(); | 
|  | 4296 | Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 4297 | } | 
| Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4298 | } | 
| Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4299 |  | 
| Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4300 | Converted = TemplateArgument(Context, Value, | 
| Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4301 | ParamType->isEnumeralType() | 
|  | 4302 | ? Context.getCanonicalType(ParamType) | 
|  | 4303 | : IntegerType); | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4304 | return Owned(Arg); | 
| Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4305 | } | 
| Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4306 |  | 
| Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4307 | QualType ArgType = Arg->getType(); | 
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4308 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction | 
|  | 4309 |  | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4310 | // Handle pointer-to-function, reference-to-function, and | 
|  | 4311 | // pointer-to-member-function all in (roughly) the same way. | 
|  | 4312 | if (// -- For a non-type template-parameter of type pointer to | 
|  | 4313 | //    function, only the function-to-pointer conversion (4.3) is | 
|  | 4314 | //    applied. If the template-argument represents a set of | 
|  | 4315 | //    overloaded functions (or a pointer to such), the matching | 
|  | 4316 | //    function is selected from the set (13.4). | 
|  | 4317 | (ParamType->isPointerType() && | 
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4318 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4319 | // -- For a non-type template-parameter of type reference to | 
|  | 4320 | //    function, no conversions apply. If the template-argument | 
|  | 4321 | //    represents a set of overloaded functions, the matching | 
|  | 4322 | //    function is selected from the set (13.4). | 
|  | 4323 | (ParamType->isReferenceType() && | 
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4324 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4325 | // -- For a non-type template-parameter of type pointer to | 
|  | 4326 | //    member function, no conversions apply. If the | 
|  | 4327 | //    template-argument represents a set of overloaded member | 
|  | 4328 | //    functions, the matching member function is selected from | 
|  | 4329 | //    the set (13.4). | 
|  | 4330 | (ParamType->isMemberPointerType() && | 
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4331 | ParamType->getAs<MemberPointerType>()->getPointeeType() | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4332 | ->isFunctionType())) { | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4333 |  | 
| Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4334 | if (Arg->getType() == Context.OverloadTy) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, | 
| Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4336 | true, | 
|  | 4337 | FoundResult)) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4338 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4339 | return ExprError(); | 
| Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4340 |  | 
|  | 4341 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); | 
|  | 4342 | ArgType = Arg->getType(); | 
|  | 4343 | } else | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4344 | return ExprError(); | 
| Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4345 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 |  | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4347 | if (!ParamType->isMemberPointerType()) { | 
|  | 4348 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, | 
|  | 4349 | ParamType, | 
|  | 4350 | Arg, Converted)) | 
|  | 4351 | return ExprError(); | 
|  | 4352 | return Owned(Arg); | 
|  | 4353 | } | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4354 |  | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4355 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, | 
|  | 4356 | Converted)) | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4357 | return ExprError(); | 
|  | 4358 | return Owned(Arg); | 
| Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4359 | } | 
|  | 4360 |  | 
| Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4361 | if (ParamType->isPointerType()) { | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4362 | //   -- for a non-type template-parameter of type pointer to | 
|  | 4363 | //      object, qualification conversions (4.4) and the | 
|  | 4364 | //      array-to-pointer conversion (4.2) are applied. | 
| Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4365 | // C++0x also allows a value of std::nullptr_t. | 
| Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4366 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4367 | "Only object pointers allowed here"); | 
| Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4368 |  | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4369 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, | 
|  | 4370 | ParamType, | 
|  | 4371 | Arg, Converted)) | 
|  | 4372 | return ExprError(); | 
|  | 4373 | return Owned(Arg); | 
| Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4374 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4375 |  | 
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4376 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4377 | //   -- For a non-type template-parameter of type reference to | 
|  | 4378 | //      object, no conversions apply. The type referred to by the | 
|  | 4379 | //      reference may be more cv-qualified than the (otherwise | 
|  | 4380 | //      identical) type of the template-argument. The | 
|  | 4381 | //      template-parameter is bound directly to the | 
|  | 4382 | //      template-argument, which must be an lvalue. | 
| Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4383 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4384 | "Only object references allowed here"); | 
| Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4385 |  | 
| Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4386 | if (Arg->getType() == Context.OverloadTy) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4387 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, | 
|  | 4388 | ParamRefType->getPointeeType(), | 
| Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4389 | true, | 
|  | 4390 | FoundResult)) { | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4391 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4392 | return ExprError(); | 
| Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4393 |  | 
|  | 4394 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); | 
|  | 4395 | ArgType = Arg->getType(); | 
|  | 4396 | } else | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4397 | return ExprError(); | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4398 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4399 |  | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4400 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, | 
|  | 4401 | ParamType, | 
|  | 4402 | Arg, Converted)) | 
|  | 4403 | return ExprError(); | 
|  | 4404 | return Owned(Arg); | 
| Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4405 | } | 
| Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4406 |  | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4407 | // Deal with parameters of type std::nullptr_t. | 
|  | 4408 | if (ParamType->isNullPtrType()) { | 
|  | 4409 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { | 
|  | 4410 | Converted = TemplateArgument(Arg); | 
|  | 4411 | return Owned(Arg); | 
|  | 4412 | } | 
|  | 4413 |  | 
|  | 4414 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { | 
|  | 4415 | case NPV_NotNullPointer: | 
|  | 4416 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) | 
|  | 4417 | << Arg->getType() << ParamType; | 
|  | 4418 | Diag(Param->getLocation(), diag::note_template_param_here); | 
|  | 4419 | return ExprError(); | 
|  | 4420 |  | 
|  | 4421 | case NPV_Error: | 
|  | 4422 | return ExprError(); | 
|  | 4423 |  | 
|  | 4424 | case NPV_NullPointer: | 
| Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4425 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4426 | Converted = TemplateArgument(ParamType, /*isNullPtr*/true); | 
| Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4427 | return Owned(Arg); | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4428 | } | 
|  | 4429 | } | 
|  | 4430 |  | 
| Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4431 | //     -- For a non-type template-parameter of type pointer to data | 
|  | 4432 | //        member, qualification conversions (4.4) are applied. | 
|  | 4433 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); | 
|  | 4434 |  | 
| Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4435 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, | 
|  | 4436 | Converted)) | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4437 | return ExprError(); | 
|  | 4438 | return Owned(Arg); | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4439 | } | 
|  | 4440 |  | 
|  | 4441 | /// \brief Check a template argument against its corresponding | 
|  | 4442 | /// template template parameter. | 
|  | 4443 | /// | 
|  | 4444 | /// This routine implements the semantics of C++ [temp.arg.template]. | 
|  | 4445 | /// It returns true if an error occurred, and false otherwise. | 
|  | 4446 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4447 | const TemplateArgumentLoc &Arg, | 
|  | 4448 | unsigned ArgumentPackIndex) { | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4449 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4450 | TemplateDecl *Template = Name.getAsTemplateDecl(); | 
|  | 4451 | if (!Template) { | 
|  | 4452 | // Any dependent template name is fine. | 
|  | 4453 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); | 
|  | 4454 | return false; | 
|  | 4455 | } | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4456 |  | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4457 | // C++0x [temp.arg.template]p1: | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4458 | //   A template-argument for a template template-parameter shall be | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4459 | //   the name of a class template or an alias template, expressed as an | 
|  | 4460 | //   id-expression. When the template-argument names a class template, only | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4461 | //   primary class templates are considered when matching the | 
|  | 4462 | //   template template argument with the corresponding parameter; | 
|  | 4463 | //   partial specializations are not considered even if their | 
|  | 4464 | //   parameter lists match that of the template template parameter. | 
| Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4465 | // | 
|  | 4466 | // Note that we also allow template template parameters here, which | 
|  | 4467 | // will happen when we are dealing with, e.g., class template | 
|  | 4468 | // partial specializations. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | if (!isa<ClassTemplateDecl>(Template) && | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4470 | !isa<TemplateTemplateParmDecl>(Template) && | 
|  | 4471 | !isa<TypeAliasTemplateDecl>(Template)) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4472 | assert(isa<FunctionTemplateDecl>(Template) && | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4473 | "Only function templates are possible here"); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4474 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4475 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4476 | << Template; | 
|  | 4477 | } | 
|  | 4478 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4479 | TemplateParameterList *Params = Param->getTemplateParameters(); | 
|  | 4480 | if (Param->isExpandedParameterPack()) | 
|  | 4481 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); | 
|  | 4482 |  | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4483 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4484 | Params, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4485 | true, | 
| Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4486 | TPL_TemplateTemplateArgumentMatch, | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4487 | Arg.getLocation()); | 
| Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4488 | } | 
|  | 4489 |  | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4490 | /// \brief Given a non-type template argument that refers to a | 
|  | 4491 | /// declaration and the type of its corresponding non-type template | 
|  | 4492 | /// parameter, produce an expression that properly refers to that | 
|  | 4493 | /// declaration. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | ExprResult | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4495 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, | 
|  | 4496 | QualType ParamType, | 
|  | 4497 | SourceLocation Loc) { | 
| David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 4498 | // C++ [temp.param]p8: | 
|  | 4499 | // | 
|  | 4500 | //   A non-type template-parameter of type "array of T" or | 
|  | 4501 | //   "function returning T" is adjusted to be of type "pointer to | 
|  | 4502 | //   T" or "pointer to function returning T", respectively. | 
|  | 4503 | if (ParamType->isArrayType()) | 
|  | 4504 | ParamType = Context.getArrayDecayedType(ParamType); | 
|  | 4505 | else if (ParamType->isFunctionType()) | 
|  | 4506 | ParamType = Context.getPointerType(ParamType); | 
|  | 4507 |  | 
| Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4508 | // For a NULL non-type template argument, return nullptr casted to the | 
|  | 4509 | // parameter's type. | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4510 | if (Arg.getKind() == TemplateArgument::NullPtr) { | 
| Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4511 | return ImpCastExprToType( | 
|  | 4512 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), | 
|  | 4513 | ParamType, | 
|  | 4514 | ParamType->getAs<MemberPointerType>() | 
|  | 4515 | ? CK_NullToMemberPointer | 
|  | 4516 | : CK_NullToPointer); | 
|  | 4517 | } | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4518 | assert(Arg.getKind() == TemplateArgument::Declaration && | 
|  | 4519 | "Only declaration template arguments permitted here"); | 
|  | 4520 |  | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4521 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); | 
|  | 4522 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4523 | if (VD->getDeclContext()->isRecord() && | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4524 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { | 
|  | 4525 | // If the value is a class member, we might have a pointer-to-member. | 
|  | 4526 | // Determine whether the non-type template template parameter is of | 
|  | 4527 | // pointer-to-member type. If so, we need to build an appropriate | 
|  | 4528 | // expression for a pointer-to-member, since a "normal" DeclRefExpr | 
|  | 4529 | // would refer to the member itself. | 
|  | 4530 | if (ParamType->isMemberPointerType()) { | 
|  | 4531 | QualType ClassType | 
|  | 4532 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); | 
|  | 4533 | NestedNameSpecifier *Qualifier | 
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4534 | = NestedNameSpecifier::Create(Context, 0, false, | 
|  | 4535 | ClassType.getTypePtr()); | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4536 | CXXScopeSpec SS; | 
| Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4537 | SS.MakeTrivial(Context, Qualifier, Loc); | 
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4538 |  | 
|  | 4539 | // The actual value-ness of this is unimportant, but for | 
|  | 4540 | // internal consistency's sake, references to instance methods | 
|  | 4541 | // are r-values. | 
|  | 4542 | ExprValueKind VK = VK_LValue; | 
|  | 4543 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) | 
|  | 4544 | VK = VK_RValue; | 
|  | 4545 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | ExprResult RefExpr = BuildDeclRefExpr(VD, | 
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4547 | VD->getType().getNonReferenceType(), | 
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4548 | VK, | 
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4549 | Loc, | 
|  | 4550 | &SS); | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4551 | if (RefExpr.isInvalid()) | 
|  | 4552 | return ExprError(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 |  | 
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4554 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4555 |  | 
| Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4556 | // We might need to perform a trailing qualification conversion, since | 
|  | 4557 | // the element type on the parameter could be more qualified than the | 
|  | 4558 | // element type in the expression we constructed. | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4559 | bool ObjCLifetimeConversion; | 
| Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4560 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), | 
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4561 | ParamType.getUnqualifiedType(), false, | 
|  | 4562 | ObjCLifetimeConversion)) | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4563 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4564 |  | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4565 | assert(!RefExpr.isInvalid() && | 
|  | 4566 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), | 
| Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4567 | ParamType.getUnqualifiedType())); | 
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4568 | return RefExpr; | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4569 | } | 
|  | 4570 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4571 |  | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4572 | QualType T = VD->getType().getNonReferenceType(); | 
| Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 4573 |  | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4574 | if (ParamType->isPointerType()) { | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4575 | // When the non-type template parameter is a pointer, take the | 
|  | 4576 | // address of the declaration. | 
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4577 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4578 | if (RefExpr.isInvalid()) | 
|  | 4579 | return ExprError(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4580 |  | 
|  | 4581 | if (T->isFunctionType() || T->isArrayType()) { | 
|  | 4582 | // Decay functions and arrays. | 
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4583 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); | 
|  | 4584 | if (RefExpr.isInvalid()) | 
|  | 4585 | return ExprError(); | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4586 |  | 
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4587 | return RefExpr; | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4588 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4589 |  | 
| Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4590 | // Take the address of everything else | 
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4591 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4592 | } | 
|  | 4593 |  | 
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4594 | ExprValueKind VK = VK_RValue; | 
|  | 4595 |  | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4596 | // If the non-type template parameter has reference type, qualify the | 
|  | 4597 | // resulting declaration reference with the extra qualifiers on the | 
|  | 4598 | // type that the reference refers to. | 
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4599 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { | 
|  | 4600 | VK = VK_LValue; | 
|  | 4601 | T = Context.getQualifiedType(T, | 
|  | 4602 | TargetRef->getPointeeType().getQualifiers()); | 
| Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 4603 | } else if (isa<FunctionDecl>(VD)) { | 
|  | 4604 | // References to functions are always lvalues. | 
|  | 4605 | VK = VK_LValue; | 
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4606 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4607 |  | 
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4608 | return BuildDeclRefExpr(VD, T, VK, Loc); | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4609 | } | 
|  | 4610 |  | 
|  | 4611 | /// \brief Construct a new expression that refers to the given | 
|  | 4612 | /// integral template argument with the given source-location | 
|  | 4613 | /// information. | 
|  | 4614 | /// | 
|  | 4615 | /// This routine takes care of the mapping from an integral template | 
|  | 4616 | /// argument (which may have any integral type) to the appropriate | 
|  | 4617 | /// literal value. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | ExprResult | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4619 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, | 
|  | 4620 | SourceLocation Loc) { | 
|  | 4621 | assert(Arg.getKind() == TemplateArgument::Integral && | 
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4622 | "Operation is only valid for integral template arguments"); | 
| Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 4623 | QualType OrigT = Arg.getIntegralType(); | 
|  | 4624 |  | 
|  | 4625 | // If this is an enum type that we're instantiating, we need to use an integer | 
|  | 4626 | // type the same size as the enumerator.  We don't want to build an | 
|  | 4627 | // IntegerLiteral with enum type.  The integer type of an enum type can be of | 
|  | 4628 | // any integral type with C++11 enum classes, make sure we create the right | 
|  | 4629 | // type of literal for it. | 
|  | 4630 | QualType T = OrigT; | 
|  | 4631 | if (const EnumType *ET = OrigT->getAs<EnumType>()) | 
|  | 4632 | T = ET->getDecl()->getIntegerType(); | 
|  | 4633 |  | 
|  | 4634 | Expr *E; | 
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4635 | if (T->isAnyCharacterType()) { | 
|  | 4636 | CharacterLiteral::CharacterKind Kind; | 
|  | 4637 | if (T->isWideCharType()) | 
|  | 4638 | Kind = CharacterLiteral::Wide; | 
|  | 4639 | else if (T->isChar16Type()) | 
|  | 4640 | Kind = CharacterLiteral::UTF16; | 
|  | 4641 | else if (T->isChar32Type()) | 
|  | 4642 | Kind = CharacterLiteral::UTF32; | 
|  | 4643 | else | 
|  | 4644 | Kind = CharacterLiteral::Ascii; | 
|  | 4645 |  | 
| Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 4646 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), | 
|  | 4647 | Kind, T, Loc); | 
|  | 4648 | } else if (T->isBooleanType()) { | 
|  | 4649 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), | 
|  | 4650 | T, Loc); | 
|  | 4651 | } else if (T->isNullPtrType()) { | 
|  | 4652 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); | 
|  | 4653 | } else { | 
|  | 4654 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); | 
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4655 | } | 
|  | 4656 |  | 
| Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 4657 | if (OrigT->isEnumeralType()) { | 
| John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4658 | // FIXME: This is a hack. We need a better way to handle substituted | 
|  | 4659 | // non-type template parameters. | 
| Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 4660 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, 0, | 
|  | 4661 | Context.getTrivialTypeSourceInfo(OrigT, Loc), | 
| John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4662 | Loc, Loc); | 
|  | 4663 | } | 
|  | 4664 |  | 
|  | 4665 | return Owned(E); | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4666 | } | 
|  | 4667 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4668 | /// \brief Match two template parameters within template parameter lists. | 
|  | 4669 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, | 
|  | 4670 | bool Complain, | 
|  | 4671 | Sema::TemplateParameterListEqualKind Kind, | 
|  | 4672 | SourceLocation TemplateArgLoc) { | 
|  | 4673 | // Check the actual kind (type, non-type, template). | 
|  | 4674 | if (Old->getKind() != New->getKind()) { | 
|  | 4675 | if (Complain) { | 
|  | 4676 | unsigned NextDiag = diag::err_template_param_different_kind; | 
|  | 4677 | if (TemplateArgLoc.isValid()) { | 
|  | 4678 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); | 
|  | 4679 | NextDiag = diag::note_template_param_different_kind; | 
|  | 4680 | } | 
|  | 4681 | S.Diag(New->getLocation(), NextDiag) | 
|  | 4682 | << (Kind != Sema::TPL_TemplateMatch); | 
|  | 4683 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) | 
|  | 4684 | << (Kind != Sema::TPL_TemplateMatch); | 
|  | 4685 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4686 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4687 | return false; | 
|  | 4688 | } | 
|  | 4689 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4690 | // Check that both are parameter packs are neither are parameter packs. | 
|  | 4691 | // However, if we are matching a template template argument to a | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4692 | // template template parameter, the template template parameter can have | 
|  | 4693 | // a parameter pack where the template template argument does not. | 
|  | 4694 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && | 
|  | 4695 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && | 
|  | 4696 | Old->isTemplateParameterPack())) { | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4697 | if (Complain) { | 
|  | 4698 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; | 
|  | 4699 | if (TemplateArgLoc.isValid()) { | 
|  | 4700 | S.Diag(TemplateArgLoc, | 
|  | 4701 | diag::err_template_arg_template_params_mismatch); | 
|  | 4702 | NextDiag = diag::note_template_parameter_pack_non_pack; | 
|  | 4703 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4704 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4705 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 | 
|  | 4706 | : isa<NonTypeTemplateParmDecl>(New)? 1 | 
|  | 4707 | : 2; | 
|  | 4708 | S.Diag(New->getLocation(), NextDiag) | 
|  | 4709 | << ParamKind << New->isParameterPack(); | 
|  | 4710 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) | 
|  | 4711 | << ParamKind << Old->isParameterPack(); | 
|  | 4712 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4714 | return false; | 
|  | 4715 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4717 | // For non-type template parameters, check the type of the parameter. | 
|  | 4718 | if (NonTypeTemplateParmDecl *OldNTTP | 
|  | 4719 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { | 
|  | 4720 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4721 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4722 | // If we are matching a template template argument to a template | 
|  | 4723 | // template parameter and one of the non-type template parameter types | 
|  | 4724 | // is dependent, then we must wait until template instantiation time | 
|  | 4725 | // to actually compare the arguments. | 
|  | 4726 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && | 
|  | 4727 | (OldNTTP->getType()->isDependentType() || | 
|  | 4728 | NewNTTP->getType()->isDependentType())) | 
|  | 4729 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4730 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4731 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { | 
|  | 4732 | if (Complain) { | 
|  | 4733 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; | 
|  | 4734 | if (TemplateArgLoc.isValid()) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4735 | S.Diag(TemplateArgLoc, | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4736 | diag::err_template_arg_template_params_mismatch); | 
|  | 4737 | NextDiag = diag::note_template_nontype_parm_different_type; | 
|  | 4738 | } | 
|  | 4739 | S.Diag(NewNTTP->getLocation(), NextDiag) | 
|  | 4740 | << NewNTTP->getType() | 
|  | 4741 | << (Kind != Sema::TPL_TemplateMatch); | 
|  | 4742 | S.Diag(OldNTTP->getLocation(), | 
|  | 4743 | diag::note_template_nontype_parm_prev_declaration) | 
|  | 4744 | << OldNTTP->getType(); | 
|  | 4745 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4746 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4747 | return false; | 
|  | 4748 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4749 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4750 | return true; | 
|  | 4751 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4752 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4753 | // For template template parameters, check the template parameter types. | 
|  | 4754 | // The template parameter lists of template template | 
|  | 4755 | // parameters must agree. | 
|  | 4756 | if (TemplateTemplateParmDecl *OldTTP | 
|  | 4757 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4758 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4759 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), | 
|  | 4760 | OldTTP->getTemplateParameters(), | 
|  | 4761 | Complain, | 
|  | 4762 | (Kind == Sema::TPL_TemplateMatch | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4763 | ? Sema::TPL_TemplateTemplateParmMatch | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4764 | : Kind), | 
|  | 4765 | TemplateArgLoc); | 
|  | 4766 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4767 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4768 | return true; | 
|  | 4769 | } | 
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4770 |  | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4771 | /// \brief Diagnose a known arity mismatch when comparing template argument | 
|  | 4772 | /// lists. | 
|  | 4773 | static | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4775 | TemplateParameterList *New, | 
|  | 4776 | TemplateParameterList *Old, | 
|  | 4777 | Sema::TemplateParameterListEqualKind Kind, | 
|  | 4778 | SourceLocation TemplateArgLoc) { | 
|  | 4779 | unsigned NextDiag = diag::err_template_param_list_different_arity; | 
|  | 4780 | if (TemplateArgLoc.isValid()) { | 
|  | 4781 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); | 
|  | 4782 | NextDiag = diag::note_template_param_list_different_arity; | 
|  | 4783 | } | 
|  | 4784 | S.Diag(New->getTemplateLoc(), NextDiag) | 
|  | 4785 | << (New->size() > Old->size()) | 
|  | 4786 | << (Kind != Sema::TPL_TemplateMatch) | 
|  | 4787 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); | 
|  | 4788 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) | 
|  | 4789 | << (Kind != Sema::TPL_TemplateMatch) | 
|  | 4790 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); | 
|  | 4791 | } | 
|  | 4792 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4793 | /// \brief Determine whether the given template parameter lists are | 
|  | 4794 | /// equivalent. | 
|  | 4795 | /// | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | /// \param New  The new template parameter list, typically written in the | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4797 | /// source code as part of a new template declaration. | 
|  | 4798 | /// | 
|  | 4799 | /// \param Old  The old template parameter list, typically found via | 
|  | 4800 | /// name lookup of the template declared with this template parameter | 
|  | 4801 | /// list. | 
|  | 4802 | /// | 
|  | 4803 | /// \param Complain  If true, this routine will produce a diagnostic if | 
|  | 4804 | /// the template parameter lists are not equivalent. | 
|  | 4805 | /// | 
| Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4806 | /// \param Kind describes how we are to match the template parameter lists. | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4807 | /// | 
|  | 4808 | /// \param TemplateArgLoc If this source location is valid, then we | 
|  | 4809 | /// are actually checking the template parameter list of a template | 
|  | 4810 | /// argument (New) against the template parameter list of its | 
|  | 4811 | /// corresponding template template parameter (Old). We produce | 
|  | 4812 | /// slightly different diagnostics in this scenario. | 
|  | 4813 | /// | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4814 | /// \returns True if the template parameter lists are equal, false | 
|  | 4815 | /// otherwise. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | bool | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4817 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, | 
|  | 4818 | TemplateParameterList *Old, | 
|  | 4819 | bool Complain, | 
| Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4820 | TemplateParameterListEqualKind Kind, | 
| Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4821 | SourceLocation TemplateArgLoc) { | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4822 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { | 
|  | 4823 | if (Complain) | 
|  | 4824 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, | 
|  | 4825 | TemplateArgLoc); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4826 |  | 
|  | 4827 | return false; | 
|  | 4828 | } | 
|  | 4829 |  | 
| Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4830 | // C++0x [temp.arg.template]p3: | 
|  | 4831 | //   A template-argument matches a template template-parameter (call it P) | 
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4832 | //   when each of the template parameters in the template-parameter-list of | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4833 | //   the template-argument's corresponding class template or alias template | 
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4834 | //   (call it A) matches the corresponding template parameter in the | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4835 | //   template-parameter-list of P. [...] | 
|  | 4836 | TemplateParameterList::iterator NewParm = New->begin(); | 
|  | 4837 | TemplateParameterList::iterator NewParmEnd = New->end(); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4838 | for (TemplateParameterList::iterator OldParm = Old->begin(), | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4839 | OldParmEnd = Old->end(); | 
|  | 4840 | OldParm != OldParmEnd; ++OldParm) { | 
| Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4841 | if (Kind != TPL_TemplateTemplateArgumentMatch || | 
|  | 4842 | !(*OldParm)->isTemplateParameterPack()) { | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4843 | if (NewParm == NewParmEnd) { | 
|  | 4844 | if (Complain) | 
|  | 4845 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, | 
|  | 4846 | TemplateArgLoc); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4847 |  | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4848 | return false; | 
|  | 4849 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4850 |  | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4851 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, | 
|  | 4852 | Kind, TemplateArgLoc)) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4853 | return false; | 
|  | 4854 |  | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4855 | ++NewParm; | 
|  | 4856 | continue; | 
|  | 4857 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4858 |  | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4859 | // C++0x [temp.arg.template]p3: | 
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4860 | //   [...] When P's template- parameter-list contains a template parameter | 
|  | 4861 | //   pack (14.5.3), the template parameter pack will match zero or more | 
|  | 4862 | //   template parameters or template parameter packs in the | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4863 | //   template-parameter-list of A with the same type and form as the | 
|  | 4864 | //   template parameter pack in P (ignoring whether those template | 
|  | 4865 | //   parameters are template parameter packs). | 
|  | 4866 | for (; NewParm != NewParmEnd; ++NewParm) { | 
|  | 4867 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, | 
|  | 4868 | Kind, TemplateArgLoc)) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4869 | return false; | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4870 | } | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4871 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4872 |  | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4873 | // Make sure we exhausted all of the arguments. | 
|  | 4874 | if (NewParm != NewParmEnd) { | 
|  | 4875 | if (Complain) | 
|  | 4876 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, | 
|  | 4877 | TemplateArgLoc); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 |  | 
| Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4879 | return false; | 
|  | 4880 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4881 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4882 | return true; | 
|  | 4883 | } | 
|  | 4884 |  | 
|  | 4885 | /// \brief Check whether a template can be declared within this scope. | 
|  | 4886 | /// | 
|  | 4887 | /// If the template declaration is valid in this scope, returns | 
|  | 4888 | /// false. Otherwise, issues a diagnostic and returns true. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4889 | bool | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4890 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { | 
| Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4891 | if (!S) | 
|  | 4892 | return false; | 
|  | 4893 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4894 | // Find the nearest enclosing declaration scope. | 
|  | 4895 | while ((S->getFlags() & Scope::DeclScope) == 0 || | 
|  | 4896 | (S->getFlags() & Scope::TemplateParamScope) != 0) | 
|  | 4897 | S = S->getParent(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4898 |  | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4899 | // C++ [temp]p2: | 
|  | 4900 | //   A template-declaration can appear only as a namespace scope or | 
|  | 4901 | //   class scope declaration. | 
|  | 4902 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); | 
| Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4903 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && | 
|  | 4904 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4905 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4906 | << TemplateParams->getSourceRange(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4907 |  | 
| Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4908 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4909 | Ctx = Ctx->getParent(); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4910 |  | 
|  | 4911 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) | 
|  | 4912 | return false; | 
|  | 4913 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4914 | return Diag(TemplateParams->getTemplateLoc(), | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4915 | diag::err_template_outside_namespace_or_class_scope) | 
|  | 4916 | << TemplateParams->getSourceRange(); | 
| Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4917 | } | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4918 |  | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4919 | /// \brief Determine what kind of template specialization the given declaration | 
|  | 4920 | /// is. | 
| Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4921 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4922 | if (!D) | 
|  | 4923 | return TSK_Undeclared; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 |  | 
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4925 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) | 
|  | 4926 | return Record->getTemplateSpecializationKind(); | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4927 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) | 
|  | 4928 | return Function->getTemplateSpecializationKind(); | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4929 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) | 
|  | 4930 | return Var->getTemplateSpecializationKind(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4931 |  | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4932 | return TSK_Undeclared; | 
|  | 4933 | } | 
|  | 4934 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | /// \brief Check whether a specialization is well-formed in the current | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4936 | /// context. | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4937 | /// | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4938 | /// This routine determines whether a template specialization can be declared | 
|  | 4939 | /// in the current context (C++ [temp.expl.spec]p2). | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4940 | /// | 
|  | 4941 | /// \param S the semantic analysis object for which this check is being | 
|  | 4942 | /// performed. | 
|  | 4943 | /// | 
|  | 4944 | /// \param Specialized the entity being specialized or instantiated, which | 
|  | 4945 | /// may be a kind of template (class template, function template, etc.) or | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4946 | /// a member of a class template (member function, static data member, | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4947 | /// member class). | 
|  | 4948 | /// | 
|  | 4949 | /// \param PrevDecl the previous declaration of this entity, if any. | 
|  | 4950 | /// | 
|  | 4951 | /// \param Loc the location of the explicit specialization or instantiation of | 
|  | 4952 | /// this entity. | 
|  | 4953 | /// | 
|  | 4954 | /// \param IsPartialSpecialization whether this is a partial specialization of | 
|  | 4955 | /// a class template. | 
|  | 4956 | /// | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4957 | /// \returns true if there was an error that we cannot recover from, false | 
|  | 4958 | /// otherwise. | 
|  | 4959 | static bool CheckTemplateSpecializationScope(Sema &S, | 
|  | 4960 | NamedDecl *Specialized, | 
|  | 4961 | NamedDecl *PrevDecl, | 
|  | 4962 | SourceLocation Loc, | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4963 | bool IsPartialSpecialization) { | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4964 | // Keep these "kind" numbers in sync with the %select statements in the | 
|  | 4965 | // various diagnostics emitted by this routine. | 
|  | 4966 | int EntityKind = 0; | 
| Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4967 | if (isa<ClassTemplateDecl>(Specialized)) | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4968 | EntityKind = IsPartialSpecialization? 1 : 0; | 
| Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4969 | else if (isa<FunctionTemplateDecl>(Specialized)) | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4970 | EntityKind = 2; | 
| Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4971 | else if (isa<CXXMethodDecl>(Specialized)) | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4972 | EntityKind = 3; | 
|  | 4973 | else if (isa<VarDecl>(Specialized)) | 
|  | 4974 | EntityKind = 4; | 
|  | 4975 | else if (isa<RecordDecl>(Specialized)) | 
|  | 4976 | EntityKind = 5; | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4977 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) | 
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4978 | EntityKind = 6; | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4979 | else { | 
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4980 | S.Diag(Loc, diag::err_template_spec_unknown_kind) | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4981 | << S.getLangOpts().CPlusPlus11; | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4982 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4983 | return true; | 
|  | 4984 | } | 
|  | 4985 |  | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4986 | // C++ [temp.expl.spec]p2: | 
|  | 4987 | //   An explicit specialization shall be declared in the namespace | 
|  | 4988 | //   of which the template is a member, or, for member templates, in | 
|  | 4989 | //   the namespace of which the enclosing class or enclosing class | 
|  | 4990 | //   template is a member. An explicit specialization of a member | 
|  | 4991 | //   function, member class or static data member of a class | 
|  | 4992 | //   template shall be declared in the namespace of which the class | 
|  | 4993 | //   template is a member. Such a declaration may also be a | 
|  | 4994 | //   definition. If the declaration is not a definition, the | 
|  | 4995 | //   specialization may be defined later in the name- space in which | 
|  | 4996 | //   the explicit specialization was declared, or in a namespace | 
|  | 4997 | //   that encloses the one in which the explicit specialization was | 
|  | 4998 | //   declared. | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4999 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5000 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5001 | << Specialized; | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5002 | return true; | 
|  | 5003 | } | 
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5004 |  | 
| Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5005 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5006 | if (S.getLangOpts().MicrosoftExt) { | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5007 | // Do not warn for class scope explicit specialization during | 
|  | 5008 | // instantiation, warning was already emitted during pattern | 
|  | 5009 | // semantic analysis. | 
|  | 5010 | if (!S.ActiveTemplateInstantiations.size()) | 
|  | 5011 | S.Diag(Loc, diag::ext_function_specialization_in_class) | 
|  | 5012 | << Specialized; | 
|  | 5013 | } else { | 
|  | 5014 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) | 
|  | 5015 | << Specialized; | 
|  | 5016 | return true; | 
|  | 5017 | } | 
| Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5018 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5019 |  | 
| Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5020 | if (S.CurContext->isRecord() && | 
|  | 5021 | !S.CurContext->Equals(Specialized->getDeclContext())) { | 
|  | 5022 | // Make sure that we're specializing in the right record context. | 
|  | 5023 | // Otherwise, things can go horribly wrong. | 
|  | 5024 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) | 
|  | 5025 | << Specialized; | 
|  | 5026 | return true; | 
|  | 5027 | } | 
|  | 5028 |  | 
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5029 | // C++ [temp.class.spec]p6: | 
|  | 5030 | //   A class template partial specialization may be declared or redeclared | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | //   in any namespace scope in which its definition may be defined (14.5.1 | 
|  | 5032 | //   and 14.5.2). | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5033 | bool ComplainedAboutScope = false; | 
| Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5034 | DeclContext *SpecializedContext | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5035 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); | 
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5036 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5037 | if ((!PrevDecl || | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5038 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || | 
|  | 5039 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ | 
| Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5040 | // C++ [temp.exp.spec]p2: | 
|  | 5041 | //   An explicit specialization shall be declared in the namespace of which | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5042 | //   the template is a member, or, for member templates, in the namespace | 
| Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5043 | //   of which the enclosing class or enclosing class template is a member. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5044 | //   An explicit specialization of a member function, member class or | 
|  | 5045 | //   static data member of a class template shall be declared in the | 
| Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5046 | //   namespace of which the class template is a member. | 
|  | 5047 | // | 
|  | 5048 | // C++0x [temp.expl.spec]p2: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | //   An explicit specialization shall be declared in a namespace enclosing | 
| Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5050 | //   the specialized template. | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5051 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5052 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5053 | if (isa<TranslationUnitDecl>(SpecializedContext)) { | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5054 | assert(!IsCPlusPlus11Extension && | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5055 | "DC encloses TU but isn't in enclosing namespace set"); | 
|  | 5056 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) | 
| Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5057 | << EntityKind << Specialized; | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5058 | } else if (isa<NamespaceDecl>(SpecializedContext)) { | 
|  | 5059 | int Diag; | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5060 | if (!IsCPlusPlus11Extension) | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5061 | Diag = diag::err_template_spec_decl_out_of_scope; | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5062 | else if (!S.getLangOpts().CPlusPlus11) | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5063 | Diag = diag::ext_template_spec_decl_out_of_scope; | 
|  | 5064 | else | 
|  | 5065 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; | 
|  | 5066 | S.Diag(Loc, Diag) | 
|  | 5067 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); | 
|  | 5068 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5069 |  | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5070 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5071 | ComplainedAboutScope = | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5072 | !(IsCPlusPlus11Extension && S.getLangOpts().CPlusPlus11); | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5073 | } | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5074 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5075 |  | 
|  | 5076 | // Make sure that this redeclaration (or definition) occurs in an enclosing | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5077 | // namespace. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5078 | // Note that HandleDeclarator() performs this check for explicit | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5079 | // specializations of function templates, static data members, and member | 
|  | 5080 | // functions, so we skip the check here for those kinds of entities. | 
|  | 5081 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. | 
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5082 | // Should we refactor that check, so that it occurs later? | 
|  | 5083 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5084 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || | 
|  | 5085 | isa<FunctionDecl>(Specialized))) { | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5086 | if (isa<TranslationUnitDecl>(SpecializedContext)) | 
|  | 5087 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) | 
|  | 5088 | << EntityKind << Specialized; | 
|  | 5089 | else if (isa<NamespaceDecl>(SpecializedContext)) | 
|  | 5090 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) | 
|  | 5091 | << EntityKind << Specialized | 
|  | 5092 | << cast<NamedDecl>(SpecializedContext); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5093 |  | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5094 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5095 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5096 |  | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5097 | // FIXME: check for specialization-after-instantiation errors and such. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5098 |  | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5099 | return false; | 
|  | 5100 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5101 |  | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5102 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs | 
|  | 5103 | /// that checks non-type template partial specialization arguments. | 
|  | 5104 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, | 
|  | 5105 | NonTypeTemplateParmDecl *Param, | 
|  | 5106 | const TemplateArgument *Args, | 
|  | 5107 | unsigned NumArgs) { | 
|  | 5108 | for (unsigned I = 0; I != NumArgs; ++I) { | 
|  | 5109 | if (Args[I].getKind() == TemplateArgument::Pack) { | 
|  | 5110 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5111 | Args[I].pack_begin(), | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5112 | Args[I].pack_size())) | 
|  | 5113 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5114 |  | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5115 | continue; | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5116 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5117 |  | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5118 | if (Args[I].getKind() != TemplateArgument::Expression) | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5119 | continue; | 
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5120 |  | 
|  | 5121 | Expr *ArgExpr = Args[I].getAsExpr(); | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5122 |  | 
| Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 5123 | // We can have a pack expansion of any of the bullets below. | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5124 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) | 
|  | 5125 | ArgExpr = Expansion->getPattern(); | 
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5126 |  | 
|  | 5127 | // Strip off any implicit casts we added as part of type checking. | 
|  | 5128 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) | 
|  | 5129 | ArgExpr = ICE->getSubExpr(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5130 |  | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5131 | // C++ [temp.class.spec]p8: | 
|  | 5132 | //   A non-type argument is non-specialized if it is the name of a | 
|  | 5133 | //   non-type parameter. All other non-type arguments are | 
|  | 5134 | //   specialized. | 
|  | 5135 | // | 
|  | 5136 | // Below, we check the two conditions that only apply to | 
|  | 5137 | // specialized non-type arguments, so skip any non-specialized | 
|  | 5138 | // arguments. | 
|  | 5139 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) | 
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5140 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5141 | continue; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5142 |  | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5143 | // C++ [temp.class.spec]p9: | 
|  | 5144 | //   Within the argument list of a class template partial | 
|  | 5145 | //   specialization, the following restrictions apply: | 
|  | 5146 | //     -- A partially specialized non-type argument expression | 
|  | 5147 | //        shall not involve a template parameter of the partial | 
|  | 5148 | //        specialization except when the argument expression is a | 
|  | 5149 | //        simple identifier. | 
|  | 5150 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5151 | S.Diag(ArgExpr->getLocStart(), | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5152 | diag::err_dependent_non_type_arg_in_partial_spec) | 
|  | 5153 | << ArgExpr->getSourceRange(); | 
|  | 5154 | return true; | 
|  | 5155 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5156 |  | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5157 | //     -- The type of a template parameter corresponding to a | 
|  | 5158 | //        specialized non-type argument shall not be dependent on a | 
|  | 5159 | //        parameter of the specialization. | 
|  | 5160 | if (Param->getType()->isDependentType()) { | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5161 | S.Diag(ArgExpr->getLocStart(), | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5162 | diag::err_dependent_typed_non_type_arg_in_partial_spec) | 
|  | 5163 | << Param->getType() | 
|  | 5164 | << ArgExpr->getSourceRange(); | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5165 | S.Diag(Param->getLocation(), diag::note_template_param_here); | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5166 | return true; | 
|  | 5167 | } | 
|  | 5168 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5169 |  | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5170 | return false; | 
|  | 5171 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5172 |  | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5173 | /// \brief Check the non-type template arguments of a class template | 
|  | 5174 | /// partial specialization according to C++ [temp.class.spec]p9. | 
|  | 5175 | /// | 
|  | 5176 | /// \param TemplateParams the template parameters of the primary class | 
|  | 5177 | /// template. | 
|  | 5178 | /// | 
| James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5179 | /// \param TemplateArgs the template arguments of the class template | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5180 | /// partial specialization. | 
|  | 5181 | /// | 
|  | 5182 | /// \returns true if there was an error, false otherwise. | 
|  | 5183 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, | 
|  | 5184 | TemplateParameterList *TemplateParams, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5185 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5186 | const TemplateArgument *ArgList = TemplateArgs.data(); | 
|  | 5187 |  | 
|  | 5188 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { | 
|  | 5189 | NonTypeTemplateParmDecl *Param | 
|  | 5190 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); | 
|  | 5191 | if (!Param) | 
|  | 5192 | continue; | 
|  | 5193 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5194 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5195 | &ArgList[I], 1)) | 
|  | 5196 | return true; | 
|  | 5197 | } | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5198 |  | 
|  | 5199 | return false; | 
|  | 5200 | } | 
|  | 5201 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5202 | DeclResult | 
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5203 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, | 
|  | 5204 | TagUseKind TUK, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5205 | SourceLocation KWLoc, | 
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5206 | SourceLocation ModulePrivateLoc, | 
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5207 | CXXScopeSpec &SS, | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5208 | TemplateTy TemplateD, | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5209 | SourceLocation TemplateNameLoc, | 
|  | 5210 | SourceLocation LAngleLoc, | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5211 | ASTTemplateArgsPtr TemplateArgsIn, | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5212 | SourceLocation RAngleLoc, | 
|  | 5213 | AttributeList *Attr, | 
|  | 5214 | MultiTemplateParamsArg TemplateParameterLists) { | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5215 | assert(TUK != TUK_Reference && "References are not specializations"); | 
| John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 5216 |  | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5217 | // NOTE: KWLoc is the location of the tag keyword. This will instead | 
|  | 5218 | // store the location of the outermost template keyword in the declaration. | 
|  | 5219 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 | 
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5220 | ? TemplateParameterLists[0]->getTemplateLoc() : SourceLocation(); | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5221 |  | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5222 | // Find the class template we're specializing | 
| Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5223 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5224 | ClassTemplateDecl *ClassTemplate | 
| Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5225 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); | 
|  | 5226 |  | 
|  | 5227 | if (!ClassTemplate) { | 
|  | 5228 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5229 | << (Name.getAsTemplateDecl() && | 
| Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5230 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); | 
|  | 5231 | return true; | 
|  | 5232 | } | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5233 |  | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5234 | bool isExplicitSpecialization = false; | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5235 | bool isPartialSpecialization = false; | 
|  | 5236 |  | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5237 | // Check the validity of the template headers that introduce this | 
|  | 5238 | // template. | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5239 | // FIXME: We probably shouldn't complain about these headers for | 
|  | 5240 | // friend declarations. | 
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5241 | bool Invalid = false; | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5242 | TemplateParameterList *TemplateParams | 
| Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 5243 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, | 
|  | 5244 | TemplateNameLoc, | 
|  | 5245 | SS, | 
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5246 | TemplateParameterLists.data(), | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5247 | TemplateParameterLists.size(), | 
| John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5248 | TUK == TUK_Friend, | 
| Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5249 | isExplicitSpecialization, | 
|  | 5250 | Invalid); | 
|  | 5251 | if (Invalid) | 
|  | 5252 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5253 |  | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5254 | if (TemplateParams && TemplateParams->size() > 0) { | 
|  | 5255 | isPartialSpecialization = true; | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5256 |  | 
| Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 5257 | if (TUK == TUK_Friend) { | 
|  | 5258 | Diag(KWLoc, diag::err_partial_specialization_friend) | 
|  | 5259 | << SourceRange(LAngleLoc, RAngleLoc); | 
|  | 5260 | return true; | 
|  | 5261 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 |  | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5263 | // C++ [temp.class.spec]p10: | 
|  | 5264 | //   The template parameter list of a specialization shall not | 
|  | 5265 | //   contain default template argument values. | 
|  | 5266 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { | 
|  | 5267 | Decl *Param = TemplateParams->getParam(I); | 
|  | 5268 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { | 
|  | 5269 | if (TTP->hasDefaultArgument()) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5270 | Diag(TTP->getDefaultArgumentLoc(), | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5271 | diag::err_default_arg_in_partial_spec); | 
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5272 | TTP->removeDefaultArgument(); | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5273 | } | 
|  | 5274 | } else if (NonTypeTemplateParmDecl *NTTP | 
|  | 5275 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { | 
|  | 5276 | if (Expr *DefArg = NTTP->getDefaultArgument()) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | Diag(NTTP->getDefaultArgumentLoc(), | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5278 | diag::err_default_arg_in_partial_spec) | 
|  | 5279 | << DefArg->getSourceRange(); | 
| Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5280 | NTTP->removeDefaultArgument(); | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5281 | } | 
|  | 5282 | } else { | 
|  | 5283 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5284 | if (TTP->hasDefaultArgument()) { | 
|  | 5285 | Diag(TTP->getDefaultArgument().getLocation(), | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5286 | diag::err_default_arg_in_partial_spec) | 
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5287 | << TTP->getDefaultArgument().getSourceRange(); | 
| Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5288 | TTP->removeDefaultArgument(); | 
| Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5289 | } | 
|  | 5290 | } | 
|  | 5291 | } | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5292 | } else if (TemplateParams) { | 
|  | 5293 | if (TUK == TUK_Friend) | 
|  | 5294 | Diag(KWLoc, diag::err_template_spec_friend) | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5295 | << FixItHint::CreateRemoval( | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5296 | SourceRange(TemplateParams->getTemplateLoc(), | 
|  | 5297 | TemplateParams->getRAngleLoc())) | 
|  | 5298 | << SourceRange(LAngleLoc, RAngleLoc); | 
|  | 5299 | else | 
|  | 5300 | isExplicitSpecialization = true; | 
|  | 5301 | } else if (TUK != TUK_Friend) { | 
| Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5302 | Diag(KWLoc, diag::err_template_spec_needs_header) | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5303 | << FixItHint::CreateInsertion(KWLoc, "template<> "); | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5304 | isExplicitSpecialization = true; | 
|  | 5305 | } | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5306 |  | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5307 | // Check that the specialization uses the same tag kind as the | 
|  | 5308 | // original template. | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5309 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | 
|  | 5310 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); | 
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5311 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), | 
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5312 | Kind, TUK == TUK_Definition, KWLoc, | 
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5313 | *ClassTemplate->getIdentifier())) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5314 | Diag(KWLoc, diag::err_use_with_wrong_tag) | 
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5315 | << ClassTemplate | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5316 | << FixItHint::CreateReplacement(KWLoc, | 
| Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5317 | ClassTemplate->getTemplatedDecl()->getKindName()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5318 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5319 | diag::note_previous_use); | 
|  | 5320 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); | 
|  | 5321 | } | 
|  | 5322 |  | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5323 | // Translate the parser's template argument list in our AST format. | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5324 | TemplateArgumentListInfo TemplateArgs; | 
|  | 5325 | TemplateArgs.setLAngleLoc(LAngleLoc); | 
|  | 5326 | TemplateArgs.setRAngleLoc(RAngleLoc); | 
| Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5327 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | 
| Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5328 |  | 
| Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5329 | // Check for unexpanded parameter packs in any of the template arguments. | 
|  | 5330 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5331 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], | 
| Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5332 | UPPC_PartialSpecialization)) | 
|  | 5333 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5334 |  | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5335 | // Check that the template argument list is well-formed for this | 
|  | 5336 | // template. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5337 | SmallVector<TemplateArgument, 4> Converted; | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5338 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, | 
|  | 5339 | TemplateArgs, false, Converted)) | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5340 | return true; | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5341 |  | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5342 | // Find the class template (partial) specialization declaration that | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5343 | // corresponds to these arguments. | 
| Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5344 | if (isPartialSpecialization) { | 
| Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5345 | if (CheckClassTemplatePartialSpecializationArgs(*this, | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5346 | ClassTemplate->getTemplateParameters(), | 
| Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5347 | Converted)) | 
| Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5348 | return true; | 
|  | 5349 |  | 
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5350 | bool InstantiationDependent; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5351 | if (!Name.isDependent() && | 
| Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5352 | !TemplateSpecializationType::anyDependentTemplateArguments( | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5353 | TemplateArgs.getArgumentArray(), | 
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5354 | TemplateArgs.size(), | 
|  | 5355 | InstantiationDependent)) { | 
| Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5356 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) | 
|  | 5357 | << ClassTemplate->getDeclName(); | 
|  | 5358 | isPartialSpecialization = false; | 
| Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5359 | } | 
|  | 5360 | } | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5361 |  | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5362 | void *InsertPos = 0; | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5363 | ClassTemplateSpecializationDecl *PrevDecl = 0; | 
|  | 5364 |  | 
|  | 5365 | if (isPartialSpecialization) | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5366 | // FIXME: Template parameter list matters, too | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5367 | PrevDecl | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5368 | = ClassTemplate->findPartialSpecialization(Converted.data(), | 
|  | 5369 | Converted.size(), | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5370 | InsertPos); | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5371 | else | 
|  | 5372 | PrevDecl | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5373 | = ClassTemplate->findSpecialization(Converted.data(), | 
|  | 5374 | Converted.size(), InsertPos); | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5375 |  | 
|  | 5376 | ClassTemplateSpecializationDecl *Specialization = 0; | 
|  | 5377 |  | 
| Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5378 | // Check whether we can declare a class template specialization in | 
|  | 5379 | // the current scope. | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5380 | if (TUK != TUK_Friend && | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, | 
|  | 5382 | TemplateNameLoc, | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5383 | isPartialSpecialization)) | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5384 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5385 |  | 
| Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5386 | // The canonical type | 
|  | 5387 | QualType CanonType; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5388 | if (PrevDecl && | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5389 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || | 
| Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5390 | TUK == TUK_Friend)) { | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5391 | // Since the only prior class template specialization with these | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5392 | // arguments was referenced but not declared, or we're only | 
|  | 5393 | // referencing this specialization as a friend, reuse that | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5394 | // declaration node as our own, updating its source location and | 
|  | 5395 | // the list of outer template parameters to reflect our new declaration. | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5396 | Specialization = PrevDecl; | 
| Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5397 | Specialization->setLocation(TemplateNameLoc); | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5398 | if (TemplateParameterLists.size() > 0) { | 
|  | 5399 | Specialization->setTemplateParameterListsInfo(Context, | 
|  | 5400 | TemplateParameterLists.size(), | 
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5401 | TemplateParameterLists.data()); | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5402 | } | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5403 | PrevDecl = 0; | 
| Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5404 | CanonType = Context.getTypeDeclType(Specialization); | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5405 | } else if (isPartialSpecialization) { | 
| Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5406 | // Build the canonical type that describes the converted template | 
|  | 5407 | // arguments of the class template partial specialization. | 
| Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5408 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); | 
|  | 5409 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, | 
| Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5410 | Converted.data(), | 
|  | 5411 | Converted.size()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5412 |  | 
|  | 5413 | if (Context.hasSameType(CanonType, | 
| Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5414 | ClassTemplate->getInjectedClassNameSpecialization())) { | 
|  | 5415 | // C++ [temp.class.spec]p9b3: | 
|  | 5416 | // | 
|  | 5417 | //   -- The argument list of the specialization shall not be identical | 
|  | 5418 | //      to the implicit argument list of the primary template. | 
|  | 5419 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) | 
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5420 | << (TUK == TUK_Definition) | 
|  | 5421 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); | 
| Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5422 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, | 
|  | 5423 | ClassTemplate->getIdentifier(), | 
|  | 5424 | TemplateNameLoc, | 
|  | 5425 | Attr, | 
|  | 5426 | TemplateParams, | 
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5427 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5428 | TemplateParameterLists.size() - 1, | 
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5429 | TemplateParameterLists.data()); | 
| Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5430 | } | 
| Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5431 |  | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5432 | // Create a new class template partial specialization declaration node. | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5433 | ClassTemplatePartialSpecializationDecl *PrevPartial | 
|  | 5434 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); | 
| Douglas Gregor | 407e961 | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5435 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5436 | : ClassTemplate->getNextPartialSpecSequenceNumber(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5437 | ClassTemplatePartialSpecializationDecl *Partial | 
| Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5438 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5439 | ClassTemplate->getDeclContext(), | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5440 | KWLoc, TemplateNameLoc, | 
| Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5441 | TemplateParams, | 
|  | 5442 | ClassTemplate, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5443 | Converted.data(), | 
|  | 5444 | Converted.size(), | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5445 | TemplateArgs, | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5446 | CanonType, | 
| Douglas Gregor | 407e961 | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5447 | PrevPartial, | 
|  | 5448 | SequenceNumber); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5449 | SetNestedNameSpecifier(Partial, SS); | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5450 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { | 
| Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5451 | Partial->setTemplateParameterListsInfo(Context, | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5452 | TemplateParameterLists.size() - 1, | 
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5453 | TemplateParameterLists.data()); | 
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5454 | } | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5455 |  | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5456 | if (!PrevPartial) | 
|  | 5457 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5458 | Specialization = Partial; | 
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5459 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5460 | // If we are providing an explicit specialization of a member class | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5461 | // template specialization, make a note of that. | 
|  | 5462 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) | 
|  | 5463 | PrevPartial->setMemberSpecialization(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 |  | 
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5465 | // Check that all of the template parameters of the class template | 
|  | 5466 | // partial specialization are deducible from the template | 
|  | 5467 | // arguments. If not, this class template partial specialization | 
|  | 5468 | // will never be used. | 
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5469 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5470 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5471 | TemplateParams->getDepth(), | 
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5472 | DeducibleParams); | 
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5473 |  | 
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5474 | if (!DeducibleParams.all()) { | 
|  | 5475 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); | 
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5476 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) | 
|  | 5477 | << (NumNonDeducible > 1) | 
|  | 5478 | << SourceRange(TemplateNameLoc, RAngleLoc); | 
|  | 5479 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { | 
|  | 5480 | if (!DeducibleParams[I]) { | 
|  | 5481 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); | 
|  | 5482 | if (Param->getDeclName()) | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5483 | Diag(Param->getLocation(), | 
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5484 | diag::note_partial_spec_unused_parameter) | 
|  | 5485 | << Param->getDeclName(); | 
|  | 5486 | else | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | Diag(Param->getLocation(), | 
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5488 | diag::note_partial_spec_unused_parameter) | 
| Benjamin Kramer | e8394df | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5489 | << "<anonymous>"; | 
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5490 | } | 
|  | 5491 | } | 
|  | 5492 | } | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5493 | } else { | 
|  | 5494 | // Create a new class template specialization declaration node for | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5495 | // this explicit specialization or friend declaration. | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5496 | Specialization | 
| Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5497 | = ClassTemplateSpecializationDecl::Create(Context, Kind, | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5498 | ClassTemplate->getDeclContext(), | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5499 | KWLoc, TemplateNameLoc, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5500 | ClassTemplate, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5501 | Converted.data(), | 
|  | 5502 | Converted.size(), | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5503 | PrevDecl); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5504 | SetNestedNameSpecifier(Specialization, SS); | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5505 | if (TemplateParameterLists.size() > 0) { | 
| Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5506 | Specialization->setTemplateParameterListsInfo(Context, | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5507 | TemplateParameterLists.size(), | 
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 5508 | TemplateParameterLists.data()); | 
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5509 | } | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5510 |  | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5511 | if (!PrevDecl) | 
|  | 5512 | ClassTemplate->AddSpecialization(Specialization, InsertPos); | 
| Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5513 |  | 
|  | 5514 | CanonType = Context.getTypeDeclType(Specialization); | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5515 | } | 
|  | 5516 |  | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5517 | // C++ [temp.expl.spec]p6: | 
|  | 5518 | //   If a template, a member template or the member of a class template is | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5519 | //   explicitly specialized then that specialization shall be declared | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5520 | //   before the first use of that specialization that would cause an implicit | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5521 | //   instantiation to take place, in every translation unit in which such a | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5522 | //   use occurs; no diagnostic is required. | 
|  | 5523 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { | 
| Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5524 | bool Okay = false; | 
| Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5525 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { | 
| Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5526 | // Is there any previous explicit specialization declaration? | 
|  | 5527 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { | 
|  | 5528 | Okay = true; | 
|  | 5529 | break; | 
|  | 5530 | } | 
|  | 5531 | } | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5532 |  | 
| Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5533 | if (!Okay) { | 
|  | 5534 | SourceRange Range(TemplateNameLoc, RAngleLoc); | 
|  | 5535 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) | 
|  | 5536 | << Context.getTypeDeclType(Specialization) << Range; | 
|  | 5537 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5538 | Diag(PrevDecl->getPointOfInstantiation(), | 
| Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5539 | diag::note_instantiation_required_here) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5540 | << (PrevDecl->getTemplateSpecializationKind() | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5541 | != TSK_ImplicitInstantiation); | 
| Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5542 | return true; | 
|  | 5543 | } | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5544 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5545 |  | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5546 | // If this is not a friend, note that this is an explicit specialization. | 
|  | 5547 | if (TUK != TUK_Friend) | 
|  | 5548 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5549 |  | 
|  | 5550 | // Check that this isn't a redefinition of this specialization. | 
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5551 | if (TUK == TUK_Definition) { | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5552 | if (RecordDecl *Def = Specialization->getDefinition()) { | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5553 | SourceRange Range(TemplateNameLoc, RAngleLoc); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5554 | Diag(TemplateNameLoc, diag::err_redefinition) | 
| Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5555 | << Context.getTypeDeclType(Specialization) << Range; | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5556 | Diag(Def->getLocation(), diag::note_previous_definition); | 
|  | 5557 | Specialization->setInvalidDecl(); | 
| Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5558 | return true; | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5559 | } | 
|  | 5560 | } | 
|  | 5561 |  | 
| John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5562 | if (Attr) | 
|  | 5563 | ProcessDeclAttributeList(S, Specialization, Attr); | 
|  | 5564 |  | 
| Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 5565 | // Add alignment attributes if necessary; these attributes are checked when | 
|  | 5566 | // the ASTContext lays out the structure. | 
|  | 5567 | if (TUK == TUK_Definition) { | 
|  | 5568 | AddAlignmentAttributesForRecord(Specialization); | 
|  | 5569 | AddMsStructLayoutForRecord(Specialization); | 
|  | 5570 | } | 
|  | 5571 |  | 
| Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5572 | if (ModulePrivateLoc.isValid()) | 
|  | 5573 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) | 
|  | 5574 | << (isPartialSpecialization? 1 : 0) | 
|  | 5575 | << FixItHint::CreateRemoval(ModulePrivateLoc); | 
|  | 5576 |  | 
| Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5577 | // Build the fully-sugared type for this class template | 
|  | 5578 | // specialization as the user wrote in the specialization | 
|  | 5579 | // itself. This means that we'll pretty-print the type retrieved | 
|  | 5580 | // from the specialization's declaration the way that the user | 
|  | 5581 | // actually wrote the specialization, rather than formatting the | 
|  | 5582 | // name based on the "canonical" representation used to store the | 
|  | 5583 | // template arguments in the specialization. | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5584 | TypeSourceInfo *WrittenTy | 
|  | 5585 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, | 
|  | 5586 | TemplateArgs, CanonType); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5587 | if (TUK != TUK_Friend) { | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5588 | Specialization->setTypeAsWritten(WrittenTy); | 
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5589 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5590 | } | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5591 |  | 
| Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5592 | // C++ [temp.expl.spec]p9: | 
|  | 5593 | //   A template explicit specialization is in the scope of the | 
|  | 5594 | //   namespace in which the template was defined. | 
|  | 5595 | // | 
|  | 5596 | // We actually implement this paragraph where we set the semantic | 
|  | 5597 | // context (in the creation of the ClassTemplateSpecializationDecl), | 
|  | 5598 | // but we also maintain the lexical context where the actual | 
|  | 5599 | // definition occurs. | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5600 | Specialization->setLexicalDeclContext(CurContext); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5601 |  | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5602 | // We may be starting the definition of this specialization. | 
| John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5603 | if (TUK == TUK_Definition) | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5604 | Specialization->startDefinition(); | 
|  | 5605 |  | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5606 | if (TUK == TUK_Friend) { | 
|  | 5607 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, | 
|  | 5608 | TemplateNameLoc, | 
| John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5609 | WrittenTy, | 
| Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5610 | /*FIXME:*/KWLoc); | 
|  | 5611 | Friend->setAccess(AS_public); | 
|  | 5612 | CurContext->addDecl(Friend); | 
|  | 5613 | } else { | 
|  | 5614 | // Add the specialization into its lexical context, so that it can | 
|  | 5615 | // be seen when iterating through the list of declarations in that | 
|  | 5616 | // context. However, specializations are not found by name lookup. | 
|  | 5617 | CurContext->addDecl(Specialization); | 
|  | 5618 | } | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5619 | return Specialization; | 
| Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5620 | } | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5621 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5622 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, | 
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5623 | MultiTemplateParamsArg TemplateParameterLists, | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5624 | Declarator &D) { | 
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5625 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); | 
| Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 5626 | ActOnDocumentableDecl(NewDecl); | 
|  | 5627 | return NewDecl; | 
| Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5628 | } | 
|  | 5629 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5630 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, | 
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5631 | MultiTemplateParamsArg TemplateParameterLists, | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5632 | Declarator &D) { | 
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5633 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); | 
| Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5634 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 |  | 
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5636 | if (FTI.hasPrototype) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5637 | // FIXME: Diagnose arguments without names in C. | 
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5638 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5639 |  | 
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5640 | Scope *ParentScope = FnBodyScope->getParent(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5641 |  | 
| Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5642 | D.setFunctionDefinitionKind(FDK_Definition); | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5643 | Decl *DP = HandleDeclarator(ParentScope, D, | 
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5644 | TemplateParameterLists); | 
| Argyrios Kyrtzidis | 6fada2d | 2012-12-14 06:53:58 +0000 | [diff] [blame] | 5645 | return ActOnStartOfFunctionDef(FnBodyScope, DP); | 
| Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5646 | } | 
|  | 5647 |  | 
| John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5648 | /// \brief Strips various properties off an implicit instantiation | 
|  | 5649 | /// that has just been explicitly specialized. | 
|  | 5650 | static void StripImplicitInstantiation(NamedDecl *D) { | 
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5651 | D->dropAttrs(); | 
| John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5652 |  | 
|  | 5653 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | 
|  | 5654 | FD->setInlineSpecified(false); | 
| Jordan Rose | a0a86be | 2013-03-08 22:25:36 +0000 | [diff] [blame] | 5655 |  | 
|  | 5656 | for (FunctionDecl::param_iterator I = FD->param_begin(), | 
|  | 5657 | E = FD->param_end(); | 
|  | 5658 | I != E; ++I) | 
|  | 5659 | (*I)->dropAttrs(); | 
| John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5660 | } | 
|  | 5661 | } | 
|  | 5662 |  | 
| Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5663 | /// \brief Compute the diagnostic location for an explicit instantiation | 
|  | 5664 | //  declaration or definition. | 
|  | 5665 | static SourceLocation DiagLocForExplicitInstantiation( | 
| Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5666 | NamedDecl* D, SourceLocation PointOfInstantiation) { | 
| Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5667 | // Explicit instantiations following a specialization have no effect and | 
|  | 5668 | // hence no PointOfInstantiation. In that case, walk decl backwards | 
|  | 5669 | // until a valid name loc is found. | 
|  | 5670 | SourceLocation PrevDiagLoc = PointOfInstantiation; | 
| Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5671 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); | 
|  | 5672 | Prev = Prev->getPreviousDecl()) { | 
| Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5673 | PrevDiagLoc = Prev->getLocation(); | 
|  | 5674 | } | 
|  | 5675 | assert(PrevDiagLoc.isValid() && | 
|  | 5676 | "Explicit instantiation without point of instantiation?"); | 
|  | 5677 | return PrevDiagLoc; | 
|  | 5678 | } | 
|  | 5679 |  | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | /// \brief Diagnose cases where we have an explicit template specialization | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5681 | /// before/after an explicit template instantiation, producing diagnostics | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5682 | /// for those cases where they are required and determining whether the | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5683 | /// new specialization/instantiation will have any effect. | 
|  | 5684 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5685 | /// \param NewLoc the location of the new explicit specialization or | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5686 | /// instantiation. | 
|  | 5687 | /// | 
|  | 5688 | /// \param NewTSK the kind of the new explicit specialization or instantiation. | 
|  | 5689 | /// | 
|  | 5690 | /// \param PrevDecl the previous declaration of the entity. | 
|  | 5691 | /// | 
|  | 5692 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. | 
|  | 5693 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5694 | /// \param PrevPointOfInstantiation if valid, indicates where the previus | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5695 | /// declaration was instantiated (either implicitly or explicitly). | 
|  | 5696 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5697 | /// \param HasNoEffect will be set to true to indicate that the new | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5698 | /// specialization or instantiation has no effect and should be ignored. | 
|  | 5699 | /// | 
|  | 5700 | /// \returns true if there was an error that should prevent the introduction of | 
|  | 5701 | /// the new declaration into the AST, false otherwise. | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5702 | bool | 
|  | 5703 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, | 
|  | 5704 | TemplateSpecializationKind NewTSK, | 
|  | 5705 | NamedDecl *PrevDecl, | 
|  | 5706 | TemplateSpecializationKind PrevTSK, | 
|  | 5707 | SourceLocation PrevPointOfInstantiation, | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5708 | bool &HasNoEffect) { | 
|  | 5709 | HasNoEffect = false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5711 | switch (NewTSK) { | 
|  | 5712 | case TSK_Undeclared: | 
|  | 5713 | case TSK_ImplicitInstantiation: | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5714 | llvm_unreachable("Don't check implicit instantiations here"); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5715 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5716 | case TSK_ExplicitSpecialization: | 
|  | 5717 | switch (PrevTSK) { | 
|  | 5718 | case TSK_Undeclared: | 
|  | 5719 | case TSK_ExplicitSpecialization: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5720 | // Okay, we're just specializing something that is either already | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5721 | // explicitly specialized or has merely been mentioned without any | 
|  | 5722 | // instantiation. | 
|  | 5723 | return false; | 
|  | 5724 |  | 
|  | 5725 | case TSK_ImplicitInstantiation: | 
|  | 5726 | if (PrevPointOfInstantiation.isInvalid()) { | 
|  | 5727 | // The declaration itself has not actually been instantiated, so it is | 
|  | 5728 | // still okay to specialize it. | 
| John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5729 | StripImplicitInstantiation(PrevDecl); | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5730 | return false; | 
|  | 5731 | } | 
|  | 5732 | // Fall through | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5733 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5734 | case TSK_ExplicitInstantiationDeclaration: | 
|  | 5735 | case TSK_ExplicitInstantiationDefinition: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5736 | assert((PrevTSK == TSK_ImplicitInstantiation || | 
|  | 5737 | PrevPointOfInstantiation.isValid()) && | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5738 | "Explicit instantiation without point of instantiation?"); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5739 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5740 | // C++ [temp.expl.spec]p6: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5741 | //   If a template, a member template or the member of a class template | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5742 | //   is explicitly specialized then that specialization shall be declared | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5743 | //   before the first use of that specialization that would cause an | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5744 | //   implicit instantiation to take place, in every translation unit in | 
|  | 5745 | //   which such a use occurs; no diagnostic is required. | 
| Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5746 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { | 
| Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5747 | // Is there any previous explicit specialization declaration? | 
|  | 5748 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) | 
|  | 5749 | return false; | 
|  | 5750 | } | 
|  | 5751 |  | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5752 | Diag(NewLoc, diag::err_specialization_after_instantiation) | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5753 | << PrevDecl; | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5754 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5755 | << (PrevTSK != TSK_ImplicitInstantiation); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5756 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5757 | return true; | 
|  | 5758 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5759 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5760 | case TSK_ExplicitInstantiationDeclaration: | 
|  | 5761 | switch (PrevTSK) { | 
|  | 5762 | case TSK_ExplicitInstantiationDeclaration: | 
|  | 5763 | // This explicit instantiation declaration is redundant (that's okay). | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5764 | HasNoEffect = true; | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5765 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5766 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5767 | case TSK_Undeclared: | 
|  | 5768 | case TSK_ImplicitInstantiation: | 
|  | 5769 | // We're explicitly instantiating something that may have already been | 
|  | 5770 | // implicitly instantiated; that's fine. | 
|  | 5771 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5772 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5773 | case TSK_ExplicitSpecialization: | 
|  | 5774 | // C++0x [temp.explicit]p4: | 
|  | 5775 | //   For a given set of template parameters, if an explicit instantiation | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5776 | //   of a template appears after a declaration of an explicit | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5777 | //   specialization for that template, the explicit instantiation has no | 
|  | 5778 | //   effect. | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5779 | HasNoEffect = true; | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5780 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5781 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5782 | case TSK_ExplicitInstantiationDefinition: | 
|  | 5783 | // C++0x [temp.explicit]p10: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5784 | //   If an entity is the subject of both an explicit instantiation | 
|  | 5785 | //   declaration and an explicit instantiation definition in the same | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5786 | //   translation unit, the definition shall follow the declaration. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5787 | Diag(NewLoc, | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5788 | diag::err_explicit_instantiation_declaration_after_definition); | 
| Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5789 |  | 
|  | 5790 | // Explicit instantiations following a specialization have no effect and | 
|  | 5791 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards | 
|  | 5792 | // until a valid name loc is found. | 
| Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5793 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), | 
|  | 5794 | diag::note_explicit_instantiation_definition_here); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5795 | HasNoEffect = true; | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5796 | return false; | 
|  | 5797 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5798 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5799 | case TSK_ExplicitInstantiationDefinition: | 
|  | 5800 | switch (PrevTSK) { | 
|  | 5801 | case TSK_Undeclared: | 
|  | 5802 | case TSK_ImplicitInstantiation: | 
|  | 5803 | // We're explicitly instantiating something that may have already been | 
|  | 5804 | // implicitly instantiated; that's fine. | 
|  | 5805 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5806 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5807 | case TSK_ExplicitSpecialization: | 
|  | 5808 | // C++ DR 259, C++0x [temp.explicit]p4: | 
|  | 5809 | //   For a given set of template parameters, if an explicit | 
|  | 5810 | //   instantiation of a template appears after a declaration of | 
|  | 5811 | //   an explicit specialization for that template, the explicit | 
|  | 5812 | //   instantiation has no effect. | 
|  | 5813 | // | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5814 | // In C++98/03 mode, we only give an extension warning here, because it | 
| Douglas Gregor | 06aa5041 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 5815 | // is not harmful to try to explicitly instantiate something that | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5816 | // has been explicitly specialized. | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5817 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5818 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : | 
|  | 5819 | diag::ext_explicit_instantiation_after_specialization) | 
|  | 5820 | << PrevDecl; | 
|  | 5821 | Diag(PrevDecl->getLocation(), | 
|  | 5822 | diag::note_previous_template_specialization); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5823 | HasNoEffect = true; | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5824 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5825 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5826 | case TSK_ExplicitInstantiationDeclaration: | 
|  | 5827 | // We're explicity instantiating a definition for something for which we | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5828 | // were previously asked to suppress instantiations. That's fine. | 
| Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5829 |  | 
|  | 5830 | // C++0x [temp.explicit]p4: | 
|  | 5831 | //   For a given set of template parameters, if an explicit instantiation | 
|  | 5832 | //   of a template appears after a declaration of an explicit | 
|  | 5833 | //   specialization for that template, the explicit instantiation has no | 
|  | 5834 | //   effect. | 
| Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5835 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { | 
| Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5836 | // Is there any previous explicit specialization declaration? | 
|  | 5837 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { | 
|  | 5838 | HasNoEffect = true; | 
|  | 5839 | break; | 
|  | 5840 | } | 
|  | 5841 | } | 
|  | 5842 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5843 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5844 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5845 | case TSK_ExplicitInstantiationDefinition: | 
|  | 5846 | // C++0x [temp.spec]p5: | 
|  | 5847 | //   For a given template and a given set of template-arguments, | 
|  | 5848 | //     - an explicit instantiation definition shall appear at most once | 
|  | 5849 | //       in a program, | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5850 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5851 | << PrevDecl; | 
| Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5852 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5853 | diag::note_previous_explicit_instantiation); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5854 | HasNoEffect = true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5855 | return false; | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5856 | } | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5857 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 |  | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5859 | llvm_unreachable("Missing specialization/instantiation case?"); | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5860 | } | 
|  | 5861 |  | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5862 | /// \brief Perform semantic analysis for the given dependent function | 
| James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 5863 | /// template specialization. | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5864 | /// | 
| James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 5865 | /// The only possible way to get a dependent function template specialization | 
|  | 5866 | /// is with a friend declaration, like so: | 
|  | 5867 | /// | 
|  | 5868 | /// \code | 
|  | 5869 | ///   template \<class T> void foo(T); | 
|  | 5870 | ///   template \<class T> class A { | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5871 | ///     friend void foo<>(T); | 
|  | 5872 | ///   }; | 
| James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 5873 | /// \endcode | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5874 | /// | 
|  | 5875 | /// There really isn't any useful analysis we can do here, so we | 
|  | 5876 | /// just store the information. | 
|  | 5877 | bool | 
|  | 5878 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, | 
|  | 5879 | const TemplateArgumentListInfo &ExplicitTemplateArgs, | 
|  | 5880 | LookupResult &Previous) { | 
|  | 5881 | // Remove anything from Previous that isn't a function template in | 
|  | 5882 | // the correct context. | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5883 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5884 | LookupResult::Filter F = Previous.makeFilter(); | 
|  | 5885 | while (F.hasNext()) { | 
|  | 5886 | NamedDecl *D = F.next()->getUnderlyingDecl(); | 
|  | 5887 | if (!isa<FunctionTemplateDecl>(D) || | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5888 | !FDLookupContext->InEnclosingNamespaceSetOf( | 
|  | 5889 | D->getDeclContext()->getRedeclContext())) | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5890 | F.erase(); | 
|  | 5891 | } | 
|  | 5892 | F.done(); | 
|  | 5893 |  | 
|  | 5894 | // Should this be diagnosed here? | 
|  | 5895 | if (Previous.empty()) return true; | 
|  | 5896 |  | 
|  | 5897 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), | 
|  | 5898 | ExplicitTemplateArgs); | 
|  | 5899 | return false; | 
|  | 5900 | } | 
|  | 5901 |  | 
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5902 | /// \brief Perform semantic analysis for the given function template | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5903 | /// specialization. | 
|  | 5904 | /// | 
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5905 | /// This routine performs all of the semantic analysis required for an | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5906 | /// explicit function template specialization. On successful completion, | 
|  | 5907 | /// the function declaration \p FD will become a function template | 
|  | 5908 | /// specialization. | 
|  | 5909 | /// | 
|  | 5910 | /// \param FD the function declaration, which will be updated to become a | 
|  | 5911 | /// function template specialization. | 
|  | 5912 | /// | 
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5913 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, | 
|  | 5914 | /// if any. Note that this may be valid info even when 0 arguments are | 
|  | 5915 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); | 
|  | 5916 | /// as it anyway contains info on the angle brackets locations. | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5917 | /// | 
| Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5918 | /// \param Previous the set of declarations that may be specialized by | 
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5919 | /// this function specialization. | 
|  | 5920 | bool | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5921 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, | 
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5922 | TemplateArgumentListInfo *ExplicitTemplateArgs, | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5923 | LookupResult &Previous) { | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5924 | // The set of function template specializations that could match this | 
|  | 5925 | // explicit function template specialization. | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5926 | UnresolvedSet<8> Candidates; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 |  | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5928 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5929 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | 
|  | 5930 | I != E; ++I) { | 
|  | 5931 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); | 
|  | 5932 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5933 | // Only consider templates found within the same semantic lookup scope as | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5934 | // FD. | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5935 | if (!FDLookupContext->InEnclosingNamespaceSetOf( | 
|  | 5936 | Ovl->getDeclContext()->getRedeclContext())) | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5937 | continue; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5938 |  | 
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 5939 | // When matching a constexpr member function template specialization | 
|  | 5940 | // against the primary template, we don't yet know whether the | 
|  | 5941 | // specialization has an implicit 'const' (because we don't know whether | 
|  | 5942 | // it will be a static member function until we know which template it | 
|  | 5943 | // specializes), so adjust it now assuming it specializes this template. | 
|  | 5944 | QualType FT = FD->getType(); | 
|  | 5945 | if (FD->isConstexpr()) { | 
|  | 5946 | CXXMethodDecl *OldMD = | 
|  | 5947 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); | 
|  | 5948 | if (OldMD && OldMD->isConst()) { | 
|  | 5949 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); | 
|  | 5950 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); | 
|  | 5951 | EPI.TypeQuals |= Qualifiers::Const; | 
|  | 5952 | FT = Context.getFunctionType(FPT->getResultType(), | 
| Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 5953 | ArrayRef<QualType>(FPT->arg_type_begin(), | 
|  | 5954 | FPT->getNumArgs()), | 
|  | 5955 | EPI); | 
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 5956 | } | 
|  | 5957 | } | 
|  | 5958 |  | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5959 | // C++ [temp.expl.spec]p11: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5960 | //   A trailing template-argument can be left unspecified in the | 
|  | 5961 | //   template-id naming an explicit function template specialization | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5962 | //   provided it can be deduced from the function argument type. | 
|  | 5963 | // Perform template argument deduction to determine whether we may be | 
|  | 5964 | // specializing this template. | 
|  | 5965 | // FIXME: It is somewhat wasteful to build | 
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 5966 | TemplateDeductionInfo Info(FD->getLocation()); | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5967 | FunctionDecl *Specialization = 0; | 
|  | 5968 | if (TemplateDeductionResult TDK | 
| Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 5969 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, FT, | 
|  | 5970 | Specialization, Info)) { | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5971 | // FIXME: Template argument deduction failed; record why it failed, so | 
|  | 5972 | // that we can provide nifty diagnostics. | 
|  | 5973 | (void)TDK; | 
|  | 5974 | continue; | 
|  | 5975 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5976 |  | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5977 | // Record this candidate. | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5978 | Candidates.addDecl(Specialization, I.getAccess()); | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5979 | } | 
|  | 5980 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5981 |  | 
| Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5982 | // Find the most specialized function template. | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5983 | UnresolvedSetIterator Result | 
|  | 5984 | = getMostSpecialized(Candidates.begin(), Candidates.end(), | 
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5985 | TPOC_Other, 0, FD->getLocation(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5986 | PDiag(diag::err_function_template_spec_no_match) | 
| Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5987 | << FD->getDeclName(), | 
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5988 | PDiag(diag::err_function_template_spec_ambiguous) | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5989 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), | 
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5990 | PDiag(diag::note_function_template_spec_matched)); | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5991 | if (Result == Candidates.end()) | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5992 | return true; | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5993 |  | 
|  | 5994 | // Ignore access information;  it doesn't figure into redeclaration checking. | 
|  | 5995 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); | 
| Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5996 |  | 
|  | 5997 | FunctionTemplateSpecializationInfo *SpecInfo | 
|  | 5998 | = Specialization->getTemplateSpecializationInfo(); | 
|  | 5999 | assert(SpecInfo && "Function template specialization info missing?"); | 
| Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6000 |  | 
|  | 6001 | // Note: do not overwrite location info if previous template | 
|  | 6002 | // specialization kind was explicit. | 
|  | 6003 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); | 
| Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6004 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { | 
| Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6005 | Specialization->setLocation(FD->getLocation()); | 
| Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6006 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr | 
|  | 6007 | // function can differ from the template declaration with respect to | 
|  | 6008 | // the constexpr specifier. | 
|  | 6009 | Specialization->setConstexpr(FD->isConstexpr()); | 
|  | 6010 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6011 |  | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6012 | // FIXME: Check if the prior specialization has a point of instantiation. | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6013 | // If so, we have run afoul of . | 
| John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6014 |  | 
|  | 6015 | // If this is a friend declaration, then we're not really declaring | 
|  | 6016 | // an explicit specialization. | 
|  | 6017 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6018 |  | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6019 | // Check the scope of this explicit specialization. | 
| John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6020 | if (!isFriend && | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6021 | CheckTemplateSpecializationScope(*this, | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6022 | Specialization->getPrimaryTemplate(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6023 | Specialization, FD->getLocation(), | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6024 | false)) | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6025 | return true; | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6026 |  | 
|  | 6027 | // C++ [temp.expl.spec]p6: | 
|  | 6028 | //   If a template, a member template or the member of a class template is | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6029 | //   explicitly specialized then that specialization shall be declared | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6030 | //   before the first use of that specialization that would cause an implicit | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6031 | //   instantiation to take place, in every translation unit in which such a | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6032 | //   use occurs; no diagnostic is required. | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6033 | bool HasNoEffect = false; | 
| John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6034 | if (!isFriend && | 
|  | 6035 | CheckSpecializationInstantiationRedecl(FD->getLocation(), | 
| John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6036 | TSK_ExplicitSpecialization, | 
|  | 6037 | Specialization, | 
|  | 6038 | SpecInfo->getTemplateSpecializationKind(), | 
|  | 6039 | SpecInfo->getPointOfInstantiation(), | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6040 | HasNoEffect)) | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6041 | return true; | 
| Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6042 |  | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6043 | // Mark the prior declaration as an explicit specialization, so that later | 
|  | 6044 | // clients know that this is an explicit specialization. | 
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6045 | if (!isFriend) { | 
| John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6046 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); | 
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6047 | MarkUnusedFileScopedDecl(Specialization); | 
|  | 6048 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6049 |  | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6050 | // Turn the given function declaration into a function template | 
|  | 6051 | // specialization, with the template arguments from the previous | 
|  | 6052 | // specialization. | 
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6053 | // Take copies of (semantic and syntactic) template argument lists. | 
|  | 6054 | const TemplateArgumentList* TemplArgs = new (Context) | 
|  | 6055 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); | 
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6056 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), | 
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6057 | TemplArgs, /*InsertPos=*/0, | 
|  | 6058 | SpecInfo->getTemplateSpecializationKind(), | 
| Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 6059 | ExplicitTemplateArgs); | 
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6060 |  | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6061 | // The "previous declaration" for this function template specialization is | 
|  | 6062 | // the prior function template specialization. | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6063 | Previous.clear(); | 
|  | 6064 | Previous.addDecl(Specialization); | 
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6065 | return false; | 
|  | 6066 | } | 
|  | 6067 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6068 | /// \brief Perform semantic analysis for the given non-template member | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6069 | /// specialization. | 
|  | 6070 | /// | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6071 | /// This routine performs all of the semantic analysis required for an | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6072 | /// explicit member function specialization. On successful completion, | 
|  | 6073 | /// the function declaration \p FD will become a member function | 
|  | 6074 | /// specialization. | 
|  | 6075 | /// | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6076 | /// \param Member the member declaration, which will be updated to become a | 
|  | 6077 | /// specialization. | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6078 | /// | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6079 | /// \param Previous the set of declarations, one of which may be specialized | 
|  | 6080 | /// by this function specialization;  the set will be modified to contain the | 
|  | 6081 | /// redeclared member. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6082 | bool | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6083 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6084 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); | 
| John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6085 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6086 | // Try to find the member we are instantiating. | 
|  | 6087 | NamedDecl *Instantiation = 0; | 
|  | 6088 | NamedDecl *InstantiatedFrom = 0; | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6089 | MemberSpecializationInfo *MSInfo = 0; | 
|  | 6090 |  | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6091 | if (Previous.empty()) { | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6092 | // Nowhere to look anyway. | 
|  | 6093 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6094 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); | 
|  | 6095 | I != E; ++I) { | 
|  | 6096 | NamedDecl *D = (*I)->getUnderlyingDecl(); | 
|  | 6097 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6098 | if (Context.hasSameType(Function->getType(), Method->getType())) { | 
|  | 6099 | Instantiation = Method; | 
|  | 6100 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6101 | MSInfo = Method->getMemberSpecializationInfo(); | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6102 | break; | 
|  | 6103 | } | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6104 | } | 
|  | 6105 | } | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6106 | } else if (isa<VarDecl>(Member)) { | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6107 | VarDecl *PrevVar; | 
|  | 6108 | if (Previous.isSingleResult() && | 
|  | 6109 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6110 | if (PrevVar->isStaticDataMember()) { | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6111 | Instantiation = PrevVar; | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6112 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6113 | MSInfo = PrevVar->getMemberSpecializationInfo(); | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6114 | } | 
|  | 6115 | } else if (isa<RecordDecl>(Member)) { | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6116 | CXXRecordDecl *PrevRecord; | 
|  | 6117 | if (Previous.isSingleResult() && | 
|  | 6118 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { | 
|  | 6119 | Instantiation = PrevRecord; | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6120 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6121 | MSInfo = PrevRecord->getMemberSpecializationInfo(); | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6122 | } | 
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6123 | } else if (isa<EnumDecl>(Member)) { | 
|  | 6124 | EnumDecl *PrevEnum; | 
|  | 6125 | if (Previous.isSingleResult() && | 
|  | 6126 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { | 
|  | 6127 | Instantiation = PrevEnum; | 
|  | 6128 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); | 
|  | 6129 | MSInfo = PrevEnum->getMemberSpecializationInfo(); | 
|  | 6130 | } | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6131 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6132 |  | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6133 | if (!Instantiation) { | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6134 | // There is no previous declaration that matches. Since member | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6135 | // specializations are always out-of-line, the caller will complain about | 
|  | 6136 | // this mismatch later. | 
|  | 6137 | return false; | 
|  | 6138 | } | 
| John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6139 |  | 
|  | 6140 | // If this is a friend, just bail out here before we start turning | 
|  | 6141 | // things into explicit specializations. | 
|  | 6142 | if (Member->getFriendObjectKind() != Decl::FOK_None) { | 
|  | 6143 | // Preserve instantiation information. | 
|  | 6144 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { | 
|  | 6145 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( | 
|  | 6146 | cast<CXXMethodDecl>(InstantiatedFrom), | 
|  | 6147 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); | 
|  | 6148 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { | 
|  | 6149 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( | 
|  | 6150 | cast<CXXRecordDecl>(InstantiatedFrom), | 
|  | 6151 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); | 
|  | 6152 | } | 
|  | 6153 |  | 
|  | 6154 | Previous.clear(); | 
|  | 6155 | Previous.addDecl(Instantiation); | 
|  | 6156 | return false; | 
|  | 6157 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6158 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6159 | // Make sure that this is a specialization of a member. | 
|  | 6160 | if (!InstantiatedFrom) { | 
|  | 6161 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) | 
|  | 6162 | << Member; | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6163 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); | 
|  | 6164 | return true; | 
|  | 6165 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6166 |  | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6167 | // C++ [temp.expl.spec]p6: | 
|  | 6168 | //   If a template, a member template or the member of a class template is | 
| Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6169 | //   explicitly specialized then that specialization shall be declared | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6170 | //   before the first use of that specialization that would cause an implicit | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6171 | //   instantiation to take place, in every translation unit in which such a | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6172 | //   use occurs; no diagnostic is required. | 
|  | 6173 | assert(MSInfo && "Member specialization info missing?"); | 
| John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6174 |  | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6175 | bool HasNoEffect = false; | 
| John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6176 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), | 
|  | 6177 | TSK_ExplicitSpecialization, | 
|  | 6178 | Instantiation, | 
|  | 6179 | MSInfo->getTemplateSpecializationKind(), | 
|  | 6180 | MSInfo->getPointOfInstantiation(), | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6181 | HasNoEffect)) | 
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6182 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6183 |  | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6184 | // Check the scope of this explicit specialization. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6185 | if (CheckTemplateSpecializationScope(*this, | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6186 | InstantiatedFrom, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6187 | Instantiation, Member->getLocation(), | 
| Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6188 | false)) | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6189 | return true; | 
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 6190 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6191 | // Note that this is an explicit instantiation of a member. | 
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6192 | // the original declaration to note that it is an explicit specialization | 
|  | 6193 | // (if it was previously an implicit instantiation). This latter step | 
|  | 6194 | // makes bookkeeping easier. | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6195 | if (isa<FunctionDecl>(Member)) { | 
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6196 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); | 
|  | 6197 | if (InstantiationFunction->getTemplateSpecializationKind() == | 
|  | 6198 | TSK_ImplicitInstantiation) { | 
|  | 6199 | InstantiationFunction->setTemplateSpecializationKind( | 
|  | 6200 | TSK_ExplicitSpecialization); | 
|  | 6201 | InstantiationFunction->setLocation(Member->getLocation()); | 
|  | 6202 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6203 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6204 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( | 
|  | 6205 | cast<CXXMethodDecl>(InstantiatedFrom), | 
|  | 6206 | TSK_ExplicitSpecialization); | 
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6207 | MarkUnusedFileScopedDecl(InstantiationFunction); | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6208 | } else if (isa<VarDecl>(Member)) { | 
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6209 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); | 
|  | 6210 | if (InstantiationVar->getTemplateSpecializationKind() == | 
|  | 6211 | TSK_ImplicitInstantiation) { | 
|  | 6212 | InstantiationVar->setTemplateSpecializationKind( | 
|  | 6213 | TSK_ExplicitSpecialization); | 
|  | 6214 | InstantiationVar->setLocation(Member->getLocation()); | 
|  | 6215 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6217 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), | 
|  | 6218 | cast<VarDecl>(InstantiatedFrom), | 
|  | 6219 | TSK_ExplicitSpecialization); | 
| Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6220 | MarkUnusedFileScopedDecl(InstantiationVar); | 
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6221 | } else if (isa<CXXRecordDecl>(Member)) { | 
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6222 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); | 
|  | 6223 | if (InstantiationClass->getTemplateSpecializationKind() == | 
|  | 6224 | TSK_ImplicitInstantiation) { | 
|  | 6225 | InstantiationClass->setTemplateSpecializationKind( | 
|  | 6226 | TSK_ExplicitSpecialization); | 
|  | 6227 | InstantiationClass->setLocation(Member->getLocation()); | 
|  | 6228 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6229 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6230 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( | 
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6231 | cast<CXXRecordDecl>(InstantiatedFrom), | 
|  | 6232 | TSK_ExplicitSpecialization); | 
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6233 | } else { | 
|  | 6234 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); | 
|  | 6235 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); | 
|  | 6236 | if (InstantiationEnum->getTemplateSpecializationKind() == | 
|  | 6237 | TSK_ImplicitInstantiation) { | 
|  | 6238 | InstantiationEnum->setTemplateSpecializationKind( | 
|  | 6239 | TSK_ExplicitSpecialization); | 
|  | 6240 | InstantiationEnum->setLocation(Member->getLocation()); | 
|  | 6241 | } | 
|  | 6242 |  | 
|  | 6243 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( | 
|  | 6244 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6245 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6246 |  | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6247 | // Save the caller the trouble of having to figure out which declaration | 
|  | 6248 | // this specialization matches. | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6249 | Previous.clear(); | 
|  | 6250 | Previous.addDecl(Instantiation); | 
| Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6251 | return false; | 
|  | 6252 | } | 
|  | 6253 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6254 | /// \brief Check the scope of an explicit instantiation. | 
| Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6255 | /// | 
|  | 6256 | /// \returns true if a serious error occurs, false otherwise. | 
|  | 6257 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6258 | SourceLocation InstLoc, | 
|  | 6259 | bool WasQualifiedName) { | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6260 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); | 
|  | 6261 | DeclContext *CurContext = S.CurContext->getRedeclContext(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6262 |  | 
| Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6263 | if (CurContext->isRecord()) { | 
|  | 6264 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) | 
|  | 6265 | << D; | 
|  | 6266 | return true; | 
|  | 6267 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6268 |  | 
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6269 | // C++11 [temp.explicit]p3: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6270 | //   An explicit instantiation shall appear in an enclosing namespace of its | 
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6271 | //   template. If the name declared in the explicit instantiation is an | 
|  | 6272 | //   unqualified name, the explicit instantiation shall appear in the | 
|  | 6273 | //   namespace where its template is declared or, if that namespace is inline | 
|  | 6274 | //   (7.3.1), any namespace from its enclosing namespace set. | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6275 | // | 
|  | 6276 | // This is DR275, which we do not retroactively apply to C++98/03. | 
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6277 | if (WasQualifiedName) { | 
|  | 6278 | if (CurContext->Encloses(OrigContext)) | 
|  | 6279 | return false; | 
|  | 6280 | } else { | 
|  | 6281 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) | 
|  | 6282 | return false; | 
|  | 6283 | } | 
|  | 6284 |  | 
|  | 6285 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { | 
|  | 6286 | if (WasQualifiedName) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6287 | S.Diag(InstLoc, | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6288 | S.getLangOpts().CPlusPlus11? | 
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6289 | diag::err_explicit_instantiation_out_of_scope : | 
|  | 6290 | diag::warn_explicit_instantiation_out_of_scope_0x) | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6291 | << D << NS; | 
|  | 6292 | else | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6293 | S.Diag(InstLoc, | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6294 | S.getLangOpts().CPlusPlus11? | 
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6295 | diag::err_explicit_instantiation_unqualified_wrong_namespace : | 
|  | 6296 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) | 
|  | 6297 | << D << NS; | 
|  | 6298 | } else | 
|  | 6299 | S.Diag(InstLoc, | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6300 | S.getLangOpts().CPlusPlus11? | 
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6301 | diag::err_explicit_instantiation_must_be_global : | 
|  | 6302 | diag::warn_explicit_instantiation_must_be_global_0x) | 
|  | 6303 | << D; | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6304 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); | 
| Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6305 | return false; | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6306 | } | 
|  | 6307 |  | 
|  | 6308 | /// \brief Determine whether the given scope specifier has a template-id in it. | 
|  | 6309 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { | 
|  | 6310 | if (!SS.isSet()) | 
|  | 6311 | return false; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6312 |  | 
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6313 | // C++11 [temp.explicit]p3: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6314 | //   If the explicit instantiation is for a member function, a member class | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6315 | //   or a static data member of a class template specialization, the name of | 
|  | 6316 | //   the class template specialization in the qualified-id for the member | 
|  | 6317 | //   name shall be a simple-template-id. | 
|  | 6318 | // | 
|  | 6319 | // C++98 has the same restriction, just worded differently. | 
|  | 6320 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); | 
|  | 6321 | NNS; NNS = NNS->getPrefix()) | 
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6322 | if (const Type *T = NNS->getAsType()) | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6323 | if (isa<TemplateSpecializationType>(T)) | 
|  | 6324 | return true; | 
|  | 6325 |  | 
|  | 6326 | return false; | 
|  | 6327 | } | 
|  | 6328 |  | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6329 | // Explicit instantiation of a class template specialization | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6330 | DeclResult | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6331 | Sema::ActOnExplicitInstantiation(Scope *S, | 
| Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6332 | SourceLocation ExternLoc, | 
|  | 6333 | SourceLocation TemplateLoc, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6334 | unsigned TagSpec, | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6335 | SourceLocation KWLoc, | 
|  | 6336 | const CXXScopeSpec &SS, | 
|  | 6337 | TemplateTy TemplateD, | 
|  | 6338 | SourceLocation TemplateNameLoc, | 
|  | 6339 | SourceLocation LAngleLoc, | 
|  | 6340 | ASTTemplateArgsPtr TemplateArgsIn, | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6341 | SourceLocation RAngleLoc, | 
|  | 6342 | AttributeList *Attr) { | 
|  | 6343 | // Find the class template we're specializing | 
|  | 6344 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6345 | ClassTemplateDecl *ClassTemplate | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6346 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); | 
|  | 6347 |  | 
|  | 6348 | // Check that the specialization uses the same tag kind as the | 
|  | 6349 | // original template. | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6350 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | 
|  | 6351 | assert(Kind != TTK_Enum && | 
|  | 6352 | "Invalid enum tag in class template explicit instantiation!"); | 
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6353 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), | 
| Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6354 | Kind, /*isDefinition*/false, KWLoc, | 
| Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6355 | *ClassTemplate->getIdentifier())) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6356 | Diag(KWLoc, diag::err_use_with_wrong_tag) | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6357 | << ClassTemplate | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6358 | << FixItHint::CreateReplacement(KWLoc, | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6359 | ClassTemplate->getTemplatedDecl()->getKindName()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6360 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6361 | diag::note_previous_use); | 
|  | 6362 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); | 
|  | 6363 | } | 
|  | 6364 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6365 | // C++0x [temp.explicit]p2: | 
|  | 6366 | //   There are two forms of explicit instantiation: an explicit instantiation | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6367 | //   definition and an explicit instantiation declaration. An explicit | 
|  | 6368 | //   instantiation declaration begins with the extern keyword. [...] | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6369 | TemplateSpecializationKind TSK | 
|  | 6370 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition | 
|  | 6371 | : TSK_ExplicitInstantiationDeclaration; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6372 |  | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6373 | // Translate the parser's template argument list in our AST format. | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6374 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | 
| Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 6375 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6376 |  | 
|  | 6377 | // Check that the template argument list is well-formed for this | 
|  | 6378 | // template. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6379 | SmallVector<TemplateArgument, 4> Converted; | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6380 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, | 
|  | 6381 | TemplateArgs, false, Converted)) | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6382 | return true; | 
|  | 6383 |  | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6384 | // Find the class template specialization declaration that | 
|  | 6385 | // corresponds to these arguments. | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6386 | void *InsertPos = 0; | 
|  | 6387 | ClassTemplateSpecializationDecl *PrevDecl | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6388 | = ClassTemplate->findSpecialization(Converted.data(), | 
|  | 6389 | Converted.size(), InsertPos); | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6390 |  | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6391 | TemplateSpecializationKind PrevDecl_TSK | 
|  | 6392 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; | 
|  | 6393 |  | 
| Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6394 | // C++0x [temp.explicit]p2: | 
|  | 6395 | //   [...] An explicit instantiation shall appear in an enclosing | 
|  | 6396 | //   namespace of its template. [...] | 
|  | 6397 | // | 
|  | 6398 | // This is C++ DR 275. | 
| Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6399 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, | 
|  | 6400 | SS.isSet())) | 
|  | 6401 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6402 |  | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6403 | ClassTemplateSpecializationDecl *Specialization = 0; | 
|  | 6404 |  | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6405 | bool HasNoEffect = false; | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6406 | if (PrevDecl) { | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6407 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6408 | PrevDecl, PrevDecl_TSK, | 
| Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6409 | PrevDecl->getPointOfInstantiation(), | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6410 | HasNoEffect)) | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6411 | return PrevDecl; | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6412 |  | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6413 | // Even though HasNoEffect == true means that this explicit instantiation | 
|  | 6414 | // has no effect on semantics, we go on to put its syntax in the AST. | 
|  | 6415 |  | 
|  | 6416 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || | 
|  | 6417 | PrevDecl_TSK == TSK_Undeclared) { | 
| Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6418 | // Since the only prior class template specialization with these | 
|  | 6419 | // arguments was referenced but not declared, reuse that | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6420 | // declaration node as our own, updating the source location | 
|  | 6421 | // for the template name to reflect our new declaration. | 
|  | 6422 | // (Other source locations will be updated later.) | 
| Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6423 | Specialization = PrevDecl; | 
|  | 6424 | Specialization->setLocation(TemplateNameLoc); | 
|  | 6425 | PrevDecl = 0; | 
|  | 6426 | } | 
| Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6427 | } | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6428 |  | 
| Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6429 | if (!Specialization) { | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6430 | // Create a new class template specialization declaration node for | 
|  | 6431 | // this explicit specialization. | 
|  | 6432 | Specialization | 
| Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6433 | = ClassTemplateSpecializationDecl::Create(Context, Kind, | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6434 | ClassTemplate->getDeclContext(), | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6435 | KWLoc, TemplateNameLoc, | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6436 | ClassTemplate, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6437 | Converted.data(), | 
|  | 6438 | Converted.size(), | 
|  | 6439 | PrevDecl); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6440 | SetNestedNameSpecifier(Specialization, SS); | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6441 |  | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6442 | if (!HasNoEffect && !PrevDecl) { | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6443 | // Insert the new specialization. | 
| Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6444 | ClassTemplate->AddSpecialization(Specialization, InsertPos); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6445 | } | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6446 | } | 
|  | 6447 |  | 
|  | 6448 | // Build the fully-sugared type for this explicit instantiation as | 
|  | 6449 | // the user wrote in the explicit instantiation itself. This means | 
|  | 6450 | // that we'll pretty-print the type retrieved from the | 
|  | 6451 | // specialization's declaration the way that the user actually wrote | 
|  | 6452 | // the explicit instantiation, rather than formatting the name based | 
|  | 6453 | // on the "canonical" representation used to store the template | 
|  | 6454 | // arguments in the specialization. | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6455 | TypeSourceInfo *WrittenTy | 
|  | 6456 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, | 
|  | 6457 | TemplateArgs, | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6458 | Context.getTypeDeclType(Specialization)); | 
|  | 6459 | Specialization->setTypeAsWritten(WrittenTy); | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6460 |  | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6461 | // Set source locations for keywords. | 
|  | 6462 | Specialization->setExternLoc(ExternLoc); | 
|  | 6463 | Specialization->setTemplateKeywordLoc(TemplateLoc); | 
|  | 6464 |  | 
| Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6465 | if (Attr) | 
|  | 6466 | ProcessDeclAttributeList(S, Specialization, Attr); | 
|  | 6467 |  | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6468 | // Add the explicit instantiation into its lexical context. However, | 
|  | 6469 | // since explicit instantiations are never found by name lookup, we | 
|  | 6470 | // just put it into the declaration context directly. | 
|  | 6471 | Specialization->setLexicalDeclContext(CurContext); | 
|  | 6472 | CurContext->addDecl(Specialization); | 
|  | 6473 |  | 
|  | 6474 | // Syntax is now OK, so return if it has no other effect on semantics. | 
|  | 6475 | if (HasNoEffect) { | 
|  | 6476 | // Set the template specialization kind. | 
|  | 6477 | Specialization->setTemplateSpecializationKind(TSK); | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6478 | return Specialization; | 
| Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6479 | } | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6480 |  | 
|  | 6481 | // C++ [temp.explicit]p3: | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6482 | //   A definition of a class template or class member template | 
|  | 6483 | //   shall be in scope at the point of the explicit instantiation of | 
|  | 6484 | //   the class template or class member template. | 
|  | 6485 | // | 
|  | 6486 | // This check comes when we actually try to perform the | 
|  | 6487 | // instantiation. | 
| Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6488 | ClassTemplateSpecializationDecl *Def | 
|  | 6489 | = cast_or_null<ClassTemplateSpecializationDecl>( | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6490 | Specialization->getDefinition()); | 
| Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6491 | if (!Def) | 
| Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6492 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6493 | else if (TSK == TSK_ExplicitInstantiationDefinition) { | 
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6494 | MarkVTableUsed(TemplateNameLoc, Specialization, true); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6495 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); | 
|  | 6496 | } | 
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6497 |  | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6498 | // Instantiate the members of this class template specialization. | 
|  | 6499 | Def = cast_or_null<ClassTemplateSpecializationDecl>( | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6500 | Specialization->getDefinition()); | 
| Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6501 | if (Def) { | 
| Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6502 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); | 
|  | 6503 |  | 
|  | 6504 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a | 
|  | 6505 | // TSK_ExplicitInstantiationDefinition | 
|  | 6506 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && | 
|  | 6507 | TSK == TSK_ExplicitInstantiationDefinition) | 
|  | 6508 | Def->setTemplateSpecializationKind(TSK); | 
| Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6509 |  | 
| Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6510 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); | 
| Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6511 | } | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6512 |  | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6513 | // Set the template specialization kind. | 
|  | 6514 | Specialization->setTemplateSpecializationKind(TSK); | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6515 | return Specialization; | 
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6516 | } | 
|  | 6517 |  | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6518 | // Explicit instantiation of a member class of a class template. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6519 | DeclResult | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6520 | Sema::ActOnExplicitInstantiation(Scope *S, | 
| Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6521 | SourceLocation ExternLoc, | 
|  | 6522 | SourceLocation TemplateLoc, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6523 | unsigned TagSpec, | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6524 | SourceLocation KWLoc, | 
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6525 | CXXScopeSpec &SS, | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6526 | IdentifierInfo *Name, | 
|  | 6527 | SourceLocation NameLoc, | 
|  | 6528 | AttributeList *Attr) { | 
|  | 6529 |  | 
| Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6530 | bool Owned = false; | 
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6531 | bool IsDependent = false; | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6532 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6533 | KWLoc, SS, Name, NameLoc, Attr, AS_none, | 
| Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6534 | /*ModulePrivateLoc=*/SourceLocation(), | 
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6535 | MultiTemplateParamsArg(), Owned, IsDependent, | 
|  | 6536 | SourceLocation(), false, TypeResult()); | 
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6537 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); | 
|  | 6538 |  | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6539 | if (!TagD) | 
|  | 6540 | return true; | 
|  | 6541 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6542 | TagDecl *Tag = cast<TagDecl>(TagD); | 
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6543 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6544 |  | 
| Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6545 | if (Tag->isInvalidDecl()) | 
|  | 6546 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6547 |  | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6548 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); | 
|  | 6549 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); | 
|  | 6550 | if (!Pattern) { | 
|  | 6551 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) | 
|  | 6552 | << Context.getTypeDeclType(Record); | 
|  | 6553 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); | 
|  | 6554 | return true; | 
|  | 6555 | } | 
|  | 6556 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6557 | // C++0x [temp.explicit]p2: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6558 | //   If the explicit instantiation is for a class or member class, the | 
|  | 6559 | //   elaborated-type-specifier in the declaration shall include a | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6560 | //   simple-template-id. | 
|  | 6561 | // | 
|  | 6562 | // C++98 has the same restriction, just worded differently. | 
|  | 6563 | if (!ScopeSpecifierHasTemplateId(SS)) | 
| Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6564 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6565 | << Record << SS.getRange(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6566 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6567 | // C++0x [temp.explicit]p2: | 
|  | 6568 | //   There are two forms of explicit instantiation: an explicit instantiation | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6569 | //   definition and an explicit instantiation declaration. An explicit | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6570 | //   instantiation declaration begins with the extern keyword. [...] | 
| Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6571 | TemplateSpecializationKind TSK | 
|  | 6572 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition | 
|  | 6573 | : TSK_ExplicitInstantiationDeclaration; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6574 |  | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6575 | // C++0x [temp.explicit]p2: | 
|  | 6576 | //   [...] An explicit instantiation shall appear in an enclosing | 
|  | 6577 | //   namespace of its template. [...] | 
|  | 6578 | // | 
|  | 6579 | // This is C++ DR 275. | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6580 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6581 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6582 | // Verify that it is okay to explicitly instantiate here. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6583 | CXXRecordDecl *PrevDecl | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6584 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6585 | if (!PrevDecl && Record->getDefinition()) | 
| Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6586 | PrevDecl = Record; | 
|  | 6587 | if (PrevDecl) { | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6588 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6589 | bool HasNoEffect = false; | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6590 | assert(MSInfo && "No member specialization information?"); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6591 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6592 | PrevDecl, | 
|  | 6593 | MSInfo->getTemplateSpecializationKind(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6594 | MSInfo->getPointOfInstantiation(), | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6595 | HasNoEffect)) | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6596 | return true; | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6597 | if (HasNoEffect) | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6598 | return TagD; | 
|  | 6599 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6600 |  | 
| Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6601 | CXXRecordDecl *RecordDef | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6602 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); | 
| Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6603 | if (!RecordDef) { | 
| Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6604 | // C++ [temp.explicit]p3: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6605 | //   A definition of a member class of a class template shall be in scope | 
| Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6606 | //   at the point of an explicit instantiation of the member class. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6607 | CXXRecordDecl *Def | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6608 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); | 
| Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6609 | if (!Def) { | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6610 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) | 
|  | 6611 | << 0 << Record->getDeclName() << Record->getDeclContext(); | 
| Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6612 | Diag(Pattern->getLocation(), diag::note_forward_declaration) | 
|  | 6613 | << Pattern; | 
|  | 6614 | return true; | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6615 | } else { | 
|  | 6616 | if (InstantiateClass(NameLoc, Record, Def, | 
|  | 6617 | getTemplateInstantiationArgs(Record), | 
|  | 6618 | TSK)) | 
|  | 6619 | return true; | 
|  | 6620 |  | 
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6621 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6622 | if (!RecordDef) | 
|  | 6623 | return true; | 
|  | 6624 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6625 | } | 
|  | 6626 |  | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6627 | // Instantiate all of the members of the class. | 
|  | 6628 | InstantiateClassMembers(NameLoc, RecordDef, | 
|  | 6629 | getTemplateInstantiationArgs(Record), TSK); | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6630 |  | 
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6631 | if (TSK == TSK_ExplicitInstantiationDefinition) | 
|  | 6632 | MarkVTableUsed(NameLoc, RecordDef, true); | 
|  | 6633 |  | 
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6634 | // FIXME: We don't have any representation for explicit instantiations of | 
|  | 6635 | // member classes. Such a representation is not needed for compilation, but it | 
|  | 6636 | // should be available for clients that want to see all of the declarations in | 
|  | 6637 | // the source code. | 
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6638 | return TagD; | 
|  | 6639 | } | 
|  | 6640 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6641 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, | 
|  | 6642 | SourceLocation ExternLoc, | 
|  | 6643 | SourceLocation TemplateLoc, | 
|  | 6644 | Declarator &D) { | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6645 | // Explicit instantiations always require a name. | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6646 | // TODO: check if/when DNInfo should replace Name. | 
|  | 6647 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); | 
|  | 6648 | DeclarationName Name = NameInfo.getName(); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6649 | if (!Name) { | 
|  | 6650 | if (!D.isInvalidType()) | 
| Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6651 | Diag(D.getDeclSpec().getLocStart(), | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6652 | diag::err_explicit_instantiation_requires_name) | 
|  | 6653 | << D.getDeclSpec().getSourceRange() | 
|  | 6654 | << D.getSourceRange(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6655 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6656 | return true; | 
|  | 6657 | } | 
|  | 6658 |  | 
|  | 6659 | // The scope passed in may not be a decl scope.  Zip up the scope tree until | 
|  | 6660 | // we find one that is. | 
|  | 6661 | while ((S->getFlags() & Scope::DeclScope) == 0 || | 
|  | 6662 | (S->getFlags() & Scope::TemplateParamScope) != 0) | 
|  | 6663 | S = S->getParent(); | 
|  | 6664 |  | 
|  | 6665 | // Determine the type of the declaration. | 
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6666 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); | 
|  | 6667 | QualType R = T->getType(); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6668 | if (R.isNull()) | 
|  | 6669 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6670 |  | 
| Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6671 | // C++ [dcl.stc]p1: | 
|  | 6672 | //   A storage-class-specifier shall not be specified in [...] an explicit | 
|  | 6673 | //   instantiation (14.7.2) directive. | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6674 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6675 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) | 
|  | 6676 | << Name; | 
|  | 6677 | return true; | 
| Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6678 | } else if (D.getDeclSpec().getStorageClassSpec() | 
|  | 6679 | != DeclSpec::SCS_unspecified) { | 
|  | 6680 | // Complain about then remove the storage class specifier. | 
|  | 6681 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) | 
|  | 6682 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); | 
|  | 6683 |  | 
|  | 6684 | D.getMutableDeclSpec().ClearStorageClassSpecs(); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6685 | } | 
|  | 6686 |  | 
| Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6687 | // C++0x [temp.explicit]p1: | 
|  | 6688 | //   [...] An explicit instantiation of a function template shall not use the | 
|  | 6689 | //   inline or constexpr specifiers. | 
|  | 6690 | // Presumably, this also applies to member functions of class templates as | 
|  | 6691 | // well. | 
| Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6692 | if (D.getDeclSpec().isInlineSpecified()) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6693 | Diag(D.getDeclSpec().getInlineSpecLoc(), | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6694 | getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6695 | diag::err_explicit_instantiation_inline : | 
|  | 6696 | diag::warn_explicit_instantiation_inline_0x) | 
| Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6697 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); | 
|  | 6698 | if (D.getDeclSpec().isConstexprSpecified()) | 
|  | 6699 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is | 
|  | 6700 | // not already specified. | 
|  | 6701 | Diag(D.getDeclSpec().getConstexprSpecLoc(), | 
|  | 6702 | diag::err_explicit_instantiation_constexpr); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6703 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6704 | // C++0x [temp.explicit]p2: | 
|  | 6705 | //   There are two forms of explicit instantiation: an explicit instantiation | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6706 | //   definition and an explicit instantiation declaration. An explicit | 
|  | 6707 | //   instantiation declaration begins with the extern keyword. [...] | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6708 | TemplateSpecializationKind TSK | 
|  | 6709 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition | 
|  | 6710 | : TSK_ExplicitInstantiationDeclaration; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6711 |  | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6712 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); | 
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6713 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6714 |  | 
|  | 6715 | if (!R->isFunctionType()) { | 
|  | 6716 | // C++ [temp.explicit]p1: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6717 | //   A [...] static data member of a class template can be explicitly | 
|  | 6718 | //   instantiated from the member definition associated with its class | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6719 | //   template. | 
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6720 | if (Previous.isAmbiguous()) | 
|  | 6721 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6722 |  | 
| John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6723 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6724 | if (!Prev || !Prev->isStaticDataMember()) { | 
|  | 6725 | // We expect to see a data data member here. | 
|  | 6726 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) | 
|  | 6727 | << Name; | 
|  | 6728 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); | 
|  | 6729 | P != PEnd; ++P) | 
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6730 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6731 | return true; | 
|  | 6732 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6733 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6734 | if (!Prev->getInstantiatedFromStaticDataMember()) { | 
|  | 6735 | // FIXME: Check for explicit specialization? | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6736 | Diag(D.getIdentifierLoc(), | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6737 | diag::err_explicit_instantiation_data_member_not_instantiated) | 
|  | 6738 | << Prev; | 
|  | 6739 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); | 
|  | 6740 | // FIXME: Can we provide a note showing where this was declared? | 
|  | 6741 | return true; | 
|  | 6742 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6743 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6744 | // C++0x [temp.explicit]p2: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6745 | //   If the explicit instantiation is for a member function, a member class | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6746 | //   or a static data member of a class template specialization, the name of | 
|  | 6747 | //   the class template specialization in the qualified-id for the member | 
|  | 6748 | //   name shall be a simple-template-id. | 
|  | 6749 | // | 
|  | 6750 | // C++98 has the same restriction, just worded differently. | 
|  | 6751 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6752 | Diag(D.getIdentifierLoc(), | 
| Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6753 | diag::ext_explicit_instantiation_without_qualified_id) | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6754 | << Prev << D.getCXXScopeSpec().getRange(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6755 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6756 | // Check the scope of this explicit instantiation. | 
|  | 6757 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6758 |  | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6759 | // Verify that it is okay to explicitly instantiate here. | 
|  | 6760 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); | 
|  | 6761 | assert(MSInfo && "Missing static data member specialization info?"); | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6762 | bool HasNoEffect = false; | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6763 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6764 | MSInfo->getTemplateSpecializationKind(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6765 | MSInfo->getPointOfInstantiation(), | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6766 | HasNoEffect)) | 
| Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6767 | return true; | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6768 | if (HasNoEffect) | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6769 | return (Decl*) 0; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6770 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6771 | // Instantiate static data member. | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6772 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6773 | if (TSK == TSK_ExplicitInstantiationDefinition) | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6774 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6775 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6776 | // FIXME: Create an ExplicitInstantiation node? | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6777 | return (Decl*) 0; | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6778 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6779 |  | 
|  | 6780 | // If the declarator is a template-id, translate the parser's template | 
| Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6781 | // argument list into our AST format. | 
| Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6782 | bool HasExplicitTemplateArgs = false; | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6783 | TemplateArgumentListInfo TemplateArgs; | 
| Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6784 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { | 
|  | 6785 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6786 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); | 
|  | 6787 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); | 
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6788 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), | 
| Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6789 | TemplateId->NumArgs); | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6790 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); | 
| Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6791 | HasExplicitTemplateArgs = true; | 
|  | 6792 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6793 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6794 | // C++ [temp.explicit]p1: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6795 | //   A [...] function [...] can be explicitly instantiated from its template. | 
|  | 6796 | //   A member function [...] of a class template can be explicitly | 
|  | 6797 | //  instantiated from the member definition associated with its class | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6798 | //  template. | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6799 | UnresolvedSet<8> Matches; | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6800 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); | 
|  | 6801 | P != PEnd; ++P) { | 
|  | 6802 | NamedDecl *Prev = *P; | 
| Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6803 | if (!HasExplicitTemplateArgs) { | 
|  | 6804 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { | 
|  | 6805 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { | 
|  | 6806 | Matches.clear(); | 
| Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6807 |  | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6808 | Matches.addDecl(Method, P.getAccess()); | 
| Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6809 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) | 
|  | 6810 | break; | 
| Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6811 | } | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6812 | } | 
|  | 6813 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6814 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6815 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); | 
|  | 6816 | if (!FunTmpl) | 
|  | 6817 | continue; | 
|  | 6818 |  | 
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 6819 | TemplateDeductionInfo Info(D.getIdentifierLoc()); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6820 | FunctionDecl *Specialization = 0; | 
|  | 6821 | if (TemplateDeductionResult TDK | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6822 | = DeduceTemplateArguments(FunTmpl, | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6823 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6824 | R, Specialization, Info)) { | 
|  | 6825 | // FIXME: Keep track of almost-matches? | 
|  | 6826 | (void)TDK; | 
|  | 6827 | continue; | 
|  | 6828 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6829 |  | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6830 | Matches.addDecl(Specialization, P.getAccess()); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6831 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6832 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6833 | // Find the most specialized function template specialization. | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6834 | UnresolvedSetIterator Result | 
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6835 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6836 | D.getIdentifierLoc(), | 
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6837 | PDiag(diag::err_explicit_instantiation_not_known) << Name, | 
|  | 6838 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, | 
|  | 6839 | PDiag(diag::note_explicit_instantiation_candidate)); | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6840 |  | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6841 | if (Result == Matches.end()) | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6842 | return true; | 
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6843 |  | 
|  | 6844 | // Ignore access control bits, we don't need them for redeclaration checking. | 
|  | 6845 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6846 |  | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6847 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6848 | Diag(D.getIdentifierLoc(), | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6849 | diag::err_explicit_instantiation_member_function_not_instantiated) | 
|  | 6850 | << Specialization | 
|  | 6851 | << (Specialization->getTemplateSpecializationKind() == | 
|  | 6852 | TSK_ExplicitSpecialization); | 
|  | 6853 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); | 
|  | 6854 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6855 | } | 
|  | 6856 |  | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6857 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); | 
| Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6858 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) | 
|  | 6859 | PrevDecl = Specialization; | 
|  | 6860 |  | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6861 | if (PrevDecl) { | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6862 | bool HasNoEffect = false; | 
| Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6863 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6864 | PrevDecl, | 
|  | 6865 | PrevDecl->getTemplateSpecializationKind(), | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6866 | PrevDecl->getPointOfInstantiation(), | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6867 | HasNoEffect)) | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6868 | return true; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6869 |  | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6870 | // FIXME: We may still want to build some representation of this | 
|  | 6871 | // explicit specialization. | 
| Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6872 | if (HasNoEffect) | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6873 | return (Decl*) 0; | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6874 | } | 
| Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6875 |  | 
|  | 6876 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); | 
| Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6877 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); | 
|  | 6878 | if (Attr) | 
|  | 6879 | ProcessDeclAttributeList(S, Specialization, Attr); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6880 |  | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6881 | if (TSK == TSK_ExplicitInstantiationDefinition) | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6882 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6883 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6884 | // C++0x [temp.explicit]p2: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6885 | //   If the explicit instantiation is for a member function, a member class | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6886 | //   or a static data member of a class template specialization, the name of | 
|  | 6887 | //   the class template specialization in the qualified-id for the member | 
|  | 6888 | //   name shall be a simple-template-id. | 
|  | 6889 | // | 
|  | 6890 | // C++98 has the same restriction, just worded differently. | 
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6891 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); | 
| Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6892 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6893 | D.getCXXScopeSpec().isSet() && | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6894 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6895 | Diag(D.getIdentifierLoc(), | 
| Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6896 | diag::ext_explicit_instantiation_without_qualified_id) | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6897 | << Specialization << D.getCXXScopeSpec().getRange(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6898 |  | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6899 | CheckExplicitInstantiationScope(*this, | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6900 | FunTmpl? (NamedDecl *)FunTmpl | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6901 | : Specialization->getInstantiatedFromMemberFunction(), | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6902 | D.getIdentifierLoc(), | 
| Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6903 | D.getCXXScopeSpec().isSet()); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6904 |  | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6905 | // FIXME: Create some kind of ExplicitInstantiationDecl here. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6906 | return (Decl*) 0; | 
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6907 | } | 
|  | 6908 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6909 | TypeResult | 
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6910 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, | 
|  | 6911 | const CXXScopeSpec &SS, IdentifierInfo *Name, | 
|  | 6912 | SourceLocation TagLoc, SourceLocation NameLoc) { | 
|  | 6913 | // This has to hold, because SS is expected to be defined. | 
|  | 6914 | assert(Name && "Expected a name in a dependent tag"); | 
|  | 6915 |  | 
|  | 6916 | NestedNameSpecifier *NNS | 
|  | 6917 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); | 
|  | 6918 | if (!NNS) | 
|  | 6919 | return true; | 
|  | 6920 |  | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6921 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); | 
| Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6922 |  | 
| Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6923 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { | 
|  | 6924 | Diag(NameLoc, diag::err_dependent_tag_decl) | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6925 | << (TUK == TUK_Definition) << Kind << SS.getRange(); | 
| Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6926 | return true; | 
|  | 6927 | } | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6928 |  | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6929 | // Create the resulting type. | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6930 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6931 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); | 
|  | 6932 |  | 
|  | 6933 | // Create type-source location information for this type. | 
|  | 6934 | TypeLocBuilder TLB; | 
|  | 6935 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); | 
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6936 | TL.setElaboratedKeywordLoc(TagLoc); | 
| Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6937 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); | 
|  | 6938 | TL.setNameLoc(NameLoc); | 
|  | 6939 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); | 
| John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6940 | } | 
|  | 6941 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6942 | TypeResult | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6943 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, | 
|  | 6944 | const CXXScopeSpec &SS, const IdentifierInfo &II, | 
| Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6945 | SourceLocation IdLoc) { | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6946 | if (SS.isInvalid()) | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6947 | return true; | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6948 |  | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6949 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) | 
|  | 6950 | Diag(TypenameLoc, | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6951 | getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6952 | diag::warn_cxx98_compat_typename_outside_of_template : | 
|  | 6953 | diag::ext_typename_outside_of_template) | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6954 | << FixItHint::CreateRemoval(TypenameLoc); | 
|  | 6955 |  | 
| Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6956 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); | 
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6957 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, | 
|  | 6958 | TypenameLoc, QualifierLoc, II, IdLoc); | 
| Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6959 | if (T.isNull()) | 
|  | 6960 | return true; | 
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6961 |  | 
|  | 6962 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); | 
|  | 6963 | if (isa<DependentNameType>(T)) { | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 6964 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); | 
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6965 | TL.setElaboratedKeywordLoc(TypenameLoc); | 
| Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6966 | TL.setQualifierLoc(QualifierLoc); | 
| John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6967 | TL.setNameLoc(IdLoc); | 
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6968 | } else { | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 6969 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); | 
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6970 | TL.setElaboratedKeywordLoc(TypenameLoc); | 
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6971 | TL.setQualifierLoc(QualifierLoc); | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 6972 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); | 
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6973 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6974 |  | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6975 | return CreateParsedType(T, TSI); | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6976 | } | 
|  | 6977 |  | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6978 | TypeResult | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6979 | Sema::ActOnTypenameType(Scope *S, | 
|  | 6980 | SourceLocation TypenameLoc, | 
|  | 6981 | const CXXScopeSpec &SS, | 
|  | 6982 | SourceLocation TemplateKWLoc, | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6983 | TemplateTy TemplateIn, | 
|  | 6984 | SourceLocation TemplateNameLoc, | 
|  | 6985 | SourceLocation LAngleLoc, | 
|  | 6986 | ASTTemplateArgsPtr TemplateArgsIn, | 
|  | 6987 | SourceLocation RAngleLoc) { | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6988 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) | 
|  | 6989 | Diag(TypenameLoc, | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6990 | getLangOpts().CPlusPlus11 ? | 
| Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6991 | diag::warn_cxx98_compat_typename_outside_of_template : | 
|  | 6992 | diag::ext_typename_outside_of_template) | 
|  | 6993 | << FixItHint::CreateRemoval(TypenameLoc); | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6994 |  | 
|  | 6995 | // Translate the parser's template argument list in our AST format. | 
|  | 6996 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); | 
|  | 6997 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); | 
|  | 6998 |  | 
|  | 6999 | TemplateName Template = TemplateIn.get(); | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7000 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { | 
|  | 7001 | // Construct a dependent template specialization type. | 
|  | 7002 | assert(DTN && "dependent template has non-dependent name?"); | 
|  | 7003 | assert(DTN->getQualifier() | 
|  | 7004 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); | 
|  | 7005 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, | 
|  | 7006 | DTN->getQualifier(), | 
|  | 7007 | DTN->getIdentifier(), | 
|  | 7008 | TemplateArgs); | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7009 |  | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7010 | // Create source-location information for this type. | 
| John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7011 | TypeLocBuilder Builder; | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7012 | DependentTemplateSpecializationTypeLoc SpecTL | 
|  | 7013 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7014 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); | 
|  | 7015 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); | 
| Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 7016 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7017 | SpecTL.setTemplateNameLoc(TemplateNameLoc); | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7018 | SpecTL.setLAngleLoc(LAngleLoc); | 
|  | 7019 | SpecTL.setRAngleLoc(RAngleLoc); | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7020 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | 
|  | 7021 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7022 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); | 
| Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 7023 | } | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7024 |  | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7025 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); | 
|  | 7026 | if (T.isNull()) | 
|  | 7027 | return true; | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7028 |  | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7029 | // Provide source-location information for the template specialization type. | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7030 | TypeLocBuilder Builder; | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7031 | TemplateSpecializationTypeLoc SpecTL | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7032 | = Builder.push<TemplateSpecializationTypeLoc>(T); | 
| Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7033 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); | 
|  | 7034 | SpecTL.setTemplateNameLoc(TemplateNameLoc); | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7035 | SpecTL.setLAngleLoc(LAngleLoc); | 
|  | 7036 | SpecTL.setRAngleLoc(RAngleLoc); | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7037 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | 
|  | 7038 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); | 
|  | 7039 |  | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7040 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); | 
|  | 7041 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); | 
| Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7042 | TL.setElaboratedKeywordLoc(TypenameLoc); | 
| Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7043 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); | 
|  | 7044 |  | 
| Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7045 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); | 
|  | 7046 | return CreateParsedType(T, TSI); | 
| Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 7047 | } | 
|  | 7048 |  | 
| Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7049 |  | 
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7050 | /// Determine whether this failed name lookup should be treated as being | 
|  | 7051 | /// disabled by a usage of std::enable_if. | 
|  | 7052 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, | 
|  | 7053 | SourceRange &CondRange) { | 
|  | 7054 | // We must be looking for a ::type... | 
|  | 7055 | if (!II.isStr("type")) | 
|  | 7056 | return false; | 
|  | 7057 |  | 
|  | 7058 | // ... within an explicitly-written template specialization... | 
|  | 7059 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) | 
|  | 7060 | return false; | 
|  | 7061 | TypeLoc EnableIfTy = NNS.getTypeLoc(); | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7062 | TemplateSpecializationTypeLoc EnableIfTSTLoc = | 
|  | 7063 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); | 
|  | 7064 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) | 
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7065 | return false; | 
|  | 7066 | const TemplateSpecializationType *EnableIfTST = | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7067 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); | 
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7068 |  | 
|  | 7069 | // ... which names a complete class template declaration... | 
|  | 7070 | const TemplateDecl *EnableIfDecl = | 
|  | 7071 | EnableIfTST->getTemplateName().getAsTemplateDecl(); | 
|  | 7072 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) | 
|  | 7073 | return false; | 
|  | 7074 |  | 
|  | 7075 | // ... called "enable_if". | 
|  | 7076 | const IdentifierInfo *EnableIfII = | 
|  | 7077 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); | 
|  | 7078 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) | 
|  | 7079 | return false; | 
|  | 7080 |  | 
|  | 7081 | // Assume the first template argument is the condition. | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7082 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); | 
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7083 | return true; | 
|  | 7084 | } | 
|  | 7085 |  | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7086 | /// \brief Build the type that describes a C++ typename specifier, | 
|  | 7087 | /// e.g., "typename T::type". | 
|  | 7088 | QualType | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7089 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, | 
|  | 7090 | SourceLocation KeywordLoc, | 
|  | 7091 | NestedNameSpecifierLoc QualifierLoc, | 
|  | 7092 | const IdentifierInfo &II, | 
| Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7093 | SourceLocation IILoc) { | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7094 | CXXScopeSpec SS; | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7095 | SS.Adopt(QualifierLoc); | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7096 |  | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7097 | DeclContext *Ctx = computeDeclContext(SS); | 
|  | 7098 | if (!Ctx) { | 
|  | 7099 | // If the nested-name-specifier is dependent and couldn't be | 
|  | 7100 | // resolved to a type, build a typename type. | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7101 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); | 
|  | 7102 | return Context.getDependentNameType(Keyword, | 
|  | 7103 | QualifierLoc.getNestedNameSpecifier(), | 
|  | 7104 | &II); | 
| Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 7105 | } | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7106 |  | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7107 | // If the nested-name-specifier refers to the current instantiation, | 
|  | 7108 | // the "typename" keyword itself is superfluous. In C++03, the | 
|  | 7109 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) | 
|  | 7110 | // allows such extraneous "typename" keywords, and we retroactively | 
| Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 7111 | // apply this DR to C++03 code with only a warning. In any case we continue. | 
| Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 7112 |  | 
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7113 | if (RequireCompleteDeclContext(SS, Ctx)) | 
|  | 7114 | return QualType(); | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7115 |  | 
|  | 7116 | DeclarationName Name(&II); | 
| Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7117 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); | 
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7118 | LookupQualifiedName(Result, Ctx); | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7119 | unsigned DiagID = 0; | 
|  | 7120 | Decl *Referenced = 0; | 
| John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7121 | switch (Result.getResultKind()) { | 
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7122 | case LookupResult::NotFound: { | 
|  | 7123 | // If we're looking up 'type' within a template named 'enable_if', produce | 
|  | 7124 | // a more specific diagnostic. | 
|  | 7125 | SourceRange CondRange; | 
|  | 7126 | if (isEnableIf(QualifierLoc, II, CondRange)) { | 
|  | 7127 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) | 
|  | 7128 | << Ctx << CondRange; | 
|  | 7129 | return QualType(); | 
|  | 7130 | } | 
|  | 7131 |  | 
| Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 7132 | DiagID = diag::err_typename_nested_not_found; | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7133 | break; | 
| Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7134 | } | 
| Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7135 |  | 
|  | 7136 | case LookupResult::FoundUnresolvedValue: { | 
|  | 7137 | // We found a using declaration that is a value. Most likely, the using | 
|  | 7138 | // declaration itself is meant to have the 'typename' keyword. | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7139 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), | 
| Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7140 | IILoc); | 
|  | 7141 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) | 
|  | 7142 | << Name << Ctx << FullRange; | 
|  | 7143 | if (UnresolvedUsingValueDecl *Using | 
|  | 7144 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ | 
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7145 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); | 
| Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7146 | Diag(Loc, diag::note_using_value_decl_missing_typename) | 
|  | 7147 | << FixItHint::CreateInsertion(Loc, "typename "); | 
|  | 7148 | } | 
|  | 7149 | } | 
|  | 7150 | // Fall through to create a dependent typename type, from which we can recover | 
|  | 7151 | // better. | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7152 |  | 
| Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 7153 | case LookupResult::NotFoundInCurrentInstantiation: | 
|  | 7154 | // Okay, it's a member of an unknown instantiation. | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7155 | return Context.getDependentNameType(Keyword, | 
|  | 7156 | QualifierLoc.getNestedNameSpecifier(), | 
|  | 7157 | &II); | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7158 |  | 
|  | 7159 | case LookupResult::Found: | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7160 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7161 | // We found a type. Build an ElaboratedType, since the | 
|  | 7162 | // typename-specifier was just sugar. | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7163 | return Context.getElaboratedType(ETK_Typename, | 
|  | 7164 | QualifierLoc.getNestedNameSpecifier(), | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7165 | Context.getTypeDeclType(Type)); | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7166 | } | 
|  | 7167 |  | 
|  | 7168 | DiagID = diag::err_typename_nested_not_type; | 
| John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7169 | Referenced = Result.getFoundDecl(); | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7170 | break; | 
|  | 7171 |  | 
|  | 7172 | case LookupResult::FoundOverloaded: | 
|  | 7173 | DiagID = diag::err_typename_nested_not_type; | 
|  | 7174 | Referenced = *Result.begin(); | 
|  | 7175 | break; | 
|  | 7176 |  | 
| John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 7177 | case LookupResult::Ambiguous: | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7178 | return QualType(); | 
|  | 7179 | } | 
|  | 7180 |  | 
|  | 7181 | // If we get here, it's because name lookup did not find a | 
|  | 7182 | // type. Emit an appropriate diagnostic and return an error. | 
| Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7183 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), | 
| Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7184 | IILoc); | 
|  | 7185 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; | 
| Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7186 | if (Referenced) | 
|  | 7187 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) | 
|  | 7188 | << Name; | 
|  | 7189 | return QualType(); | 
|  | 7190 | } | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7191 |  | 
|  | 7192 | namespace { | 
|  | 7193 | // See Sema::RebuildTypeInCurrentInstantiation | 
| Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 7194 | class CurrentInstantiationRebuilder | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7195 | : public TreeTransform<CurrentInstantiationRebuilder> { | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7196 | SourceLocation Loc; | 
|  | 7197 | DeclarationName Entity; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7198 |  | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7199 | public: | 
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 7200 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7201 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7202 | CurrentInstantiationRebuilder(Sema &SemaRef, | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7203 | SourceLocation Loc, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7204 | DeclarationName Entity) | 
|  | 7205 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7206 | Loc(Loc), Entity(Entity) { } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7207 |  | 
|  | 7208 | /// \brief Determine whether the given type \p T has already been | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7209 | /// transformed. | 
|  | 7210 | /// | 
|  | 7211 | /// For the purposes of type reconstruction, a type has already been | 
|  | 7212 | /// transformed if it is NULL or if it is not dependent. | 
|  | 7213 | bool AlreadyTransformed(QualType T) { | 
|  | 7214 | return T.isNull() || !T->isDependentType(); | 
|  | 7215 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7216 |  | 
|  | 7217 | /// \brief Returns the location of the entity whose type is being | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7218 | /// rebuilt. | 
|  | 7219 | SourceLocation getBaseLocation() { return Loc; } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7220 |  | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7221 | /// \brief Returns the name of the entity whose type is being rebuilt. | 
|  | 7222 | DeclarationName getBaseEntity() { return Entity; } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7223 |  | 
| Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7224 | /// \brief Sets the "base" location and entity when that | 
|  | 7225 | /// information is known based on another transformation. | 
|  | 7226 | void setBase(SourceLocation Loc, DeclarationName Entity) { | 
|  | 7227 | this->Loc = Loc; | 
|  | 7228 | this->Entity = Entity; | 
|  | 7229 | } | 
| Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 7230 |  | 
|  | 7231 | ExprResult TransformLambdaExpr(LambdaExpr *E) { | 
|  | 7232 | // Lambdas never need to be transformed. | 
|  | 7233 | return E; | 
|  | 7234 | } | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7235 | }; | 
|  | 7236 | } | 
|  | 7237 |  | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7238 | /// \brief Rebuilds a type within the context of the current instantiation. | 
|  | 7239 | /// | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7240 | /// The type \p T is part of the type of an out-of-line member definition of | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7241 | /// a class template (or class template partial specialization) that was parsed | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7242 | /// and constructed before we entered the scope of the class template (or | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7243 | /// partial specialization thereof). This routine will rebuild that type now | 
|  | 7244 | /// that we have entered the declarator's scope, which may produce different | 
|  | 7245 | /// canonical types, e.g., | 
|  | 7246 | /// | 
|  | 7247 | /// \code | 
|  | 7248 | /// template<typename T> | 
|  | 7249 | /// struct X { | 
|  | 7250 | ///   typedef T* pointer; | 
|  | 7251 | ///   pointer data(); | 
|  | 7252 | /// }; | 
|  | 7253 | /// | 
|  | 7254 | /// template<typename T> | 
|  | 7255 | /// typename X<T>::pointer X<T>::data() { ... } | 
|  | 7256 | /// \endcode | 
|  | 7257 | /// | 
| Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 7258 | /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7259 | /// since we do not know that we can look into X<T> when we parsed the type. | 
|  | 7260 | /// This function will rebuild the type, performing the lookup of "pointer" | 
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7261 | /// in X<T> and returning an ElaboratedType whose canonical type is the same | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7262 | /// as the canonical type of T*, allowing the return types of the out-of-line | 
|  | 7263 | /// definition and the declaration to match. | 
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7264 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, | 
|  | 7265 | SourceLocation Loc, | 
|  | 7266 | DeclarationName Name) { | 
|  | 7267 | if (!T || !T->getType()->isDependentType()) | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7268 | return T; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7269 |  | 
| Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7270 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); | 
|  | 7271 | return Rebuilder.TransformType(T); | 
| Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 7272 | } | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7273 |  | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7274 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { | 
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7275 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), | 
|  | 7276 | DeclarationName()); | 
|  | 7277 | return Rebuilder.TransformExpr(E); | 
|  | 7278 | } | 
|  | 7279 |  | 
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7280 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { | 
| Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7281 | if (SS.isInvalid()) | 
|  | 7282 | return true; | 
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7283 |  | 
| Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7284 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); | 
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7285 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), | 
|  | 7286 | DeclarationName()); | 
| Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7287 | NestedNameSpecifierLoc Rebuilt | 
|  | 7288 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); | 
|  | 7289 | if (!Rebuilt) | 
|  | 7290 | return true; | 
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7291 |  | 
| Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7292 | SS.Adopt(Rebuilt); | 
| John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7293 | return false; | 
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7294 | } | 
|  | 7295 |  | 
| Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 7296 | /// \brief Rebuild the template parameters now that we know we're in a current | 
|  | 7297 | /// instantiation. | 
|  | 7298 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( | 
|  | 7299 | TemplateParameterList *Params) { | 
|  | 7300 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { | 
|  | 7301 | Decl *Param = Params->getParam(I); | 
|  | 7302 |  | 
|  | 7303 | // There is nothing to rebuild in a type parameter. | 
|  | 7304 | if (isa<TemplateTypeParmDecl>(Param)) | 
|  | 7305 | continue; | 
|  | 7306 |  | 
|  | 7307 | // Rebuild the template parameter list of a template template parameter. | 
|  | 7308 | if (TemplateTemplateParmDecl *TTP | 
|  | 7309 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { | 
|  | 7310 | if (RebuildTemplateParamsInCurrentInstantiation( | 
|  | 7311 | TTP->getTemplateParameters())) | 
|  | 7312 | return true; | 
|  | 7313 |  | 
|  | 7314 | continue; | 
|  | 7315 | } | 
|  | 7316 |  | 
|  | 7317 | // Rebuild the type of a non-type template parameter. | 
|  | 7318 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); | 
|  | 7319 | TypeSourceInfo *NewTSI | 
|  | 7320 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), | 
|  | 7321 | NTTP->getLocation(), | 
|  | 7322 | NTTP->getDeclName()); | 
|  | 7323 | if (!NewTSI) | 
|  | 7324 | return true; | 
|  | 7325 |  | 
|  | 7326 | if (NewTSI != NTTP->getTypeSourceInfo()) { | 
|  | 7327 | NTTP->setTypeSourceInfo(NewTSI); | 
|  | 7328 | NTTP->setType(NewTSI->getType()); | 
|  | 7329 | } | 
|  | 7330 | } | 
|  | 7331 |  | 
|  | 7332 | return false; | 
|  | 7333 | } | 
|  | 7334 |  | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7335 | /// \brief Produces a formatted string that describes the binding of | 
|  | 7336 | /// template parameters to template arguments. | 
|  | 7337 | std::string | 
|  | 7338 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, | 
|  | 7339 | const TemplateArgumentList &Args) { | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7340 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); | 
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7341 | } | 
|  | 7342 |  | 
|  | 7343 | std::string | 
|  | 7344 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, | 
|  | 7345 | const TemplateArgument *Args, | 
|  | 7346 | unsigned NumArgs) { | 
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7347 | SmallString<128> Str; | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7348 | llvm::raw_svector_ostream Out(Str); | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7349 |  | 
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7350 | if (!Params || Params->size() == 0 || NumArgs == 0) | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7351 | return std::string(); | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7352 |  | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7353 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { | 
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7354 | if (I >= NumArgs) | 
|  | 7355 | break; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7356 |  | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7357 | if (I == 0) | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7358 | Out << "[with "; | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7359 | else | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7360 | Out << ", "; | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7361 |  | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7362 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7363 | Out << Id->getName(); | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7364 | } else { | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7365 | Out << '$' << I; | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7366 | } | 
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7367 |  | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7368 | Out << " = "; | 
| Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7369 | Args[I].print(getPrintingPolicy(), Out); | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7370 | } | 
| Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7371 |  | 
|  | 7372 | Out << ']'; | 
|  | 7373 | return Out.str(); | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7374 | } | 
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 7375 |  | 
|  | 7376 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { | 
|  | 7377 | if (!FD) | 
|  | 7378 | return; | 
|  | 7379 | FD->setLateTemplateParsed(Flag); | 
|  | 7380 | } | 
|  | 7381 |  | 
|  | 7382 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { | 
|  | 7383 | DeclContext *DC = CurContext; | 
|  | 7384 |  | 
|  | 7385 | while (DC) { | 
|  | 7386 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { | 
|  | 7387 | const FunctionDecl *FD = RD->isLocalClass(); | 
|  | 7388 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); | 
|  | 7389 | } else if (DC->isTranslationUnit() || DC->isNamespace()) | 
|  | 7390 | return false; | 
|  | 7391 |  | 
|  | 7392 | DC = DC->getParent(); | 
|  | 7393 | } | 
|  | 7394 | return false; | 
|  | 7395 | } |