blob: 680e6eaf2e50e17cc067d1e963e7ea75bc1bbdab [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
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 Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer013b3662012-01-30 16:17:39 +000029#include "llvm/ADT/SmallBitVector.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000030#include "llvm/ADT/SmallString.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000031#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000032using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000033using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000034
John McCall78b81052010-11-10 02:40:36 +000035// Exported for use by Parser.
36SourceRange
37clang::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 Gregor2dd078a2009-09-02 22:59:36 +000043/// \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 McCallad00b772010-06-16 08:42:20 +000046static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000047 NamedDecl *Orig,
48 bool AllowFunctionTemplates) {
John McCallad00b772010-06-16 08:42:20 +000049 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000051 if (isa<TemplateDecl>(D)) {
52 if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D))
53 return 0;
54
John McCallad00b772010-06-16 08:42:20 +000055 return Orig;
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000056 }
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregor2dd078a2009-09-02 22:59:36 +000058 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 Gregor542b5482009-10-14 17:30:58 +000071 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000072 if (Record->getDescribedClassTemplate())
73 return Record->getDescribedClassTemplate();
74
75 if (ClassTemplateSpecializationDecl *Spec
76 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
77 return Spec->getSpecializedTemplate();
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor2dd078a2009-09-02 22:59:36 +000080 return 0;
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Douglas Gregor2dd078a2009-09-02 22:59:36 +000083 return 0;
84}
85
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000086void Sema::FilterAcceptableTemplateNames(LookupResult &R,
87 bool AllowFunctionTemplates) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 // The set of class templates we've already seen.
89 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000090 LookupResult::Filter filter = R.makeFilter();
91 while (filter.hasNext()) {
92 NamedDecl *Orig = filter.next();
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +000093 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig,
94 AllowFunctionTemplates);
John McCallf7a1a742009-11-24 19:00:30 +000095 if (!Repl)
96 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 else if (Repl != Orig) {
98
99 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000100 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000101 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000102 // 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 Smith3e4c6c42011-05-05 21:57:07 +0000104 // is used as a template-name, the reference refers to the class
105 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000106 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000107 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
108 if (!ClassTemplates.insert(ClassTmpl)) {
109 filter.erase();
110 continue;
111 }
John McCall8ba66912010-08-13 07:02:08 +0000112
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 Gregor01e56ae2010-04-12 20:54:26 +0000119 }
John McCallf7a1a742009-11-24 19:00:30 +0000120 }
121 filter.done();
122}
123
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000124bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R,
125 bool AllowFunctionTemplates) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000126 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000127 if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates))
Douglas Gregor312eadb2011-04-24 05:37:28 +0000128 return true;
129
Douglas Gregor3b887352011-04-27 04:48:22 +0000130 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000131}
132
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000133TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000134 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000135 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000137 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000138 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000139 TemplateTy &TemplateResult,
140 bool &MemberOfUnknownSpecialization) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000141 assert(getLangOpts().CPlusPlus && "No template names in C!");
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000142
Douglas Gregor014e88d2009-11-03 23:16:33 +0000143 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000144 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000145
Douglas Gregor014e88d2009-11-03 23:16:33 +0000146 switch (Name.getKind()) {
147 case UnqualifiedId::IK_Identifier:
148 TName = DeclarationName(Name.Identifier);
149 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150
Douglas Gregor014e88d2009-11-03 23:16:33 +0000151 case UnqualifiedId::IK_OperatorFunctionId:
152 TName = Context.DeclarationNames.getCXXOperatorName(
153 Name.OperatorFunctionId.Operator);
154 break;
155
Sean Hunte6252d12009-11-28 08:58:14 +0000156 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000157 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
158 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000159
Douglas Gregor014e88d2009-11-03 23:16:33 +0000160 default:
161 return TNK_Non_template;
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
John McCallb3d87482010-08-24 05:47:05 +0000164 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Daniel Dunbar96a00142012-03-09 18:35:03 +0000166 LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000167 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
168 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000169 if (R.empty()) return TNK_Non_template;
170 if (R.isAmbiguous()) {
171 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000172 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000173
174 // FIXME: we might have ambiguous templates, in which case we
175 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000177 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000178
John McCall0bd6feb2009-12-02 08:04:21 +0000179 TemplateName Template;
180 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
John McCall0bd6feb2009-12-02 08:04:21 +0000182 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 McCallb8592062010-08-13 02:23:42 +0000188
189 // We'll do this lookup again later.
190 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000191 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000192 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
193
194 if (SS.isSet() && !SS.isInvalid()) {
195 NestedNameSpecifier *Qualifier
196 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000197 Template = Context.getQualifiedTemplateName(Qualifier,
198 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000199 } else {
200 Template = TemplateName(TD);
201 }
202
John McCallb8592062010-08-13 02:23:42 +0000203 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000204 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000205
206 // We'll do this lookup again later.
207 R.suppressDiagnostics();
208 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000209 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
210 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000211 TemplateKind = TNK_Type_template;
212 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
John McCall0bd6feb2009-12-02 08:04:21 +0000215 TemplateResult = TemplateTy::make(Template);
216 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000217}
218
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000219bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000220 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 Gregord5ab9b02010-05-21 23:43:39 +0000227 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000228 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
229 computeDeclContext(*SS))
230 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000231
Douglas Gregor84d0a192010-01-12 21:28:44 +0000232 // 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 Gregor849b2432010-03-31 17:46:05 +0000237 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000238 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000239 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
240 SuggestedKind = TNK_Dependent_template_name;
241 return true;
242}
243
John McCallf7a1a742009-11-24 19:00:30 +0000244void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000245 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000246 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000247 bool EnteringContext,
248 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000249 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000250 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000251 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 Takumidfbb02a2011-01-27 07:10:08 +0000259 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000260 "Caller should have completed object type");
Douglas Gregor1d7049a2012-01-12 16:11:24 +0000261
262 // Template names cannot appear inside an Objective-C class or object type.
263 if (ObjectType->isObjCObjectOrInterfaceType()) {
264 Found.clear();
265 return;
266 }
John McCallf7a1a742009-11-24 19:00:30 +0000267 } 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 Takumidfbb02a2011-01-27 07:10:08 +0000272
John McCallf7a1a742009-11-24 19:00:30 +0000273 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000274 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000275 return;
276 }
277
278 bool ObjectTypeSearchedInScope = false;
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000279 bool AllowFunctionTemplatesInLookup = true;
John McCallf7a1a742009-11-24 19:00:30 +0000280 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 McCallf7a1a742009-11-24 19:00:30 +0000286 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 McCallf7a1a742009-11-24 19:00:30 +0000296 if (S) LookupName(Found, S);
297 ObjectTypeSearchedInScope = true;
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000298 AllowFunctionTemplatesInLookup = false;
John McCallf7a1a742009-11-24 19:00:30 +0000299 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000300 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000301 // We cannot look into a dependent object type or nested nme
302 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000303 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000304 return;
305 } else {
306 // Perform unqualified name lookup in the current scope.
307 LookupName(Found, S);
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000308
309 if (!ObjectType.isNull())
310 AllowFunctionTemplatesInLookup = false;
John McCallf7a1a742009-11-24 19:00:30 +0000311 }
312
Douglas Gregor2e933882010-01-12 17:06:20 +0000313 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000314 // If we did not find any names, attempt to correct any typos.
315 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000316 Found.clear();
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000317 // 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 Gregord8bba9c2011-06-28 16:20:02 +0000323 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
324 Found.getLookupKind(), S, &SS,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000325 FilterCCC, LookupCtx)) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000326 Found.setLookupName(Corrected.getCorrection());
327 if (Corrected.getCorrectionDecl())
328 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000329 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000330 if (!Found.empty()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000331 std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
332 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000333 if (LookupCtx)
334 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000335 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
336 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000337 else
338 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000339 << Name << CorrectedQuotedStr
340 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000341 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
342 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000343 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000344 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000345 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000346 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000347 }
348 }
349
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000350 FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000351 if (Found.empty()) {
352 if (isDependent)
353 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000354 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000355 }
John McCallf7a1a742009-11-24 19:00:30 +0000356
357 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
358 // C++ [basic.lookup.classref]p1:
359 // [...] If the lookup in the class of the object expression finds a
360 // template, the name is also looked up in the context of the entire
361 // postfix-expression and [...]
362 //
363 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
364 LookupOrdinaryName);
365 LookupName(FoundOuter, S);
Douglas Gregor5a7a5bb2012-03-10 23:52:41 +0000366 FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000367
John McCallf7a1a742009-11-24 19:00:30 +0000368 if (FoundOuter.empty()) {
369 // - if the name is not found, the name found in the class of the
370 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000371 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
372 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000373 // - if the name is found in the context of the entire
374 // postfix-expression and does not name a class template, the name
375 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000376 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000377 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000378 // - if the name found is a class template, it must refer to the same
379 // entity as the one found in the class of the object expression,
380 // otherwise the program is ill-formed.
381 if (!Found.isSingleResult() ||
382 Found.getFoundDecl()->getCanonicalDecl()
383 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000384 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000385 diag::ext_nested_name_member_ref_lookup_ambiguous)
386 << Found.getLookupName()
387 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000388 Diag(Found.getRepresentativeDecl()->getLocation(),
389 diag::note_ambig_member_ref_object_type)
390 << ObjectType;
391 Diag(FoundOuter.getFoundDecl()->getLocation(),
392 diag::note_ambig_member_ref_scope);
393
394 // Recover by taking the template that we found in the object
395 // expression's type.
396 }
397 }
398 }
399}
400
John McCall2f841ba2009-12-02 03:53:29 +0000401/// ActOnDependentIdExpression - Handle a dependent id-expression that
402/// was just parsed. This is only possible with an explicit scope
403/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000404ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000405Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000406 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000407 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000408 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000409 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000410 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000411
John McCall2f841ba2009-12-02 03:53:29 +0000412 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000413 isa<CXXMethodDecl>(DC) &&
414 cast<CXXMethodDecl>(DC)->isInstance()) {
415 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000416
John McCallf7a1a742009-11-24 19:00:30 +0000417 // Since the 'this' expression is synthesized, we don't need to
418 // perform the double-lookup check.
419 NamedDecl *FirstQualifierInScope = 0;
420
John McCallaa81e162009-12-01 22:10:20 +0000421 return Owned(CXXDependentScopeMemberExpr::Create(Context,
422 /*This*/ 0, ThisType,
423 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000424 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000425 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000426 TemplateKWLoc,
John McCallf7a1a742009-11-24 19:00:30 +0000427 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000428 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000429 TemplateArgs));
430 }
431
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000432 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000433}
434
John McCall60d7b3a2010-08-24 06:29:42 +0000435ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000436Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000437 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000438 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000439 const TemplateArgumentListInfo *TemplateArgs) {
440 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000441 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000442 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000443 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000444 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000445}
446
Douglas Gregor72c3f312008-12-05 18:15:24 +0000447/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
448/// that the template parameter 'PrevDecl' is being shadowed by a new
449/// declaration at location Loc. Returns true to indicate that this is
450/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000451void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000452 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000453
454 // Microsoft Visual C++ permits template parameters to be shadowed.
David Blaikie4e4d0842012-03-11 07:00:24 +0000455 if (getLangOpts().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000456 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000457
458 // C++ [temp.local]p4:
459 // A template-parameter shall not be redeclared within its
460 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000461 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000462 << cast<NamedDecl>(PrevDecl)->getDeclName();
463 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000464 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000465}
466
Douglas Gregor2943aed2009-03-03 04:44:36 +0000467/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000468/// the parameter D to reference the templated declaration and return a pointer
469/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000470TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
471 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
472 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000473 return Temp;
474 }
475 return 0;
476}
477
Douglas Gregorba68eca2011-01-05 17:40:24 +0000478ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
479 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000480 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000481 "Only template template arguments can be pack expansions here");
482 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
483 "Template template argument pack expansion without packs");
484 ParsedTemplateArgument Result(*this);
485 Result.EllipsisLoc = EllipsisLoc;
486 return Result;
487}
488
Douglas Gregor788cd062009-11-11 01:00:40 +0000489static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
490 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000491
Douglas Gregor788cd062009-11-11 01:00:40 +0000492 switch (Arg.getKind()) {
493 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000494 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000495 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000496 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000497 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000498 return TemplateArgumentLoc(TemplateArgument(T), DI);
499 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000500
Douglas Gregor788cd062009-11-11 01:00:40 +0000501 case ParsedTemplateArgument::NonType: {
502 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
503 return TemplateArgumentLoc(TemplateArgument(E), E);
504 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000505
Douglas Gregor788cd062009-11-11 01:00:40 +0000506 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000507 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000508 TemplateArgument TArg;
509 if (Arg.getEllipsisLoc().isValid())
510 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
511 else
512 TArg = Template;
513 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000514 Arg.getScopeSpec().getWithLocInContext(
515 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000516 Arg.getLocation(),
517 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000518 }
519 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000520
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000521 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000522}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000523
Douglas Gregor788cd062009-11-11 01:00:40 +0000524/// \brief Translates template arguments as provided by the parser
525/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000526void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
527 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000528 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000529 TemplateArgs.addArgument(translateTemplateArgument(*this,
530 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000531}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000532
Douglas Gregor72c3f312008-12-05 18:15:24 +0000533/// ActOnTypeParameter - Called when a C++ template type parameter
534/// (e.g., "typename T") has been parsed. Typename specifies whether
535/// the keyword "typename" was used to declare the type parameter
536/// (otherwise, "class" was used), and KeyLoc is the location of the
537/// "class" or "typename" keyword. ParamName is the name of the
538/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000539/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000540/// If the type parameter has a default argument, it will be added
541/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000542Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
543 SourceLocation EllipsisLoc,
544 SourceLocation KeyLoc,
545 IdentifierInfo *ParamName,
546 SourceLocation ParamNameLoc,
547 unsigned Depth, unsigned Position,
548 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000549 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000550 assert(S->isTemplateParamScope() &&
551 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000552 bool Invalid = false;
553
554 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000555 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000556 LookupOrdinaryName,
557 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000558 if (PrevDecl && PrevDecl->isTemplateParameter()) {
559 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
560 PrevDecl = 0;
561 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000562 }
563
Douglas Gregorddc29e12009-02-06 22:42:48 +0000564 SourceLocation Loc = ParamNameLoc;
565 if (!ParamName)
566 Loc = KeyLoc;
567
Douglas Gregor72c3f312008-12-05 18:15:24 +0000568 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000569 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000570 KeyLoc, Loc, Depth, Position, ParamName,
571 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000572 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000573 if (Invalid)
574 Param->setInvalidDecl();
575
576 if (ParamName) {
577 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000578 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000579 IdResolver.AddDecl(Param);
580 }
581
Douglas Gregor61c4d282011-01-05 15:48:55 +0000582 // C++0x [temp.param]p9:
583 // A default template-argument may be specified for any kind of
584 // template-parameter that is not a template parameter pack.
585 if (DefaultArg && Ellipsis) {
586 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
587 DefaultArg = ParsedType();
588 }
589
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000590 // Handle the default argument, if provided.
591 if (DefaultArg) {
592 TypeSourceInfo *DefaultTInfo;
593 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000594
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000595 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000596
Douglas Gregor6f526752010-12-16 08:48:57 +0000597 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000598 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000599 UPPC_DefaultArgument))
600 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000601
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000602 // Check the template argument itself.
603 if (CheckTemplateArgument(Param, DefaultTInfo)) {
604 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000605 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000606 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000607
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000608 Param->setDefaultArgument(DefaultTInfo, false);
609 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000610
John McCalld226f652010-08-21 09:40:31 +0000611 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000612}
613
Douglas Gregor2943aed2009-03-03 04:44:36 +0000614/// \brief Check that the type of a non-type template parameter is
615/// well-formed.
616///
617/// \returns the (possibly-promoted) parameter type if valid;
618/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000619QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000620Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000621 // We don't allow variably-modified types as the type of non-type template
622 // parameters.
623 if (T->isVariablyModifiedType()) {
624 Diag(Loc, diag::err_variably_modified_nontype_template_param)
625 << T;
626 return QualType();
627 }
628
Douglas Gregor2943aed2009-03-03 04:44:36 +0000629 // C++ [temp.param]p4:
630 //
631 // A non-type template-parameter shall have one of the following
632 // (optionally cv-qualified) types:
633 //
634 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000635 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000636 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000637 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000638 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000639 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000640 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000641 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000642 // -- std::nullptr_t.
643 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000644 // If T is a dependent type, we can't do the check now, so we
645 // assume that it is well-formed.
Richard Smithe37f4842012-03-13 07:21:50 +0000646 T->isDependentType()) {
647 // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter
648 // are ignored when determining its type.
649 return T.getUnqualifiedType();
650 }
651
Douglas Gregor2943aed2009-03-03 04:44:36 +0000652 // C++ [temp.param]p8:
653 //
654 // A non-type template-parameter of type "array of T" or
655 // "function returning T" is adjusted to be of type "pointer to
656 // T" or "pointer to function returning T", respectively.
657 else if (T->isArrayType())
658 // FIXME: Keep the type prior to promotion?
659 return Context.getArrayDecayedType(T);
660 else if (T->isFunctionType())
661 // FIXME: Keep the type prior to promotion?
662 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663
Douglas Gregor2943aed2009-03-03 04:44:36 +0000664 Diag(Loc, diag::err_template_nontype_parm_bad_type)
665 << T;
666
667 return QualType();
668}
669
John McCalld226f652010-08-21 09:40:31 +0000670Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
671 unsigned Depth,
672 unsigned Position,
673 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000674 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000675 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
676 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000677
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000678 assert(S->isTemplateParamScope() &&
679 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000680 bool Invalid = false;
681
682 IdentifierInfo *ParamName = D.getIdentifier();
683 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000684 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000685 LookupOrdinaryName,
686 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000687 if (PrevDecl && PrevDecl->isTemplateParameter()) {
688 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
689 PrevDecl = 0;
690 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000691 }
692
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000693 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
694 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000695 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000696 Invalid = true;
697 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000698
Douglas Gregor10738d32010-12-23 23:51:58 +0000699 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000700 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000701 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Daniel Dunbar96a00142012-03-09 18:35:03 +0000702 D.getLocStart(),
John McCall7a9813c2010-01-22 00:28:27 +0000703 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000704 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000705 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000706 Param->setAccess(AS_public);
707
Douglas Gregor72c3f312008-12-05 18:15:24 +0000708 if (Invalid)
709 Param->setInvalidDecl();
710
711 if (D.getIdentifier()) {
712 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000713 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000714 IdResolver.AddDecl(Param);
715 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000716
Douglas Gregor61c4d282011-01-05 15:48:55 +0000717 // C++0x [temp.param]p9:
718 // A default template-argument may be specified for any kind of
719 // template-parameter that is not a template parameter pack.
720 if (Default && IsParameterPack) {
721 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
722 Default = 0;
723 }
724
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000725 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000726 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000727 // Check for unexpanded parameter packs.
728 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
729 return Param;
730
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000731 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000732 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
733 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000734 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000735 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000736 }
John Wiegley429bb272011-04-08 18:41:53 +0000737 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000738
John McCall9ae2f072010-08-23 23:25:46 +0000739 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000741
John McCalld226f652010-08-21 09:40:31 +0000742 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000743}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000744
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000745/// ActOnTemplateTemplateParameter - Called when a C++ template template
746/// parameter (e.g. T in template <template <typename> class T> class array)
747/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000748Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
749 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000750 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000751 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000752 IdentifierInfo *Name,
753 SourceLocation NameLoc,
754 unsigned Depth,
755 unsigned Position,
756 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000757 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000758 assert(S->isTemplateParamScope() &&
759 "Template template parameter not in template parameter scope!");
760
761 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000762 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000763 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000764 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000765 NameLoc.isInvalid()? TmpLoc : NameLoc,
766 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000767 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000768 Param->setAccess(AS_public);
769
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000770 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000771 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000772 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000773 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000774 IdResolver.AddDecl(Param);
775 }
776
Douglas Gregor6f526752010-12-16 08:48:57 +0000777 if (Params->size() == 0) {
778 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
779 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
780 Param->setInvalidDecl();
781 }
782
Douglas Gregor61c4d282011-01-05 15:48:55 +0000783 // C++0x [temp.param]p9:
784 // A default template-argument may be specified for any kind of
785 // template-parameter that is not a template parameter pack.
786 if (IsParameterPack && !Default.isInvalid()) {
787 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
788 Default = ParsedTemplateArgument();
789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000790
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000791 if (!Default.isInvalid()) {
792 // Check only that we have a template template argument. We don't want to
793 // try to check well-formedness now, because our template template parameter
794 // might have dependent types in its template parameters, which we wouldn't
795 // be able to match now.
796 //
797 // If none of the template template parameter's template arguments mention
798 // other template parameters, we could actually perform more checking here.
799 // However, it isn't worth doing.
800 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
801 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
802 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
803 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000804 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000805 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000806
Douglas Gregor6f526752010-12-16 08:48:57 +0000807 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000808 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000809 DefaultArg.getArgument().getAsTemplate(),
810 UPPC_DefaultArgument))
811 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000812
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000813 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000814 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000815
John McCalld226f652010-08-21 09:40:31 +0000816 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000817}
818
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000819/// ActOnTemplateParameterList - Builds a TemplateParameterList that
820/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000821TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000822Sema::ActOnTemplateParameterList(unsigned Depth,
823 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000824 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000825 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000826 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000827 SourceLocation RAngleLoc) {
828 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000829 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000830
Douglas Gregorddc29e12009-02-06 22:42:48 +0000831 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000832 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000833 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000834}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000835
John McCallb6217662010-03-15 10:12:16 +0000836static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
837 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000838 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000839}
840
John McCallf312b1e2010-08-26 23:41:50 +0000841DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000842Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000843 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844 IdentifierInfo *Name, SourceLocation NameLoc,
845 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000846 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000847 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000848 unsigned NumOuterTemplateParamLists,
849 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000850 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000851 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000852 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000853 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000854
855 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000856 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000857 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000858
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000859 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
860 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000861
862 // There is no such thing as an unnamed class template.
863 if (!Name) {
864 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000865 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000866 }
867
868 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000869 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000870 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000871 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000872 if (SS.isNotEmpty() && !SS.isInvalid()) {
873 SemanticContext = computeDeclContext(SS, true);
874 if (!SemanticContext) {
Douglas Gregor957ff272012-03-18 00:15:42 +0000875 Diag(NameLoc, diag::err_template_qualified_declarator_no_match)
876 << SS.getScopeRep() << SS.getRange();
Douglas Gregor05396e22009-08-25 17:23:04 +0000877 return true;
878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
John McCall77bb1aa2010-05-01 00:40:08 +0000880 if (RequireCompleteDeclContext(SS, SemanticContext))
881 return true;
882
Douglas Gregor20606502011-10-14 15:31:12 +0000883 // If we're adding a template to a dependent context, we may need to
884 // rebuilding some of the types used within the template parameter list,
885 // now that we know what the current instantiation is.
886 if (SemanticContext->isDependentContext()) {
887 ContextRAII SavedContext(*this, SemanticContext);
888 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
889 Invalid = true;
Douglas Gregor42acead2012-03-17 23:06:31 +0000890 } else if (CurContext->isRecord() && TUK != TUK_Friend &&
891 TUK != TUK_Reference)
892 diagnoseQualifiedDeclInClass(SS, SemanticContext, Name, NameLoc);
Douglas Gregor20606502011-10-14 15:31:12 +0000893
John McCalla24dc2e2009-11-17 02:14:36 +0000894 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000895 } else {
896 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000897 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Douglas Gregor57265e32010-04-12 16:00:01 +0000900 if (Previous.isAmbiguous())
901 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000902
Douglas Gregorddc29e12009-02-06 22:42:48 +0000903 NamedDecl *PrevDecl = 0;
904 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000905 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000906
Douglas Gregorddc29e12009-02-06 22:42:48 +0000907 // If there is a previous declaration with the same name, check
908 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000909 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000910 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000911
912 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000913 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000914 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000915 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000916 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
917 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000918 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000919 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
920 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
921 PrevClassTemplate
922 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
923 ->getSpecializedTemplate();
924 }
925 }
926
John McCall65c49462009-12-18 11:25:59 +0000927 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000928 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000929 // [...] When looking for a prior declaration of a class or a function
930 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000931 // function is neither a qualified name nor a template-id, scopes outside
932 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000933 if (!SS.isSet()) {
934 DeclContext *OutermostContext = CurContext;
935 while (!OutermostContext->isFileContext())
936 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000937
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000938 if (PrevDecl &&
939 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
940 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
941 SemanticContext = PrevDecl->getDeclContext();
942 } else {
943 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000944 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000945 // declaration.
946 PrevDecl = PrevClassTemplate = 0;
947 SemanticContext = OutermostContext;
948 }
John McCalle129d442009-12-17 23:21:11 +0000949 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000950
John McCalle129d442009-12-17 23:21:11 +0000951 if (CurContext->isDependentContext()) {
952 // If this is a dependent context, we don't want to link the friend
953 // class template to the template in scope, because that would perform
954 // checking of the template parameter lists that can't be performed
955 // until the outer context is instantiated.
956 PrevDecl = PrevClassTemplate = 0;
957 }
958 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
959 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000960
Douglas Gregorddc29e12009-02-06 22:42:48 +0000961 if (PrevClassTemplate) {
962 // Ensure that the template parameter lists are compatible.
963 if (!TemplateParameterListsAreEqual(TemplateParams,
964 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000965 /*Complain=*/true,
966 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000967 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000968
969 // C++ [temp.class]p4:
970 // In a redeclaration, partial specialization, explicit
971 // specialization or explicit instantiation of a class template,
972 // the class-key shall agree in kind with the original class
973 // template declaration (7.1.5.3).
974 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000975 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
976 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000977 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000978 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000979 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000980 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000981 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000982 }
983
Douglas Gregorddc29e12009-02-06 22:42:48 +0000984 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000985 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000986 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000987 Diag(NameLoc, diag::err_redefinition) << Name;
988 Diag(Def->getLocation(), diag::note_previous_definition);
989 // FIXME: Would it make sense to try to "forget" the previous
990 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000991 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000992 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000993 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000994 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
995 // Maybe we will complain about the shadowed template parameter.
996 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
997 // Just pretend that we didn't see the previous declaration.
998 PrevDecl = 0;
999 } else if (PrevDecl) {
1000 // C++ [temp]p5:
1001 // A class template shall not have the same name as any other
1002 // template, class, function, object, enumeration, enumerator,
1003 // namespace, or type in the same scope (3.3), except as specified
1004 // in (14.5.4).
1005 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
1006 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +00001007 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001008 }
1009
Douglas Gregord684b002009-02-10 19:49:53 +00001010 // Check the template parameter list of this declaration, possibly
1011 // merging in the template parameter list from the previous class
1012 // template declaration.
1013 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001014 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +00001015 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +00001016 SemanticContext->isRecord() &&
1017 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +00001018 ? TPC_ClassTemplateMember
1019 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +00001020 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregor57265e32010-04-12 16:00:01 +00001022 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001023 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001024 // template out-of-line.
1025 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001026 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001027 Diag(NameLoc, diag::err_member_def_does_not_match)
1028 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001029 Invalid = true;
1030 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001031 }
1032
Mike Stump1eb44332009-09-09 15:08:12 +00001033 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001034 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001035 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001036 PrevClassTemplate->getTemplatedDecl() : 0,
1037 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001038 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001039 if (NumOuterTemplateParamLists > 0)
1040 NewClass->setTemplateParameterListsInfo(Context,
1041 NumOuterTemplateParamLists,
1042 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001043
Eli Friedman572ae0a2012-02-10 02:02:21 +00001044 // Add alignment attributes if necessary; these attributes are checked when
1045 // the ASTContext lays out the structure.
1046 AddAlignmentAttributesForRecord(NewClass);
1047 AddMsStructLayoutForRecord(NewClass);
1048
Douglas Gregorddc29e12009-02-06 22:42:48 +00001049 ClassTemplateDecl *NewTemplate
1050 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1051 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001052 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001053 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001054
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001055 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001056 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001057
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001058 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001059 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001060 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001061 assert(T->isDependentType() && "Class template type is not dependent?");
1062 (void)T;
1063
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001064 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001065 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001066 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001067 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1068 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001070 // Set the access specifier.
Douglas Gregor42acead2012-03-17 23:06:31 +00001071 if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord())
John McCall05b23ea2009-09-14 21:59:20 +00001072 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Douglas Gregorddc29e12009-02-06 22:42:48 +00001074 // Set the lexical context of these templates
1075 NewClass->setLexicalDeclContext(CurContext);
1076 NewTemplate->setLexicalDeclContext(CurContext);
1077
John McCall0f434ec2009-07-31 02:45:11 +00001078 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001079 NewClass->startDefinition();
1080
1081 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001082 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001083
John McCall05b23ea2009-09-14 21:59:20 +00001084 if (TUK != TUK_Friend)
1085 PushOnScopeChains(NewTemplate, S);
1086 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001087 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001088 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001089 NewClass->setAccess(PrevClassTemplate->getAccess());
1090 }
John McCall05b23ea2009-09-14 21:59:20 +00001091
Douglas Gregord85bea22009-09-26 06:47:28 +00001092 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1093 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001094
John McCall05b23ea2009-09-14 21:59:20 +00001095 // Friend templates are visible in fairly strange ways.
1096 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001097 DeclContext *DC = SemanticContext->getRedeclContext();
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001098 DC->makeDeclVisibleInContext(NewTemplate);
John McCall05b23ea2009-09-14 21:59:20 +00001099 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1100 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001101 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001102 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001103
Douglas Gregord85bea22009-09-26 06:47:28 +00001104 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1105 NewClass->getLocation(),
1106 NewTemplate,
1107 /*FIXME:*/NewClass->getLocation());
1108 Friend->setAccess(AS_public);
1109 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001110 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001111
Douglas Gregord684b002009-02-10 19:49:53 +00001112 if (Invalid) {
1113 NewTemplate->setInvalidDecl();
1114 NewClass->setInvalidDecl();
1115 }
John McCalld226f652010-08-21 09:40:31 +00001116 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001117}
1118
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001119/// \brief Diagnose the presence of a default template argument on a
1120/// template parameter, which is ill-formed in certain contexts.
1121///
1122/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001123static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001124 Sema::TemplateParamListContext TPC,
1125 SourceLocation ParamLoc,
1126 SourceRange DefArgRange) {
1127 switch (TPC) {
1128 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001129 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001130 return false;
1131
1132 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001133 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001134 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001135 // A default template-argument shall not be specified in a
1136 // function template declaration or a function template
1137 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001138 // If a friend function template declaration specifies a default
1139 // template-argument, that declaration shall be a definition and shall be
1140 // the only declaration of the function template in the translation unit.
1141 // (C++98/03 doesn't have this wording; see DR226).
David Blaikie4e4d0842012-03-11 07:00:24 +00001142 S.Diag(ParamLoc, S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00001143 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1144 : diag::ext_template_parameter_default_in_function_template)
1145 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001146 return false;
1147
1148 case Sema::TPC_ClassTemplateMember:
1149 // C++0x [temp.param]p9:
1150 // A default template-argument shall not be specified in the
1151 // template-parameter-lists of the definition of a member of a
1152 // class template that appears outside of the member's class.
1153 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1154 << DefArgRange;
1155 return true;
1156
1157 case Sema::TPC_FriendFunctionTemplate:
1158 // C++ [temp.param]p9:
1159 // A default template-argument shall not be specified in a
1160 // friend template declaration.
1161 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1162 << DefArgRange;
1163 return true;
1164
1165 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1166 // for friend function templates if there is only a single
1167 // declaration (and it is a definition). Strange!
1168 }
1169
David Blaikie7530c032012-01-17 06:56:22 +00001170 llvm_unreachable("Invalid TemplateParamListContext!");
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001171}
1172
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001173/// \brief Check for unexpanded parameter packs within the template parameters
1174/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001175static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1176 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001177 TemplateParameterList *Params = TTP->getTemplateParameters();
1178 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1179 NamedDecl *P = Params->getParam(I);
1180 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001181 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001182 NTTP->getTypeSourceInfo(),
1183 Sema::UPPC_NonTypeTemplateParameterType))
1184 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001185
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001186 continue;
1187 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001188
1189 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001190 = dyn_cast<TemplateTemplateParmDecl>(P))
1191 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1192 return true;
1193 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001194
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001195 return false;
1196}
1197
Douglas Gregord684b002009-02-10 19:49:53 +00001198/// \brief Checks the validity of a template parameter list, possibly
1199/// considering the template parameter list from a previous
1200/// declaration.
1201///
1202/// If an "old" template parameter list is provided, it must be
1203/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1204/// template parameter list.
1205///
1206/// \param NewParams Template parameter list for a new template
1207/// declaration. This template parameter list will be updated with any
1208/// default arguments that are carried through from the previous
1209/// template parameter list.
1210///
1211/// \param OldParams If provided, template parameter list from a
1212/// previous declaration of the same template. Default template
1213/// arguments will be merged from the old template parameter list to
1214/// the new template parameter list.
1215///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001216/// \param TPC Describes the context in which we are checking the given
1217/// template parameter list.
1218///
Douglas Gregord684b002009-02-10 19:49:53 +00001219/// \returns true if an error occurred, false otherwise.
1220bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001221 TemplateParameterList *OldParams,
1222 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001223 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregord684b002009-02-10 19:49:53 +00001225 // C++ [temp.param]p10:
1226 // The set of default template-arguments available for use with a
1227 // template declaration or definition is obtained by merging the
1228 // default arguments from the definition (if in scope) and all
1229 // declarations in scope in the same way default function
1230 // arguments are (8.3.6).
1231 bool SawDefaultArgument = false;
1232 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001233
Mike Stump1a35fde2009-02-11 23:03:27 +00001234 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001235 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001236 if (OldParams)
1237 OldParam = OldParams->begin();
1238
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001239 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001240 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1241 NewParamEnd = NewParams->end();
1242 NewParam != NewParamEnd; ++NewParam) {
1243 // Variables used to diagnose redundant default arguments
1244 bool RedundantDefaultArg = false;
1245 SourceLocation OldDefaultLoc;
1246 SourceLocation NewDefaultLoc;
1247
David Blaikie1368e582011-10-19 05:19:50 +00001248 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001249 bool MissingDefaultArg = false;
1250
David Blaikie1368e582011-10-19 05:19:50 +00001251 // Variable used to diagnose non-final parameter packs
1252 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001253
Douglas Gregord684b002009-02-10 19:49:53 +00001254 if (TemplateTypeParmDecl *NewTypeParm
1255 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001256 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001257 if (NewTypeParm->hasDefaultArgument() &&
1258 DiagnoseDefaultTemplateArgument(*this, TPC,
1259 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001260 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001261 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001262 NewTypeParm->removeDefaultArgument();
1263
1264 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001265 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001266 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Anders Carlsson49d25572009-06-12 23:20:15 +00001268 if (NewTypeParm->isParameterPack()) {
1269 assert(!NewTypeParm->hasDefaultArgument() &&
1270 "Parameter packs can't have a default argument!");
1271 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001272 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001273 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001274 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1275 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1276 SawDefaultArgument = true;
1277 RedundantDefaultArg = true;
1278 PreviousDefaultArgLoc = NewDefaultLoc;
1279 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1280 // Merge the default argument from the old declaration to the
1281 // new declaration.
1282 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001283 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001284 true);
1285 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1286 } else if (NewTypeParm->hasDefaultArgument()) {
1287 SawDefaultArgument = true;
1288 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1289 } else if (SawDefaultArgument)
1290 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001291 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001292 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001293 // Check for unexpanded parameter packs.
1294 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001295 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001296 UPPC_NonTypeTemplateParameterType)) {
1297 Invalid = true;
1298 continue;
1299 }
1300
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001301 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001302 if (NewNonTypeParm->hasDefaultArgument() &&
1303 DiagnoseDefaultTemplateArgument(*this, TPC,
1304 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001305 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001306 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001307 }
1308
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001309 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001310 NonTypeTemplateParmDecl *OldNonTypeParm
1311 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001312 if (NewNonTypeParm->isParameterPack()) {
1313 assert(!NewNonTypeParm->hasDefaultArgument() &&
1314 "Parameter packs can't have a default argument!");
1315 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001316 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001317 NewNonTypeParm->hasDefaultArgument()) {
1318 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1319 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1320 SawDefaultArgument = true;
1321 RedundantDefaultArg = true;
1322 PreviousDefaultArgLoc = NewDefaultLoc;
1323 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1324 // Merge the default argument from the old declaration to the
1325 // new declaration.
1326 SawDefaultArgument = true;
1327 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001328 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001329 // parameter.
1330 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001331 OldNonTypeParm->getDefaultArgument(),
1332 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001333 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1334 } else if (NewNonTypeParm->hasDefaultArgument()) {
1335 SawDefaultArgument = true;
1336 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1337 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001338 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001339 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001340 TemplateTemplateParmDecl *NewTemplateParm
1341 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001342
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001343 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001344 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001345 Invalid = true;
1346 continue;
1347 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001348
David Blaikie1368e582011-10-19 05:19:50 +00001349 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001350 if (NewTemplateParm->hasDefaultArgument() &&
1351 DiagnoseDefaultTemplateArgument(*this, TPC,
1352 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001353 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001354 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001355
1356 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001357 TemplateTemplateParmDecl *OldTemplateParm
1358 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001359 if (NewTemplateParm->isParameterPack()) {
1360 assert(!NewTemplateParm->hasDefaultArgument() &&
1361 "Parameter packs can't have a default argument!");
1362 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001363 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001364 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001365 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1366 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001367 SawDefaultArgument = true;
1368 RedundantDefaultArg = true;
1369 PreviousDefaultArgLoc = NewDefaultLoc;
1370 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1371 // Merge the default argument from the old declaration to the
1372 // new declaration.
1373 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001374 // FIXME: We need to create a new kind of "default argument" expression
1375 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001376 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001377 OldTemplateParm->getDefaultArgument(),
1378 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001379 PreviousDefaultArgLoc
1380 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001381 } else if (NewTemplateParm->hasDefaultArgument()) {
1382 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001383 PreviousDefaultArgLoc
1384 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001385 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001386 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001387 }
1388
David Blaikie1368e582011-10-19 05:19:50 +00001389 // C++0x [temp.param]p11:
1390 // If a template parameter of a primary class template or alias template
1391 // is a template parameter pack, it shall be the last template parameter.
1392 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1393 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1394 Diag((*NewParam)->getLocation(),
1395 diag::err_template_param_pack_must_be_last_template_parameter);
1396 Invalid = true;
1397 }
1398
Douglas Gregord684b002009-02-10 19:49:53 +00001399 if (RedundantDefaultArg) {
1400 // C++ [temp.param]p12:
1401 // A template-parameter shall not be given default arguments
1402 // by two different declarations in the same scope.
1403 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1404 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1405 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001406 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001407 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001408 // If a template-parameter of a class template has a default
1409 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001410 // have a default template-argument supplied or be a template parameter
1411 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001412 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001413 diag::err_template_param_default_arg_missing);
1414 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1415 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001416 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001417 }
1418
1419 // If we have an old template parameter list that we're merging
1420 // in, move on to the next parameter.
1421 if (OldParams)
1422 ++OldParam;
1423 }
1424
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001425 // We were missing some default arguments at the end of the list, so remove
1426 // all of the default arguments.
1427 if (RemoveDefaultArguments) {
1428 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1429 NewParamEnd = NewParams->end();
1430 NewParam != NewParamEnd; ++NewParam) {
1431 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1432 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001433 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001434 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1435 NTTP->removeDefaultArgument();
1436 else
1437 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1438 }
1439 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001440
Douglas Gregord684b002009-02-10 19:49:53 +00001441 return Invalid;
1442}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001443
John McCall4e2cbb22010-10-20 05:44:58 +00001444namespace {
1445
1446/// A class which looks for a use of a certain level of template
1447/// parameter.
1448struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1449 typedef RecursiveASTVisitor<DependencyChecker> super;
1450
1451 unsigned Depth;
1452 bool Match;
1453
1454 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1455 NamedDecl *ND = Params->getParam(0);
1456 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1457 Depth = PD->getDepth();
1458 } else if (NonTypeTemplateParmDecl *PD =
1459 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1460 Depth = PD->getDepth();
1461 } else {
1462 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1463 }
1464 }
1465
1466 bool Matches(unsigned ParmDepth) {
1467 if (ParmDepth >= Depth) {
1468 Match = true;
1469 return true;
1470 }
1471 return false;
1472 }
1473
1474 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1475 return !Matches(T->getDepth());
1476 }
1477
1478 bool TraverseTemplateName(TemplateName N) {
1479 if (TemplateTemplateParmDecl *PD =
1480 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1481 if (Matches(PD->getDepth())) return false;
1482 return super::TraverseTemplateName(N);
1483 }
1484
1485 bool VisitDeclRefExpr(DeclRefExpr *E) {
1486 if (NonTypeTemplateParmDecl *PD =
1487 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1488 if (PD->getDepth() == Depth) {
1489 Match = true;
1490 return false;
1491 }
1492 }
1493 return super::VisitDeclRefExpr(E);
1494 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001495
1496 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1497 return TraverseType(T->getInjectedSpecializationType());
1498 }
John McCall4e2cbb22010-10-20 05:44:58 +00001499};
1500}
1501
Douglas Gregorc8406492011-05-10 18:27:06 +00001502/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001503/// list.
1504static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001505DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001506 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001507 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001508 return Checker.Match;
1509}
1510
Douglas Gregorc8406492011-05-10 18:27:06 +00001511// Find the source range corresponding to the named type in the given
1512// nested-name-specifier, if any.
1513static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1514 QualType T,
1515 const CXXScopeSpec &SS) {
1516 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1517 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1518 if (const Type *CurType = NNS->getAsType()) {
1519 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1520 return NNSLoc.getTypeLoc().getSourceRange();
1521 } else
1522 break;
1523
1524 NNSLoc = NNSLoc.getPrefix();
1525 }
1526
1527 return SourceRange();
1528}
1529
Mike Stump1eb44332009-09-09 15:08:12 +00001530/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001531/// specifier, returning the template parameter list that applies to the
1532/// name.
1533///
1534/// \param DeclStartLoc the start of the declaration that has a scope
1535/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001536///
Douglas Gregorc8406492011-05-10 18:27:06 +00001537/// \param DeclLoc The location of the declaration itself.
1538///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001539/// \param SS the scope specifier that will be matched to the given template
1540/// parameter lists. This scope specifier precedes a qualified name that is
1541/// being declared.
1542///
1543/// \param ParamLists the template parameter lists, from the outermost to the
1544/// innermost template parameter lists.
1545///
1546/// \param NumParamLists the number of template parameter lists in ParamLists.
1547///
John McCall77e8b112010-04-13 20:37:33 +00001548/// \param IsFriend Whether to apply the slightly different rules for
1549/// matching template parameters to scope specifiers in friend
1550/// declarations.
1551///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001552/// \param IsExplicitSpecialization will be set true if the entity being
1553/// declared is an explicit specialization, false otherwise.
1554///
Mike Stump1eb44332009-09-09 15:08:12 +00001555/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001556/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001557/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001558/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001559/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560/// itself a template).
1561TemplateParameterList *
1562Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001563 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001564 const CXXScopeSpec &SS,
1565 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001566 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001567 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001568 bool &IsExplicitSpecialization,
1569 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001570 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001571 Invalid = false;
1572
1573 // The sequence of nested types to which we will match up the template
1574 // parameter lists. We first build this list by starting with the type named
1575 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001576 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001577 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001578 if (SS.getScopeRep()) {
1579 if (CXXRecordDecl *Record
1580 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1581 T = Context.getTypeDeclType(Record);
1582 else
1583 T = QualType(SS.getScopeRep()->getAsType(), 0);
1584 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001585
1586 // If we found an explicit specialization that prevents us from needing
1587 // 'template<>' headers, this will be set to the location of that
1588 // explicit specialization.
1589 SourceLocation ExplicitSpecLoc;
1590
1591 while (!T.isNull()) {
1592 NestedTypes.push_back(T);
1593
1594 // Retrieve the parent of a record type.
1595 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1596 // If this type is an explicit specialization, we're done.
1597 if (ClassTemplateSpecializationDecl *Spec
1598 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1599 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1600 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1601 ExplicitSpecLoc = Spec->getLocation();
1602 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001603 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001604 } else if (Record->getTemplateSpecializationKind()
1605 == TSK_ExplicitSpecialization) {
1606 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001607 break;
1608 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001609
1610 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1611 T = Context.getTypeDeclType(Parent);
1612 else
1613 T = QualType();
1614 continue;
1615 }
1616
1617 if (const TemplateSpecializationType *TST
1618 = T->getAs<TemplateSpecializationType>()) {
1619 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1620 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1621 T = Context.getTypeDeclType(Parent);
1622 else
1623 T = QualType();
1624 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001625 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001626 }
1627
1628 // Look one step prior in a dependent template specialization type.
1629 if (const DependentTemplateSpecializationType *DependentTST
1630 = T->getAs<DependentTemplateSpecializationType>()) {
1631 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1632 T = QualType(NNS->getAsType(), 0);
1633 else
1634 T = QualType();
1635 continue;
1636 }
1637
1638 // Look one step prior in a dependent name type.
1639 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1640 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1641 T = QualType(NNS->getAsType(), 0);
1642 else
1643 T = QualType();
1644 continue;
1645 }
1646
1647 // Retrieve the parent of an enumeration type.
1648 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1649 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1650 // check here.
1651 EnumDecl *Enum = EnumT->getDecl();
1652
1653 // Get to the parent type.
1654 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1655 T = Context.getTypeDeclType(Parent);
1656 else
1657 T = QualType();
1658 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001659 }
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Douglas Gregorc8406492011-05-10 18:27:06 +00001661 T = QualType();
1662 }
1663 // Reverse the nested types list, since we want to traverse from the outermost
1664 // to the innermost while checking template-parameter-lists.
1665 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001666
Douglas Gregorc8406492011-05-10 18:27:06 +00001667 // C++0x [temp.expl.spec]p17:
1668 // A member or a member template may be nested within many
1669 // enclosing class templates. In an explicit specialization for
1670 // such a member, the member declaration shall be preceded by a
1671 // template<> for each enclosing class template that is
1672 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001673 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001674 unsigned ParamIdx = 0;
1675 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1676 ++TypeIdx) {
1677 T = NestedTypes[TypeIdx];
1678
1679 // Whether we expect a 'template<>' header.
1680 bool NeedEmptyTemplateHeader = false;
1681
1682 // Whether we expect a template header with parameters.
1683 bool NeedNonemptyTemplateHeader = false;
1684
1685 // For a dependent type, the set of template parameters that we
1686 // expect to see.
1687 TemplateParameterList *ExpectedTemplateParams = 0;
1688
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001689 // C++0x [temp.expl.spec]p15:
1690 // A member or a member template may be nested within many enclosing
1691 // class templates. In an explicit specialization for such a member, the
1692 // member declaration shall be preceded by a template<> for each
1693 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001694 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1695 if (ClassTemplatePartialSpecializationDecl *Partial
1696 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1697 ExpectedTemplateParams = Partial->getTemplateParameters();
1698 NeedNonemptyTemplateHeader = true;
1699 } else if (Record->isDependentType()) {
1700 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001701 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001702 ->getTemplateParameters();
1703 NeedNonemptyTemplateHeader = true;
1704 }
1705 } else if (ClassTemplateSpecializationDecl *Spec
1706 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1707 // C++0x [temp.expl.spec]p4:
1708 // Members of an explicitly specialized class template are defined
1709 // in the same manner as members of normal classes, and not using
1710 // the template<> syntax.
1711 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1712 NeedEmptyTemplateHeader = true;
1713 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001714 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001715 } else if (Record->getTemplateSpecializationKind()) {
1716 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001717 != TSK_ExplicitSpecialization &&
1718 TypeIdx == NumTypes - 1)
1719 IsExplicitSpecialization = true;
1720
1721 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001722 }
1723 } else if (const TemplateSpecializationType *TST
1724 = T->getAs<TemplateSpecializationType>()) {
1725 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1726 ExpectedTemplateParams = Template->getTemplateParameters();
1727 NeedNonemptyTemplateHeader = true;
1728 }
1729 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1730 // FIXME: We actually could/should check the template arguments here
1731 // against the corresponding template parameter list.
1732 NeedNonemptyTemplateHeader = false;
1733 }
1734
Douglas Gregor89b9f102011-06-06 15:22:55 +00001735 // C++ [temp.expl.spec]p16:
1736 // In an explicit specialization declaration for a member of a class
1737 // template or a member template that ap- pears in namespace scope, the
1738 // member template and some of its enclosing class templates may remain
1739 // unspecialized, except that the declaration shall not explicitly
1740 // specialize a class member template if its en- closing class templates
1741 // are not explicitly specialized as well.
1742 if (ParamIdx < NumParamLists) {
1743 if (ParamLists[ParamIdx]->size() == 0) {
1744 if (SawNonEmptyTemplateParameterList) {
1745 Diag(DeclLoc, diag::err_specialize_member_of_template)
1746 << ParamLists[ParamIdx]->getSourceRange();
1747 Invalid = true;
1748 IsExplicitSpecialization = false;
1749 return 0;
1750 }
1751 } else
1752 SawNonEmptyTemplateParameterList = true;
1753 }
1754
Douglas Gregorc8406492011-05-10 18:27:06 +00001755 if (NeedEmptyTemplateHeader) {
1756 // If we're on the last of the types, and we need a 'template<>' header
1757 // here, then it's an explicit specialization.
1758 if (TypeIdx == NumTypes - 1)
1759 IsExplicitSpecialization = true;
1760
1761 if (ParamIdx < NumParamLists) {
1762 if (ParamLists[ParamIdx]->size() > 0) {
1763 // The header has template parameters when it shouldn't. Complain.
1764 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1765 diag::err_template_param_list_matches_nontemplate)
1766 << T
1767 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1768 ParamLists[ParamIdx]->getRAngleLoc())
1769 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1770 Invalid = true;
1771 return 0;
1772 }
1773
1774 // Consume this template header.
1775 ++ParamIdx;
1776 continue;
1777 }
1778
1779 if (!IsFriend) {
1780 // We don't have a template header, but we should.
1781 SourceLocation ExpectedTemplateLoc;
1782 if (NumParamLists > 0)
1783 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1784 else
1785 ExpectedTemplateLoc = DeclStartLoc;
1786
1787 Diag(DeclLoc, diag::err_template_spec_needs_header)
1788 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1789 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1790 }
1791
1792 continue;
1793 }
1794
1795 if (NeedNonemptyTemplateHeader) {
1796 // In friend declarations we can have template-ids which don't
1797 // depend on the corresponding template parameter lists. But
1798 // assume that empty parameter lists are supposed to match this
1799 // template-id.
1800 if (IsFriend && T->isDependentType()) {
1801 if (ParamIdx < NumParamLists &&
1802 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1803 ExpectedTemplateParams = 0;
1804 else
1805 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001806 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001807
Douglas Gregorc8406492011-05-10 18:27:06 +00001808 if (ParamIdx < NumParamLists) {
1809 // Check the template parameter list, if we can.
1810 if (ExpectedTemplateParams &&
1811 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1812 ExpectedTemplateParams,
1813 true, TPL_TemplateMatch))
1814 Invalid = true;
1815
1816 if (!Invalid &&
1817 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1818 TPC_ClassTemplateMember))
1819 Invalid = true;
1820
1821 ++ParamIdx;
1822 continue;
1823 }
1824
1825 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1826 << T
1827 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1828 Invalid = true;
1829 continue;
1830 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001831 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001832
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001833 // If there were at least as many template-ids as there were template
1834 // parameter lists, then there are no template parameter lists remaining for
1835 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001836 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001837 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001839 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001840 if (ParamIdx < NumParamLists - 1) {
1841 bool HasAnyExplicitSpecHeader = false;
1842 bool AllExplicitSpecHeaders = true;
1843 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1844 if (ParamLists[I]->size() == 0)
1845 HasAnyExplicitSpecHeader = true;
1846 else
1847 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001848 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001849
1850 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1851 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1852 : diag::err_template_spec_extra_headers)
1853 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1854 ParamLists[NumParamLists - 2]->getRAngleLoc());
1855
1856 // If there was a specialization somewhere, such that 'template<>' is
1857 // not required, and there were any 'template<>' headers, note where the
1858 // specialization occurred.
1859 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1860 Diag(ExplicitSpecLoc,
1861 diag::note_explicit_template_spec_does_not_need_header)
1862 << NestedTypes.back();
1863
1864 // We have a template parameter list with no corresponding scope, which
1865 // means that the resulting template declaration can't be instantiated
1866 // properly (we'll end up with dependent nodes when we shouldn't).
1867 if (!AllExplicitSpecHeaders)
1868 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001869 }
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Douglas Gregor89b9f102011-06-06 15:22:55 +00001871 // C++ [temp.expl.spec]p16:
1872 // In an explicit specialization declaration for a member of a class
1873 // template or a member template that ap- pears in namespace scope, the
1874 // member template and some of its enclosing class templates may remain
1875 // unspecialized, except that the declaration shall not explicitly
1876 // specialize a class member template if its en- closing class templates
1877 // are not explicitly specialized as well.
1878 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1879 SawNonEmptyTemplateParameterList) {
1880 Diag(DeclLoc, diag::err_specialize_member_of_template)
1881 << ParamLists[ParamIdx]->getSourceRange();
1882 Invalid = true;
1883 IsExplicitSpecialization = false;
1884 return 0;
1885 }
1886
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001887 // Return the last template parameter list, which corresponds to the
1888 // entity being declared.
1889 return ParamLists[NumParamLists - 1];
1890}
1891
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001892void Sema::NoteAllFoundTemplates(TemplateName Name) {
1893 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1894 Diag(Template->getLocation(), diag::note_template_declared_here)
1895 << (isa<FunctionTemplateDecl>(Template)? 0
1896 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001897 : isa<TypeAliasTemplateDecl>(Template)? 2
1898 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001899 << Template->getDeclName();
1900 return;
1901 }
1902
1903 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1904 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1905 IEnd = OST->end();
1906 I != IEnd; ++I)
1907 Diag((*I)->getLocation(), diag::note_template_declared_here)
1908 << 0 << (*I)->getDeclName();
1909
1910 return;
1911 }
1912}
1913
Douglas Gregor7532dc62009-03-30 22:58:21 +00001914QualType Sema::CheckTemplateIdType(TemplateName Name,
1915 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001916 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001917 DependentTemplateName *DTN
1918 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001919 if (DTN && DTN->isIdentifier())
1920 // When building a template-id where the template-name is dependent,
1921 // assume the template is a type template. Either our assumption is
1922 // correct, or the code is ill-formed and will be diagnosed when the
1923 // dependent name is substituted.
1924 return Context.getDependentTemplateSpecializationType(ETK_None,
1925 DTN->getQualifier(),
1926 DTN->getIdentifier(),
1927 TemplateArgs);
1928
Douglas Gregor7532dc62009-03-30 22:58:21 +00001929 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001930 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1931 // We might have a substituted template template parameter pack. If so,
1932 // build a template specialization type for it.
1933 if (Name.getAsSubstTemplateTemplateParmPack())
1934 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001935
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001936 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1937 << Name;
1938 NoteAllFoundTemplates(Name);
1939 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001940 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001941
Douglas Gregor40808ce2009-03-09 23:48:35 +00001942 // Check that the template argument list is well-formed for this
1943 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001944 SmallVector<TemplateArgument, 4> Converted;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001945 bool ExpansionIntoFixedList = false;
John McCalld5532b62009-11-23 01:53:49 +00001946 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00001947 false, Converted, &ExpansionIntoFixedList))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001948 return QualType();
1949
Douglas Gregor40808ce2009-03-09 23:48:35 +00001950 QualType CanonType;
1951
Douglas Gregor561f8122011-07-01 01:22:09 +00001952 bool InstantiationDependent = false;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001953 TypeAliasTemplateDecl *AliasTemplate = 0;
1954 if (!ExpansionIntoFixedList &&
1955 (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001956 // Find the canonical type for this type alias template specialization.
1957 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1958 if (Pattern->isInvalidDecl())
1959 return QualType();
1960
1961 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1962 Converted.data(), Converted.size());
1963
1964 // Only substitute for the innermost template argument list.
1965 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001966 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001967 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1968 for (unsigned I = 0; I < Depth; ++I)
1969 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001970
1971 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1972 CanonType = SubstType(Pattern->getUnderlyingType(),
1973 TemplateArgLists, AliasTemplate->getLocation(),
1974 AliasTemplate->getDeclName());
1975 if (CanonType.isNull())
1976 return QualType();
1977 } else if (Name.isDependent() ||
1978 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001979 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001980 // This class template specialization is a dependent
1981 // type. Therefore, its canonical type is another class template
1982 // specialization type that contains all of the converted
1983 // arguments in canonical form. This ensures that, e.g., A<T> and
1984 // A<T, T> have identical types when A is declared as:
1985 //
1986 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001987 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001988 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001989 Converted.data(),
1990 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Douglas Gregor1275ae02009-07-28 23:00:59 +00001992 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001993 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001994 // In the future, we need to teach getTemplateSpecializationType to only
1995 // build the canonical type and return that to us.
1996 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001997
1998 // This might work out to be a current instantiation, in which
1999 // case the canonical type needs to be the InjectedClassNameType.
2000 //
2001 // TODO: in theory this could be a simple hashtable lookup; most
2002 // changes to CurContext don't change the set of current
2003 // instantiations.
2004 if (isa<ClassTemplateDecl>(Template)) {
2005 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
2006 // If we get out to a namespace, we're done.
2007 if (Ctx->isFileContext()) break;
2008
2009 // If this isn't a record, keep looking.
2010 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
2011 if (!Record) continue;
2012
2013 // Look for one of the two cases with InjectedClassNameTypes
2014 // and check whether it's the same template.
2015 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
2016 !Record->getDescribedClassTemplate())
2017 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002018
John McCall31f17ec2010-04-27 00:57:59 +00002019 // Fetch the injected class name type and check whether its
2020 // injected type is equal to the type we just built.
2021 QualType ICNT = Context.getTypeDeclType(Record);
2022 QualType Injected = cast<InjectedClassNameType>(ICNT)
2023 ->getInjectedSpecializationType();
2024
2025 if (CanonType != Injected->getCanonicalTypeInternal())
2026 continue;
2027
2028 // If so, the canonical type of this TST is the injected
2029 // class name type of the record we just found.
2030 assert(ICNT.isCanonical());
2031 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002032 break;
2033 }
2034 }
Mike Stump1eb44332009-09-09 15:08:12 +00002035 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002036 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002037 // Find the class template specialization declaration that
2038 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002039 void *InsertPos = 0;
2040 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002041 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002042 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002043 if (!Decl) {
2044 // This is the first time we have referenced this class template
2045 // specialization. Create the canonical declaration and add it to
2046 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002047 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002048 ClassTemplate->getTemplatedDecl()->getTagKind(),
2049 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002050 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002051 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002052 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002053 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002054 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002055 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002056 Decl->setLexicalDeclContext(CurContext);
2057 }
2058
2059 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002060 assert(isa<RecordType>(CanonType) &&
2061 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002062 }
Mike Stump1eb44332009-09-09 15:08:12 +00002063
Douglas Gregor40808ce2009-03-09 23:48:35 +00002064 // Build the fully-sugared type for this class template
2065 // specialization, which refers back to the class template
2066 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002067 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002068}
2069
John McCallf312b1e2010-08-26 23:41:50 +00002070TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002071Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00002072 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002073 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002074 ASTTemplateArgsPtr TemplateArgsIn,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002075 SourceLocation RAngleLoc,
2076 bool IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002077 if (SS.isInvalid())
2078 return true;
2079
Douglas Gregor7532dc62009-03-30 22:58:21 +00002080 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002081
Douglas Gregor40808ce2009-03-09 23:48:35 +00002082 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002083 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002084 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002085
Douglas Gregora88f09f2011-02-28 17:23:35 +00002086 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002087 QualType T
2088 = Context.getDependentTemplateSpecializationType(ETK_None,
2089 DTN->getQualifier(),
2090 DTN->getIdentifier(),
2091 TemplateArgs);
2092 // Build type-source information.
Douglas Gregora88f09f2011-02-28 17:23:35 +00002093 TypeLocBuilder TLB;
2094 DependentTemplateSpecializationTypeLoc SpecTL
2095 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002096 SpecTL.setElaboratedKeywordLoc(SourceLocation());
2097 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002098 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002099 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002100 SpecTL.setLAngleLoc(LAngleLoc);
2101 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002102 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2103 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2104 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2105 }
2106
John McCalld5532b62009-11-23 01:53:49 +00002107 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002108 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002109
2110 if (Result.isNull())
2111 return true;
2112
Douglas Gregor059101f2011-03-02 00:47:37 +00002113 // Build type-source information.
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002114 TypeLocBuilder TLB;
Douglas Gregor059101f2011-03-02 00:47:37 +00002115 TemplateSpecializationTypeLoc SpecTL
2116 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002117 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002118 SpecTL.setTemplateNameLoc(TemplateLoc);
2119 SpecTL.setLAngleLoc(LAngleLoc);
2120 SpecTL.setRAngleLoc(RAngleLoc);
2121 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2122 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002123
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002124 // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2125 // constructor or destructor name (in such a case, the scope specifier
2126 // will be attached to the enclosing Decl or Expr node).
2127 if (SS.isNotEmpty() && !IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002128 // Create an elaborated-type-specifier containing the nested-name-specifier.
2129 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2130 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002131 ElabTL.setElaboratedKeywordLoc(SourceLocation());
Douglas Gregor059101f2011-03-02 00:47:37 +00002132 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2133 }
2134
2135 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002136}
John McCallf1bbbb42009-09-04 01:14:41 +00002137
Douglas Gregor059101f2011-03-02 00:47:37 +00002138TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002139 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002140 SourceLocation TagLoc,
2141 CXXScopeSpec &SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002142 SourceLocation TemplateKWLoc,
2143 TemplateTy TemplateD,
Douglas Gregor059101f2011-03-02 00:47:37 +00002144 SourceLocation TemplateLoc,
2145 SourceLocation LAngleLoc,
2146 ASTTemplateArgsPtr TemplateArgsIn,
2147 SourceLocation RAngleLoc) {
2148 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2149
2150 // Translate the parser's template argument list in our AST format.
2151 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2152 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2153
2154 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002155 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002156 ElaboratedTypeKeyword Keyword
2157 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Douglas Gregor059101f2011-03-02 00:47:37 +00002159 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2160 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2161 DTN->getQualifier(),
2162 DTN->getIdentifier(),
2163 TemplateArgs);
2164
2165 // Build type-source information.
2166 TypeLocBuilder TLB;
2167 DependentTemplateSpecializationTypeLoc SpecTL
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002168 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2169 SpecTL.setElaboratedKeywordLoc(TagLoc);
2170 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002171 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002172 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002173 SpecTL.setLAngleLoc(LAngleLoc);
2174 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002175 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2176 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2177 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2178 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002179
2180 if (TypeAliasTemplateDecl *TAT =
2181 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2182 // C++0x [dcl.type.elab]p2:
2183 // If the identifier resolves to a typedef-name or the simple-template-id
2184 // resolves to an alias template specialization, the
2185 // elaborated-type-specifier is ill-formed.
2186 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2187 Diag(TAT->getLocation(), diag::note_declared_at);
2188 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002189
2190 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2191 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002192 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002193
2194 // Check the tag kind
2195 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002196 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002197
John McCall6b2becf2009-09-08 17:47:29 +00002198 IdentifierInfo *Id = D->getIdentifier();
2199 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002200
Richard Trieubbf34c02011-06-10 03:11:26 +00002201 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2202 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002203 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002204 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002205 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002206 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002207 }
2208 }
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002209
Douglas Gregor059101f2011-03-02 00:47:37 +00002210 // Provide source-location information for the template specialization.
2211 TypeLocBuilder TLB;
2212 TemplateSpecializationTypeLoc SpecTL
2213 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002214 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002215 SpecTL.setTemplateNameLoc(TemplateLoc);
2216 SpecTL.setLAngleLoc(LAngleLoc);
2217 SpecTL.setRAngleLoc(RAngleLoc);
2218 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2219 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002220
Douglas Gregor059101f2011-03-02 00:47:37 +00002221 // Construct an elaborated type containing the nested-name-specifier (if any)
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002222 // and tag keyword.
Douglas Gregor059101f2011-03-02 00:47:37 +00002223 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2224 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002225 ElabTL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002226 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2227 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002228}
2229
John McCall60d7b3a2010-08-24 06:29:42 +00002230ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002231 SourceLocation TemplateKWLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002232 LookupResult &R,
2233 bool RequiresADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002234 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002235 // FIXME: Can we do any checking at this point? I guess we could check the
2236 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002237 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002238 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002239 // foo<int> could identify a single function unambiguously
2240 // This approach does NOT work, since f<int>(1);
2241 // gets resolved prior to resorting to overload resolution
2242 // i.e., template<class T> void f(double);
2243 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002244
2245 // These should be filtered out by our callers.
2246 assert(!R.empty() && "empty lookup results when building templateid");
2247 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2248
John McCallc373d482010-01-27 01:50:18 +00002249 // We don't want lookup warnings at this point.
2250 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002251
John McCallf7a1a742009-11-24 19:00:30 +00002252 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002253 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002254 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002255 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002256 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002257 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002258 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002259
2260 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002261}
2262
John McCallf7a1a742009-11-24 19:00:30 +00002263// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002264ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002265Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002266 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002267 const DeclarationNameInfo &NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002268 const TemplateArgumentListInfo *TemplateArgs) {
2269 assert(TemplateArgs || TemplateKWLoc.isValid());
John McCallf7a1a742009-11-24 19:00:30 +00002270 DeclContext *DC;
2271 if (!(DC = computeDeclContext(SS, false)) ||
2272 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002273 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002274 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002276 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002277 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002278 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2279 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002280
John McCallf7a1a742009-11-24 19:00:30 +00002281 if (R.isAmbiguous())
2282 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002283
John McCallf7a1a742009-11-24 19:00:30 +00002284 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002285 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2286 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002287 return ExprError();
2288 }
2289
2290 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002291 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2292 << (NestedNameSpecifier*) SS.getScopeRep()
2293 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002294 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2295 return ExprError();
2296 }
2297
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002298 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002299}
2300
Douglas Gregorc45c2322009-03-31 00:43:58 +00002301/// \brief Form a dependent template name.
2302///
2303/// This action forms a dependent template name given the template
2304/// name and its (presumably dependent) scope specifier. For
2305/// example, given "MetaFun::template apply", the scope specifier \p
2306/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2307/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002308TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002309 CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002310 SourceLocation TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002311 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002312 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002313 bool EnteringContext,
2314 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002315 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2316 Diag(TemplateKWLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00002317 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00002318 diag::warn_cxx98_compat_template_outside_of_template :
2319 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002320 << FixItHint::CreateRemoval(TemplateKWLoc);
2321
Douglas Gregor0707bc52010-01-19 16:01:07 +00002322 DeclContext *LookupCtx = 0;
2323 if (SS.isSet())
2324 LookupCtx = computeDeclContext(SS, EnteringContext);
2325 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002326 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002327 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002328 // C++0x [temp.names]p5:
2329 // If a name prefixed by the keyword template is not the name of
2330 // a template, the program is ill-formed. [Note: the keyword
2331 // template may not be applied to non-template members of class
2332 // templates. -end note ] [ Note: as is the case with the
2333 // typename prefix, the template prefix is allowed in cases
2334 // where it is not strictly necessary; i.e., when the
2335 // nested-name-specifier or the expression on the left of the ->
2336 // or . is not dependent on a template-parameter, or the use
2337 // does not appear in the scope of a template. -end note]
2338 //
2339 // Note: C++03 was more strict here, because it banned the use of
2340 // the "template" keyword prior to a template-name that was not a
2341 // dependent name. C++ DR468 relaxed this requirement (the
2342 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002343 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002344 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002345 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2346 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002347 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002348 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2349 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002350 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2351 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002352 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002353 } else if (TNK == TNK_Non_template) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00002354 Diag(Name.getLocStart(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002355 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002356 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002357 << Name.getSourceRange()
2358 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002359 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002360 } else {
2361 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002362 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002363 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002364 }
2365
Mike Stump1eb44332009-09-09 15:08:12 +00002366 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002367 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002368
Douglas Gregor014e88d2009-11-03 23:16:33 +00002369 switch (Name.getKind()) {
2370 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002371 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002372 Name.Identifier));
2373 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002374
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002375 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002376 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002377 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002378 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002379
2380 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002381 llvm_unreachable(
2382 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002383
Douglas Gregor014e88d2009-11-03 23:16:33 +00002384 default:
2385 break;
2386 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002387
Daniel Dunbar96a00142012-03-09 18:35:03 +00002388 Diag(Name.getLocStart(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002389 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002390 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002391 << Name.getSourceRange()
2392 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002393 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002394}
2395
Mike Stump1eb44332009-09-09 15:08:12 +00002396bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002397 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002398 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002399 const TemplateArgument &Arg = AL.getArgument();
2400
Anders Carlsson436b1562009-06-13 00:33:33 +00002401 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002402 switch(Arg.getKind()) {
2403 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002404 // C++ [temp.arg.type]p1:
2405 // A template-argument for a template-parameter which is a
2406 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002407 break;
2408 case TemplateArgument::Template: {
2409 // We have a template type parameter but the template argument
2410 // is a template without any arguments.
2411 SourceRange SR = AL.getSourceRange();
2412 TemplateName Name = Arg.getAsTemplate();
2413 Diag(SR.getBegin(), diag::err_template_missing_args)
2414 << Name << SR;
2415 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2416 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002417
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002418 return true;
2419 }
2420 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002421 // We have a template type parameter but the template argument
2422 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002423 SourceRange SR = AL.getSourceRange();
2424 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002425 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Anders Carlsson436b1562009-06-13 00:33:33 +00002427 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002428 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002429 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002430
John McCalla93c9342009-12-07 02:54:59 +00002431 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002432 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Anders Carlsson436b1562009-06-13 00:33:33 +00002434 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002435 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2436
2437 // Objective-C ARC:
2438 // If an explicitly-specified template argument type is a lifetime type
2439 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
David Blaikie4e4d0842012-03-11 07:00:24 +00002440 if (getLangOpts().ObjCAutoRefCount &&
Douglas Gregore559ca12011-06-17 22:11:49 +00002441 ArgType->isObjCLifetimeType() &&
2442 !ArgType.getObjCLifetime()) {
2443 Qualifiers Qs;
2444 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2445 ArgType = Context.getQualifiedType(ArgType, Qs);
2446 }
2447
2448 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002449 return false;
2450}
2451
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002452/// \brief Substitute template arguments into the default template argument for
2453/// the given template type parameter.
2454///
2455/// \param SemaRef the semantic analysis object for which we are performing
2456/// the substitution.
2457///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002458/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002459/// for.
2460///
2461/// \param TemplateLoc the location of the template name that started the
2462/// template-id we are checking.
2463///
2464/// \param RAngleLoc the location of the right angle bracket ('>') that
2465/// terminates the template-id.
2466///
2467/// \param Param the template template parameter whose default we are
2468/// substituting into.
2469///
2470/// \param Converted the list of template arguments provided for template
2471/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002472/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002473static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002474SubstDefaultTemplateArgument(Sema &SemaRef,
2475 TemplateDecl *Template,
2476 SourceLocation TemplateLoc,
2477 SourceLocation RAngleLoc,
2478 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002479 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002480 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002481
2482 // If the argument type is dependent, instantiate it now based
2483 // on the previously-computed template arguments.
2484 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002485 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002486 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002487
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002488 MultiLevelTemplateArgumentList AllTemplateArgs
2489 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2490
2491 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002492 Template, Converted.data(),
2493 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002494 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002495
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002496 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2497 Param->getDefaultArgumentLoc(),
2498 Param->getDeclName());
2499 }
2500
2501 return ArgType;
2502}
2503
2504/// \brief Substitute template arguments into the default template argument for
2505/// the given non-type template parameter.
2506///
2507/// \param SemaRef the semantic analysis object for which we are performing
2508/// the substitution.
2509///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002510/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002511/// for.
2512///
2513/// \param TemplateLoc the location of the template name that started the
2514/// template-id we are checking.
2515///
2516/// \param RAngleLoc the location of the right angle bracket ('>') that
2517/// terminates the template-id.
2518///
Douglas Gregor788cd062009-11-11 01:00:40 +00002519/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002520/// substituting into.
2521///
2522/// \param Converted the list of template arguments provided for template
2523/// parameters that precede \p Param in the template parameter list.
2524///
2525/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002526static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002527SubstDefaultTemplateArgument(Sema &SemaRef,
2528 TemplateDecl *Template,
2529 SourceLocation TemplateLoc,
2530 SourceLocation RAngleLoc,
2531 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002532 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002533 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002534 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002536 MultiLevelTemplateArgumentList AllTemplateArgs
2537 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002538
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002539 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002540 Template, Converted.data(),
2541 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002542 SourceRange(TemplateLoc, RAngleLoc));
2543
2544 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2545}
2546
Douglas Gregor788cd062009-11-11 01:00:40 +00002547/// \brief Substitute template arguments into the default template argument for
2548/// the given template template parameter.
2549///
2550/// \param SemaRef the semantic analysis object for which we are performing
2551/// the substitution.
2552///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002553/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002554/// for.
2555///
2556/// \param TemplateLoc the location of the template name that started the
2557/// template-id we are checking.
2558///
2559/// \param RAngleLoc the location of the right angle bracket ('>') that
2560/// terminates the template-id.
2561///
2562/// \param Param the template template parameter whose default we are
2563/// substituting into.
2564///
2565/// \param Converted the list of template arguments provided for template
2566/// parameters that precede \p Param in the template parameter list.
2567///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002568/// \param QualifierLoc Will be set to the nested-name-specifier (with
2569/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002570///
Douglas Gregor788cd062009-11-11 01:00:40 +00002571/// \returns the substituted template argument, or NULL if an error occurred.
2572static TemplateName
2573SubstDefaultTemplateArgument(Sema &SemaRef,
2574 TemplateDecl *Template,
2575 SourceLocation TemplateLoc,
2576 SourceLocation RAngleLoc,
2577 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002578 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002579 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002580 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002581 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002582
Douglas Gregor788cd062009-11-11 01:00:40 +00002583 MultiLevelTemplateArgumentList AllTemplateArgs
2584 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002585
Douglas Gregor788cd062009-11-11 01:00:40 +00002586 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002587 Template, Converted.data(),
2588 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002589 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002590
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002591 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002592 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002593 if (QualifierLoc) {
2594 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2595 AllTemplateArgs);
2596 if (!QualifierLoc)
2597 return TemplateName();
2598 }
2599
Douglas Gregor1d752d72011-03-02 18:46:51 +00002600 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002601 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002602 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002603 AllTemplateArgs);
2604}
2605
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002606/// \brief If the given template parameter has a default template
2607/// argument, substitute into that default template argument and
2608/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002609TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002610Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2611 SourceLocation TemplateLoc,
2612 SourceLocation RAngleLoc,
2613 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002614 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002615 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002616 if (!TypeParm->hasDefaultArgument())
2617 return TemplateArgumentLoc();
2618
John McCalla93c9342009-12-07 02:54:59 +00002619 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002620 TemplateLoc,
2621 RAngleLoc,
2622 TypeParm,
2623 Converted);
2624 if (DI)
2625 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2626
2627 return TemplateArgumentLoc();
2628 }
2629
2630 if (NonTypeTemplateParmDecl *NonTypeParm
2631 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2632 if (!NonTypeParm->hasDefaultArgument())
2633 return TemplateArgumentLoc();
2634
John McCall60d7b3a2010-08-24 06:29:42 +00002635 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002636 TemplateLoc,
2637 RAngleLoc,
2638 NonTypeParm,
2639 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002640 if (Arg.isInvalid())
2641 return TemplateArgumentLoc();
2642
2643 Expr *ArgE = Arg.takeAs<Expr>();
2644 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2645 }
2646
2647 TemplateTemplateParmDecl *TempTempParm
2648 = cast<TemplateTemplateParmDecl>(Param);
2649 if (!TempTempParm->hasDefaultArgument())
2650 return TemplateArgumentLoc();
2651
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002652
Douglas Gregor1d752d72011-03-02 18:46:51 +00002653 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002654 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002655 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002656 RAngleLoc,
2657 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002658 Converted,
2659 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002660 if (TName.isNull())
2661 return TemplateArgumentLoc();
2662
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002663 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002664 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002665 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2666}
2667
Douglas Gregore7526412009-11-11 19:31:23 +00002668/// \brief Check that the given template argument corresponds to the given
2669/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002670///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002671/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002672/// checked.
2673///
2674/// \param Arg The template argument.
2675///
2676/// \param Template The template in which the template argument resides.
2677///
2678/// \param TemplateLoc The location of the template name for the template
2679/// whose argument list we're matching.
2680///
2681/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2682/// the template argument list.
2683///
2684/// \param ArgumentPackIndex The index into the argument pack where this
2685/// argument will be placed. Only valid if the parameter is a parameter pack.
2686///
2687/// \param Converted The checked, converted argument will be added to the
2688/// end of this small vector.
2689///
2690/// \param CTAK Describes how we arrived at this particular template argument:
2691/// explicitly written, deduced, etc.
2692///
2693/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002694bool Sema::CheckTemplateArgument(NamedDecl *Param,
2695 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002696 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002697 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002698 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002699 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002700 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002701 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002702 // Check template type parameters.
2703 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002704 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002705
Douglas Gregord9e15302009-11-11 19:41:09 +00002706 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002707 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002708 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002709 // with the template arguments we've seen thus far. But if the
2710 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002711 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002712 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2713 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002714
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002715 if (NTTPType->isDependentType() &&
2716 !isa<TemplateTemplateParmDecl>(Template) &&
2717 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002718 // Do substitution on the type of the non-type template parameter.
2719 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002720 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002721 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722
2723 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002724 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002725 NTTPType = SubstType(NTTPType,
2726 MultiLevelTemplateArgumentList(TemplateArgs),
2727 NTTP->getLocation(),
2728 NTTP->getDeclName());
2729 // If that worked, check the non-type template parameter type
2730 // for validity.
2731 if (!NTTPType.isNull())
2732 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2733 NTTP->getLocation());
2734 if (NTTPType.isNull())
2735 return true;
2736 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737
Douglas Gregore7526412009-11-11 19:31:23 +00002738 switch (Arg.getArgument().getKind()) {
2739 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002740 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002741
Douglas Gregore7526412009-11-11 19:31:23 +00002742 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002743 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002744 ExprResult Res =
2745 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2746 Result, CTAK);
2747 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002748 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749
Douglas Gregor910f8002010-11-07 23:05:16 +00002750 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002751 break;
2752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002753
Douglas Gregore7526412009-11-11 19:31:23 +00002754 case TemplateArgument::Declaration:
2755 case TemplateArgument::Integral:
2756 // We've already checked this template argument, so just copy
2757 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002758 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002759 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002760
Douglas Gregore7526412009-11-11 19:31:23 +00002761 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002762 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002763 // We were given a template template argument. It may not be ill-formed;
2764 // see below.
2765 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002766 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2767 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002768 // We have a template argument such as \c T::template X, which we
2769 // parsed as a template template argument. However, since we now
2770 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002771 // template name into an expression.
2772
2773 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2774 Arg.getTemplateNameLoc());
2775
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002776 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002777 SS.Adopt(Arg.getTemplateQualifierLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002778 // FIXME: the template-template arg was a DependentTemplateName,
2779 // so it was provided with a template keyword. However, its source
2780 // location is not stored in the template argument structure.
2781 SourceLocation TemplateKWLoc;
John Wiegley429bb272011-04-08 18:41:53 +00002782 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002783 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002784 TemplateKWLoc,
2785 NameInfo, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002786
Douglas Gregora7fc9012011-01-05 18:58:31 +00002787 // If we parsed the template argument as a pack expansion, create a
2788 // pack expansion expression.
2789 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002790 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2791 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002792 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794
Douglas Gregore7526412009-11-11 19:31:23 +00002795 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002796 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2797 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002798 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002799
Douglas Gregor910f8002010-11-07 23:05:16 +00002800 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002801 break;
2802 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002803
Douglas Gregore7526412009-11-11 19:31:23 +00002804 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002805 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002806 // therefore cannot be a non-type template argument.
2807 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2808 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809
Douglas Gregore7526412009-11-11 19:31:23 +00002810 Diag(Param->getLocation(), diag::note_template_param_here);
2811 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002812
Douglas Gregore7526412009-11-11 19:31:23 +00002813 case TemplateArgument::Type: {
2814 // We have a non-type template parameter but the template
2815 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816
Douglas Gregore7526412009-11-11 19:31:23 +00002817 // C++ [temp.arg]p2:
2818 // In a template-argument, an ambiguity between a type-id and
2819 // an expression is resolved to a type-id, regardless of the
2820 // form of the corresponding template-parameter.
2821 //
2822 // We warn specifically about this case, since it can be rather
2823 // confusing for users.
2824 QualType T = Arg.getArgument().getAsType();
2825 SourceRange SR = Arg.getSourceRange();
2826 if (T->isFunctionType())
2827 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2828 else
2829 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2830 Diag(Param->getLocation(), diag::note_template_param_here);
2831 return true;
2832 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002833
Douglas Gregore7526412009-11-11 19:31:23 +00002834 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002835 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002836 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002837
Douglas Gregore7526412009-11-11 19:31:23 +00002838 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002839 }
2840
2841
Douglas Gregore7526412009-11-11 19:31:23 +00002842 // Check template template parameters.
2843 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002844
Douglas Gregore7526412009-11-11 19:31:23 +00002845 // Substitute into the template parameter list of the template
2846 // template parameter, since previously-supplied template arguments
2847 // may appear within the template template parameter.
2848 {
2849 // Set up a template instantiation context.
2850 LocalInstantiationScope Scope(*this);
2851 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002852 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002853 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002854
2855 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002856 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002857 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002858 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002859 MultiLevelTemplateArgumentList(TemplateArgs)));
2860 if (!TempParm)
2861 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002862 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002863
Douglas Gregore7526412009-11-11 19:31:23 +00002864 switch (Arg.getArgument().getKind()) {
2865 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002866 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002867
Douglas Gregore7526412009-11-11 19:31:23 +00002868 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002869 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002870 if (CheckTemplateArgument(TempParm, Arg))
2871 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002872
Douglas Gregor910f8002010-11-07 23:05:16 +00002873 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002874 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002875
Douglas Gregore7526412009-11-11 19:31:23 +00002876 case TemplateArgument::Expression:
2877 case TemplateArgument::Type:
2878 // We have a template template parameter but the template
2879 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002880 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
David Blaikie4e4d0842012-03-11 07:00:24 +00002881 << getLangOpts().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002882 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002883
Douglas Gregore7526412009-11-11 19:31:23 +00002884 case TemplateArgument::Declaration:
David Blaikie7530c032012-01-17 06:56:22 +00002885 llvm_unreachable("Declaration argument with template template parameter");
Douglas Gregore7526412009-11-11 19:31:23 +00002886 case TemplateArgument::Integral:
David Blaikie7530c032012-01-17 06:56:22 +00002887 llvm_unreachable("Integral argument with template template parameter");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002888
Douglas Gregore7526412009-11-11 19:31:23 +00002889 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002890 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002891 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002892
Douglas Gregore7526412009-11-11 19:31:23 +00002893 return false;
2894}
2895
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002896/// \brief Diagnose an arity mismatch in the
2897static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
2898 SourceLocation TemplateLoc,
2899 TemplateArgumentListInfo &TemplateArgs) {
2900 TemplateParameterList *Params = Template->getTemplateParameters();
2901 unsigned NumParams = Params->size();
2902 unsigned NumArgs = TemplateArgs.size();
2903
2904 SourceRange Range;
2905 if (NumArgs > NumParams)
2906 Range = SourceRange(TemplateArgs[NumParams].getLocation(),
2907 TemplateArgs.getRAngleLoc());
2908 S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2909 << (NumArgs > NumParams)
2910 << (isa<ClassTemplateDecl>(Template)? 0 :
2911 isa<FunctionTemplateDecl>(Template)? 1 :
2912 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2913 << Template << Range;
2914 S.Diag(Template->getLocation(), diag::note_template_decl_here)
2915 << Params->getSourceRange();
2916 return true;
2917}
2918
Douglas Gregorc15cb382009-02-09 23:23:08 +00002919/// \brief Check that the given template argument list is well-formed
2920/// for specializing the given template.
2921bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2922 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002923 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002924 bool PartialTemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00002925 SmallVectorImpl<TemplateArgument> &Converted,
2926 bool *ExpansionIntoFixedList) {
2927 if (ExpansionIntoFixedList)
2928 *ExpansionIntoFixedList = false;
2929
Douglas Gregorc15cb382009-02-09 23:23:08 +00002930 TemplateParameterList *Params = Template->getTemplateParameters();
2931 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002932 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002933 bool Invalid = false;
2934
John McCalld5532b62009-11-23 01:53:49 +00002935 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2936
Mike Stump1eb44332009-09-09 15:08:12 +00002937 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002938 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Douglas Gregorb70126a2012-02-03 17:16:23 +00002939
Mike Stump1eb44332009-09-09 15:08:12 +00002940 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002941 // [...] The type and form of each template-argument specified in
2942 // a template-id shall match the type and form specified for the
2943 // corresponding parameter declared by the template in its
2944 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002945 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002946 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002947 TemplateParameterList::iterator Param = Params->begin(),
2948 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002949 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002950 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002951 bool SawPackExpansion = false;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002952 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002953 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002954 // If we have an expanded parameter pack, make sure we don't have too
2955 // many arguments.
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002956 // FIXME: This really should fall out from the normal arity checking.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002957 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002958 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002959 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002960 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2961 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2962 << true
2963 << (isa<ClassTemplateDecl>(Template)? 0 :
2964 isa<FunctionTemplateDecl>(Template)? 1 :
2965 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2966 << Template;
2967 Diag(Template->getLocation(), diag::note_template_decl_here)
2968 << Params->getSourceRange();
2969 return true;
2970 }
2971 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002972
Douglas Gregorf35f8282009-11-11 21:54:23 +00002973 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002974 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2975 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002976 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002977 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002978
Douglas Gregor14be16b2010-12-20 16:57:52 +00002979 if ((*Param)->isTemplateParameterPack()) {
2980 // The template parameter was a template parameter pack, so take the
2981 // deduced argument and place it on the argument pack. Note that we
2982 // stay on the same template parameter so that we can deduce more
2983 // arguments.
2984 ArgumentPack.push_back(Converted.back());
2985 Converted.pop_back();
2986 } else {
2987 // Move to the next template parameter.
2988 ++Param;
2989 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002990
2991 // If this template argument is a pack expansion, record that fact
2992 // and break out; we can't actually check any more.
2993 if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) {
2994 SawPackExpansion = true;
2995 ++ArgIdx;
2996 break;
2997 }
2998
Douglas Gregor14be16b2010-12-20 16:57:52 +00002999 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003000 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003001 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003002
Douglas Gregor8735b292011-06-03 02:59:40 +00003003 // If we're checking a partial template argument list, we're done.
3004 if (PartialTemplateArgs) {
3005 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
3006 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3007 ArgumentPack.data(),
3008 ArgumentPack.size()));
3009
3010 return Invalid;
3011 }
3012
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003013 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00003014 // arguments, just break out now and we'll fill in the argument pack below.
3015 if ((*Param)->isTemplateParameterPack())
3016 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00003017
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003018 // Check whether we have a default argument.
Douglas Gregorf35f8282009-11-11 21:54:23 +00003019 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003020
Douglas Gregorf35f8282009-11-11 21:54:23 +00003021 // Retrieve the default template argument from the template
3022 // parameter. For each kind of template parameter, we substitute the
3023 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003024 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00003025 // the default argument.
3026 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003027 if (!TTP->hasDefaultArgument())
3028 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3029 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003030
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003032 Template,
3033 TemplateLoc,
3034 RAngleLoc,
3035 TTP,
3036 Converted);
3037 if (!ArgType)
3038 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003039
Douglas Gregorf35f8282009-11-11 21:54:23 +00003040 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3041 ArgType);
3042 } else if (NonTypeTemplateParmDecl *NTTP
3043 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003044 if (!NTTP->hasDefaultArgument())
3045 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3046 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003047
John McCall60d7b3a2010-08-24 06:29:42 +00003048 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003049 TemplateLoc,
3050 RAngleLoc,
3051 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003052 Converted);
3053 if (E.isInvalid())
3054 return true;
3055
3056 Expr *Ex = E.takeAs<Expr>();
3057 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3058 } else {
3059 TemplateTemplateParmDecl *TempParm
3060 = cast<TemplateTemplateParmDecl>(*Param);
3061
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003062 if (!TempParm->hasDefaultArgument())
3063 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3064 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003065
Douglas Gregor1d752d72011-03-02 18:46:51 +00003066 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003067 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003068 TemplateLoc,
3069 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003070 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003071 Converted,
3072 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003073 if (Name.isNull())
3074 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003075
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003076 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3077 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003079
Douglas Gregorf35f8282009-11-11 21:54:23 +00003080 // Introduce an instantiation record that describes where we are using
3081 // the default template argument.
3082 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003083 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003084 SourceRange(TemplateLoc, RAngleLoc));
3085
Douglas Gregorf35f8282009-11-11 21:54:23 +00003086 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003087 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003088 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003089 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003090
Douglas Gregor67714232011-03-03 02:41:12 +00003091 // Core issue 150 (assumed resolution): if this is a template template
3092 // parameter, keep track of the default template arguments from the
3093 // template definition.
3094 if (isTemplateTemplateParameter)
3095 TemplateArgs.addArgument(Arg);
3096
Douglas Gregor14be16b2010-12-20 16:57:52 +00003097 // Move to the next template parameter and argument.
3098 ++Param;
3099 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003100 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003101
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003102 // If we saw a pack expansion, then directly convert the remaining arguments,
3103 // because we don't know what parameters they'll match up with.
3104 if (SawPackExpansion) {
3105 bool AddToArgumentPack
3106 = Param != ParamEnd && (*Param)->isTemplateParameterPack();
3107 while (ArgIdx < NumArgs) {
3108 if (AddToArgumentPack)
3109 ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument());
3110 else
3111 Converted.push_back(TemplateArgs[ArgIdx].getArgument());
3112 ++ArgIdx;
3113 }
3114
3115 // Push the argument pack onto the list of converted arguments.
3116 if (AddToArgumentPack) {
3117 if (ArgumentPack.empty())
3118 Converted.push_back(TemplateArgument(0, 0));
3119 else {
3120 Converted.push_back(
3121 TemplateArgument::CreatePackCopy(Context,
3122 ArgumentPack.data(),
3123 ArgumentPack.size()));
3124 ArgumentPack.clear();
3125 }
Douglas Gregorb70126a2012-02-03 17:16:23 +00003126 } else if (ExpansionIntoFixedList) {
3127 // We have expanded a pack into a fixed list.
3128 *ExpansionIntoFixedList = true;
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003129 }
3130
3131 return Invalid;
3132 }
3133
3134 // If we have any leftover arguments, then there were too many arguments.
3135 // Complain and fail.
3136 if (ArgIdx < NumArgs)
3137 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
3138
3139 // If we have an expanded parameter pack, make sure we don't have too
3140 // many arguments.
3141 // FIXME: This really should fall out from the normal arity checking.
3142 if (Param != ParamEnd) {
3143 if (NonTypeTemplateParmDecl *NTTP
3144 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3145 if (NTTP->isExpandedParameterPack() &&
3146 ArgumentPack.size() < NTTP->getNumExpansionTypes()) {
3147 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3148 << false
3149 << (isa<ClassTemplateDecl>(Template)? 0 :
3150 isa<FunctionTemplateDecl>(Template)? 1 :
3151 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3152 << Template;
3153 Diag(Template->getLocation(), diag::note_template_decl_here)
3154 << Params->getSourceRange();
3155 return true;
3156 }
3157 }
3158 }
3159
Douglas Gregor14be16b2010-12-20 16:57:52 +00003160 // Form argument packs for each of the parameter packs remaining.
3161 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003162 // If we're checking a partial list of template arguments, don't fill
3163 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003164 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003165 if (!HasParameterPack)
3166 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003167 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003168 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003169 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003170 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3171 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003172 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003173 ArgumentPack.clear();
3174 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003175 } else if (!PartialTemplateArgs)
3176 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003177
Douglas Gregor14be16b2010-12-20 16:57:52 +00003178 ++Param;
3179 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003180
Douglas Gregorc15cb382009-02-09 23:23:08 +00003181 return Invalid;
3182}
3183
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003184namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003185 class UnnamedLocalNoLinkageFinder
3186 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003187 {
3188 Sema &S;
3189 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003190
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003191 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003192
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003193 public:
3194 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3195
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003196 bool Visit(QualType T) {
3197 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003198 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003199
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003200#define TYPE(Class, Parent) \
3201 bool Visit##Class##Type(const Class##Type *);
3202#define ABSTRACT_TYPE(Class, Parent) \
3203 bool Visit##Class##Type(const Class##Type *) { return false; }
3204#define NON_CANONICAL_TYPE(Class, Parent) \
3205 bool Visit##Class##Type(const Class##Type *) { return false; }
3206#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003207
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003208 bool VisitTagDecl(const TagDecl *Tag);
3209 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3210 };
3211}
3212
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003213bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003214 return false;
3215}
3216
3217bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3218 return Visit(T->getElementType());
3219}
3220
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003221bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003222 return Visit(T->getPointeeType());
3223}
3224
3225bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003226 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003227 return Visit(T->getPointeeType());
3228}
3229
3230bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003231 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003232 return Visit(T->getPointeeType());
3233}
3234
3235bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003236 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003237 return Visit(T->getPointeeType());
3238}
3239
3240bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003241 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003242 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3243}
3244
3245bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003246 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003247 return Visit(T->getElementType());
3248}
3249
3250bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003251 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003252 return Visit(T->getElementType());
3253}
3254
3255bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003256 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003257 return Visit(T->getElementType());
3258}
3259
3260bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003261 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003262 return Visit(T->getElementType());
3263}
3264
3265bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003266 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003267 return Visit(T->getElementType());
3268}
3269
3270bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3271 return Visit(T->getElementType());
3272}
3273
3274bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3275 return Visit(T->getElementType());
3276}
3277
3278bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3279 const FunctionProtoType* T) {
3280 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003281 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003282 A != AEnd; ++A) {
3283 if (Visit(*A))
3284 return true;
3285 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003286
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003287 return Visit(T->getResultType());
3288}
3289
3290bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3291 const FunctionNoProtoType* T) {
3292 return Visit(T->getResultType());
3293}
3294
3295bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3296 const UnresolvedUsingType*) {
3297 return false;
3298}
3299
3300bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3301 return false;
3302}
3303
3304bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3305 return Visit(T->getUnderlyingType());
3306}
3307
3308bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3309 return false;
3310}
3311
Sean Huntca63c202011-05-24 22:41:36 +00003312bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3313 const UnaryTransformType*) {
3314 return false;
3315}
3316
Richard Smith34b41d92011-02-20 03:19:35 +00003317bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3318 return Visit(T->getDeducedType());
3319}
3320
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003321bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3322 return VisitTagDecl(T->getDecl());
3323}
3324
3325bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3326 return VisitTagDecl(T->getDecl());
3327}
3328
3329bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3330 const TemplateTypeParmType*) {
3331 return false;
3332}
3333
Douglas Gregorc3069d62011-01-14 02:55:32 +00003334bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3335 const SubstTemplateTypeParmPackType *) {
3336 return false;
3337}
3338
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003339bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3340 const TemplateSpecializationType*) {
3341 return false;
3342}
3343
3344bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3345 const InjectedClassNameType* T) {
3346 return VisitTagDecl(T->getDecl());
3347}
3348
3349bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3350 const DependentNameType* T) {
3351 return VisitNestedNameSpecifier(T->getQualifier());
3352}
3353
3354bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3355 const DependentTemplateSpecializationType* T) {
3356 return VisitNestedNameSpecifier(T->getQualifier());
3357}
3358
Douglas Gregor7536dd52010-12-20 02:24:11 +00003359bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3360 const PackExpansionType* T) {
3361 return Visit(T->getPattern());
3362}
3363
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003364bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3365 return false;
3366}
3367
3368bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3369 const ObjCInterfaceType *) {
3370 return false;
3371}
3372
3373bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3374 const ObjCObjectPointerType *) {
3375 return false;
3376}
3377
Eli Friedmanb001de72011-10-06 23:00:33 +00003378bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3379 return Visit(T->getValueType());
3380}
3381
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003382bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3383 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003384 S.Diag(SR.getBegin(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003385 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003386 diag::warn_cxx98_compat_template_arg_local_type :
3387 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003388 << S.Context.getTypeDeclType(Tag) << SR;
3389 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003390 }
3391
Richard Smith162e1c12011-04-15 14:24:37 +00003392 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003393 S.Diag(SR.getBegin(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003394 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003395 diag::warn_cxx98_compat_template_arg_unnamed_type :
3396 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003397 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3398 return true;
3399 }
3400
3401 return false;
3402}
3403
3404bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3405 NestedNameSpecifier *NNS) {
3406 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3407 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003408
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003409 switch (NNS->getKind()) {
3410 case NestedNameSpecifier::Identifier:
3411 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003412 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003413 case NestedNameSpecifier::Global:
3414 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003415
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003416 case NestedNameSpecifier::TypeSpec:
3417 case NestedNameSpecifier::TypeSpecWithTemplate:
3418 return Visit(QualType(NNS->getAsType(), 0));
3419 }
David Blaikie7530c032012-01-17 06:56:22 +00003420 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003421}
3422
3423
Douglas Gregorc15cb382009-02-09 23:23:08 +00003424/// \brief Check a template argument against its corresponding
3425/// template type parameter.
3426///
3427/// This routine implements the semantics of C++ [temp.arg.type]. It
3428/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003429bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003430 TypeSourceInfo *ArgInfo) {
3431 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003432 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003433 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003434
3435 if (Arg->isVariablyModifiedType()) {
3436 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003437 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003438 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003439 }
3440
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003441 // C++03 [temp.arg.type]p2:
3442 // A local type, a type with no linkage, an unnamed type or a type
3443 // compounded from any of these types shall not be used as a
3444 // template-argument for a template type-parameter.
3445 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003446 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003447 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003448 if (LangOpts.CPlusPlus0x ?
3449 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3450 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3451 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3452 SR.getBegin()) != DiagnosticsEngine::Ignored :
3453 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003454 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3455 (void)Finder.Visit(Context.getCanonicalType(Arg));
3456 }
3457
Douglas Gregorc15cb382009-02-09 23:23:08 +00003458 return false;
3459}
3460
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003461/// \brief Checks whether the given template argument is the address
3462/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003463static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003464CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3465 NonTypeTemplateParmDecl *Param,
3466 QualType ParamType,
3467 Expr *ArgIn,
3468 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003469 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003470 Expr *Arg = ArgIn;
3471 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003472
3473 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003474 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003475
3476 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003477 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003478 // A template-argument for a non-type, non-template
3479 // template-parameter shall be one of: [...]
3480 //
3481 // -- the address of an object or function with external
3482 // linkage, including function templates and function
3483 // template-ids but excluding non-static class members,
3484 // expressed as & id-expression where the & is optional if
3485 // the name refers to a function or array, or if the
3486 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003487
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003488 // In C++98/03 mode, give an extension warning on any extra parentheses.
3489 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3490 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003491 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003492 if (!Invalid && !ExtraParens) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003493 S.Diag(Arg->getLocStart(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003494 S.getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003495 diag::warn_cxx98_compat_template_arg_extra_parens :
3496 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003497 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003498 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003499 }
3500
3501 Arg = Parens->getSubExpr();
3502 }
3503
John McCall91a57552011-07-15 05:09:51 +00003504 while (SubstNonTypeTemplateParmExpr *subst =
3505 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3506 Arg = subst->getReplacement()->IgnoreImpCasts();
3507
Douglas Gregorb7a09262010-04-01 18:32:35 +00003508 bool AddressTaken = false;
3509 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003510 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003511 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003512 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003513 AddressTaken = true;
3514 AddrOpLoc = UnOp->getOperatorLoc();
3515 }
Francois Picheta343a412011-04-29 09:08:14 +00003516 }
John McCall91a57552011-07-15 05:09:51 +00003517
David Blaikie4e4d0842012-03-11 07:00:24 +00003518 if (S.getLangOpts().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003519 Converted = TemplateArgument(ArgIn);
3520 return false;
3521 }
3522
3523 while (SubstNonTypeTemplateParmExpr *subst =
3524 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3525 Arg = subst->getReplacement()->IgnoreImpCasts();
3526
3527 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003528 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003529 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3530 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003531 S.Diag(Param->getLocation(), diag::note_template_param_here);
3532 return true;
3533 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003534
3535 // Stop checking the precise nature of the argument if it is value dependent,
3536 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003537 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003538 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003539 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003540 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003541
Douglas Gregorb7a09262010-04-01 18:32:35 +00003542 if (!isa<ValueDecl>(DRE->getDecl())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003543 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003544 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003545 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003546 S.Diag(Param->getLocation(), diag::note_template_param_here);
3547 return true;
3548 }
3549
3550 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003551
3552 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003553 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003554 S.Diag(Arg->getLocStart(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003555 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003556 S.Diag(Param->getLocation(), diag::note_template_param_here);
3557 return true;
3558 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003559
3560 // Cannot refer to non-static member functions
3561 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003562 if (!Method->isStatic()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003563 S.Diag(Arg->getLocStart(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003564 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003565 S.Diag(Param->getLocation(), diag::note_template_param_here);
3566 return true;
3567 }
Mike Stump1eb44332009-09-09 15:08:12 +00003568
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003569 // Functions must have external linkage.
3570 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003571 if (!isExternalLinkage(Func->getLinkage())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003572 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003573 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003574 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003575 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003576 << true;
3577 return true;
3578 }
3579
3580 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003581 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003582
Douglas Gregorb7a09262010-04-01 18:32:35 +00003583 // If the template parameter has pointer type, the function decays.
3584 if (ParamType->isPointerType() && !AddressTaken)
3585 ArgType = S.Context.getPointerType(Func->getType());
3586 else if (AddressTaken && ParamType->isReferenceType()) {
3587 // If we originally had an address-of operator, but the
3588 // parameter has reference type, complain and (if things look
3589 // like they will work) drop the address-of operator.
3590 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3591 ParamType.getNonReferenceType())) {
3592 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3593 << ParamType;
3594 S.Diag(Param->getLocation(), diag::note_template_param_here);
3595 return true;
3596 }
3597
3598 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3599 << ParamType
3600 << FixItHint::CreateRemoval(AddrOpLoc);
3601 S.Diag(Param->getLocation(), diag::note_template_param_here);
3602
3603 ArgType = Func->getType();
3604 }
3605 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003606 if (!isExternalLinkage(Var->getLinkage())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003607 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003608 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003609 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003610 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003611 << true;
3612 return true;
3613 }
3614
Douglas Gregorb7a09262010-04-01 18:32:35 +00003615 // A value of reference type is not an object.
3616 if (Var->getType()->isReferenceType()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003617 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003618 diag::err_template_arg_reference_var)
3619 << Var->getType() << Arg->getSourceRange();
3620 S.Diag(Param->getLocation(), diag::note_template_param_here);
3621 return true;
3622 }
3623
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003624 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003625 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003626
3627 // If the template parameter has pointer type, we must have taken
3628 // the address of this object.
3629 if (ParamType->isReferenceType()) {
3630 if (AddressTaken) {
3631 // If we originally had an address-of operator, but the
3632 // parameter has reference type, complain and (if things look
3633 // like they will work) drop the address-of operator.
3634 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3635 ParamType.getNonReferenceType())) {
3636 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3637 << ParamType;
3638 S.Diag(Param->getLocation(), diag::note_template_param_here);
3639 return true;
3640 }
3641
3642 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3643 << ParamType
3644 << FixItHint::CreateRemoval(AddrOpLoc);
3645 S.Diag(Param->getLocation(), diag::note_template_param_here);
3646
3647 ArgType = Var->getType();
3648 }
3649 } else if (!AddressTaken && ParamType->isPointerType()) {
3650 if (Var->getType()->isArrayType()) {
3651 // Array-to-pointer decay.
3652 ArgType = S.Context.getArrayDecayedType(Var->getType());
3653 } else {
3654 // If the template parameter has pointer type but the address of
3655 // this object was not taken, complain and (possibly) recover by
3656 // taking the address of the entity.
3657 ArgType = S.Context.getPointerType(Var->getType());
3658 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3659 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3660 << ParamType;
3661 S.Diag(Param->getLocation(), diag::note_template_param_here);
3662 return true;
3663 }
3664
3665 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3666 << ParamType
3667 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3668
3669 S.Diag(Param->getLocation(), diag::note_template_param_here);
3670 }
3671 }
3672 } else {
3673 // We found something else, but we don't know specifically what it is.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003674 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003675 diag::err_template_arg_not_object_or_func)
3676 << Arg->getSourceRange();
3677 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3678 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003679 }
Mike Stump1eb44332009-09-09 15:08:12 +00003680
John McCallf85e1932011-06-15 23:02:42 +00003681 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003682 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003683 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003684 S.IsQualificationConversion(ArgType, ParamType, false,
3685 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003686 // For pointer-to-object types, qualification conversions are
3687 // permitted.
3688 } else {
3689 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3690 if (!ParamRef->getPointeeType()->isFunctionType()) {
3691 // C++ [temp.arg.nontype]p5b3:
3692 // For a non-type template-parameter of type reference to
3693 // object, no conversions apply. The type referred to by the
3694 // reference may be more cv-qualified than the (otherwise
3695 // identical) type of the template- argument. The
3696 // template-parameter is bound directly to the
3697 // template-argument, which shall be an lvalue.
3698
3699 // FIXME: Other qualifiers?
3700 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3701 unsigned ArgQuals = ArgType.getCVRQualifiers();
3702
3703 if ((ParamQuals | ArgQuals) != ParamQuals) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003704 S.Diag(Arg->getLocStart(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003705 diag::err_template_arg_ref_bind_ignores_quals)
3706 << ParamType << Arg->getType()
3707 << Arg->getSourceRange();
3708 S.Diag(Param->getLocation(), diag::note_template_param_here);
3709 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003710 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003711 }
3712 }
3713
3714 // At this point, the template argument refers to an object or
3715 // function with external linkage. We now need to check whether the
3716 // argument and parameter types are compatible.
3717 if (!S.Context.hasSameUnqualifiedType(ArgType,
3718 ParamType.getNonReferenceType())) {
3719 // We can't perform this conversion or binding.
3720 if (ParamType->isReferenceType())
3721 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003722 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003723 else
3724 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003725 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003726 S.Diag(Param->getLocation(), diag::note_template_param_here);
3727 return true;
3728 }
3729 }
3730
3731 // Create the template argument.
3732 Converted = TemplateArgument(Entity->getCanonicalDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00003733 S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003734 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003735}
3736
3737/// \brief Checks whether the given template argument is a pointer to
3738/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003739bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003740 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003741 bool Invalid = false;
3742
3743 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003744 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003745 Arg = Cast->getSubExpr();
3746
3747 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003748 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003749 // A template-argument for a non-type, non-template
3750 // template-parameter shall be one of: [...]
3751 //
3752 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003753 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003754
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003755 // In C++98/03 mode, give an extension warning on any extra parentheses.
3756 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3757 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003758 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003759 if (!Invalid && !ExtraParens) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003760 Diag(Arg->getLocStart(),
David Blaikie4e4d0842012-03-11 07:00:24 +00003761 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00003762 diag::warn_cxx98_compat_template_arg_extra_parens :
3763 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003764 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003765 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003766 }
3767
3768 Arg = Parens->getSubExpr();
3769 }
3770
John McCall91a57552011-07-15 05:09:51 +00003771 while (SubstNonTypeTemplateParmExpr *subst =
3772 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3773 Arg = subst->getReplacement()->IgnoreImpCasts();
3774
Douglas Gregorcaddba02009-11-12 18:38:13 +00003775 // A pointer-to-member constant written &Class::member.
3776 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003777 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003778 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3779 if (DRE && !DRE->getQualifier())
3780 DRE = 0;
3781 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003782 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003783 // A constant of pointer-to-member type.
3784 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3785 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3786 if (VD->getType()->isMemberPointerType()) {
3787 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003788 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003789 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3790 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003791 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003792 else
3793 Converted = TemplateArgument(VD->getCanonicalDecl());
3794 return Invalid;
3795 }
3796 }
3797 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003798
Douglas Gregorcaddba02009-11-12 18:38:13 +00003799 DRE = 0;
3800 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003801
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003802 if (!DRE)
Daniel Dunbar96a00142012-03-09 18:35:03 +00003803 return Diag(Arg->getLocStart(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003804 diag::err_template_arg_not_pointer_to_member_form)
3805 << Arg->getSourceRange();
3806
3807 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3808 assert((isa<FieldDecl>(DRE->getDecl()) ||
3809 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3810 "Only non-static member pointers can make it here");
3811
3812 // Okay: this is the address of a non-static member, and therefore
3813 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003814 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003815 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003816 else
3817 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003818 return Invalid;
3819 }
3820
3821 // We found something else, but we don't know specifically what it is.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003822 Diag(Arg->getLocStart(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003823 diag::err_template_arg_not_pointer_to_member_form)
3824 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003825 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003826 diag::note_template_arg_refers_here);
3827 return true;
3828}
3829
Douglas Gregorc15cb382009-02-09 23:23:08 +00003830/// \brief Check a template argument against its corresponding
3831/// non-type template parameter.
3832///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003833/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003834/// If an error occurred, it returns ExprError(); otherwise, it
3835/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003836/// InstantiatedParamType is the type of the non-type template
3837/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003838ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3839 QualType InstantiatedParamType, Expr *Arg,
3840 TemplateArgument &Converted,
3841 CheckTemplateArgumentKind CTAK) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003842 SourceLocation StartLoc = Arg->getLocStart();
Douglas Gregor40808ce2009-03-09 23:48:35 +00003843
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003844 // If either the parameter has a dependent type or the argument is
3845 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003846 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3847 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003848 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003849 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003850 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003851
3852 // C++ [temp.arg.nontype]p5:
3853 // The following conversions are performed on each expression used
3854 // as a non-type template-argument. If a non-type
3855 // template-argument cannot be converted to the type of the
3856 // corresponding template-parameter then the program is
3857 // ill-formed.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003858 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003859 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003860 // C++11:
3861 // -- for a non-type template-parameter of integral or
3862 // enumeration type, conversions permitted in a converted
3863 // constant expression are applied.
3864 //
3865 // C++98:
3866 // -- for a non-type template-parameter of integral or
3867 // enumeration type, integral promotions (4.5) and integral
3868 // conversions (4.7) are applied.
3869
3870 if (CTAK == CTAK_Deduced &&
3871 !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
3872 // C++ [temp.deduct.type]p17:
3873 // If, in the declaration of a function template with a non-type
3874 // template-parameter, the non-type template-parameter is used
3875 // in an expression in the function parameter-list and, if the
3876 // corresponding template-argument is deduced, the
3877 // template-argument type shall match the type of the
3878 // template-parameter exactly, except that a template-argument
3879 // deduced from an array bound may be of any integral type.
3880 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3881 << Arg->getType().getUnqualifiedType()
3882 << ParamType.getUnqualifiedType();
3883 Diag(Param->getLocation(), diag::note_template_param_here);
3884 return ExprError();
3885 }
3886
David Blaikie4e4d0842012-03-11 07:00:24 +00003887 if (getLangOpts().CPlusPlus0x) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003888 // We can't check arbitrary value-dependent arguments.
3889 // FIXME: If there's no viable conversion to the template parameter type,
3890 // we should be able to diagnose that prior to instantiation.
3891 if (Arg->isValueDependent()) {
3892 Converted = TemplateArgument(Arg);
3893 return Owned(Arg);
3894 }
3895
3896 // C++ [temp.arg.nontype]p1:
3897 // A template-argument for a non-type, non-template template-parameter
3898 // shall be one of:
3899 //
3900 // -- for a non-type template-parameter of integral or enumeration
3901 // type, a converted constant expression of the type of the
3902 // template-parameter; or
3903 llvm::APSInt Value;
3904 ExprResult ArgResult =
3905 CheckConvertedConstantExpression(Arg, ParamType, Value,
3906 CCEK_TemplateArg);
3907 if (ArgResult.isInvalid())
3908 return ExprError();
3909
3910 // Widen the argument value to sizeof(parameter type). This is almost
3911 // always a no-op, except when the parameter type is bool. In
3912 // that case, this may extend the argument from 1 bit to 8 bits.
3913 QualType IntegerType = ParamType;
3914 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
3915 IntegerType = Enum->getDecl()->getIntegerType();
3916 Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
3917
3918 Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
3919 return ArgResult;
3920 }
3921
Richard Smith4f870622011-10-27 22:11:44 +00003922 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3923 if (ArgResult.isInvalid())
3924 return ExprError();
3925 Arg = ArgResult.take();
3926
3927 QualType ArgType = Arg->getType();
3928
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003929 // C++ [temp.arg.nontype]p1:
3930 // A template-argument for a non-type, non-template
3931 // template-parameter shall be one of:
3932 //
3933 // -- an integral constant-expression of integral or enumeration
3934 // type; or
3935 // -- the name of a non-type template-parameter; or
3936 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003937 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003938 if (!ArgType->isIntegralOrEnumerationType()) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00003939 Diag(Arg->getLocStart(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003940 diag::err_template_arg_not_integral_or_enumeral)
3941 << ArgType << Arg->getSourceRange();
3942 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003943 return ExprError();
Richard Smith282e7e62012-02-04 09:53:13 +00003944 } else if (!Arg->isValueDependent()) {
3945 Arg = VerifyIntegerConstantExpression(Arg, &Value,
3946 PDiag(diag::err_template_arg_not_ice) << ArgType, false).take();
3947 if (!Arg)
3948 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003949 }
3950
Douglas Gregor02024a92010-03-28 02:42:43 +00003951 // From here on out, all we care about are the unqualified forms
3952 // of the parameter and argument types.
3953 ParamType = ParamType.getUnqualifiedType();
3954 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003955
3956 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003957 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003958 // Okay: no conversion necessary
John McCalldaa8e4e2010-11-15 09:13:47 +00003959 } else if (ParamType->isBooleanType()) {
3960 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003961 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003962 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3963 !ParamType->isEnumeralType()) {
3964 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003965 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003966 } else {
3967 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00003968 Diag(Arg->getLocStart(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003969 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003970 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003971 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003972 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003973 }
3974
Douglas Gregorc7469372011-05-04 21:55:00 +00003975 // Add the value of this argument to the list of converted
3976 // arguments. We use the bitwidth and signedness of the template
3977 // parameter.
3978 if (Arg->isValueDependent()) {
3979 // The argument is value-dependent. Create a new
3980 // TemplateArgument with the converted expression.
3981 Converted = TemplateArgument(Arg);
3982 return Owned(Arg);
3983 }
3984
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003985 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003986 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003987 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003988
Douglas Gregorc7469372011-05-04 21:55:00 +00003989 if (ParamType->isBooleanType()) {
3990 // Value must be zero or one.
3991 Value = Value != 0;
3992 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3993 if (Value.getBitWidth() != AllowedBits)
3994 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003995 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003996 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003997 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003998
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003999 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004000 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00004001 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00004002 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00004003 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00004004 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00004005
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004006 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00004007 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00004008 && (OldValue.isSigned() && OldValue.isNegative())) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004009 Diag(Arg->getLocStart(), diag::warn_template_arg_negative)
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004010 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4011 << Arg->getSourceRange();
4012 Diag(Param->getLocation(), diag::note_template_param_here);
4013 }
Douglas Gregorc7469372011-05-04 21:55:00 +00004014
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004015 // Complain if we overflowed the template parameter's type.
4016 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00004017 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004018 RequiredBits = OldValue.getActiveBits();
4019 else if (OldValue.isUnsigned())
4020 RequiredBits = OldValue.getActiveBits() + 1;
4021 else
4022 RequiredBits = OldValue.getMinSignedBits();
4023 if (RequiredBits > AllowedBits) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004024 Diag(Arg->getLocStart(),
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004025 diag::warn_template_arg_too_large)
4026 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4027 << Arg->getSourceRange();
4028 Diag(Param->getLocation(), diag::note_template_param_here);
4029 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00004030 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00004031
John McCall833ca992009-10-29 08:12:44 +00004032 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00004033 ParamType->isEnumeralType()
4034 ? Context.getCanonicalType(ParamType)
4035 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00004036 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00004037 }
Douglas Gregora35284b2009-02-11 00:19:33 +00004038
Richard Smith4f870622011-10-27 22:11:44 +00004039 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00004040 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
4041
Douglas Gregorb7a09262010-04-01 18:32:35 +00004042 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
4043 // from a template argument of type std::nullptr_t to a non-type
4044 // template parameter of type pointer to object, pointer to
4045 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004046 if (ArgType->isNullPtrType()) {
4047 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
4048 Converted = TemplateArgument((NamedDecl *)0);
4049 return Owned(Arg);
4050 }
4051
4052 if (ParamType->isNullPtrType()) {
4053 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
4054 Converted = TemplateArgument(Zero, Context.NullPtrTy);
4055 return Owned(Arg);
4056 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004057 }
4058
Douglas Gregorb86b0572009-02-11 01:18:59 +00004059 // Handle pointer-to-function, reference-to-function, and
4060 // pointer-to-member-function all in (roughly) the same way.
4061 if (// -- For a non-type template-parameter of type pointer to
4062 // function, only the function-to-pointer conversion (4.3) is
4063 // applied. If the template-argument represents a set of
4064 // overloaded functions (or a pointer to such), the matching
4065 // function is selected from the set (13.4).
4066 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004067 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004068 // -- For a non-type template-parameter of type reference to
4069 // function, no conversions apply. If the template-argument
4070 // represents a set of overloaded functions, the matching
4071 // function is selected from the set (13.4).
4072 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004073 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004074 // -- For a non-type template-parameter of type pointer to
4075 // member function, no conversions apply. If the
4076 // template-argument represents a set of overloaded member
4077 // functions, the matching member function is selected from
4078 // the set (13.4).
4079 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004080 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00004081 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004082
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004083 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004084 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004085 true,
4086 FoundResult)) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004087 if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
John Wiegley429bb272011-04-08 18:41:53 +00004088 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004089
4090 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4091 ArgType = Arg->getType();
4092 } else
John Wiegley429bb272011-04-08 18:41:53 +00004093 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004094 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004095
John Wiegley429bb272011-04-08 18:41:53 +00004096 if (!ParamType->isMemberPointerType()) {
4097 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4098 ParamType,
4099 Arg, Converted))
4100 return ExprError();
4101 return Owned(Arg);
4102 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004103
John McCallf85e1932011-06-15 23:02:42 +00004104 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00004105 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00004106 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004107 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4108 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004109 } else if (!Context.hasSameUnqualifiedType(ArgType,
4110 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00004111 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00004112 Diag(Arg->getLocStart(),
Douglas Gregora35284b2009-02-11 00:19:33 +00004113 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004114 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00004115 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004116 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004117 }
Mike Stump1eb44332009-09-09 15:08:12 +00004118
John Wiegley429bb272011-04-08 18:41:53 +00004119 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4120 return ExprError();
4121 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00004122 }
4123
Chris Lattnerfe90de72009-02-20 21:37:53 +00004124 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004125 // -- for a non-type template-parameter of type pointer to
4126 // object, qualification conversions (4.4) and the
4127 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00004128 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00004129 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004130 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004131
John Wiegley429bb272011-04-08 18:41:53 +00004132 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4133 ParamType,
4134 Arg, Converted))
4135 return ExprError();
4136 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004137 }
Mike Stump1eb44332009-09-09 15:08:12 +00004138
Ted Kremenek6217b802009-07-29 21:53:49 +00004139 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004140 // -- For a non-type template-parameter of type reference to
4141 // object, no conversions apply. The type referred to by the
4142 // reference may be more cv-qualified than the (otherwise
4143 // identical) type of the template-argument. The
4144 // template-parameter is bound directly to the
4145 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00004146 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004147 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004148
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004149 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004150 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
4151 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004152 true,
4153 FoundResult)) {
Daniel Dunbar96a00142012-03-09 18:35:03 +00004154 if (DiagnoseUseOfDecl(Fn, Arg->getLocStart()))
John Wiegley429bb272011-04-08 18:41:53 +00004155 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004156
4157 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4158 ArgType = Arg->getType();
4159 } else
John Wiegley429bb272011-04-08 18:41:53 +00004160 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004161 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004162
John Wiegley429bb272011-04-08 18:41:53 +00004163 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4164 ParamType,
4165 Arg, Converted))
4166 return ExprError();
4167 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004168 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004169
4170 // -- For a non-type template-parameter of type pointer to data
4171 // member, qualification conversions (4.4) are applied.
4172 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4173
John McCallf85e1932011-06-15 23:02:42 +00004174 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004175 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004176 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004177 } else if (IsQualificationConversion(ArgType, ParamType, false,
4178 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004179 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4180 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004181 } else {
4182 // We can't perform this conversion.
Daniel Dunbar96a00142012-03-09 18:35:03 +00004183 Diag(Arg->getLocStart(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004184 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004185 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004186 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004187 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004188 }
4189
John Wiegley429bb272011-04-08 18:41:53 +00004190 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4191 return ExprError();
4192 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004193}
4194
4195/// \brief Check a template argument against its corresponding
4196/// template template parameter.
4197///
4198/// This routine implements the semantics of C++ [temp.arg.template].
4199/// It returns true if an error occurred, and false otherwise.
4200bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004201 const TemplateArgumentLoc &Arg) {
4202 TemplateName Name = Arg.getArgument().getAsTemplate();
4203 TemplateDecl *Template = Name.getAsTemplateDecl();
4204 if (!Template) {
4205 // Any dependent template name is fine.
4206 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4207 return false;
4208 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004209
Richard Smith3e4c6c42011-05-05 21:57:07 +00004210 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004211 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004212 // the name of a class template or an alias template, expressed as an
4213 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004214 // primary class templates are considered when matching the
4215 // template template argument with the corresponding parameter;
4216 // partial specializations are not considered even if their
4217 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004218 //
4219 // Note that we also allow template template parameters here, which
4220 // will happen when we are dealing with, e.g., class template
4221 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004222 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004223 !isa<TemplateTemplateParmDecl>(Template) &&
4224 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004225 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004226 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004227 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004228 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004229 << Template;
4230 }
4231
4232 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4233 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004234 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004235 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004236 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004237}
4238
Douglas Gregor02024a92010-03-28 02:42:43 +00004239/// \brief Given a non-type template argument that refers to a
4240/// declaration and the type of its corresponding non-type template
4241/// parameter, produce an expression that properly refers to that
4242/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004243ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004244Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4245 QualType ParamType,
4246 SourceLocation Loc) {
4247 assert(Arg.getKind() == TemplateArgument::Declaration &&
4248 "Only declaration template arguments permitted here");
4249 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4250
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004251 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004252 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4253 // If the value is a class member, we might have a pointer-to-member.
4254 // Determine whether the non-type template template parameter is of
4255 // pointer-to-member type. If so, we need to build an appropriate
4256 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4257 // would refer to the member itself.
4258 if (ParamType->isMemberPointerType()) {
4259 QualType ClassType
4260 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4261 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004262 = NestedNameSpecifier::Create(Context, 0, false,
4263 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004264 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004265 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004266
4267 // The actual value-ness of this is unimportant, but for
4268 // internal consistency's sake, references to instance methods
4269 // are r-values.
4270 ExprValueKind VK = VK_LValue;
4271 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4272 VK = VK_RValue;
4273
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004274 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004275 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004276 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004277 Loc,
4278 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004279 if (RefExpr.isInvalid())
4280 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
John McCall2de56d12010-08-25 11:45:40 +00004282 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004283
Douglas Gregorc0c83002010-04-30 21:46:38 +00004284 // We might need to perform a trailing qualification conversion, since
4285 // the element type on the parameter could be more qualified than the
4286 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004287 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004288 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004289 ParamType.getUnqualifiedType(), false,
4290 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004291 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004292
Douglas Gregor02024a92010-03-28 02:42:43 +00004293 assert(!RefExpr.isInvalid() &&
4294 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004295 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004296 return move(RefExpr);
4297 }
4298 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004299
Douglas Gregor02024a92010-03-28 02:42:43 +00004300 QualType T = VD->getType().getNonReferenceType();
4301 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004302 // When the non-type template parameter is a pointer, take the
4303 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004304 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004305 if (RefExpr.isInvalid())
4306 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004307
4308 if (T->isFunctionType() || T->isArrayType()) {
4309 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004310 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4311 if (RefExpr.isInvalid())
4312 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004313
4314 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004315 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004316
Douglas Gregorb7a09262010-04-01 18:32:35 +00004317 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004318 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004319 }
4320
John McCallf89e55a2010-11-18 06:31:45 +00004321 ExprValueKind VK = VK_RValue;
4322
Douglas Gregor02024a92010-03-28 02:42:43 +00004323 // If the non-type template parameter has reference type, qualify the
4324 // resulting declaration reference with the extra qualifiers on the
4325 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004326 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4327 VK = VK_LValue;
4328 T = Context.getQualifiedType(T,
4329 TargetRef->getPointeeType().getQualifiers());
4330 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004331
John McCallf89e55a2010-11-18 06:31:45 +00004332 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004333}
4334
4335/// \brief Construct a new expression that refers to the given
4336/// integral template argument with the given source-location
4337/// information.
4338///
4339/// This routine takes care of the mapping from an integral template
4340/// argument (which may have any integral type) to the appropriate
4341/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004342ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004343Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4344 SourceLocation Loc) {
4345 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004346 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004347 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004348 if (T->isAnyCharacterType()) {
4349 CharacterLiteral::CharacterKind Kind;
4350 if (T->isWideCharType())
4351 Kind = CharacterLiteral::Wide;
4352 else if (T->isChar16Type())
4353 Kind = CharacterLiteral::UTF16;
4354 else if (T->isChar32Type())
4355 Kind = CharacterLiteral::UTF32;
4356 else
4357 Kind = CharacterLiteral::Ascii;
4358
Douglas Gregor02024a92010-03-28 02:42:43 +00004359 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004360 Arg.getAsIntegral()->getZExtValue(),
4361 Kind, T, Loc));
4362 }
4363
Douglas Gregor02024a92010-03-28 02:42:43 +00004364 if (T->isBooleanType())
4365 return Owned(new (Context) CXXBoolLiteralExpr(
4366 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004367 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004368
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004369 if (T->isNullPtrType())
4370 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4371
Chris Lattner223de242011-04-25 20:37:58 +00004372 // If this is an enum type that we're instantiating, we need to use an integer
4373 // type the same size as the enumerator. We don't want to build an
4374 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004375 QualType BT;
4376 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004377 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004378 else
4379 BT = T;
4380
John McCall4e9272d2011-07-15 07:47:58 +00004381 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4382 if (T->isEnumeralType()) {
4383 // FIXME: This is a hack. We need a better way to handle substituted
4384 // non-type template parameters.
4385 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4386 Context.getTrivialTypeSourceInfo(T, Loc),
4387 Loc, Loc);
4388 }
4389
4390 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004391}
4392
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004393/// \brief Match two template parameters within template parameter lists.
4394static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4395 bool Complain,
4396 Sema::TemplateParameterListEqualKind Kind,
4397 SourceLocation TemplateArgLoc) {
4398 // Check the actual kind (type, non-type, template).
4399 if (Old->getKind() != New->getKind()) {
4400 if (Complain) {
4401 unsigned NextDiag = diag::err_template_param_different_kind;
4402 if (TemplateArgLoc.isValid()) {
4403 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4404 NextDiag = diag::note_template_param_different_kind;
4405 }
4406 S.Diag(New->getLocation(), NextDiag)
4407 << (Kind != Sema::TPL_TemplateMatch);
4408 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4409 << (Kind != Sema::TPL_TemplateMatch);
4410 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004411
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004412 return false;
4413 }
4414
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004415 // Check that both are parameter packs are neither are parameter packs.
4416 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004417 // template template parameter, the template template parameter can have
4418 // a parameter pack where the template template argument does not.
4419 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4420 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4421 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004422 if (Complain) {
4423 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4424 if (TemplateArgLoc.isValid()) {
4425 S.Diag(TemplateArgLoc,
4426 diag::err_template_arg_template_params_mismatch);
4427 NextDiag = diag::note_template_parameter_pack_non_pack;
4428 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004429
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004430 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4431 : isa<NonTypeTemplateParmDecl>(New)? 1
4432 : 2;
4433 S.Diag(New->getLocation(), NextDiag)
4434 << ParamKind << New->isParameterPack();
4435 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4436 << ParamKind << Old->isParameterPack();
4437 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004438
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004439 return false;
4440 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004441
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004442 // For non-type template parameters, check the type of the parameter.
4443 if (NonTypeTemplateParmDecl *OldNTTP
4444 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4445 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004446
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004447 // If we are matching a template template argument to a template
4448 // template parameter and one of the non-type template parameter types
4449 // is dependent, then we must wait until template instantiation time
4450 // to actually compare the arguments.
4451 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4452 (OldNTTP->getType()->isDependentType() ||
4453 NewNTTP->getType()->isDependentType()))
4454 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004455
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004456 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4457 if (Complain) {
4458 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4459 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004460 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004461 diag::err_template_arg_template_params_mismatch);
4462 NextDiag = diag::note_template_nontype_parm_different_type;
4463 }
4464 S.Diag(NewNTTP->getLocation(), NextDiag)
4465 << NewNTTP->getType()
4466 << (Kind != Sema::TPL_TemplateMatch);
4467 S.Diag(OldNTTP->getLocation(),
4468 diag::note_template_nontype_parm_prev_declaration)
4469 << OldNTTP->getType();
4470 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004471
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004472 return false;
4473 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004474
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004475 return true;
4476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004477
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004478 // For template template parameters, check the template parameter types.
4479 // The template parameter lists of template template
4480 // parameters must agree.
4481 if (TemplateTemplateParmDecl *OldTTP
4482 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004483 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004484 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4485 OldTTP->getTemplateParameters(),
4486 Complain,
4487 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004488 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004489 : Kind),
4490 TemplateArgLoc);
4491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004492
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004493 return true;
4494}
Douglas Gregor02024a92010-03-28 02:42:43 +00004495
Douglas Gregora0347822011-01-13 00:08:50 +00004496/// \brief Diagnose a known arity mismatch when comparing template argument
4497/// lists.
4498static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004499void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004500 TemplateParameterList *New,
4501 TemplateParameterList *Old,
4502 Sema::TemplateParameterListEqualKind Kind,
4503 SourceLocation TemplateArgLoc) {
4504 unsigned NextDiag = diag::err_template_param_list_different_arity;
4505 if (TemplateArgLoc.isValid()) {
4506 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4507 NextDiag = diag::note_template_param_list_different_arity;
4508 }
4509 S.Diag(New->getTemplateLoc(), NextDiag)
4510 << (New->size() > Old->size())
4511 << (Kind != Sema::TPL_TemplateMatch)
4512 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4513 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4514 << (Kind != Sema::TPL_TemplateMatch)
4515 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4516}
4517
Douglas Gregorddc29e12009-02-06 22:42:48 +00004518/// \brief Determine whether the given template parameter lists are
4519/// equivalent.
4520///
Mike Stump1eb44332009-09-09 15:08:12 +00004521/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004522/// source code as part of a new template declaration.
4523///
4524/// \param Old The old template parameter list, typically found via
4525/// name lookup of the template declared with this template parameter
4526/// list.
4527///
4528/// \param Complain If true, this routine will produce a diagnostic if
4529/// the template parameter lists are not equivalent.
4530///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004531/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004532///
4533/// \param TemplateArgLoc If this source location is valid, then we
4534/// are actually checking the template parameter list of a template
4535/// argument (New) against the template parameter list of its
4536/// corresponding template template parameter (Old). We produce
4537/// slightly different diagnostics in this scenario.
4538///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004539/// \returns True if the template parameter lists are equal, false
4540/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004541bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004542Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4543 TemplateParameterList *Old,
4544 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004545 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004546 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004547 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4548 if (Complain)
4549 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4550 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004551
4552 return false;
4553 }
4554
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004555 // C++0x [temp.arg.template]p3:
4556 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004557 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004558 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004559 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004560 // template-parameter-list of P. [...]
4561 TemplateParameterList::iterator NewParm = New->begin();
4562 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004563 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004564 OldParmEnd = Old->end();
4565 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004566 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4567 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004568 if (NewParm == NewParmEnd) {
4569 if (Complain)
4570 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4571 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004572
Douglas Gregora0347822011-01-13 00:08:50 +00004573 return false;
4574 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004575
Douglas Gregora0347822011-01-13 00:08:50 +00004576 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4577 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004578 return false;
4579
Douglas Gregora0347822011-01-13 00:08:50 +00004580 ++NewParm;
4581 continue;
4582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004583
Douglas Gregora0347822011-01-13 00:08:50 +00004584 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004585 // [...] When P's template- parameter-list contains a template parameter
4586 // pack (14.5.3), the template parameter pack will match zero or more
4587 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004588 // template-parameter-list of A with the same type and form as the
4589 // template parameter pack in P (ignoring whether those template
4590 // parameters are template parameter packs).
4591 for (; NewParm != NewParmEnd; ++NewParm) {
4592 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4593 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004594 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004595 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004596 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004597
Douglas Gregora0347822011-01-13 00:08:50 +00004598 // Make sure we exhausted all of the arguments.
4599 if (NewParm != NewParmEnd) {
4600 if (Complain)
4601 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4602 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004603
Douglas Gregora0347822011-01-13 00:08:50 +00004604 return false;
4605 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004606
Douglas Gregorddc29e12009-02-06 22:42:48 +00004607 return true;
4608}
4609
4610/// \brief Check whether a template can be declared within this scope.
4611///
4612/// If the template declaration is valid in this scope, returns
4613/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004614bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004615Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004616 if (!S)
4617 return false;
4618
Douglas Gregorddc29e12009-02-06 22:42:48 +00004619 // Find the nearest enclosing declaration scope.
4620 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4621 (S->getFlags() & Scope::TemplateParamScope) != 0)
4622 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004623
Douglas Gregorddc29e12009-02-06 22:42:48 +00004624 // C++ [temp]p2:
4625 // A template-declaration can appear only as a namespace scope or
4626 // class scope declaration.
4627 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004628 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4629 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004630 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004631 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004632
Eli Friedman1503f772009-07-31 01:43:05 +00004633 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004634 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004635
4636 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4637 return false;
4638
Mike Stump1eb44332009-09-09 15:08:12 +00004639 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004640 diag::err_template_outside_namespace_or_class_scope)
4641 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004642}
Douglas Gregorcc636682009-02-17 23:15:12 +00004643
Douglas Gregord5cb8762009-10-07 00:13:32 +00004644/// \brief Determine what kind of template specialization the given declaration
4645/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004646static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004647 if (!D)
4648 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004649
Douglas Gregorf6b11852009-10-08 15:14:33 +00004650 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4651 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004652 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4653 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004654 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4655 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004656
Douglas Gregord5cb8762009-10-07 00:13:32 +00004657 return TSK_Undeclared;
4658}
4659
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004660/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004661/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004662///
Douglas Gregor9302da62009-10-14 23:50:59 +00004663/// This routine determines whether a template specialization can be declared
4664/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004665///
4666/// \param S the semantic analysis object for which this check is being
4667/// performed.
4668///
4669/// \param Specialized the entity being specialized or instantiated, which
4670/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004671/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004672/// member class).
4673///
4674/// \param PrevDecl the previous declaration of this entity, if any.
4675///
4676/// \param Loc the location of the explicit specialization or instantiation of
4677/// this entity.
4678///
4679/// \param IsPartialSpecialization whether this is a partial specialization of
4680/// a class template.
4681///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004682/// \returns true if there was an error that we cannot recover from, false
4683/// otherwise.
4684static bool CheckTemplateSpecializationScope(Sema &S,
4685 NamedDecl *Specialized,
4686 NamedDecl *PrevDecl,
4687 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004688 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004689 // Keep these "kind" numbers in sync with the %select statements in the
4690 // various diagnostics emitted by this routine.
4691 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004692 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004693 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004694 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004695 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004696 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004697 EntityKind = 3;
4698 else if (isa<VarDecl>(Specialized))
4699 EntityKind = 4;
4700 else if (isa<RecordDecl>(Specialized))
4701 EntityKind = 5;
4702 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004703 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4704 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004705 return true;
4706 }
4707
Douglas Gregor88b70942009-02-25 22:02:03 +00004708 // C++ [temp.expl.spec]p2:
4709 // An explicit specialization shall be declared in the namespace
4710 // of which the template is a member, or, for member templates, in
4711 // the namespace of which the enclosing class or enclosing class
4712 // template is a member. An explicit specialization of a member
4713 // function, member class or static data member of a class
4714 // template shall be declared in the namespace of which the class
4715 // template is a member. Such a declaration may also be a
4716 // definition. If the declaration is not a definition, the
4717 // specialization may be defined later in the name- space in which
4718 // the explicit specialization was declared, or in a namespace
4719 // that encloses the one in which the explicit specialization was
4720 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004721 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004722 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004723 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004724 return true;
4725 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004726
Douglas Gregor0a407472009-10-07 17:30:37 +00004727 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004728 if (S.getLangOpts().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004729 // Do not warn for class scope explicit specialization during
4730 // instantiation, warning was already emitted during pattern
4731 // semantic analysis.
4732 if (!S.ActiveTemplateInstantiations.size())
4733 S.Diag(Loc, diag::ext_function_specialization_in_class)
4734 << Specialized;
4735 } else {
4736 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4737 << Specialized;
4738 return true;
4739 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004740 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004741
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004742 if (S.CurContext->isRecord() &&
4743 !S.CurContext->Equals(Specialized->getDeclContext())) {
4744 // Make sure that we're specializing in the right record context.
4745 // Otherwise, things can go horribly wrong.
4746 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4747 << Specialized;
4748 return true;
4749 }
4750
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004751 // C++ [temp.class.spec]p6:
4752 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004753 // in any namespace scope in which its definition may be defined (14.5.1
4754 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004755 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004756 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004757 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004758 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004759 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004760 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4761 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004762 // C++ [temp.exp.spec]p2:
4763 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004764 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004765 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004766 // An explicit specialization of a member function, member class or
4767 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004768 // namespace of which the class template is a member.
4769 //
4770 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004771 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004772 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004773 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4774 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4775 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4776 assert(!IsCPlusPlus0xExtension &&
4777 "DC encloses TU but isn't in enclosing namespace set");
4778 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004779 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004780 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4781 int Diag;
4782 if (!IsCPlusPlus0xExtension)
4783 Diag = diag::err_template_spec_decl_out_of_scope;
David Blaikie4e4d0842012-03-11 07:00:24 +00004784 else if (!S.getLangOpts().CPlusPlus0x)
Richard Smithebaf0e62011-10-18 20:49:44 +00004785 Diag = diag::ext_template_spec_decl_out_of_scope;
4786 else
4787 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4788 S.Diag(Loc, Diag)
4789 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4790 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004791
Douglas Gregor9302da62009-10-14 23:50:59 +00004792 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004793 ComplainedAboutScope =
David Blaikie4e4d0842012-03-11 07:00:24 +00004794 !(IsCPlusPlus0xExtension && S.getLangOpts().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004795 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004796 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797
4798 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004799 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004800 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004801 // specializations of function templates, static data members, and member
4802 // functions, so we skip the check here for those kinds of entities.
4803 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004804 // Should we refactor that check, so that it occurs later?
4805 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004806 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4807 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004808 if (isa<TranslationUnitDecl>(SpecializedContext))
4809 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4810 << EntityKind << Specialized;
4811 else if (isa<NamespaceDecl>(SpecializedContext))
4812 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4813 << EntityKind << Specialized
4814 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004815
Douglas Gregor9302da62009-10-14 23:50:59 +00004816 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004817 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004818
Douglas Gregord5cb8762009-10-07 00:13:32 +00004819 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004820
Douglas Gregor88b70942009-02-25 22:02:03 +00004821 return false;
4822}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004823
Douglas Gregorbacb9492011-01-03 21:13:47 +00004824/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4825/// that checks non-type template partial specialization arguments.
4826static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4827 NonTypeTemplateParmDecl *Param,
4828 const TemplateArgument *Args,
4829 unsigned NumArgs) {
4830 for (unsigned I = 0; I != NumArgs; ++I) {
4831 if (Args[I].getKind() == TemplateArgument::Pack) {
4832 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004833 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004834 Args[I].pack_size()))
4835 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004836
Douglas Gregore94866f2009-06-12 21:21:02 +00004837 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004838 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004839
Douglas Gregorbacb9492011-01-03 21:13:47 +00004840 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004841 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004842 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004843 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004844
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004845 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004846 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4847 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004848
4849 // Strip off any implicit casts we added as part of type checking.
4850 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4851 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004852
Douglas Gregore94866f2009-06-12 21:21:02 +00004853 // C++ [temp.class.spec]p8:
4854 // A non-type argument is non-specialized if it is the name of a
4855 // non-type parameter. All other non-type arguments are
4856 // specialized.
4857 //
4858 // Below, we check the two conditions that only apply to
4859 // specialized non-type arguments, so skip any non-specialized
4860 // arguments.
4861 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004862 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004863 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004864
Douglas Gregore94866f2009-06-12 21:21:02 +00004865 // C++ [temp.class.spec]p9:
4866 // Within the argument list of a class template partial
4867 // specialization, the following restrictions apply:
4868 // -- A partially specialized non-type argument expression
4869 // shall not involve a template parameter of the partial
4870 // specialization except when the argument expression is a
4871 // simple identifier.
4872 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004873 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004874 diag::err_dependent_non_type_arg_in_partial_spec)
4875 << ArgExpr->getSourceRange();
4876 return true;
4877 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004878
Douglas Gregore94866f2009-06-12 21:21:02 +00004879 // -- The type of a template parameter corresponding to a
4880 // specialized non-type argument shall not be dependent on a
4881 // parameter of the specialization.
4882 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004883 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004884 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4885 << Param->getType()
4886 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004887 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004888 return true;
4889 }
4890 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004891
Douglas Gregorbacb9492011-01-03 21:13:47 +00004892 return false;
4893}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004894
Douglas Gregorbacb9492011-01-03 21:13:47 +00004895/// \brief Check the non-type template arguments of a class template
4896/// partial specialization according to C++ [temp.class.spec]p9.
4897///
4898/// \param TemplateParams the template parameters of the primary class
4899/// template.
4900///
4901/// \param TemplateArg the template arguments of the class template
4902/// partial specialization.
4903///
4904/// \returns true if there was an error, false otherwise.
4905static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4906 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004907 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004908 const TemplateArgument *ArgList = TemplateArgs.data();
4909
4910 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4911 NonTypeTemplateParmDecl *Param
4912 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4913 if (!Param)
4914 continue;
4915
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004916 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004917 &ArgList[I], 1))
4918 return true;
4919 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004920
4921 return false;
4922}
4923
John McCalld226f652010-08-21 09:40:31 +00004924DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004925Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4926 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004927 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004928 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004929 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004930 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004931 SourceLocation TemplateNameLoc,
4932 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004933 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004934 SourceLocation RAngleLoc,
4935 AttributeList *Attr,
4936 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004937 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004938
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004939 // NOTE: KWLoc is the location of the tag keyword. This will instead
4940 // store the location of the outermost template keyword in the declaration.
4941 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4942 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4943
Douglas Gregorcc636682009-02-17 23:15:12 +00004944 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004945 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004946 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004947 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4948
4949 if (!ClassTemplate) {
4950 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004951 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004952 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4953 return true;
4954 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004955
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004956 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004957 bool isPartialSpecialization = false;
4958
Douglas Gregor88b70942009-02-25 22:02:03 +00004959 // Check the validity of the template headers that introduce this
4960 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004961 // FIXME: We probably shouldn't complain about these headers for
4962 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004963 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004964 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004965 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4966 TemplateNameLoc,
4967 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004968 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004969 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004970 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004971 isExplicitSpecialization,
4972 Invalid);
4973 if (Invalid)
4974 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004975
Douglas Gregor05396e22009-08-25 17:23:04 +00004976 if (TemplateParams && TemplateParams->size() > 0) {
4977 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004978
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004979 if (TUK == TUK_Friend) {
4980 Diag(KWLoc, diag::err_partial_specialization_friend)
4981 << SourceRange(LAngleLoc, RAngleLoc);
4982 return true;
4983 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004984
Douglas Gregor05396e22009-08-25 17:23:04 +00004985 // C++ [temp.class.spec]p10:
4986 // The template parameter list of a specialization shall not
4987 // contain default template argument values.
4988 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4989 Decl *Param = TemplateParams->getParam(I);
4990 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4991 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004992 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004993 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004994 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004995 }
4996 } else if (NonTypeTemplateParmDecl *NTTP
4997 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4998 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004999 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00005000 diag::err_default_arg_in_partial_spec)
5001 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00005002 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00005003 }
5004 } else {
5005 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00005006 if (TTP->hasDefaultArgument()) {
5007 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00005008 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00005009 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00005010 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005011 }
5012 }
5013 }
Douglas Gregora735b202009-10-13 14:39:41 +00005014 } else if (TemplateParams) {
5015 if (TUK == TUK_Friend)
5016 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00005017 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00005018 SourceRange(TemplateParams->getTemplateLoc(),
5019 TemplateParams->getRAngleLoc()))
5020 << SourceRange(LAngleLoc, RAngleLoc);
5021 else
5022 isExplicitSpecialization = true;
5023 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00005024 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00005025 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005026 isExplicitSpecialization = true;
5027 }
Douglas Gregor88b70942009-02-25 22:02:03 +00005028
Douglas Gregorcc636682009-02-17 23:15:12 +00005029 // Check that the specialization uses the same tag kind as the
5030 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005031 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5032 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005033 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005034 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005035 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005036 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00005037 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005038 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00005039 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005040 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005041 diag::note_previous_use);
5042 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5043 }
5044
Douglas Gregor40808ce2009-03-09 23:48:35 +00005045 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005046 TemplateArgumentListInfo TemplateArgs;
5047 TemplateArgs.setLAngleLoc(LAngleLoc);
5048 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005049 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00005050
Douglas Gregor925910d2011-01-03 20:35:03 +00005051 // Check for unexpanded parameter packs in any of the template arguments.
5052 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005053 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00005054 UPPC_PartialSpecialization))
5055 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005056
Douglas Gregorcc636682009-02-17 23:15:12 +00005057 // Check that the template argument list is well-formed for this
5058 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005059 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005060 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5061 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005062 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005063
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005064 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00005065 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005066 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00005067 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00005068 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00005069 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00005070 return true;
5071
Douglas Gregor561f8122011-07-01 01:22:09 +00005072 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005073 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00005074 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005075 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00005076 TemplateArgs.size(),
5077 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00005078 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
5079 << ClassTemplate->getDeclName();
5080 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00005081 }
5082 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005083
Douglas Gregorcc636682009-02-17 23:15:12 +00005084 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005085 ClassTemplateSpecializationDecl *PrevDecl = 0;
5086
5087 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005088 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005089 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005090 = ClassTemplate->findPartialSpecialization(Converted.data(),
5091 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005092 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005093 else
5094 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005095 = ClassTemplate->findSpecialization(Converted.data(),
5096 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00005097
5098 ClassTemplateSpecializationDecl *Specialization = 0;
5099
Douglas Gregor88b70942009-02-25 22:02:03 +00005100 // Check whether we can declare a class template specialization in
5101 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005102 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005103 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
5104 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00005105 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005106 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005107
Douglas Gregorb88e8882009-07-30 17:40:51 +00005108 // The canonical type
5109 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005110 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005111 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00005112 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005113 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005114 // arguments was referenced but not declared, or we're only
5115 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005116 // declaration node as our own, updating its source location and
5117 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005118 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005119 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005120 if (TemplateParameterLists.size() > 0) {
5121 Specialization->setTemplateParameterListsInfo(Context,
5122 TemplateParameterLists.size(),
5123 (TemplateParameterList**) TemplateParameterLists.release());
5124 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005125 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00005126 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005127 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00005128 // Build the canonical type that describes the converted template
5129 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00005130 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
5131 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005132 Converted.data(),
5133 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005134
5135 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005136 ClassTemplate->getInjectedClassNameSpecialization())) {
5137 // C++ [temp.class.spec]p9b3:
5138 //
5139 // -- The argument list of the specialization shall not be identical
5140 // to the implicit argument list of the primary template.
5141 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005142 << (TUK == TUK_Definition)
5143 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005144 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5145 ClassTemplate->getIdentifier(),
5146 TemplateNameLoc,
5147 Attr,
5148 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005149 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005150 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005151 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005152 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005153
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005154 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005155 ClassTemplatePartialSpecializationDecl *PrevPartial
5156 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005157 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005158 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005159 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005160 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005161 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005162 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005163 TemplateParams,
5164 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005165 Converted.data(),
5166 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005167 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005168 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005169 PrevPartial,
5170 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005171 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005172 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005173 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005174 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005175 (TemplateParameterList**) TemplateParameterLists.release());
5176 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005177
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005178 if (!PrevPartial)
5179 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005180 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005181
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005182 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005183 // template specialization, make a note of that.
5184 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5185 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005186
Douglas Gregor031a5882009-06-13 00:26:55 +00005187 // Check that all of the template parameters of the class template
5188 // partial specialization are deducible from the template
5189 // arguments. If not, this class template partial specialization
5190 // will never be used.
Benjamin Kramer013b3662012-01-30 16:17:39 +00005191 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005192 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005193 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005194 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005195
Benjamin Kramer013b3662012-01-30 16:17:39 +00005196 if (!DeducibleParams.all()) {
5197 unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
Douglas Gregor031a5882009-06-13 00:26:55 +00005198 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5199 << (NumNonDeducible > 1)
5200 << SourceRange(TemplateNameLoc, RAngleLoc);
5201 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5202 if (!DeducibleParams[I]) {
5203 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5204 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005205 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005206 diag::note_partial_spec_unused_parameter)
5207 << Param->getDeclName();
5208 else
Mike Stump1eb44332009-09-09 15:08:12 +00005209 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005210 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005211 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005212 }
5213 }
5214 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005215 } else {
5216 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005217 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005218 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005219 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005220 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005221 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005222 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005223 Converted.data(),
5224 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005225 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005226 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005227 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005228 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005229 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005230 (TemplateParameterList**) TemplateParameterLists.release());
5231 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005232
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005233 if (!PrevDecl)
5234 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005235
5236 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005237 }
5238
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005239 // C++ [temp.expl.spec]p6:
5240 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005241 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005242 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005243 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005244 // use occurs; no diagnostic is required.
5245 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005246 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005247 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005248 // Is there any previous explicit specialization declaration?
5249 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5250 Okay = true;
5251 break;
5252 }
5253 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005254
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005255 if (!Okay) {
5256 SourceRange Range(TemplateNameLoc, RAngleLoc);
5257 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5258 << Context.getTypeDeclType(Specialization) << Range;
5259
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005260 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005261 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005262 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005263 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005264 return true;
5265 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005266 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005267
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005268 // If this is not a friend, note that this is an explicit specialization.
5269 if (TUK != TUK_Friend)
5270 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005271
5272 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005273 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005274 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005275 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005276 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005277 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005278 Diag(Def->getLocation(), diag::note_previous_definition);
5279 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005280 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005281 }
5282 }
5283
John McCall7f1b9872010-12-18 03:30:47 +00005284 if (Attr)
5285 ProcessDeclAttributeList(S, Specialization, Attr);
5286
Douglas Gregord023aec2011-09-09 20:53:38 +00005287 if (ModulePrivateLoc.isValid())
5288 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5289 << (isPartialSpecialization? 1 : 0)
5290 << FixItHint::CreateRemoval(ModulePrivateLoc);
5291
Douglas Gregorfc705b82009-02-26 22:19:44 +00005292 // Build the fully-sugared type for this class template
5293 // specialization as the user wrote in the specialization
5294 // itself. This means that we'll pretty-print the type retrieved
5295 // from the specialization's declaration the way that the user
5296 // actually wrote the specialization, rather than formatting the
5297 // name based on the "canonical" representation used to store the
5298 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005299 TypeSourceInfo *WrittenTy
5300 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5301 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005302 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005303 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005304 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005305 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005306 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005307
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005308 // C++ [temp.expl.spec]p9:
5309 // A template explicit specialization is in the scope of the
5310 // namespace in which the template was defined.
5311 //
5312 // We actually implement this paragraph where we set the semantic
5313 // context (in the creation of the ClassTemplateSpecializationDecl),
5314 // but we also maintain the lexical context where the actual
5315 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005316 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005317
Douglas Gregorcc636682009-02-17 23:15:12 +00005318 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005319 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005320 Specialization->startDefinition();
5321
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005322 if (TUK == TUK_Friend) {
5323 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5324 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005325 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005326 /*FIXME:*/KWLoc);
5327 Friend->setAccess(AS_public);
5328 CurContext->addDecl(Friend);
5329 } else {
5330 // Add the specialization into its lexical context, so that it can
5331 // be seen when iterating through the list of declarations in that
5332 // context. However, specializations are not found by name lookup.
5333 CurContext->addDecl(Specialization);
5334 }
John McCalld226f652010-08-21 09:40:31 +00005335 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005336}
Douglas Gregord57959a2009-03-27 23:10:48 +00005337
John McCalld226f652010-08-21 09:40:31 +00005338Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005339 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005340 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005341 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005342}
5343
John McCalld226f652010-08-21 09:40:31 +00005344Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005345 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005346 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005347 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005348 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005349
Douglas Gregor52591bf2009-06-24 00:54:41 +00005350 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005351 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005352 }
Mike Stump1eb44332009-09-09 15:08:12 +00005353
Douglas Gregor52591bf2009-06-24 00:54:41 +00005354 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005355
Douglas Gregor45fa5602011-11-07 20:56:01 +00005356 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005357 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005358 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005359 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005360 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005361 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005362 FunctionTemplate->getTemplatedDecl());
5363 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5364 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5365 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005366}
5367
John McCall75042392010-02-11 01:33:53 +00005368/// \brief Strips various properties off an implicit instantiation
5369/// that has just been explicitly specialized.
5370static void StripImplicitInstantiation(NamedDecl *D) {
Rafael Espindola860097c2012-02-23 04:17:32 +00005371 // FIXME: "make check" is clean if the call to dropAttrs() is commented out.
Sean Huntcf807c42010-08-18 23:23:40 +00005372 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005373
5374 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5375 FD->setInlineSpecified(false);
5376 }
5377}
5378
Nico Weberd1d512a2012-01-09 19:52:25 +00005379/// \brief Compute the diagnostic location for an explicit instantiation
5380// declaration or definition.
5381static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005382 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005383 // Explicit instantiations following a specialization have no effect and
5384 // hence no PointOfInstantiation. In that case, walk decl backwards
5385 // until a valid name loc is found.
5386 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005387 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5388 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005389 PrevDiagLoc = Prev->getLocation();
5390 }
5391 assert(PrevDiagLoc.isValid() &&
5392 "Explicit instantiation without point of instantiation?");
5393 return PrevDiagLoc;
5394}
5395
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005396/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005397/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005398/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005399/// new specialization/instantiation will have any effect.
5400///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005401/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005402/// instantiation.
5403///
5404/// \param NewTSK the kind of the new explicit specialization or instantiation.
5405///
5406/// \param PrevDecl the previous declaration of the entity.
5407///
5408/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5409///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005410/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005411/// declaration was instantiated (either implicitly or explicitly).
5412///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005413/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005414/// specialization or instantiation has no effect and should be ignored.
5415///
5416/// \returns true if there was an error that should prevent the introduction of
5417/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005418bool
5419Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5420 TemplateSpecializationKind NewTSK,
5421 NamedDecl *PrevDecl,
5422 TemplateSpecializationKind PrevTSK,
5423 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005424 bool &HasNoEffect) {
5425 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005426
Douglas Gregor454885e2009-10-15 15:54:05 +00005427 switch (NewTSK) {
5428 case TSK_Undeclared:
5429 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005430 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005431
Douglas Gregor454885e2009-10-15 15:54:05 +00005432 case TSK_ExplicitSpecialization:
5433 switch (PrevTSK) {
5434 case TSK_Undeclared:
5435 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005436 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005437 // explicitly specialized or has merely been mentioned without any
5438 // instantiation.
5439 return false;
5440
5441 case TSK_ImplicitInstantiation:
5442 if (PrevPointOfInstantiation.isInvalid()) {
5443 // The declaration itself has not actually been instantiated, so it is
5444 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005445 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005446 return false;
5447 }
5448 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005449
Douglas Gregor454885e2009-10-15 15:54:05 +00005450 case TSK_ExplicitInstantiationDeclaration:
5451 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005452 assert((PrevTSK == TSK_ImplicitInstantiation ||
5453 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005454 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005455
Douglas Gregor454885e2009-10-15 15:54:05 +00005456 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005457 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005458 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005459 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005460 // implicit instantiation to take place, in every translation unit in
5461 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005462 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005463 // Is there any previous explicit specialization declaration?
5464 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5465 return false;
5466 }
5467
Douglas Gregor0d035142009-10-27 18:42:08 +00005468 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005469 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005470 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005471 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005472
Douglas Gregor454885e2009-10-15 15:54:05 +00005473 return true;
5474 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005475
Douglas Gregor454885e2009-10-15 15:54:05 +00005476 case TSK_ExplicitInstantiationDeclaration:
5477 switch (PrevTSK) {
5478 case TSK_ExplicitInstantiationDeclaration:
5479 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005480 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005481 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005482
Douglas Gregor454885e2009-10-15 15:54:05 +00005483 case TSK_Undeclared:
5484 case TSK_ImplicitInstantiation:
5485 // We're explicitly instantiating something that may have already been
5486 // implicitly instantiated; that's fine.
5487 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005488
Douglas Gregor454885e2009-10-15 15:54:05 +00005489 case TSK_ExplicitSpecialization:
5490 // C++0x [temp.explicit]p4:
5491 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005493 // specialization for that template, the explicit instantiation has no
5494 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005495 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005496 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005497
Douglas Gregor454885e2009-10-15 15:54:05 +00005498 case TSK_ExplicitInstantiationDefinition:
5499 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005500 // If an entity is the subject of both an explicit instantiation
5501 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005502 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005503 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005504 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005505
5506 // Explicit instantiations following a specialization have no effect and
5507 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5508 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005509 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5510 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005511 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005512 return false;
5513 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005514
Douglas Gregor454885e2009-10-15 15:54:05 +00005515 case TSK_ExplicitInstantiationDefinition:
5516 switch (PrevTSK) {
5517 case TSK_Undeclared:
5518 case TSK_ImplicitInstantiation:
5519 // We're explicitly instantiating something that may have already been
5520 // implicitly instantiated; that's fine.
5521 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005522
Douglas Gregor454885e2009-10-15 15:54:05 +00005523 case TSK_ExplicitSpecialization:
5524 // C++ DR 259, C++0x [temp.explicit]p4:
5525 // For a given set of template parameters, if an explicit
5526 // instantiation of a template appears after a declaration of
5527 // an explicit specialization for that template, the explicit
5528 // instantiation has no effect.
5529 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005530 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005531 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005532 // has been explicitly specialized.
David Blaikie4e4d0842012-03-11 07:00:24 +00005533 Diag(NewLoc, getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00005534 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5535 diag::ext_explicit_instantiation_after_specialization)
5536 << PrevDecl;
5537 Diag(PrevDecl->getLocation(),
5538 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005539 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005540 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005541
Douglas Gregor454885e2009-10-15 15:54:05 +00005542 case TSK_ExplicitInstantiationDeclaration:
5543 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005544 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005545
5546 // C++0x [temp.explicit]p4:
5547 // For a given set of template parameters, if an explicit instantiation
5548 // of a template appears after a declaration of an explicit
5549 // specialization for that template, the explicit instantiation has no
5550 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005551 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005552 // Is there any previous explicit specialization declaration?
5553 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5554 HasNoEffect = true;
5555 break;
5556 }
5557 }
5558
Douglas Gregor454885e2009-10-15 15:54:05 +00005559 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005560
Douglas Gregor454885e2009-10-15 15:54:05 +00005561 case TSK_ExplicitInstantiationDefinition:
5562 // C++0x [temp.spec]p5:
5563 // For a given template and a given set of template-arguments,
5564 // - an explicit instantiation definition shall appear at most once
5565 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005566 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005567 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005568 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005569 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005570 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005571 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005572 }
Douglas Gregor454885e2009-10-15 15:54:05 +00005573 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005574
David Blaikieb219cfc2011-09-23 05:06:16 +00005575 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005576}
5577
John McCallaf2094e2010-04-08 09:05:18 +00005578/// \brief Perform semantic analysis for the given dependent function
5579/// template specialization. The only possible way to get a dependent
5580/// function template specialization is with a friend declaration,
5581/// like so:
5582///
5583/// template <class T> void foo(T);
5584/// template <class T> class A {
5585/// friend void foo<>(T);
5586/// };
5587///
5588/// There really isn't any useful analysis we can do here, so we
5589/// just store the information.
5590bool
5591Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5592 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5593 LookupResult &Previous) {
5594 // Remove anything from Previous that isn't a function template in
5595 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005596 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005597 LookupResult::Filter F = Previous.makeFilter();
5598 while (F.hasNext()) {
5599 NamedDecl *D = F.next()->getUnderlyingDecl();
5600 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005601 !FDLookupContext->InEnclosingNamespaceSetOf(
5602 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005603 F.erase();
5604 }
5605 F.done();
5606
5607 // Should this be diagnosed here?
5608 if (Previous.empty()) return true;
5609
5610 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5611 ExplicitTemplateArgs);
5612 return false;
5613}
5614
Abramo Bagnarae03db982010-05-20 15:32:11 +00005615/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005616/// specialization.
5617///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005618/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005619/// explicit function template specialization. On successful completion,
5620/// the function declaration \p FD will become a function template
5621/// specialization.
5622///
5623/// \param FD the function declaration, which will be updated to become a
5624/// function template specialization.
5625///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005626/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5627/// if any. Note that this may be valid info even when 0 arguments are
5628/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5629/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005630///
Francois Pichet59e7c562011-07-08 06:21:47 +00005631/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005632/// this function specialization.
5633bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005634Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005635 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005636 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005637 // The set of function template specializations that could match this
5638 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005639 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005640
Sebastian Redl7a126a42010-08-31 00:36:30 +00005641 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005642 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5643 I != E; ++I) {
5644 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5645 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005646 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005647 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005648 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5649 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005650 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005651
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005652 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005653 // A trailing template-argument can be left unspecified in the
5654 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005655 // provided it can be deduced from the function argument type.
5656 // Perform template argument deduction to determine whether we may be
5657 // specializing this template.
5658 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005659 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005660 FunctionDecl *Specialization = 0;
5661 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005662 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005663 FD->getType(),
5664 Specialization,
5665 Info)) {
5666 // FIXME: Template argument deduction failed; record why it failed, so
5667 // that we can provide nifty diagnostics.
5668 (void)TDK;
5669 continue;
5670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005671
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005672 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005673 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005674 }
5675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005676
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005677 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005678 UnresolvedSetIterator Result
5679 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005680 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005681 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005682 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005683 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005684 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005685 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005686 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005687 return true;
John McCallc373d482010-01-27 01:50:18 +00005688
5689 // Ignore access information; it doesn't figure into redeclaration checking.
5690 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005691
5692 FunctionTemplateSpecializationInfo *SpecInfo
5693 = Specialization->getTemplateSpecializationInfo();
5694 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005695
5696 // Note: do not overwrite location info if previous template
5697 // specialization kind was explicit.
5698 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
Richard Smithff234882012-02-20 23:28:05 +00005699 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) {
Francois Pichet59e7c562011-07-08 06:21:47 +00005700 Specialization->setLocation(FD->getLocation());
Richard Smithff234882012-02-20 23:28:05 +00005701 // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr
5702 // function can differ from the template declaration with respect to
5703 // the constexpr specifier.
5704 Specialization->setConstexpr(FD->isConstexpr());
5705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005706
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005707 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005708 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005709
5710 // If this is a friend declaration, then we're not really declaring
5711 // an explicit specialization.
5712 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005713
Douglas Gregord5cb8762009-10-07 00:13:32 +00005714 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005715 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005716 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005717 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005718 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005719 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005720 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005721
5722 // C++ [temp.expl.spec]p6:
5723 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005724 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005725 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005726 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005727 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005728 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005729 if (!isFriend &&
5730 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005731 TSK_ExplicitSpecialization,
5732 Specialization,
5733 SpecInfo->getTemplateSpecializationKind(),
5734 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005735 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005736 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005737
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005738 // Mark the prior declaration as an explicit specialization, so that later
5739 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005740 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005741 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005742 MarkUnusedFileScopedDecl(Specialization);
5743 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005744
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005745 // Turn the given function declaration into a function template
5746 // specialization, with the template arguments from the previous
5747 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005748 // Take copies of (semantic and syntactic) template argument lists.
5749 const TemplateArgumentList* TemplArgs = new (Context)
5750 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005751 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005752 TemplArgs, /*InsertPos=*/0,
5753 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005754 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005755 FD->setStorageClass(Specialization->getStorageClass());
5756
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005757 // The "previous declaration" for this function template specialization is
5758 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005759 Previous.clear();
5760 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005761 return false;
5762}
5763
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005764/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005765/// specialization.
5766///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005767/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005768/// explicit member function specialization. On successful completion,
5769/// the function declaration \p FD will become a member function
5770/// specialization.
5771///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005772/// \param Member the member declaration, which will be updated to become a
5773/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005774///
John McCall68263142009-11-18 22:49:29 +00005775/// \param Previous the set of declarations, one of which may be specialized
5776/// by this function specialization; the set will be modified to contain the
5777/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005778bool
John McCall68263142009-11-18 22:49:29 +00005779Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005780 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005781
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005782 // Try to find the member we are instantiating.
5783 NamedDecl *Instantiation = 0;
5784 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005785 MemberSpecializationInfo *MSInfo = 0;
5786
John McCall68263142009-11-18 22:49:29 +00005787 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005788 // Nowhere to look anyway.
5789 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005790 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5791 I != E; ++I) {
5792 NamedDecl *D = (*I)->getUnderlyingDecl();
5793 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005794 if (Context.hasSameType(Function->getType(), Method->getType())) {
5795 Instantiation = Method;
5796 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005797 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005798 break;
5799 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005800 }
5801 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005802 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005803 VarDecl *PrevVar;
5804 if (Previous.isSingleResult() &&
5805 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005806 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005807 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005808 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005809 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005810 }
5811 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005812 CXXRecordDecl *PrevRecord;
5813 if (Previous.isSingleResult() &&
5814 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5815 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005816 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005817 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005818 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005819 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005820
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005821 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005822 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005823 // specializations are always out-of-line, the caller will complain about
5824 // this mismatch later.
5825 return false;
5826 }
John McCall77e8b112010-04-13 20:37:33 +00005827
5828 // If this is a friend, just bail out here before we start turning
5829 // things into explicit specializations.
5830 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5831 // Preserve instantiation information.
5832 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5833 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5834 cast<CXXMethodDecl>(InstantiatedFrom),
5835 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5836 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5837 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5838 cast<CXXRecordDecl>(InstantiatedFrom),
5839 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5840 }
5841
5842 Previous.clear();
5843 Previous.addDecl(Instantiation);
5844 return false;
5845 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005846
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005847 // Make sure that this is a specialization of a member.
5848 if (!InstantiatedFrom) {
5849 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5850 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005851 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5852 return true;
5853 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005854
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005855 // C++ [temp.expl.spec]p6:
5856 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005857 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005858 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005859 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005860 // use occurs; no diagnostic is required.
5861 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005862
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005863 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005864 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5865 TSK_ExplicitSpecialization,
5866 Instantiation,
5867 MSInfo->getTemplateSpecializationKind(),
5868 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005869 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005870 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005871
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005872 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005873 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005874 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005875 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005876 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005877 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005878
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005879 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005880 // the original declaration to note that it is an explicit specialization
5881 // (if it was previously an implicit instantiation). This latter step
5882 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005883 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005884 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5885 if (InstantiationFunction->getTemplateSpecializationKind() ==
5886 TSK_ImplicitInstantiation) {
5887 InstantiationFunction->setTemplateSpecializationKind(
5888 TSK_ExplicitSpecialization);
5889 InstantiationFunction->setLocation(Member->getLocation());
5890 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005891
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005892 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5893 cast<CXXMethodDecl>(InstantiatedFrom),
5894 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005895 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005896 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005897 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5898 if (InstantiationVar->getTemplateSpecializationKind() ==
5899 TSK_ImplicitInstantiation) {
5900 InstantiationVar->setTemplateSpecializationKind(
5901 TSK_ExplicitSpecialization);
5902 InstantiationVar->setLocation(Member->getLocation());
5903 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005904
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005905 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5906 cast<VarDecl>(InstantiatedFrom),
5907 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005908 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005909 } else {
5910 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005911 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5912 if (InstantiationClass->getTemplateSpecializationKind() ==
5913 TSK_ImplicitInstantiation) {
5914 InstantiationClass->setTemplateSpecializationKind(
5915 TSK_ExplicitSpecialization);
5916 InstantiationClass->setLocation(Member->getLocation());
5917 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005918
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005919 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005920 cast<CXXRecordDecl>(InstantiatedFrom),
5921 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005922 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005923
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005924 // Save the caller the trouble of having to figure out which declaration
5925 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005926 Previous.clear();
5927 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005928 return false;
5929}
5930
Douglas Gregor558c0322009-10-14 23:41:34 +00005931/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005932///
5933/// \returns true if a serious error occurs, false otherwise.
5934static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005935 SourceLocation InstLoc,
5936 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005937 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5938 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005939
Douglas Gregor669eed82010-07-13 00:10:04 +00005940 if (CurContext->isRecord()) {
5941 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5942 << D;
5943 return true;
5944 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005945
Richard Smith3e2e91e2011-10-18 02:28:33 +00005946 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005947 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005948 // template. If the name declared in the explicit instantiation is an
5949 // unqualified name, the explicit instantiation shall appear in the
5950 // namespace where its template is declared or, if that namespace is inline
5951 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005952 //
5953 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005954 if (WasQualifiedName) {
5955 if (CurContext->Encloses(OrigContext))
5956 return false;
5957 } else {
5958 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5959 return false;
5960 }
5961
5962 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5963 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005964 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005965 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005966 diag::err_explicit_instantiation_out_of_scope :
5967 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005968 << D << NS;
5969 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005970 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005971 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005972 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5973 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5974 << D << NS;
5975 } else
5976 S.Diag(InstLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00005977 S.getLangOpts().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005978 diag::err_explicit_instantiation_must_be_global :
5979 diag::warn_explicit_instantiation_must_be_global_0x)
5980 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005981 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005982 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005983}
5984
5985/// \brief Determine whether the given scope specifier has a template-id in it.
5986static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5987 if (!SS.isSet())
5988 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005989
Richard Smith3e2e91e2011-10-18 02:28:33 +00005990 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005991 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005992 // or a static data member of a class template specialization, the name of
5993 // the class template specialization in the qualified-id for the member
5994 // name shall be a simple-template-id.
5995 //
5996 // C++98 has the same restriction, just worded differently.
5997 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5998 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005999 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00006000 if (isa<TemplateSpecializationType>(T))
6001 return true;
6002
6003 return false;
6004}
6005
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006006// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00006007DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006008Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006009 SourceLocation ExternLoc,
6010 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006011 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006012 SourceLocation KWLoc,
6013 const CXXScopeSpec &SS,
6014 TemplateTy TemplateD,
6015 SourceLocation TemplateNameLoc,
6016 SourceLocation LAngleLoc,
6017 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006018 SourceLocation RAngleLoc,
6019 AttributeList *Attr) {
6020 // Find the class template we're specializing
6021 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00006022 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006023 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
6024
6025 // Check that the specialization uses the same tag kind as the
6026 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006027 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
6028 assert(Kind != TTK_Enum &&
6029 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006030 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00006031 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006032 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00006033 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006034 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00006035 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006036 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00006037 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006038 diag::note_previous_use);
6039 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
6040 }
6041
Douglas Gregor558c0322009-10-14 23:41:34 +00006042 // C++0x [temp.explicit]p2:
6043 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006044 // definition and an explicit instantiation declaration. An explicit
6045 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00006046 TemplateSpecializationKind TSK
6047 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6048 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006049
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006050 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00006051 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00006052 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006053
6054 // Check that the template argument list is well-formed for this
6055 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00006056 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00006057 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
6058 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006059 return true;
6060
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006061 // Find the class template specialization declaration that
6062 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006063 void *InsertPos = 0;
6064 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00006065 = ClassTemplate->findSpecialization(Converted.data(),
6066 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006067
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006068 TemplateSpecializationKind PrevDecl_TSK
6069 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
6070
Douglas Gregord5cb8762009-10-07 00:13:32 +00006071 // C++0x [temp.explicit]p2:
6072 // [...] An explicit instantiation shall appear in an enclosing
6073 // namespace of its template. [...]
6074 //
6075 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00006076 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
6077 SS.isSet()))
6078 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006079
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006080 ClassTemplateSpecializationDecl *Specialization = 0;
6081
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006082 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006083 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00006084 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006085 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006086 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006087 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00006088 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006089
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006090 // Even though HasNoEffect == true means that this explicit instantiation
6091 // has no effect on semantics, we go on to put its syntax in the AST.
6092
6093 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
6094 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00006095 // Since the only prior class template specialization with these
6096 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006097 // declaration node as our own, updating the source location
6098 // for the template name to reflect our new declaration.
6099 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00006100 Specialization = PrevDecl;
6101 Specialization->setLocation(TemplateNameLoc);
6102 PrevDecl = 0;
6103 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006104 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006105
Douglas Gregor52604ab2009-09-11 21:19:12 +00006106 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006107 // Create a new class template specialization declaration node for
6108 // this explicit specialization.
6109 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00006110 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006111 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00006112 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006113 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00006114 Converted.data(),
6115 Converted.size(),
6116 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00006117 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006118
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006119 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006120 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006121 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006122 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006123 }
6124
6125 // Build the fully-sugared type for this explicit instantiation as
6126 // the user wrote in the explicit instantiation itself. This means
6127 // that we'll pretty-print the type retrieved from the
6128 // specialization's declaration the way that the user actually wrote
6129 // the explicit instantiation, rather than formatting the name based
6130 // on the "canonical" representation used to store the template
6131 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00006132 TypeSourceInfo *WrittenTy
6133 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6134 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006135 Context.getTypeDeclType(Specialization));
6136 Specialization->setTypeAsWritten(WrittenTy);
6137 TemplateArgsIn.release();
6138
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006139 // Set source locations for keywords.
6140 Specialization->setExternLoc(ExternLoc);
6141 Specialization->setTemplateKeywordLoc(TemplateLoc);
6142
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006143 if (Attr)
6144 ProcessDeclAttributeList(S, Specialization, Attr);
6145
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006146 // Add the explicit instantiation into its lexical context. However,
6147 // since explicit instantiations are never found by name lookup, we
6148 // just put it into the declaration context directly.
6149 Specialization->setLexicalDeclContext(CurContext);
6150 CurContext->addDecl(Specialization);
6151
6152 // Syntax is now OK, so return if it has no other effect on semantics.
6153 if (HasNoEffect) {
6154 // Set the template specialization kind.
6155 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006156 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006157 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006158
6159 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006160 // A definition of a class template or class member template
6161 // shall be in scope at the point of the explicit instantiation of
6162 // the class template or class member template.
6163 //
6164 // This check comes when we actually try to perform the
6165 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006166 ClassTemplateSpecializationDecl *Def
6167 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006168 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006169 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006170 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006171 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006172 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006173 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6174 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006175
Douglas Gregor0d035142009-10-27 18:42:08 +00006176 // Instantiate the members of this class template specialization.
6177 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006178 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006179 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006180 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6181
6182 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6183 // TSK_ExplicitInstantiationDefinition
6184 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6185 TSK == TSK_ExplicitInstantiationDefinition)
6186 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006187
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006188 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006189 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006190
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006191 // Set the template specialization kind.
6192 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006193 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006194}
6195
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006196// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006197DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006198Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006199 SourceLocation ExternLoc,
6200 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006201 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006202 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006203 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006204 IdentifierInfo *Name,
6205 SourceLocation NameLoc,
6206 AttributeList *Attr) {
6207
Douglas Gregor402abb52009-05-28 23:31:59 +00006208 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006209 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006210 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006211 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006212 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006213 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006214 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006215 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006216 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6217
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006218 if (!TagD)
6219 return true;
6220
John McCalld226f652010-08-21 09:40:31 +00006221 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006222 if (Tag->isEnum()) {
6223 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6224 << Context.getTypeDeclType(Tag);
6225 return true;
6226 }
6227
Douglas Gregord0c87372009-05-27 17:30:49 +00006228 if (Tag->isInvalidDecl())
6229 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006230
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006231 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6232 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6233 if (!Pattern) {
6234 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6235 << Context.getTypeDeclType(Record);
6236 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6237 return true;
6238 }
6239
Douglas Gregor558c0322009-10-14 23:41:34 +00006240 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006241 // If the explicit instantiation is for a class or member class, the
6242 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006243 // simple-template-id.
6244 //
6245 // C++98 has the same restriction, just worded differently.
6246 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006247 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006248 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006249
Douglas Gregor558c0322009-10-14 23:41:34 +00006250 // C++0x [temp.explicit]p2:
6251 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006252 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006253 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006254 TemplateSpecializationKind TSK
6255 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6256 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006257
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006258 // C++0x [temp.explicit]p2:
6259 // [...] An explicit instantiation shall appear in an enclosing
6260 // namespace of its template. [...]
6261 //
6262 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006263 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006264
Douglas Gregor454885e2009-10-15 15:54:05 +00006265 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006266 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006267 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006268 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006269 PrevDecl = Record;
6270 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006271 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006272 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006273 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006274 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006275 PrevDecl,
6276 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006277 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006278 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006279 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006280 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006281 return TagD;
6282 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006283
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006284 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006285 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006286 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006287 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006288 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006289 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006290 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006291 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006292 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006293 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6294 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006295 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6296 << Pattern;
6297 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006298 } else {
6299 if (InstantiateClass(NameLoc, Record, Def,
6300 getTemplateInstantiationArgs(Record),
6301 TSK))
6302 return true;
6303
Douglas Gregor952b0172010-02-11 01:04:33 +00006304 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006305 if (!RecordDef)
6306 return true;
6307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006308 }
6309
Douglas Gregor0d035142009-10-27 18:42:08 +00006310 // Instantiate all of the members of the class.
6311 InstantiateClassMembers(NameLoc, RecordDef,
6312 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006313
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006314 if (TSK == TSK_ExplicitInstantiationDefinition)
6315 MarkVTableUsed(NameLoc, RecordDef, true);
6316
Mike Stump390b4cc2009-05-16 07:39:55 +00006317 // FIXME: We don't have any representation for explicit instantiations of
6318 // member classes. Such a representation is not needed for compilation, but it
6319 // should be available for clients that want to see all of the declarations in
6320 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006321 return TagD;
6322}
6323
John McCallf312b1e2010-08-26 23:41:50 +00006324DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6325 SourceLocation ExternLoc,
6326 SourceLocation TemplateLoc,
6327 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006328 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006329 // TODO: check if/when DNInfo should replace Name.
6330 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6331 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006332 if (!Name) {
6333 if (!D.isInvalidType())
Daniel Dunbar96a00142012-03-09 18:35:03 +00006334 Diag(D.getDeclSpec().getLocStart(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006335 diag::err_explicit_instantiation_requires_name)
6336 << D.getDeclSpec().getSourceRange()
6337 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006338
Douglas Gregord5a423b2009-09-25 18:43:00 +00006339 return true;
6340 }
6341
6342 // The scope passed in may not be a decl scope. Zip up the scope tree until
6343 // we find one that is.
6344 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6345 (S->getFlags() & Scope::TemplateParamScope) != 0)
6346 S = S->getParent();
6347
6348 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006349 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6350 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006351 if (R.isNull())
6352 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006353
Douglas Gregore885e182011-05-21 18:53:30 +00006354 // C++ [dcl.stc]p1:
6355 // A storage-class-specifier shall not be specified in [...] an explicit
6356 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006357 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006358 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6359 << Name;
6360 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006361 } else if (D.getDeclSpec().getStorageClassSpec()
6362 != DeclSpec::SCS_unspecified) {
6363 // Complain about then remove the storage class specifier.
6364 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6365 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6366
6367 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006368 }
6369
Douglas Gregor663b5a02009-10-14 20:14:33 +00006370 // C++0x [temp.explicit]p1:
6371 // [...] An explicit instantiation of a function template shall not use the
6372 // inline or constexpr specifiers.
6373 // Presumably, this also applies to member functions of class templates as
6374 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006375 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006376 Diag(D.getDeclSpec().getInlineSpecLoc(),
David Blaikie4e4d0842012-03-11 07:00:24 +00006377 getLangOpts().CPlusPlus0x ?
Richard Smith2dc7ece2011-10-18 03:44:03 +00006378 diag::err_explicit_instantiation_inline :
6379 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006380 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6381 if (D.getDeclSpec().isConstexprSpecified())
6382 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6383 // not already specified.
6384 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6385 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006386
Douglas Gregor558c0322009-10-14 23:41:34 +00006387 // C++0x [temp.explicit]p2:
6388 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006389 // definition and an explicit instantiation declaration. An explicit
6390 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006391 TemplateSpecializationKind TSK
6392 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6393 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006394
Abramo Bagnara25777432010-08-11 22:01:17 +00006395 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006396 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006397
6398 if (!R->isFunctionType()) {
6399 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006400 // A [...] static data member of a class template can be explicitly
6401 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006402 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006403 if (Previous.isAmbiguous())
6404 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006405
John McCall1bcee0a2009-12-02 08:25:40 +00006406 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006407 if (!Prev || !Prev->isStaticDataMember()) {
6408 // We expect to see a data data member here.
6409 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6410 << Name;
6411 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6412 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006413 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006414 return true;
6415 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006416
Douglas Gregord5a423b2009-09-25 18:43:00 +00006417 if (!Prev->getInstantiatedFromStaticDataMember()) {
6418 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006419 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006420 diag::err_explicit_instantiation_data_member_not_instantiated)
6421 << Prev;
6422 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6423 // FIXME: Can we provide a note showing where this was declared?
6424 return true;
6425 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006426
Douglas Gregor558c0322009-10-14 23:41:34 +00006427 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006428 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006429 // or a static data member of a class template specialization, the name of
6430 // the class template specialization in the qualified-id for the member
6431 // name shall be a simple-template-id.
6432 //
6433 // C++98 has the same restriction, just worded differently.
6434 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006435 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006436 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006437 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006438
Douglas Gregor558c0322009-10-14 23:41:34 +00006439 // Check the scope of this explicit instantiation.
6440 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006441
Douglas Gregor454885e2009-10-15 15:54:05 +00006442 // Verify that it is okay to explicitly instantiate here.
6443 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6444 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006445 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006446 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006447 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006448 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006449 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006450 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006451 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006452 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006453
Douglas Gregord5a423b2009-09-25 18:43:00 +00006454 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006455 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006456 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006457 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006458
Douglas Gregord5a423b2009-09-25 18:43:00 +00006459 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006460 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006461 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006462
6463 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006464 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006465 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006466 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006467 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6468 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006469 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6470 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006471 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6472 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006473 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006474 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006475 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006476 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006478
Douglas Gregord5a423b2009-09-25 18:43:00 +00006479 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006480 // A [...] function [...] can be explicitly instantiated from its template.
6481 // A member function [...] of a class template can be explicitly
6482 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006483 // template.
John McCallc373d482010-01-27 01:50:18 +00006484 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006485 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6486 P != PEnd; ++P) {
6487 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006488 if (!HasExplicitTemplateArgs) {
6489 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6490 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6491 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006492
John McCallc373d482010-01-27 01:50:18 +00006493 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006494 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6495 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006496 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006497 }
6498 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006499
Douglas Gregord5a423b2009-09-25 18:43:00 +00006500 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6501 if (!FunTmpl)
6502 continue;
6503
John McCall5769d612010-02-08 23:07:23 +00006504 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006505 FunctionDecl *Specialization = 0;
6506 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006507 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006508 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006509 R, Specialization, Info)) {
6510 // FIXME: Keep track of almost-matches?
6511 (void)TDK;
6512 continue;
6513 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006514
John McCallc373d482010-01-27 01:50:18 +00006515 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006516 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006517
Douglas Gregord5a423b2009-09-25 18:43:00 +00006518 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006519 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006520 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006521 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006522 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6523 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6524 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006525
John McCallc373d482010-01-27 01:50:18 +00006526 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006527 return true;
John McCallc373d482010-01-27 01:50:18 +00006528
6529 // Ignore access control bits, we don't need them for redeclaration checking.
6530 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006531
Douglas Gregor0a897e32009-10-15 17:21:20 +00006532 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006533 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006534 diag::err_explicit_instantiation_member_function_not_instantiated)
6535 << Specialization
6536 << (Specialization->getTemplateSpecializationKind() ==
6537 TSK_ExplicitSpecialization);
6538 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6539 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006540 }
6541
Douglas Gregoref96ee02012-01-14 16:38:05 +00006542 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006543 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6544 PrevDecl = Specialization;
6545
Douglas Gregor0a897e32009-10-15 17:21:20 +00006546 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006547 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006548 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006549 PrevDecl,
6550 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006551 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006552 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006553 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006554
Douglas Gregor0a897e32009-10-15 17:21:20 +00006555 // FIXME: We may still want to build some representation of this
6556 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006557 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006558 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006559 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006560
6561 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006562 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6563 if (Attr)
6564 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006565
Douglas Gregor0a897e32009-10-15 17:21:20 +00006566 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006567 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006568
Douglas Gregor558c0322009-10-14 23:41:34 +00006569 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006570 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006571 // or a static data member of a class template specialization, the name of
6572 // the class template specialization in the qualified-id for the member
6573 // name shall be a simple-template-id.
6574 //
6575 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006576 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006577 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006578 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006579 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006580 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006581 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006582 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006583
Douglas Gregor558c0322009-10-14 23:41:34 +00006584 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006585 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006586 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006587 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006588 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006589
Douglas Gregord5a423b2009-09-25 18:43:00 +00006590 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006591 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006592}
6593
John McCallf312b1e2010-08-26 23:41:50 +00006594TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006595Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6596 const CXXScopeSpec &SS, IdentifierInfo *Name,
6597 SourceLocation TagLoc, SourceLocation NameLoc) {
6598 // This has to hold, because SS is expected to be defined.
6599 assert(Name && "Expected a name in a dependent tag");
6600
6601 NestedNameSpecifier *NNS
6602 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6603 if (!NNS)
6604 return true;
6605
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006606 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006607
Douglas Gregor48c89f42010-04-24 16:38:41 +00006608 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6609 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006610 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006611 return true;
6612 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006613
Douglas Gregor059101f2011-03-02 00:47:37 +00006614 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006615 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006616 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6617
6618 // Create type-source location information for this type.
6619 TypeLocBuilder TLB;
6620 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006621 TL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00006622 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6623 TL.setNameLoc(NameLoc);
6624 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006625}
6626
John McCallf312b1e2010-08-26 23:41:50 +00006627TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006628Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6629 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006630 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006631 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006632 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006633
Richard Smithebaf0e62011-10-18 20:49:44 +00006634 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6635 Diag(TypenameLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00006636 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00006637 diag::warn_cxx98_compat_typename_outside_of_template :
6638 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006639 << FixItHint::CreateRemoval(TypenameLoc);
6640
Douglas Gregor2494dd02011-03-01 01:34:45 +00006641 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006642 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6643 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006644 if (T.isNull())
6645 return true;
John McCall63b43852010-04-29 23:50:39 +00006646
6647 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6648 if (isa<DependentNameType>(T)) {
6649 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006650 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006651 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006652 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006653 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006654 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006655 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006656 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006657 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006658 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006659
John McCallb3d87482010-08-24 05:47:05 +00006660 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006661}
6662
John McCallf312b1e2010-08-26 23:41:50 +00006663TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006664Sema::ActOnTypenameType(Scope *S,
6665 SourceLocation TypenameLoc,
6666 const CXXScopeSpec &SS,
6667 SourceLocation TemplateKWLoc,
Douglas Gregora02411e2011-02-27 22:46:49 +00006668 TemplateTy TemplateIn,
6669 SourceLocation TemplateNameLoc,
6670 SourceLocation LAngleLoc,
6671 ASTTemplateArgsPtr TemplateArgsIn,
6672 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006673 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6674 Diag(TypenameLoc,
David Blaikie4e4d0842012-03-11 07:00:24 +00006675 getLangOpts().CPlusPlus0x ?
Richard Smithebaf0e62011-10-18 20:49:44 +00006676 diag::warn_cxx98_compat_typename_outside_of_template :
6677 diag::ext_typename_outside_of_template)
6678 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006679
6680 // Translate the parser's template argument list in our AST format.
6681 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6682 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6683
6684 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006685 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6686 // Construct a dependent template specialization type.
6687 assert(DTN && "dependent template has non-dependent name?");
6688 assert(DTN->getQualifier()
6689 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6690 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6691 DTN->getQualifier(),
6692 DTN->getIdentifier(),
6693 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006694
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006695 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006696 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006697 DependentTemplateSpecializationTypeLoc SpecTL
6698 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006699 SpecTL.setElaboratedKeywordLoc(TypenameLoc);
6700 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00006701 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006702 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006703 SpecTL.setLAngleLoc(LAngleLoc);
6704 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006705 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6706 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006707 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006708 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006709
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006710 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6711 if (T.isNull())
6712 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006713
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006714 // Provide source-location information for the template specialization type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006715 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006716 TemplateSpecializationTypeLoc SpecTL
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006717 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006718 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
6719 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006720 SpecTL.setLAngleLoc(LAngleLoc);
6721 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006722 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6723 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6724
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006725 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6726 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006727 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006728 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6729
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006730 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6731 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006732}
6733
Douglas Gregora02411e2011-02-27 22:46:49 +00006734
Douglas Gregord57959a2009-03-27 23:10:48 +00006735/// \brief Build the type that describes a C++ typename specifier,
6736/// e.g., "typename T::type".
6737QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006738Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6739 SourceLocation KeywordLoc,
6740 NestedNameSpecifierLoc QualifierLoc,
6741 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006742 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006743 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006744 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006745
John McCall77bb1aa2010-05-01 00:40:08 +00006746 DeclContext *Ctx = computeDeclContext(SS);
6747 if (!Ctx) {
6748 // If the nested-name-specifier is dependent and couldn't be
6749 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006750 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6751 return Context.getDependentNameType(Keyword,
6752 QualifierLoc.getNestedNameSpecifier(),
6753 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006754 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006755
John McCall77bb1aa2010-05-01 00:40:08 +00006756 // If the nested-name-specifier refers to the current instantiation,
6757 // the "typename" keyword itself is superfluous. In C++03, the
6758 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6759 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006760 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006761
John McCall77bb1aa2010-05-01 00:40:08 +00006762 if (RequireCompleteDeclContext(SS, Ctx))
6763 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006764
6765 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006766 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006767 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006768 unsigned DiagID = 0;
6769 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006770 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006771 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006772 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006773 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006774
6775 case LookupResult::FoundUnresolvedValue: {
6776 // We found a using declaration that is a value. Most likely, the using
6777 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006778 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006779 IILoc);
6780 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6781 << Name << Ctx << FullRange;
6782 if (UnresolvedUsingValueDecl *Using
6783 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006784 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006785 Diag(Loc, diag::note_using_value_decl_missing_typename)
6786 << FixItHint::CreateInsertion(Loc, "typename ");
6787 }
6788 }
6789 // Fall through to create a dependent typename type, from which we can recover
6790 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006791
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006792 case LookupResult::NotFoundInCurrentInstantiation:
6793 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006794 return Context.getDependentNameType(Keyword,
6795 QualifierLoc.getNestedNameSpecifier(),
6796 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006797
6798 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006799 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006800 // We found a type. Build an ElaboratedType, since the
6801 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006802 return Context.getElaboratedType(ETK_Typename,
6803 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006804 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006805 }
6806
6807 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006808 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006809 break;
6810
6811 case LookupResult::FoundOverloaded:
6812 DiagID = diag::err_typename_nested_not_type;
6813 Referenced = *Result.begin();
6814 break;
6815
John McCall6e247262009-10-10 05:48:19 +00006816 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006817 return QualType();
6818 }
6819
6820 // If we get here, it's because name lookup did not find a
6821 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006822 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006823 IILoc);
6824 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006825 if (Referenced)
6826 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6827 << Name;
6828 return QualType();
6829}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006830
6831namespace {
6832 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006833 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006834 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006835 SourceLocation Loc;
6836 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006837
Douglas Gregor4a959d82009-08-06 16:20:37 +00006838 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006839 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006840
Mike Stump1eb44332009-09-09 15:08:12 +00006841 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006842 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006843 DeclarationName Entity)
6844 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006845 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006846
6847 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006848 /// transformed.
6849 ///
6850 /// For the purposes of type reconstruction, a type has already been
6851 /// transformed if it is NULL or if it is not dependent.
6852 bool AlreadyTransformed(QualType T) {
6853 return T.isNull() || !T->isDependentType();
6854 }
Mike Stump1eb44332009-09-09 15:08:12 +00006855
6856 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006857 /// rebuilt.
6858 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006859
Douglas Gregor4a959d82009-08-06 16:20:37 +00006860 /// \brief Returns the name of the entity whose type is being rebuilt.
6861 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006862
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006863 /// \brief Sets the "base" location and entity when that
6864 /// information is known based on another transformation.
6865 void setBase(SourceLocation Loc, DeclarationName Entity) {
6866 this->Loc = Loc;
6867 this->Entity = Entity;
6868 }
Douglas Gregordfca6f52012-02-13 22:00:16 +00006869
6870 ExprResult TransformLambdaExpr(LambdaExpr *E) {
6871 // Lambdas never need to be transformed.
6872 return E;
6873 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006874 };
6875}
6876
Douglas Gregor4a959d82009-08-06 16:20:37 +00006877/// \brief Rebuilds a type within the context of the current instantiation.
6878///
Mike Stump1eb44332009-09-09 15:08:12 +00006879/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006880/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006881/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006882/// partial specialization thereof). This routine will rebuild that type now
6883/// that we have entered the declarator's scope, which may produce different
6884/// canonical types, e.g.,
6885///
6886/// \code
6887/// template<typename T>
6888/// struct X {
6889/// typedef T* pointer;
6890/// pointer data();
6891/// };
6892///
6893/// template<typename T>
6894/// typename X<T>::pointer X<T>::data() { ... }
6895/// \endcode
6896///
Douglas Gregor4714c122010-03-31 17:34:00 +00006897/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006898/// since we do not know that we can look into X<T> when we parsed the type.
6899/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006900/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006901/// as the canonical type of T*, allowing the return types of the out-of-line
6902/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006903TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6904 SourceLocation Loc,
6905 DeclarationName Name) {
6906 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006907 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006908
Douglas Gregor4a959d82009-08-06 16:20:37 +00006909 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6910 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006911}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006912
John McCall60d7b3a2010-08-24 06:29:42 +00006913ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006914 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6915 DeclarationName());
6916 return Rebuilder.TransformExpr(E);
6917}
6918
John McCall63b43852010-04-29 23:50:39 +00006919bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006920 if (SS.isInvalid())
6921 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006922
Douglas Gregor7e384942011-02-25 16:07:42 +00006923 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006924 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6925 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006926 NestedNameSpecifierLoc Rebuilt
6927 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6928 if (!Rebuilt)
6929 return true;
John McCall63b43852010-04-29 23:50:39 +00006930
Douglas Gregor7e384942011-02-25 16:07:42 +00006931 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006932 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006933}
6934
Douglas Gregor20606502011-10-14 15:31:12 +00006935/// \brief Rebuild the template parameters now that we know we're in a current
6936/// instantiation.
6937bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6938 TemplateParameterList *Params) {
6939 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6940 Decl *Param = Params->getParam(I);
6941
6942 // There is nothing to rebuild in a type parameter.
6943 if (isa<TemplateTypeParmDecl>(Param))
6944 continue;
6945
6946 // Rebuild the template parameter list of a template template parameter.
6947 if (TemplateTemplateParmDecl *TTP
6948 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6949 if (RebuildTemplateParamsInCurrentInstantiation(
6950 TTP->getTemplateParameters()))
6951 return true;
6952
6953 continue;
6954 }
6955
6956 // Rebuild the type of a non-type template parameter.
6957 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6958 TypeSourceInfo *NewTSI
6959 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6960 NTTP->getLocation(),
6961 NTTP->getDeclName());
6962 if (!NewTSI)
6963 return true;
6964
6965 if (NewTSI != NTTP->getTypeSourceInfo()) {
6966 NTTP->setTypeSourceInfo(NewTSI);
6967 NTTP->setType(NewTSI->getType());
6968 }
6969 }
6970
6971 return false;
6972}
6973
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006974/// \brief Produces a formatted string that describes the binding of
6975/// template parameters to template arguments.
6976std::string
6977Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6978 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006979 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006980}
6981
6982std::string
6983Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6984 const TemplateArgument *Args,
6985 unsigned NumArgs) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006986 SmallString<128> Str;
Douglas Gregor87dd6972010-12-20 16:52:59 +00006987 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006988
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006989 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006990 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006991
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006992 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006993 if (I >= NumArgs)
6994 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006995
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006996 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006997 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006998 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006999 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007000
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007001 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00007002 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007003 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00007004 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007005 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00007006
Douglas Gregor87dd6972010-12-20 16:52:59 +00007007 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00007008 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007009 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00007010
7011 Out << ']';
7012 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00007013}
Francois Pichet8387e2a2011-04-22 22:18:13 +00007014
7015void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
7016 if (!FD)
7017 return;
7018 FD->setLateTemplateParsed(Flag);
7019}
7020
7021bool Sema::IsInsideALocalClassWithinATemplateFunction() {
7022 DeclContext *DC = CurContext;
7023
7024 while (DC) {
7025 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
7026 const FunctionDecl *FD = RD->isLocalClass();
7027 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
7028 } else if (DC->isTranslationUnit() || DC->isNamespace())
7029 return false;
7030
7031 DC = DC->getParent();
7032 }
7033 return false;
7034}