blob: 44b0d83b466cd863e1c8136cc3c0d4abf6e5000c [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
12#include "Sema.h"
John McCall7d384dd2009-11-18 07:57:50 +000013#include "Lookup.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000014#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000016#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000019#include "clang/Parse/DeclSpec.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000020#include "clang/Parse/Template.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000021#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000022#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000023#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000024using namespace clang;
25
Douglas Gregor2dd078a2009-09-02 22:59:36 +000026/// \brief Determine whether the declaration found is acceptable as the name
27/// of a template and, if so, return that template declaration. Otherwise,
28/// returns NULL.
29static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) {
30 if (!D)
31 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000032
Douglas Gregor2dd078a2009-09-02 22:59:36 +000033 if (isa<TemplateDecl>(D))
34 return D;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Douglas Gregor2dd078a2009-09-02 22:59:36 +000036 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
37 // C++ [temp.local]p1:
38 // Like normal (non-template) classes, class templates have an
39 // injected-class-name (Clause 9). The injected-class-name
40 // can be used with or without a template-argument-list. When
41 // it is used without a template-argument-list, it is
42 // equivalent to the injected-class-name followed by the
43 // template-parameters of the class template enclosed in
44 // <>. When it is used with a template-argument-list, it
45 // refers to the specified class template specialization,
46 // which could be the current specialization or another
47 // specialization.
48 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000049 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000050 if (Record->getDescribedClassTemplate())
51 return Record->getDescribedClassTemplate();
52
53 if (ClassTemplateSpecializationDecl *Spec
54 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
55 return Spec->getSpecializedTemplate();
56 }
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregor2dd078a2009-09-02 22:59:36 +000058 return 0;
59 }
Mike Stump1eb44332009-09-09 15:08:12 +000060
Douglas Gregor2dd078a2009-09-02 22:59:36 +000061 return 0;
62}
63
John McCallf7a1a742009-11-24 19:00:30 +000064static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
65 LookupResult::Filter filter = R.makeFilter();
66 while (filter.hasNext()) {
67 NamedDecl *Orig = filter.next();
68 NamedDecl *Repl = isAcceptableTemplateName(C, Orig->getUnderlyingDecl());
69 if (!Repl)
70 filter.erase();
71 else if (Repl != Orig)
72 filter.replace(Repl);
73 }
74 filter.done();
75}
76
Douglas Gregor2dd078a2009-09-02 22:59:36 +000077TemplateNameKind Sema::isTemplateName(Scope *S,
Douglas Gregor014e88d2009-11-03 23:16:33 +000078 const CXXScopeSpec &SS,
79 UnqualifiedId &Name,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000080 TypeTy *ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +000081 bool EnteringContext,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000082 TemplateTy &TemplateResult) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +000083 assert(getLangOptions().CPlusPlus && "No template names in C!");
84
Douglas Gregor014e88d2009-11-03 23:16:33 +000085 DeclarationName TName;
86
87 switch (Name.getKind()) {
88 case UnqualifiedId::IK_Identifier:
89 TName = DeclarationName(Name.Identifier);
90 break;
91
92 case UnqualifiedId::IK_OperatorFunctionId:
93 TName = Context.DeclarationNames.getCXXOperatorName(
94 Name.OperatorFunctionId.Operator);
95 break;
96
Sean Hunte6252d12009-11-28 08:58:14 +000097 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +000098 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
99 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000100
Douglas Gregor014e88d2009-11-03 23:16:33 +0000101 default:
102 return TNK_Non_template;
103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
John McCallf7a1a742009-11-24 19:00:30 +0000105 QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Douglas Gregorbfea2392009-12-31 08:11:17 +0000107 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
108 LookupOrdinaryName);
John McCallf7a1a742009-11-24 19:00:30 +0000109 R.suppressDiagnostics();
110 LookupTemplateName(R, S, SS, ObjectType, EnteringContext);
111 if (R.empty())
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000112 return TNK_Non_template;
113
John McCall0bd6feb2009-12-02 08:04:21 +0000114 TemplateName Template;
115 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
John McCall0bd6feb2009-12-02 08:04:21 +0000117 unsigned ResultCount = R.end() - R.begin();
118 if (ResultCount > 1) {
119 // We assume that we'll preserve the qualifier from a function
120 // template name in other ways.
121 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
122 TemplateKind = TNK_Function_template;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000124 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
125
126 if (SS.isSet() && !SS.isInvalid()) {
127 NestedNameSpecifier *Qualifier
128 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
129 Template = Context.getQualifiedTemplateName(Qualifier, false, TD);
130 } else {
131 Template = TemplateName(TD);
132 }
133
134 if (isa<FunctionTemplateDecl>(TD))
135 TemplateKind = TNK_Function_template;
136 else {
137 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
138 TemplateKind = TNK_Type_template;
139 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
John McCall0bd6feb2009-12-02 08:04:21 +0000142 TemplateResult = TemplateTy::make(Template);
143 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000144}
145
Douglas Gregor84d0a192010-01-12 21:28:44 +0000146bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
147 SourceLocation IILoc,
148 Scope *S,
149 const CXXScopeSpec *SS,
150 TemplateTy &SuggestedTemplate,
151 TemplateNameKind &SuggestedKind) {
152 // We can't recover unless there's a dependent scope specifier preceding the
153 // template name.
154 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
155 computeDeclContext(*SS))
156 return false;
157
158 // The code is missing a 'template' keyword prior to the dependent template
159 // name.
160 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
161 Diag(IILoc, diag::err_template_kw_missing)
162 << Qualifier << II.getName()
163 << CodeModificationHint::CreateInsertion(IILoc, "template ");
164 SuggestedTemplate
165 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
166 SuggestedKind = TNK_Dependent_template_name;
167 return true;
168}
169
John McCallf7a1a742009-11-24 19:00:30 +0000170void Sema::LookupTemplateName(LookupResult &Found,
171 Scope *S, const CXXScopeSpec &SS,
172 QualType ObjectType,
173 bool EnteringContext) {
174 // Determine where to perform name lookup
175 DeclContext *LookupCtx = 0;
176 bool isDependent = false;
177 if (!ObjectType.isNull()) {
178 // This nested-name-specifier occurs in a member access expression, e.g.,
179 // x->B::f, and we are looking into the type of the object.
180 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
181 LookupCtx = computeDeclContext(ObjectType);
182 isDependent = ObjectType->isDependentType();
183 assert((isDependent || !ObjectType->isIncompleteType()) &&
184 "Caller should have completed object type");
185 } else if (SS.isSet()) {
186 // This nested-name-specifier occurs after another nested-name-specifier,
187 // so long into the context associated with the prior nested-name-specifier.
188 LookupCtx = computeDeclContext(SS, EnteringContext);
189 isDependent = isDependentScopeSpecifier(SS);
190
191 // The declaration context must be complete.
192 if (LookupCtx && RequireCompleteDeclContext(SS))
193 return;
194 }
195
196 bool ObjectTypeSearchedInScope = false;
197 if (LookupCtx) {
198 // Perform "qualified" name lookup into the declaration context we
199 // computed, which is either the type of the base of a member access
200 // expression or the declaration context associated with a prior
201 // nested-name-specifier.
202 LookupQualifiedName(Found, LookupCtx);
203
204 if (!ObjectType.isNull() && Found.empty()) {
205 // C++ [basic.lookup.classref]p1:
206 // In a class member access expression (5.2.5), if the . or -> token is
207 // immediately followed by an identifier followed by a <, the
208 // identifier must be looked up to determine whether the < is the
209 // beginning of a template argument list (14.2) or a less-than operator.
210 // The identifier is first looked up in the class of the object
211 // expression. If the identifier is not found, it is then looked up in
212 // the context of the entire postfix-expression and shall name a class
213 // or function template.
214 //
215 // FIXME: When we're instantiating a template, do we actually have to
216 // look in the scope of the template? Seems fishy...
217 if (S) LookupName(Found, S);
218 ObjectTypeSearchedInScope = true;
219 }
220 } else if (isDependent) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000221 // We cannot look into a dependent object type or nested nme
222 // specifier.
John McCallf7a1a742009-11-24 19:00:30 +0000223 return;
224 } else {
225 // Perform unqualified name lookup in the current scope.
226 LookupName(Found, S);
227 }
228
229 // FIXME: Cope with ambiguous name-lookup results.
230 assert(!Found.isAmbiguous() &&
231 "Cannot handle template name-lookup ambiguities");
232
Douglas Gregor2e933882010-01-12 17:06:20 +0000233 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000234 // If we did not find any names, attempt to correct any typos.
235 DeclarationName Name = Found.getLookupName();
236 if (CorrectTypo(Found, S, &SS, LookupCtx)) {
237 FilterAcceptableTemplateNames(Context, Found);
238 if (!Found.empty() && isa<TemplateDecl>(*Found.begin())) {
239 if (LookupCtx)
240 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
241 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
242 << CodeModificationHint::CreateReplacement(Found.getNameLoc(),
243 Found.getLookupName().getAsString());
244 else
245 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
246 << Name << Found.getLookupName()
247 << CodeModificationHint::CreateReplacement(Found.getNameLoc(),
248 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000249 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
250 Diag(Template->getLocation(), diag::note_previous_decl)
251 << Template->getDeclName();
Douglas Gregorbfea2392009-12-31 08:11:17 +0000252 } else
253 Found.clear();
254 } else {
255 Found.clear();
256 }
257 }
258
John McCallf7a1a742009-11-24 19:00:30 +0000259 FilterAcceptableTemplateNames(Context, Found);
260 if (Found.empty())
261 return;
262
263 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
264 // C++ [basic.lookup.classref]p1:
265 // [...] If the lookup in the class of the object expression finds a
266 // template, the name is also looked up in the context of the entire
267 // postfix-expression and [...]
268 //
269 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
270 LookupOrdinaryName);
271 LookupName(FoundOuter, S);
272 FilterAcceptableTemplateNames(Context, FoundOuter);
273 // FIXME: Handle ambiguities in this lookup better
274
275 if (FoundOuter.empty()) {
276 // - if the name is not found, the name found in the class of the
277 // object expression is used, otherwise
278 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
279 // - if the name is found in the context of the entire
280 // postfix-expression and does not name a class template, the name
281 // found in the class of the object expression is used, otherwise
282 } else {
283 // - if the name found is a class template, it must refer to the same
284 // entity as the one found in the class of the object expression,
285 // otherwise the program is ill-formed.
286 if (!Found.isSingleResult() ||
287 Found.getFoundDecl()->getCanonicalDecl()
288 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
289 Diag(Found.getNameLoc(),
290 diag::err_nested_name_member_ref_lookup_ambiguous)
291 << Found.getLookupName();
292 Diag(Found.getRepresentativeDecl()->getLocation(),
293 diag::note_ambig_member_ref_object_type)
294 << ObjectType;
295 Diag(FoundOuter.getFoundDecl()->getLocation(),
296 diag::note_ambig_member_ref_scope);
297
298 // Recover by taking the template that we found in the object
299 // expression's type.
300 }
301 }
302 }
303}
304
John McCall2f841ba2009-12-02 03:53:29 +0000305/// ActOnDependentIdExpression - Handle a dependent id-expression that
306/// was just parsed. This is only possible with an explicit scope
307/// specifier naming a dependent type.
John McCallf7a1a742009-11-24 19:00:30 +0000308Sema::OwningExprResult
309Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
310 DeclarationName Name,
311 SourceLocation NameLoc,
John McCall2f841ba2009-12-02 03:53:29 +0000312 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000313 const TemplateArgumentListInfo *TemplateArgs) {
314 NestedNameSpecifier *Qualifier
315 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
316
John McCall2f841ba2009-12-02 03:53:29 +0000317 if (!isAddressOfOperand &&
318 isa<CXXMethodDecl>(CurContext) &&
319 cast<CXXMethodDecl>(CurContext)->isInstance()) {
320 QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context);
321
John McCallf7a1a742009-11-24 19:00:30 +0000322 // Since the 'this' expression is synthesized, we don't need to
323 // perform the double-lookup check.
324 NamedDecl *FirstQualifierInScope = 0;
325
John McCallaa81e162009-12-01 22:10:20 +0000326 return Owned(CXXDependentScopeMemberExpr::Create(Context,
327 /*This*/ 0, ThisType,
328 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000329 /*Op*/ SourceLocation(),
330 Qualifier, SS.getRange(),
331 FirstQualifierInScope,
332 Name, NameLoc,
333 TemplateArgs));
334 }
335
336 return BuildDependentDeclRefExpr(SS, Name, NameLoc, TemplateArgs);
337}
338
339Sema::OwningExprResult
340Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
341 DeclarationName Name,
342 SourceLocation NameLoc,
343 const TemplateArgumentListInfo *TemplateArgs) {
344 return Owned(DependentScopeDeclRefExpr::Create(Context,
345 static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
346 SS.getRange(),
347 Name, NameLoc,
348 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000349}
350
Douglas Gregor72c3f312008-12-05 18:15:24 +0000351/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
352/// that the template parameter 'PrevDecl' is being shadowed by a new
353/// declaration at location Loc. Returns true to indicate that this is
354/// an error, and false otherwise.
355bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000356 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000357
358 // Microsoft Visual C++ permits template parameters to be shadowed.
359 if (getLangOptions().Microsoft)
360 return false;
361
362 // C++ [temp.local]p4:
363 // A template-parameter shall not be redeclared within its
364 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000365 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000366 << cast<NamedDecl>(PrevDecl)->getDeclName();
367 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
368 return true;
369}
370
Douglas Gregor2943aed2009-03-03 04:44:36 +0000371/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000372/// the parameter D to reference the templated declaration and return a pointer
373/// to the template declaration. Otherwise, do nothing to D and return null.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000374TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) {
Douglas Gregor13d2d6c2009-10-06 21:27:51 +0000375 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000376 D = DeclPtrTy::make(Temp->getTemplatedDecl());
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000377 return Temp;
378 }
379 return 0;
380}
381
Douglas Gregor788cd062009-11-11 01:00:40 +0000382static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
383 const ParsedTemplateArgument &Arg) {
384
385 switch (Arg.getKind()) {
386 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000387 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000388 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
389 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000390 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000391 return TemplateArgumentLoc(TemplateArgument(T), DI);
392 }
393
394 case ParsedTemplateArgument::NonType: {
395 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
396 return TemplateArgumentLoc(TemplateArgument(E), E);
397 }
398
399 case ParsedTemplateArgument::Template: {
400 TemplateName Template
401 = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get());
402 return TemplateArgumentLoc(TemplateArgument(Template),
403 Arg.getScopeSpec().getRange(),
404 Arg.getLocation());
405 }
406 }
407
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000408 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000409 return TemplateArgumentLoc();
410}
411
412/// \brief Translates template arguments as provided by the parser
413/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000414void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
415 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000416 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000417 TemplateArgs.addArgument(translateTemplateArgument(*this,
418 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000419}
420
Douglas Gregor72c3f312008-12-05 18:15:24 +0000421/// ActOnTypeParameter - Called when a C++ template type parameter
422/// (e.g., "typename T") has been parsed. Typename specifies whether
423/// the keyword "typename" was used to declare the type parameter
424/// (otherwise, "class" was used), and KeyLoc is the location of the
425/// "class" or "typename" keyword. ParamName is the name of the
426/// parameter (NULL indicates an unnamed template parameter) and
Mike Stump1eb44332009-09-09 15:08:12 +0000427/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000428/// If the type parameter has a default argument, it will be added
429/// later via ActOnTypeParameterDefault.
Mike Stump1eb44332009-09-09 15:08:12 +0000430Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
Anders Carlsson941df7d2009-06-12 19:58:00 +0000431 SourceLocation EllipsisLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000432 SourceLocation KeyLoc,
433 IdentifierInfo *ParamName,
434 SourceLocation ParamNameLoc,
435 unsigned Depth, unsigned Position) {
Mike Stump1eb44332009-09-09 15:08:12 +0000436 assert(S->isTemplateParamScope() &&
437 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000438 bool Invalid = false;
439
440 if (ParamName) {
John McCallf36e02d2009-10-09 21:13:30 +0000441 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000442 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000443 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000444 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000445 }
446
Douglas Gregorddc29e12009-02-06 22:42:48 +0000447 SourceLocation Loc = ParamNameLoc;
448 if (!ParamName)
449 Loc = KeyLoc;
450
Douglas Gregor72c3f312008-12-05 18:15:24 +0000451 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000452 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
453 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000454 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000455 if (Invalid)
456 Param->setInvalidDecl();
457
458 if (ParamName) {
459 // Add the template parameter into the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000460 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregor72c3f312008-12-05 18:15:24 +0000461 IdResolver.AddDecl(Param);
462 }
463
Chris Lattnerb28317a2009-03-28 19:18:32 +0000464 return DeclPtrTy::make(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000465}
466
Douglas Gregord684b002009-02-10 19:49:53 +0000467/// ActOnTypeParameterDefault - Adds a default argument (the type
Mike Stump1eb44332009-09-09 15:08:12 +0000468/// Default) to the given template type parameter (TypeParam).
469void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam,
Douglas Gregord684b002009-02-10 19:49:53 +0000470 SourceLocation EqualLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000471 SourceLocation DefaultLoc,
Douglas Gregord684b002009-02-10 19:49:53 +0000472 TypeTy *DefaultT) {
Mike Stump1eb44332009-09-09 15:08:12 +0000473 TemplateTypeParmDecl *Parm
Chris Lattnerb28317a2009-03-28 19:18:32 +0000474 = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>());
John McCall833ca992009-10-29 08:12:44 +0000475
John McCalla93c9342009-12-07 02:54:59 +0000476 TypeSourceInfo *DefaultTInfo;
477 GetTypeFromParser(DefaultT, &DefaultTInfo);
John McCall833ca992009-10-29 08:12:44 +0000478
John McCalla93c9342009-12-07 02:54:59 +0000479 assert(DefaultTInfo && "expected source information for type");
Douglas Gregord684b002009-02-10 19:49:53 +0000480
Anders Carlsson9c4c5c82009-06-12 22:30:13 +0000481 // C++0x [temp.param]p9:
482 // A default template-argument may be specified for any kind of
Mike Stump1eb44332009-09-09 15:08:12 +0000483 // template-parameter that is not a template parameter pack.
Anders Carlsson9c4c5c82009-06-12 22:30:13 +0000484 if (Parm->isParameterPack()) {
485 Diag(DefaultLoc, diag::err_template_param_pack_default_arg);
Anders Carlsson9c4c5c82009-06-12 22:30:13 +0000486 return;
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregord684b002009-02-10 19:49:53 +0000489 // C++ [temp.param]p14:
490 // A template-parameter shall not be used in its own default argument.
491 // FIXME: Implement this check! Needs a recursive walk over the types.
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregord684b002009-02-10 19:49:53 +0000493 // Check the template argument itself.
John McCalla93c9342009-12-07 02:54:59 +0000494 if (CheckTemplateArgument(Parm, DefaultTInfo)) {
Douglas Gregord684b002009-02-10 19:49:53 +0000495 Parm->setInvalidDecl();
496 return;
497 }
498
John McCalla93c9342009-12-07 02:54:59 +0000499 Parm->setDefaultArgument(DefaultTInfo, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000500}
501
Douglas Gregor2943aed2009-03-03 04:44:36 +0000502/// \brief Check that the type of a non-type template parameter is
503/// well-formed.
504///
505/// \returns the (possibly-promoted) parameter type if valid;
506/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000507QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000508Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
509 // C++ [temp.param]p4:
510 //
511 // A non-type template-parameter shall have one of the following
512 // (optionally cv-qualified) types:
513 //
514 // -- integral or enumeration type,
515 if (T->isIntegralType() || T->isEnumeralType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000516 // -- pointer to object or pointer to function,
517 (T->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +0000518 (T->getAs<PointerType>()->getPointeeType()->isObjectType() ||
519 T->getAs<PointerType>()->getPointeeType()->isFunctionType())) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000520 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000521 T->isReferenceType() ||
522 // -- pointer to member.
523 T->isMemberPointerType() ||
524 // If T is a dependent type, we can't do the check now, so we
525 // assume that it is well-formed.
526 T->isDependentType())
527 return T;
528 // C++ [temp.param]p8:
529 //
530 // A non-type template-parameter of type "array of T" or
531 // "function returning T" is adjusted to be of type "pointer to
532 // T" or "pointer to function returning T", respectively.
533 else if (T->isArrayType())
534 // FIXME: Keep the type prior to promotion?
535 return Context.getArrayDecayedType(T);
536 else if (T->isFunctionType())
537 // FIXME: Keep the type prior to promotion?
538 return Context.getPointerType(T);
539
540 Diag(Loc, diag::err_template_nontype_parm_bad_type)
541 << T;
542
543 return QualType();
544}
545
Douglas Gregor72c3f312008-12-05 18:15:24 +0000546/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
547/// template parameter (e.g., "int Size" in "template<int Size>
548/// class Array") has been parsed. S is the current scope and D is
549/// the parsed declarator.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000550Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
Mike Stump1eb44332009-09-09 15:08:12 +0000551 unsigned Depth,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000552 unsigned Position) {
John McCalla93c9342009-12-07 02:54:59 +0000553 TypeSourceInfo *TInfo = 0;
554 QualType T = GetTypeForDeclarator(D, S, &TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000555
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000556 assert(S->isTemplateParamScope() &&
557 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000558 bool Invalid = false;
559
560 IdentifierInfo *ParamName = D.getIdentifier();
561 if (ParamName) {
John McCallf36e02d2009-10-09 21:13:30 +0000562 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000563 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000564 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000565 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000566 }
567
Douglas Gregor2943aed2009-03-03 04:44:36 +0000568 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
Douglas Gregorceef30c2009-03-09 16:46:39 +0000569 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000570 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000571 Invalid = true;
572 }
Douglas Gregor5d290d52009-02-10 17:43:50 +0000573
Douglas Gregor72c3f312008-12-05 18:15:24 +0000574 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000575 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
576 D.getIdentifierLoc(),
John McCalla93c9342009-12-07 02:54:59 +0000577 Depth, Position, ParamName, T, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000578 if (Invalid)
579 Param->setInvalidDecl();
580
581 if (D.getIdentifier()) {
582 // Add the template parameter into the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000583 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregor72c3f312008-12-05 18:15:24 +0000584 IdResolver.AddDecl(Param);
585 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000586 return DeclPtrTy::make(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000587}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000588
Douglas Gregord684b002009-02-10 19:49:53 +0000589/// \brief Adds a default argument to the given non-type template
590/// parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000591void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregord684b002009-02-10 19:49:53 +0000592 SourceLocation EqualLoc,
593 ExprArg DefaultE) {
Mike Stump1eb44332009-09-09 15:08:12 +0000594 NonTypeTemplateParmDecl *TemplateParm
Chris Lattnerb28317a2009-03-28 19:18:32 +0000595 = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregord684b002009-02-10 19:49:53 +0000596 Expr *Default = static_cast<Expr *>(DefaultE.get());
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregord684b002009-02-10 19:49:53 +0000598 // C++ [temp.param]p14:
599 // A template-parameter shall not be used in its own default argument.
600 // FIXME: Implement this check! Needs a recursive walk over the types.
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Douglas Gregord684b002009-02-10 19:49:53 +0000602 // Check the well-formedness of the default template argument.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000603 TemplateArgument Converted;
604 if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default,
605 Converted)) {
Douglas Gregord684b002009-02-10 19:49:53 +0000606 TemplateParm->setInvalidDecl();
607 return;
608 }
609
Anders Carlssone9146f22009-05-01 19:49:17 +0000610 TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>());
Douglas Gregord684b002009-02-10 19:49:53 +0000611}
612
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000613
614/// ActOnTemplateTemplateParameter - Called when a C++ template template
615/// parameter (e.g. T in template <template <typename> class T> class array)
616/// has been parsed. S is the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000617Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S,
618 SourceLocation TmpLoc,
619 TemplateParamsTy *Params,
620 IdentifierInfo *Name,
621 SourceLocation NameLoc,
622 unsigned Depth,
Mike Stump1eb44332009-09-09 15:08:12 +0000623 unsigned Position) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000624 assert(S->isTemplateParamScope() &&
625 "Template template parameter not in template parameter scope!");
626
627 // Construct the parameter object.
628 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000629 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
630 TmpLoc, Depth, Position, Name,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000631 (TemplateParameterList*)Params);
632
633 // Make sure the parameter is valid.
634 // FIXME: Decl object is not currently invalidated anywhere so this doesn't
635 // do anything yet. However, if the template parameter list or (eventual)
636 // default value is ever invalidated, that will propagate here.
637 bool Invalid = false;
638 if (Invalid) {
639 Param->setInvalidDecl();
640 }
641
642 // If the tt-param has a name, then link the identifier into the scope
643 // and lookup mechanisms.
644 if (Name) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000645 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000646 IdResolver.AddDecl(Param);
647 }
648
Chris Lattnerb28317a2009-03-28 19:18:32 +0000649 return DeclPtrTy::make(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000650}
651
Douglas Gregord684b002009-02-10 19:49:53 +0000652/// \brief Adds a default argument to the given template template
653/// parameter.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000654void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregord684b002009-02-10 19:49:53 +0000655 SourceLocation EqualLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +0000656 const ParsedTemplateArgument &Default) {
Mike Stump1eb44332009-09-09 15:08:12 +0000657 TemplateTemplateParmDecl *TemplateParm
Chris Lattnerb28317a2009-03-28 19:18:32 +0000658 = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregor788cd062009-11-11 01:00:40 +0000659
Douglas Gregord684b002009-02-10 19:49:53 +0000660 // C++ [temp.param]p14:
661 // A template-parameter shall not be used in its own default argument.
662 // FIXME: Implement this check! Needs a recursive walk over the types.
663
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000664 // Check only that we have a template template argument. We don't want to
665 // try to check well-formedness now, because our template template parameter
666 // might have dependent types in its template parameters, which we wouldn't
667 // be able to match now.
668 //
669 // If none of the template template parameter's template arguments mention
670 // other template parameters, we could actually perform more checking here.
671 // However, it isn't worth doing.
Douglas Gregor788cd062009-11-11 01:00:40 +0000672 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000673 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
674 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
675 << DefaultArg.getSourceRange();
Douglas Gregord684b002009-02-10 19:49:53 +0000676 return;
677 }
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000678
Douglas Gregor788cd062009-11-11 01:00:40 +0000679 TemplateParm->setDefaultArgument(DefaultArg);
Douglas Gregord684b002009-02-10 19:49:53 +0000680}
681
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000682/// ActOnTemplateParameterList - Builds a TemplateParameterList that
683/// contains the template parameters in Params/NumParams.
684Sema::TemplateParamsTy *
685Sema::ActOnTemplateParameterList(unsigned Depth,
686 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000687 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000688 SourceLocation LAngleLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000689 DeclPtrTy *Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000690 SourceLocation RAngleLoc) {
691 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000692 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000693
Douglas Gregorddc29e12009-02-06 22:42:48 +0000694 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000695 (NamedDecl**)Params, NumParams,
696 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000697}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000698
Douglas Gregor212e81c2009-03-25 00:13:59 +0000699Sema::DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000700Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000701 SourceLocation KWLoc, const CXXScopeSpec &SS,
702 IdentifierInfo *Name, SourceLocation NameLoc,
703 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000704 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000705 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000706 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000707 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000708 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000709 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000710
711 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000712 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000713 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000714
John McCall05b23ea2009-09-14 21:59:20 +0000715 TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec);
716 assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000717
718 // There is no such thing as an unnamed class template.
719 if (!Name) {
720 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000721 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000722 }
723
724 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000725 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000726 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000727 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000728 if (SS.isNotEmpty() && !SS.isInvalid()) {
Douglas Gregorf0510d42009-10-12 23:11:44 +0000729 if (RequireCompleteDeclContext(SS))
730 return true;
731
Douglas Gregor05396e22009-08-25 17:23:04 +0000732 SemanticContext = computeDeclContext(SS, true);
733 if (!SemanticContext) {
734 // FIXME: Produce a reasonable diagnostic here
735 return true;
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
John McCalla24dc2e2009-11-17 02:14:36 +0000738 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000739 } else {
740 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000741 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Douglas Gregorddc29e12009-02-06 22:42:48 +0000744 assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
745 NamedDecl *PrevDecl = 0;
746 if (Previous.begin() != Previous.end())
747 PrevDecl = *Previous.begin();
748
Douglas Gregorddc29e12009-02-06 22:42:48 +0000749 // If there is a previous declaration with the same name, check
750 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000751 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000752 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000753
754 // We may have found the injected-class-name of a class template,
755 // class template partial specialization, or class template specialization.
756 // In these cases, grab the template that is being defined or specialized.
757 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
758 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
759 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
760 PrevClassTemplate
761 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
762 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
763 PrevClassTemplate
764 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
765 ->getSpecializedTemplate();
766 }
767 }
768
John McCall65c49462009-12-18 11:25:59 +0000769 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000770 // C++ [namespace.memdef]p3:
771 // [...] When looking for a prior declaration of a class or a function
772 // declared as a friend, and when the name of the friend class or
773 // function is neither a qualified name nor a template-id, scopes outside
774 // the innermost enclosing namespace scope are not considered.
775 DeclContext *OutermostContext = CurContext;
776 while (!OutermostContext->isFileContext())
777 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000778
779 if (PrevDecl &&
780 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
781 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
John McCalle129d442009-12-17 23:21:11 +0000782 SemanticContext = PrevDecl->getDeclContext();
783 } else {
784 // Declarations in outer scopes don't matter. However, the outermost
785 // context we computed is the semantic context for our new
786 // declaration.
787 PrevDecl = PrevClassTemplate = 0;
788 SemanticContext = OutermostContext;
789 }
790
791 if (CurContext->isDependentContext()) {
792 // If this is a dependent context, we don't want to link the friend
793 // class template to the template in scope, because that would perform
794 // checking of the template parameter lists that can't be performed
795 // until the outer context is instantiated.
796 PrevDecl = PrevClassTemplate = 0;
797 }
798 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
799 PrevDecl = PrevClassTemplate = 0;
800
Douglas Gregorddc29e12009-02-06 22:42:48 +0000801 if (PrevClassTemplate) {
802 // Ensure that the template parameter lists are compatible.
803 if (!TemplateParameterListsAreEqual(TemplateParams,
804 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000805 /*Complain=*/true,
806 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000807 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000808
809 // C++ [temp.class]p4:
810 // In a redeclaration, partial specialization, explicit
811 // specialization or explicit instantiation of a class template,
812 // the class-key shall agree in kind with the original class
813 // template declaration (7.1.5.3).
814 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000815 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000816 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000817 << Name
Mike Stump1eb44332009-09-09 15:08:12 +0000818 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +0000819 PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000820 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000821 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000822 }
823
Douglas Gregorddc29e12009-02-06 22:42:48 +0000824 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000825 if (TUK == TUK_Definition) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000826 if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
827 Diag(NameLoc, diag::err_redefinition) << Name;
828 Diag(Def->getLocation(), diag::note_previous_definition);
829 // FIXME: Would it make sense to try to "forget" the previous
830 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000831 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000832 }
833 }
834 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
835 // Maybe we will complain about the shadowed template parameter.
836 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
837 // Just pretend that we didn't see the previous declaration.
838 PrevDecl = 0;
839 } else if (PrevDecl) {
840 // C++ [temp]p5:
841 // A class template shall not have the same name as any other
842 // template, class, function, object, enumeration, enumerator,
843 // namespace, or type in the same scope (3.3), except as specified
844 // in (14.5.4).
845 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
846 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000847 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000848 }
849
Douglas Gregord684b002009-02-10 19:49:53 +0000850 // Check the template parameter list of this declaration, possibly
851 // merging in the template parameter list from the previous class
852 // template declaration.
853 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000854 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
855 TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000856 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Douglas Gregor7da97d02009-05-10 22:57:19 +0000858 // FIXME: If we had a scope specifier, we better have a previous template
Douglas Gregorddc29e12009-02-06 22:42:48 +0000859 // declaration!
860
Mike Stump1eb44332009-09-09 15:08:12 +0000861 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000862 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000863 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000864 PrevClassTemplate->getTemplatedDecl() : 0,
865 /*DelayTypeCreation=*/true);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000866
867 ClassTemplateDecl *NewTemplate
868 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
869 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000870 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000871 NewClass->setDescribedClassTemplate(NewTemplate);
872
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000873 // Build the type for the class template declaration now.
Mike Stump1eb44332009-09-09 15:08:12 +0000874 QualType T =
875 Context.getTypeDeclType(NewClass,
876 PrevClassTemplate?
877 PrevClassTemplate->getTemplatedDecl() : 0);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000878 assert(T->isDependentType() && "Class template type is not dependent?");
879 (void)T;
880
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000881 // If we are providing an explicit specialization of a member that is a
882 // class template, make a note of that.
883 if (PrevClassTemplate &&
884 PrevClassTemplate->getInstantiatedFromMemberTemplate())
885 PrevClassTemplate->setMemberSpecialization();
886
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000887 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000888 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000889 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Douglas Gregorddc29e12009-02-06 22:42:48 +0000891 // Set the lexical context of these templates
892 NewClass->setLexicalDeclContext(CurContext);
893 NewTemplate->setLexicalDeclContext(CurContext);
894
John McCall0f434ec2009-07-31 02:45:11 +0000895 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000896 NewClass->startDefinition();
897
898 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000899 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000900
John McCall05b23ea2009-09-14 21:59:20 +0000901 if (TUK != TUK_Friend)
902 PushOnScopeChains(NewTemplate, S);
903 else {
Douglas Gregord85bea22009-09-26 06:47:28 +0000904 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +0000905 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +0000906 NewClass->setAccess(PrevClassTemplate->getAccess());
907 }
John McCall05b23ea2009-09-14 21:59:20 +0000908
Douglas Gregord85bea22009-09-26 06:47:28 +0000909 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
910 PrevClassTemplate != NULL);
911
John McCall05b23ea2009-09-14 21:59:20 +0000912 // Friend templates are visible in fairly strange ways.
913 if (!CurContext->isDependentContext()) {
914 DeclContext *DC = SemanticContext->getLookupContext();
915 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
916 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
917 PushOnScopeChains(NewTemplate, EnclosingScope,
918 /* AddToContext = */ false);
919 }
Douglas Gregord85bea22009-09-26 06:47:28 +0000920
921 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
922 NewClass->getLocation(),
923 NewTemplate,
924 /*FIXME:*/NewClass->getLocation());
925 Friend->setAccess(AS_public);
926 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +0000927 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000928
Douglas Gregord684b002009-02-10 19:49:53 +0000929 if (Invalid) {
930 NewTemplate->setInvalidDecl();
931 NewClass->setInvalidDecl();
932 }
Chris Lattnerb28317a2009-03-28 19:18:32 +0000933 return DeclPtrTy::make(NewTemplate);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000934}
935
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000936/// \brief Diagnose the presence of a default template argument on a
937/// template parameter, which is ill-formed in certain contexts.
938///
939/// \returns true if the default template argument should be dropped.
940static bool DiagnoseDefaultTemplateArgument(Sema &S,
941 Sema::TemplateParamListContext TPC,
942 SourceLocation ParamLoc,
943 SourceRange DefArgRange) {
944 switch (TPC) {
945 case Sema::TPC_ClassTemplate:
946 return false;
947
948 case Sema::TPC_FunctionTemplate:
949 // C++ [temp.param]p9:
950 // A default template-argument shall not be specified in a
951 // function template declaration or a function template
952 // definition [...]
953 // (This sentence is not in C++0x, per DR226).
954 if (!S.getLangOptions().CPlusPlus0x)
955 S.Diag(ParamLoc,
956 diag::err_template_parameter_default_in_function_template)
957 << DefArgRange;
958 return false;
959
960 case Sema::TPC_ClassTemplateMember:
961 // C++0x [temp.param]p9:
962 // A default template-argument shall not be specified in the
963 // template-parameter-lists of the definition of a member of a
964 // class template that appears outside of the member's class.
965 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
966 << DefArgRange;
967 return true;
968
969 case Sema::TPC_FriendFunctionTemplate:
970 // C++ [temp.param]p9:
971 // A default template-argument shall not be specified in a
972 // friend template declaration.
973 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
974 << DefArgRange;
975 return true;
976
977 // FIXME: C++0x [temp.param]p9 allows default template-arguments
978 // for friend function templates if there is only a single
979 // declaration (and it is a definition). Strange!
980 }
981
982 return false;
983}
984
Douglas Gregord684b002009-02-10 19:49:53 +0000985/// \brief Checks the validity of a template parameter list, possibly
986/// considering the template parameter list from a previous
987/// declaration.
988///
989/// If an "old" template parameter list is provided, it must be
990/// equivalent (per TemplateParameterListsAreEqual) to the "new"
991/// template parameter list.
992///
993/// \param NewParams Template parameter list for a new template
994/// declaration. This template parameter list will be updated with any
995/// default arguments that are carried through from the previous
996/// template parameter list.
997///
998/// \param OldParams If provided, template parameter list from a
999/// previous declaration of the same template. Default template
1000/// arguments will be merged from the old template parameter list to
1001/// the new template parameter list.
1002///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001003/// \param TPC Describes the context in which we are checking the given
1004/// template parameter list.
1005///
Douglas Gregord684b002009-02-10 19:49:53 +00001006/// \returns true if an error occurred, false otherwise.
1007bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001008 TemplateParameterList *OldParams,
1009 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001010 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Douglas Gregord684b002009-02-10 19:49:53 +00001012 // C++ [temp.param]p10:
1013 // The set of default template-arguments available for use with a
1014 // template declaration or definition is obtained by merging the
1015 // default arguments from the definition (if in scope) and all
1016 // declarations in scope in the same way default function
1017 // arguments are (8.3.6).
1018 bool SawDefaultArgument = false;
1019 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001020
Anders Carlsson49d25572009-06-12 23:20:15 +00001021 bool SawParameterPack = false;
1022 SourceLocation ParameterPackLoc;
1023
Mike Stump1a35fde2009-02-11 23:03:27 +00001024 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001025 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001026 if (OldParams)
1027 OldParam = OldParams->begin();
1028
1029 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1030 NewParamEnd = NewParams->end();
1031 NewParam != NewParamEnd; ++NewParam) {
1032 // Variables used to diagnose redundant default arguments
1033 bool RedundantDefaultArg = false;
1034 SourceLocation OldDefaultLoc;
1035 SourceLocation NewDefaultLoc;
1036
1037 // Variables used to diagnose missing default arguments
1038 bool MissingDefaultArg = false;
1039
Anders Carlsson49d25572009-06-12 23:20:15 +00001040 // C++0x [temp.param]p11:
1041 // If a template parameter of a class template is a template parameter pack,
1042 // it must be the last template parameter.
1043 if (SawParameterPack) {
Mike Stump1eb44332009-09-09 15:08:12 +00001044 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001045 diag::err_template_param_pack_must_be_last_template_parameter);
1046 Invalid = true;
1047 }
1048
Douglas Gregord684b002009-02-10 19:49:53 +00001049 if (TemplateTypeParmDecl *NewTypeParm
1050 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001051 // Check the presence of a default argument here.
1052 if (NewTypeParm->hasDefaultArgument() &&
1053 DiagnoseDefaultTemplateArgument(*this, TPC,
1054 NewTypeParm->getLocation(),
1055 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
1056 .getFullSourceRange()))
1057 NewTypeParm->removeDefaultArgument();
1058
1059 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001060 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001061 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Anders Carlsson49d25572009-06-12 23:20:15 +00001063 if (NewTypeParm->isParameterPack()) {
1064 assert(!NewTypeParm->hasDefaultArgument() &&
1065 "Parameter packs can't have a default argument!");
1066 SawParameterPack = true;
1067 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001068 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001069 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001070 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1071 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1072 SawDefaultArgument = true;
1073 RedundantDefaultArg = true;
1074 PreviousDefaultArgLoc = NewDefaultLoc;
1075 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1076 // Merge the default argument from the old declaration to the
1077 // new declaration.
1078 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001079 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001080 true);
1081 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1082 } else if (NewTypeParm->hasDefaultArgument()) {
1083 SawDefaultArgument = true;
1084 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1085 } else if (SawDefaultArgument)
1086 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001087 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001088 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001089 // Check the presence of a default argument here.
1090 if (NewNonTypeParm->hasDefaultArgument() &&
1091 DiagnoseDefaultTemplateArgument(*this, TPC,
1092 NewNonTypeParm->getLocation(),
1093 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
1094 NewNonTypeParm->getDefaultArgument()->Destroy(Context);
1095 NewNonTypeParm->setDefaultArgument(0);
1096 }
1097
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001098 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001099 NonTypeTemplateParmDecl *OldNonTypeParm
1100 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001101 if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001102 NewNonTypeParm->hasDefaultArgument()) {
1103 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1104 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1105 SawDefaultArgument = true;
1106 RedundantDefaultArg = true;
1107 PreviousDefaultArgLoc = NewDefaultLoc;
1108 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1109 // Merge the default argument from the old declaration to the
1110 // new declaration.
1111 SawDefaultArgument = true;
1112 // FIXME: We need to create a new kind of "default argument"
1113 // expression that points to a previous template template
1114 // parameter.
1115 NewNonTypeParm->setDefaultArgument(
1116 OldNonTypeParm->getDefaultArgument());
1117 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1118 } else if (NewNonTypeParm->hasDefaultArgument()) {
1119 SawDefaultArgument = true;
1120 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1121 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001122 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001123 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001124 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001125 TemplateTemplateParmDecl *NewTemplateParm
1126 = cast<TemplateTemplateParmDecl>(*NewParam);
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001127 if (NewTemplateParm->hasDefaultArgument() &&
1128 DiagnoseDefaultTemplateArgument(*this, TPC,
1129 NewTemplateParm->getLocation(),
1130 NewTemplateParm->getDefaultArgument().getSourceRange()))
1131 NewTemplateParm->setDefaultArgument(TemplateArgumentLoc());
1132
1133 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001134 TemplateTemplateParmDecl *OldTemplateParm
1135 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001136 if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001137 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001138 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1139 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001140 SawDefaultArgument = true;
1141 RedundantDefaultArg = true;
1142 PreviousDefaultArgLoc = NewDefaultLoc;
1143 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1144 // Merge the default argument from the old declaration to the
1145 // new declaration.
1146 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001147 // FIXME: We need to create a new kind of "default argument" expression
1148 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001149 NewTemplateParm->setDefaultArgument(
1150 OldTemplateParm->getDefaultArgument());
Douglas Gregor788cd062009-11-11 01:00:40 +00001151 PreviousDefaultArgLoc
1152 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001153 } else if (NewTemplateParm->hasDefaultArgument()) {
1154 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001155 PreviousDefaultArgLoc
1156 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001157 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001158 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001159 }
1160
1161 if (RedundantDefaultArg) {
1162 // C++ [temp.param]p12:
1163 // A template-parameter shall not be given default arguments
1164 // by two different declarations in the same scope.
1165 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1166 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1167 Invalid = true;
1168 } else if (MissingDefaultArg) {
1169 // C++ [temp.param]p11:
1170 // If a template-parameter has a default template-argument,
1171 // all subsequent template-parameters shall have a default
1172 // template-argument supplied.
Mike Stump1eb44332009-09-09 15:08:12 +00001173 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001174 diag::err_template_param_default_arg_missing);
1175 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1176 Invalid = true;
1177 }
1178
1179 // If we have an old template parameter list that we're merging
1180 // in, move on to the next parameter.
1181 if (OldParams)
1182 ++OldParam;
1183 }
1184
1185 return Invalid;
1186}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001187
Mike Stump1eb44332009-09-09 15:08:12 +00001188/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001189/// specifier, returning the template parameter list that applies to the
1190/// name.
1191///
1192/// \param DeclStartLoc the start of the declaration that has a scope
1193/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001194///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001195/// \param SS the scope specifier that will be matched to the given template
1196/// parameter lists. This scope specifier precedes a qualified name that is
1197/// being declared.
1198///
1199/// \param ParamLists the template parameter lists, from the outermost to the
1200/// innermost template parameter lists.
1201///
1202/// \param NumParamLists the number of template parameter lists in ParamLists.
1203///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001204/// \param IsExplicitSpecialization will be set true if the entity being
1205/// declared is an explicit specialization, false otherwise.
1206///
Mike Stump1eb44332009-09-09 15:08:12 +00001207/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001208/// name that is preceded by the scope specifier @p SS. This template
1209/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001210/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001211/// template specialization), or may be NULL (if we were's declaring isn't
1212/// itself a template).
1213TemplateParameterList *
1214Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1215 const CXXScopeSpec &SS,
1216 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001217 unsigned NumParamLists,
1218 bool &IsExplicitSpecialization) {
1219 IsExplicitSpecialization = false;
1220
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001221 // Find the template-ids that occur within the nested-name-specifier. These
1222 // template-ids will match up with the template parameter lists.
1223 llvm::SmallVector<const TemplateSpecializationType *, 4>
1224 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001225 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1226 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001227 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1228 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001229 const Type *T = NNS->getAsType();
1230 if (!T) break;
1231
1232 // C++0x [temp.expl.spec]p17:
1233 // A member or a member template may be nested within many
1234 // enclosing class templates. In an explicit specialization for
1235 // such a member, the member declaration shall be preceded by a
1236 // template<> for each enclosing class template that is
1237 // explicitly specialized.
1238 // We interpret this as forbidding typedefs of template
1239 // specializations in the scope specifiers of out-of-line decls.
1240 if (const TypedefType *TT = dyn_cast<TypedefType>(T)) {
1241 const Type *UnderlyingT = TT->LookThroughTypedefs().getTypePtr();
1242 if (isa<TemplateSpecializationType>(UnderlyingT))
1243 // FIXME: better source location information.
1244 Diag(DeclStartLoc, diag::err_typedef_in_def_scope) << QualType(T,0);
1245 T = UnderlyingT;
1246 }
1247
Mike Stump1eb44332009-09-09 15:08:12 +00001248 if (const TemplateSpecializationType *SpecType
John McCall4b2b02b2009-12-15 02:19:47 +00001249 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001250 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1251 if (!Template)
1252 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek6217b802009-07-29 21:53:49 +00001254 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001255 ClassTemplateSpecializationDecl *SpecDecl
1256 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1257 // If the nested name specifier refers to an explicit specialization,
1258 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001259 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1260 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001261 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001262 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001265 TemplateIdsInSpecifier.push_back(SpecType);
1266 }
1267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001269 // Reverse the list of template-ids in the scope specifier, so that we can
1270 // more easily match up the template-ids and the template parameter lists.
1271 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001273 SourceLocation FirstTemplateLoc = DeclStartLoc;
1274 if (NumParamLists)
1275 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001277 // Match the template-ids found in the specifier to the template parameter
1278 // lists.
1279 unsigned Idx = 0;
1280 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
1281 Idx != NumTemplateIds; ++Idx) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001282 QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0);
1283 bool DependentTemplateId = TemplateId->isDependentType();
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001284 if (Idx >= NumParamLists) {
1285 // We have a template-id without a corresponding template parameter
1286 // list.
1287 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001288 // FIXME: the location information here isn't great.
1289 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001290 diag::err_template_spec_needs_template_parameters)
Douglas Gregorb88e8882009-07-30 17:40:51 +00001291 << TemplateId
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001292 << SS.getRange();
1293 } else {
1294 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1295 << SS.getRange()
1296 << CodeModificationHint::CreateInsertion(FirstTemplateLoc,
1297 "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001298 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001299 }
1300 return 0;
1301 }
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001303 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001304 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001305 TemplateDecl *Template
Douglas Gregorb88e8882009-07-30 17:40:51 +00001306 = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl();
1307
Mike Stump1eb44332009-09-09 15:08:12 +00001308 if (ClassTemplateDecl *ClassTemplate
Douglas Gregorb88e8882009-07-30 17:40:51 +00001309 = dyn_cast<ClassTemplateDecl>(Template)) {
1310 TemplateParameterList *ExpectedTemplateParams = 0;
1311 // Is this template-id naming the primary template?
1312 if (Context.hasSameType(TemplateId,
1313 ClassTemplate->getInjectedClassNameType(Context)))
1314 ExpectedTemplateParams = ClassTemplate->getTemplateParameters();
1315 // ... or a partial specialization?
1316 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1317 = ClassTemplate->findPartialSpecialization(TemplateId))
1318 ExpectedTemplateParams = PartialSpec->getTemplateParameters();
1319
1320 if (ExpectedTemplateParams)
Mike Stump1eb44332009-09-09 15:08:12 +00001321 TemplateParameterListsAreEqual(ParamLists[Idx],
Douglas Gregorb88e8882009-07-30 17:40:51 +00001322 ExpectedTemplateParams,
Douglas Gregorfb898e12009-11-12 16:20:59 +00001323 true, TPL_TemplateMatch);
Mike Stump1eb44332009-09-09 15:08:12 +00001324 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001325
1326 CheckTemplateParameterList(ParamLists[Idx], 0, TPC_ClassTemplateMember);
Douglas Gregorb88e8882009-07-30 17:40:51 +00001327 } else if (ParamLists[Idx]->size() > 0)
Mike Stump1eb44332009-09-09 15:08:12 +00001328 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001329 diag::err_template_param_list_matches_nontemplate)
1330 << TemplateId
1331 << ParamLists[Idx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001332 else
1333 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001334 }
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001336 // If there were at least as many template-ids as there were template
1337 // parameter lists, then there are no template parameter lists remaining for
1338 // the declaration itself.
1339 if (Idx >= NumParamLists)
1340 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001342 // If there were too many template parameter lists, complain about that now.
1343 if (Idx != NumParamLists - 1) {
1344 while (Idx < NumParamLists - 1) {
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001345 bool isExplicitSpecHeader = ParamLists[Idx]->size() == 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001346 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001347 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1348 : diag::err_template_spec_extra_headers)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001349 << SourceRange(ParamLists[Idx]->getTemplateLoc(),
1350 ParamLists[Idx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001351
1352 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1353 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1354 diag::note_explicit_template_spec_does_not_need_header)
1355 << ExplicitSpecializationsInSpecifier.back();
1356 ExplicitSpecializationsInSpecifier.pop_back();
1357 }
1358
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001359 ++Idx;
1360 }
1361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001363 // Return the last template parameter list, which corresponds to the
1364 // entity being declared.
1365 return ParamLists[NumParamLists - 1];
1366}
1367
Douglas Gregor7532dc62009-03-30 22:58:21 +00001368QualType Sema::CheckTemplateIdType(TemplateName Name,
1369 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001370 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001371 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001372 if (!Template) {
1373 // The template name does not resolve to a template, so we just
1374 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001375 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001376 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001377
Douglas Gregor40808ce2009-03-09 23:48:35 +00001378 // Check that the template argument list is well-formed for this
1379 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00001380 TemplateArgumentListBuilder Converted(Template->getTemplateParameters(),
John McCalld5532b62009-11-23 01:53:49 +00001381 TemplateArgs.size());
1382 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001383 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001384 return QualType();
1385
Mike Stump1eb44332009-09-09 15:08:12 +00001386 assert((Converted.structuredSize() ==
Douglas Gregor7532dc62009-03-30 22:58:21 +00001387 Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001388 "Converted template argument list is too short!");
1389
1390 QualType CanonType;
1391
Douglas Gregorcaddba02009-11-12 18:38:13 +00001392 if (Name.isDependent() ||
1393 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001394 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001395 // This class template specialization is a dependent
1396 // type. Therefore, its canonical type is another class template
1397 // specialization type that contains all of the converted
1398 // arguments in canonical form. This ensures that, e.g., A<T> and
1399 // A<T, T> have identical types when A is declared as:
1400 //
1401 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001402 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001403 CanonType = Context.getTemplateSpecializationType(CanonName,
Anders Carlssonfb250522009-06-23 01:26:57 +00001404 Converted.getFlatArguments(),
1405 Converted.flatSize());
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Douglas Gregor1275ae02009-07-28 23:00:59 +00001407 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001408 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001409 // In the future, we need to teach getTemplateSpecializationType to only
1410 // build the canonical type and return that to us.
1411 CanonType = Context.getCanonicalType(CanonType);
Mike Stump1eb44332009-09-09 15:08:12 +00001412 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001413 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001414 // Find the class template specialization declaration that
1415 // corresponds to these arguments.
1416 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001417 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00001418 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00001419 Converted.flatSize(),
1420 Context);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001421 void *InsertPos = 0;
1422 ClassTemplateSpecializationDecl *Decl
1423 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
1424 if (!Decl) {
1425 // This is the first time we have referenced this class template
1426 // specialization. Create the canonical declaration and add it to
1427 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001428 Decl = ClassTemplateSpecializationDecl::Create(Context,
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001429 ClassTemplate->getDeclContext(),
John McCall9cc78072009-09-11 07:25:08 +00001430 ClassTemplate->getLocation(),
Anders Carlsson1c5976e2009-06-05 03:43:12 +00001431 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00001432 Converted, 0);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001433 ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos);
1434 Decl->setLexicalDeclContext(CurContext);
1435 }
1436
1437 CanonType = Context.getTypeDeclType(Decl);
1438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Douglas Gregor40808ce2009-03-09 23:48:35 +00001440 // Build the fully-sugared type for this class template
1441 // specialization, which refers back to the class template
1442 // specialization we created or found.
John McCalld5532b62009-11-23 01:53:49 +00001443 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001444}
1445
Douglas Gregorcc636682009-02-17 23:15:12 +00001446Action::TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001447Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001448 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001449 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001450 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001451 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001452
Douglas Gregor40808ce2009-03-09 23:48:35 +00001453 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001454 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001455 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001456
John McCalld5532b62009-11-23 01:53:49 +00001457 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001458 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001459
1460 if (Result.isNull())
1461 return true;
1462
John McCalla93c9342009-12-07 02:54:59 +00001463 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result);
John McCall833ca992009-10-29 08:12:44 +00001464 TemplateSpecializationTypeLoc TL
1465 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1466 TL.setTemplateNameLoc(TemplateLoc);
1467 TL.setLAngleLoc(LAngleLoc);
1468 TL.setRAngleLoc(RAngleLoc);
1469 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1470 TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
1471
1472 return CreateLocInfoType(Result, DI).getAsOpaquePtr();
John McCall6b2becf2009-09-08 17:47:29 +00001473}
John McCallf1bbbb42009-09-04 01:14:41 +00001474
John McCall6b2becf2009-09-08 17:47:29 +00001475Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult,
1476 TagUseKind TUK,
1477 DeclSpec::TST TagSpec,
1478 SourceLocation TagLoc) {
1479 if (TypeResult.isInvalid())
1480 return Sema::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001481
John McCall833ca992009-10-29 08:12:44 +00001482 // FIXME: preserve source info, ideally without copying the DI.
John McCalla93c9342009-12-07 02:54:59 +00001483 TypeSourceInfo *DI;
John McCall833ca992009-10-29 08:12:44 +00001484 QualType Type = GetTypeFromParser(TypeResult.get(), &DI);
John McCallf1bbbb42009-09-04 01:14:41 +00001485
John McCall6b2becf2009-09-08 17:47:29 +00001486 // Verify the tag specifier.
1487 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001488
John McCall6b2becf2009-09-08 17:47:29 +00001489 if (const RecordType *RT = Type->getAs<RecordType>()) {
1490 RecordDecl *D = RT->getDecl();
1491
1492 IdentifierInfo *Id = D->getIdentifier();
1493 assert(Id && "templated class must have an identifier");
1494
1495 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1496 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001497 << Type
John McCall6b2becf2009-09-08 17:47:29 +00001498 << CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
1499 D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001500 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001501 }
1502 }
1503
John McCall6b2becf2009-09-08 17:47:29 +00001504 QualType ElabType = Context.getElaboratedType(Type, TagKind);
1505
1506 return ElabType.getAsOpaquePtr();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001507}
1508
John McCallf7a1a742009-11-24 19:00:30 +00001509Sema::OwningExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
1510 LookupResult &R,
1511 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001512 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001513 // FIXME: Can we do any checking at this point? I guess we could check the
1514 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001515 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001516 // though.
John McCallf7a1a742009-11-24 19:00:30 +00001517
1518 // These should be filtered out by our callers.
1519 assert(!R.empty() && "empty lookup results when building templateid");
1520 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1521
1522 NestedNameSpecifier *Qualifier = 0;
1523 SourceRange QualifierRange;
1524 if (SS.isSet()) {
1525 Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
1526 QualifierRange = SS.getRange();
Douglas Gregora9e29aa2009-10-22 07:19:14 +00001527 }
John McCallc373d482010-01-27 01:50:18 +00001528
1529 // We don't want lookup warnings at this point.
1530 R.suppressDiagnostics();
Douglas Gregora9e29aa2009-10-22 07:19:14 +00001531
John McCallf7a1a742009-11-24 19:00:30 +00001532 bool Dependent
1533 = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(),
1534 &TemplateArgs);
1535 UnresolvedLookupExpr *ULE
John McCallc373d482010-01-27 01:50:18 +00001536 = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(),
John McCallf7a1a742009-11-24 19:00:30 +00001537 Qualifier, QualifierRange,
1538 R.getLookupName(), R.getNameLoc(),
1539 RequiresADL, TemplateArgs);
John McCallc373d482010-01-27 01:50:18 +00001540 ULE->addDecls(R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001541
1542 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001543}
1544
John McCallf7a1a742009-11-24 19:00:30 +00001545// We actually only call this from template instantiation.
1546Sema::OwningExprResult
1547Sema::BuildQualifiedTemplateIdExpr(const CXXScopeSpec &SS,
1548 DeclarationName Name,
1549 SourceLocation NameLoc,
1550 const TemplateArgumentListInfo &TemplateArgs) {
1551 DeclContext *DC;
1552 if (!(DC = computeDeclContext(SS, false)) ||
1553 DC->isDependentContext() ||
1554 RequireCompleteDeclContext(SS))
1555 return BuildDependentDeclRefExpr(SS, Name, NameLoc, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001556
John McCallf7a1a742009-11-24 19:00:30 +00001557 LookupResult R(*this, Name, NameLoc, LookupOrdinaryName);
1558 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
John McCallf7a1a742009-11-24 19:00:30 +00001560 if (R.isAmbiguous())
1561 return ExprError();
1562
1563 if (R.empty()) {
1564 Diag(NameLoc, diag::err_template_kw_refers_to_non_template)
1565 << Name << SS.getRange();
1566 return ExprError();
1567 }
1568
1569 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
1570 Diag(NameLoc, diag::err_template_kw_refers_to_class_template)
1571 << (NestedNameSpecifier*) SS.getScopeRep() << Name << SS.getRange();
1572 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1573 return ExprError();
1574 }
1575
1576 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001577}
1578
Douglas Gregorc45c2322009-03-31 00:43:58 +00001579/// \brief Form a dependent template name.
1580///
1581/// This action forms a dependent template name given the template
1582/// name and its (presumably dependent) scope specifier. For
1583/// example, given "MetaFun::template apply", the scope specifier \p
1584/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1585/// of the "template" keyword, and "apply" is the \p Name.
Mike Stump1eb44332009-09-09 15:08:12 +00001586Sema::TemplateTy
Douglas Gregorc45c2322009-03-31 00:43:58 +00001587Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc,
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001588 const CXXScopeSpec &SS,
Douglas Gregor014e88d2009-11-03 23:16:33 +00001589 UnqualifiedId &Name,
Douglas Gregora481edb2009-11-20 23:39:24 +00001590 TypeTy *ObjectType,
1591 bool EnteringContext) {
Douglas Gregor0707bc52010-01-19 16:01:07 +00001592 DeclContext *LookupCtx = 0;
1593 if (SS.isSet())
1594 LookupCtx = computeDeclContext(SS, EnteringContext);
1595 if (!LookupCtx && ObjectType)
1596 LookupCtx = computeDeclContext(QualType::getFromOpaquePtr(ObjectType));
1597 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001598 // C++0x [temp.names]p5:
1599 // If a name prefixed by the keyword template is not the name of
1600 // a template, the program is ill-formed. [Note: the keyword
1601 // template may not be applied to non-template members of class
1602 // templates. -end note ] [ Note: as is the case with the
1603 // typename prefix, the template prefix is allowed in cases
1604 // where it is not strictly necessary; i.e., when the
1605 // nested-name-specifier or the expression on the left of the ->
1606 // or . is not dependent on a template-parameter, or the use
1607 // does not appear in the scope of a template. -end note]
1608 //
1609 // Note: C++03 was more strict here, because it banned the use of
1610 // the "template" keyword prior to a template-name that was not a
1611 // dependent name. C++ DR468 relaxed this requirement (the
1612 // "template" keyword is now permitted). We follow the C++0x
1613 // rules, even in C++03 mode, retroactively applying the DR.
1614 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +00001615 TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType,
Douglas Gregora481edb2009-11-20 23:39:24 +00001616 EnteringContext, Template);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001617 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1618 isa<CXXRecordDecl>(LookupCtx) &&
1619 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001620 // This is a dependent template.
1621 } else if (TNK == TNK_Non_template) {
Douglas Gregor014e88d2009-11-03 23:16:33 +00001622 Diag(Name.getSourceRange().getBegin(),
1623 diag::err_template_kw_refers_to_non_template)
1624 << GetNameFromUnqualifiedId(Name)
1625 << Name.getSourceRange();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001626 return TemplateTy();
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001627 } else {
1628 // We found something; return it.
1629 return Template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001630 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00001631 }
1632
Mike Stump1eb44332009-09-09 15:08:12 +00001633 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001634 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregor014e88d2009-11-03 23:16:33 +00001635
1636 switch (Name.getKind()) {
1637 case UnqualifiedId::IK_Identifier:
1638 return TemplateTy::make(Context.getDependentTemplateName(Qualifier,
1639 Name.Identifier));
1640
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001641 case UnqualifiedId::IK_OperatorFunctionId:
1642 return TemplateTy::make(Context.getDependentTemplateName(Qualifier,
1643 Name.OperatorFunctionId.Operator));
Sean Hunte6252d12009-11-28 08:58:14 +00001644
1645 case UnqualifiedId::IK_LiteralOperatorId:
1646 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
1647
Douglas Gregor014e88d2009-11-03 23:16:33 +00001648 default:
1649 break;
1650 }
1651
1652 Diag(Name.getSourceRange().getBegin(),
1653 diag::err_template_kw_refers_to_non_template)
1654 << GetNameFromUnqualifiedId(Name)
1655 << Name.getSourceRange();
1656 return TemplateTy();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001657}
1658
Mike Stump1eb44332009-09-09 15:08:12 +00001659bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00001660 const TemplateArgumentLoc &AL,
Anders Carlsson436b1562009-06-13 00:33:33 +00001661 TemplateArgumentListBuilder &Converted) {
John McCall833ca992009-10-29 08:12:44 +00001662 const TemplateArgument &Arg = AL.getArgument();
1663
Anders Carlsson436b1562009-06-13 00:33:33 +00001664 // Check template type parameter.
1665 if (Arg.getKind() != TemplateArgument::Type) {
1666 // C++ [temp.arg.type]p1:
1667 // A template-argument for a template-parameter which is a
1668 // type shall be a type-id.
1669
1670 // We have a template type parameter but the template argument
1671 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00001672 SourceRange SR = AL.getSourceRange();
1673 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00001674 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Anders Carlsson436b1562009-06-13 00:33:33 +00001676 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001677 }
Anders Carlsson436b1562009-06-13 00:33:33 +00001678
John McCalla93c9342009-12-07 02:54:59 +00001679 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00001680 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Anders Carlsson436b1562009-06-13 00:33:33 +00001682 // Add the converted template type argument.
Anders Carlssonfb250522009-06-23 01:26:57 +00001683 Converted.Append(
John McCall833ca992009-10-29 08:12:44 +00001684 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00001685 return false;
1686}
1687
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001688/// \brief Substitute template arguments into the default template argument for
1689/// the given template type parameter.
1690///
1691/// \param SemaRef the semantic analysis object for which we are performing
1692/// the substitution.
1693///
1694/// \param Template the template that we are synthesizing template arguments
1695/// for.
1696///
1697/// \param TemplateLoc the location of the template name that started the
1698/// template-id we are checking.
1699///
1700/// \param RAngleLoc the location of the right angle bracket ('>') that
1701/// terminates the template-id.
1702///
1703/// \param Param the template template parameter whose default we are
1704/// substituting into.
1705///
1706/// \param Converted the list of template arguments provided for template
1707/// parameters that precede \p Param in the template parameter list.
1708///
1709/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00001710static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001711SubstDefaultTemplateArgument(Sema &SemaRef,
1712 TemplateDecl *Template,
1713 SourceLocation TemplateLoc,
1714 SourceLocation RAngleLoc,
1715 TemplateTypeParmDecl *Param,
1716 TemplateArgumentListBuilder &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00001717 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001718
1719 // If the argument type is dependent, instantiate it now based
1720 // on the previously-computed template arguments.
1721 if (ArgType->getType()->isDependentType()) {
1722 TemplateArgumentList TemplateArgs(SemaRef.Context, Converted,
1723 /*TakeArgs=*/false);
1724
1725 MultiLevelTemplateArgumentList AllTemplateArgs
1726 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
1727
1728 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
1729 Template, Converted.getFlatArguments(),
1730 Converted.flatSize(),
1731 SourceRange(TemplateLoc, RAngleLoc));
1732
1733 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
1734 Param->getDefaultArgumentLoc(),
1735 Param->getDeclName());
1736 }
1737
1738 return ArgType;
1739}
1740
1741/// \brief Substitute template arguments into the default template argument for
1742/// the given non-type template parameter.
1743///
1744/// \param SemaRef the semantic analysis object for which we are performing
1745/// the substitution.
1746///
1747/// \param Template the template that we are synthesizing template arguments
1748/// for.
1749///
1750/// \param TemplateLoc the location of the template name that started the
1751/// template-id we are checking.
1752///
1753/// \param RAngleLoc the location of the right angle bracket ('>') that
1754/// terminates the template-id.
1755///
Douglas Gregor788cd062009-11-11 01:00:40 +00001756/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001757/// substituting into.
1758///
1759/// \param Converted the list of template arguments provided for template
1760/// parameters that precede \p Param in the template parameter list.
1761///
1762/// \returns the substituted template argument, or NULL if an error occurred.
1763static Sema::OwningExprResult
1764SubstDefaultTemplateArgument(Sema &SemaRef,
1765 TemplateDecl *Template,
1766 SourceLocation TemplateLoc,
1767 SourceLocation RAngleLoc,
1768 NonTypeTemplateParmDecl *Param,
1769 TemplateArgumentListBuilder &Converted) {
1770 TemplateArgumentList TemplateArgs(SemaRef.Context, Converted,
1771 /*TakeArgs=*/false);
1772
1773 MultiLevelTemplateArgumentList AllTemplateArgs
1774 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
1775
1776 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
1777 Template, Converted.getFlatArguments(),
1778 Converted.flatSize(),
1779 SourceRange(TemplateLoc, RAngleLoc));
1780
1781 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
1782}
1783
Douglas Gregor788cd062009-11-11 01:00:40 +00001784/// \brief Substitute template arguments into the default template argument for
1785/// the given template template parameter.
1786///
1787/// \param SemaRef the semantic analysis object for which we are performing
1788/// the substitution.
1789///
1790/// \param Template the template that we are synthesizing template arguments
1791/// for.
1792///
1793/// \param TemplateLoc the location of the template name that started the
1794/// template-id we are checking.
1795///
1796/// \param RAngleLoc the location of the right angle bracket ('>') that
1797/// terminates the template-id.
1798///
1799/// \param Param the template template parameter whose default we are
1800/// substituting into.
1801///
1802/// \param Converted the list of template arguments provided for template
1803/// parameters that precede \p Param in the template parameter list.
1804///
1805/// \returns the substituted template argument, or NULL if an error occurred.
1806static TemplateName
1807SubstDefaultTemplateArgument(Sema &SemaRef,
1808 TemplateDecl *Template,
1809 SourceLocation TemplateLoc,
1810 SourceLocation RAngleLoc,
1811 TemplateTemplateParmDecl *Param,
1812 TemplateArgumentListBuilder &Converted) {
1813 TemplateArgumentList TemplateArgs(SemaRef.Context, Converted,
1814 /*TakeArgs=*/false);
1815
1816 MultiLevelTemplateArgumentList AllTemplateArgs
1817 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
1818
1819 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
1820 Template, Converted.getFlatArguments(),
1821 Converted.flatSize(),
1822 SourceRange(TemplateLoc, RAngleLoc));
1823
1824 return SemaRef.SubstTemplateName(
1825 Param->getDefaultArgument().getArgument().getAsTemplate(),
1826 Param->getDefaultArgument().getTemplateNameLoc(),
1827 AllTemplateArgs);
1828}
1829
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001830/// \brief If the given template parameter has a default template
1831/// argument, substitute into that default template argument and
1832/// return the corresponding template argument.
1833TemplateArgumentLoc
1834Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
1835 SourceLocation TemplateLoc,
1836 SourceLocation RAngleLoc,
1837 Decl *Param,
1838 TemplateArgumentListBuilder &Converted) {
1839 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
1840 if (!TypeParm->hasDefaultArgument())
1841 return TemplateArgumentLoc();
1842
John McCalla93c9342009-12-07 02:54:59 +00001843 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001844 TemplateLoc,
1845 RAngleLoc,
1846 TypeParm,
1847 Converted);
1848 if (DI)
1849 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
1850
1851 return TemplateArgumentLoc();
1852 }
1853
1854 if (NonTypeTemplateParmDecl *NonTypeParm
1855 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1856 if (!NonTypeParm->hasDefaultArgument())
1857 return TemplateArgumentLoc();
1858
1859 OwningExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
1860 TemplateLoc,
1861 RAngleLoc,
1862 NonTypeParm,
1863 Converted);
1864 if (Arg.isInvalid())
1865 return TemplateArgumentLoc();
1866
1867 Expr *ArgE = Arg.takeAs<Expr>();
1868 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
1869 }
1870
1871 TemplateTemplateParmDecl *TempTempParm
1872 = cast<TemplateTemplateParmDecl>(Param);
1873 if (!TempTempParm->hasDefaultArgument())
1874 return TemplateArgumentLoc();
1875
1876 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
1877 TemplateLoc,
1878 RAngleLoc,
1879 TempTempParm,
1880 Converted);
1881 if (TName.isNull())
1882 return TemplateArgumentLoc();
1883
1884 return TemplateArgumentLoc(TemplateArgument(TName),
1885 TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
1886 TempTempParm->getDefaultArgument().getTemplateNameLoc());
1887}
1888
Douglas Gregore7526412009-11-11 19:31:23 +00001889/// \brief Check that the given template argument corresponds to the given
1890/// template parameter.
1891bool Sema::CheckTemplateArgument(NamedDecl *Param,
1892 const TemplateArgumentLoc &Arg,
Douglas Gregore7526412009-11-11 19:31:23 +00001893 TemplateDecl *Template,
1894 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00001895 SourceLocation RAngleLoc,
1896 TemplateArgumentListBuilder &Converted) {
Douglas Gregord9e15302009-11-11 19:41:09 +00001897 // Check template type parameters.
1898 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00001899 return CheckTemplateTypeArgument(TTP, Arg, Converted);
Douglas Gregore7526412009-11-11 19:31:23 +00001900
Douglas Gregord9e15302009-11-11 19:41:09 +00001901 // Check non-type template parameters.
1902 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00001903 // Do substitution on the type of the non-type template parameter
1904 // with the template arguments we've seen thus far.
1905 QualType NTTPType = NTTP->getType();
1906 if (NTTPType->isDependentType()) {
1907 // Do substitution on the type of the non-type template parameter.
1908 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
1909 NTTP, Converted.getFlatArguments(),
1910 Converted.flatSize(),
1911 SourceRange(TemplateLoc, RAngleLoc));
1912
1913 TemplateArgumentList TemplateArgs(Context, Converted,
1914 /*TakeArgs=*/false);
1915 NTTPType = SubstType(NTTPType,
1916 MultiLevelTemplateArgumentList(TemplateArgs),
1917 NTTP->getLocation(),
1918 NTTP->getDeclName());
1919 // If that worked, check the non-type template parameter type
1920 // for validity.
1921 if (!NTTPType.isNull())
1922 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
1923 NTTP->getLocation());
1924 if (NTTPType.isNull())
1925 return true;
1926 }
1927
1928 switch (Arg.getArgument().getKind()) {
1929 case TemplateArgument::Null:
1930 assert(false && "Should never see a NULL template argument here");
1931 return true;
1932
1933 case TemplateArgument::Expression: {
1934 Expr *E = Arg.getArgument().getAsExpr();
1935 TemplateArgument Result;
1936 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
1937 return true;
1938
1939 Converted.Append(Result);
1940 break;
1941 }
1942
1943 case TemplateArgument::Declaration:
1944 case TemplateArgument::Integral:
1945 // We've already checked this template argument, so just copy
1946 // it to the list of converted arguments.
1947 Converted.Append(Arg.getArgument());
1948 break;
1949
1950 case TemplateArgument::Template:
1951 // We were given a template template argument. It may not be ill-formed;
1952 // see below.
1953 if (DependentTemplateName *DTN
1954 = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) {
1955 // We have a template argument such as \c T::template X, which we
1956 // parsed as a template template argument. However, since we now
1957 // know that we need a non-type template argument, convert this
1958 // template name into an expression.
John McCallf7a1a742009-11-24 19:00:30 +00001959 Expr *E = DependentScopeDeclRefExpr::Create(Context,
1960 DTN->getQualifier(),
Douglas Gregore7526412009-11-11 19:31:23 +00001961 Arg.getTemplateQualifierRange(),
John McCallf7a1a742009-11-24 19:00:30 +00001962 DTN->getIdentifier(),
1963 Arg.getTemplateNameLoc());
Douglas Gregore7526412009-11-11 19:31:23 +00001964
1965 TemplateArgument Result;
1966 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
1967 return true;
1968
1969 Converted.Append(Result);
1970 break;
1971 }
1972
1973 // We have a template argument that actually does refer to a class
1974 // template, template alias, or template template parameter, and
1975 // therefore cannot be a non-type template argument.
1976 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
1977 << Arg.getSourceRange();
1978
1979 Diag(Param->getLocation(), diag::note_template_param_here);
1980 return true;
1981
1982 case TemplateArgument::Type: {
1983 // We have a non-type template parameter but the template
1984 // argument is a type.
1985
1986 // C++ [temp.arg]p2:
1987 // In a template-argument, an ambiguity between a type-id and
1988 // an expression is resolved to a type-id, regardless of the
1989 // form of the corresponding template-parameter.
1990 //
1991 // We warn specifically about this case, since it can be rather
1992 // confusing for users.
1993 QualType T = Arg.getArgument().getAsType();
1994 SourceRange SR = Arg.getSourceRange();
1995 if (T->isFunctionType())
1996 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
1997 else
1998 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
1999 Diag(Param->getLocation(), diag::note_template_param_here);
2000 return true;
2001 }
2002
2003 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002004 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002005 break;
2006 }
2007
2008 return false;
2009 }
2010
2011
2012 // Check template template parameters.
2013 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
2014
2015 // Substitute into the template parameter list of the template
2016 // template parameter, since previously-supplied template arguments
2017 // may appear within the template template parameter.
2018 {
2019 // Set up a template instantiation context.
2020 LocalInstantiationScope Scope(*this);
2021 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
2022 TempParm, Converted.getFlatArguments(),
2023 Converted.flatSize(),
2024 SourceRange(TemplateLoc, RAngleLoc));
2025
2026 TemplateArgumentList TemplateArgs(Context, Converted,
2027 /*TakeArgs=*/false);
2028 TempParm = cast_or_null<TemplateTemplateParmDecl>(
2029 SubstDecl(TempParm, CurContext,
2030 MultiLevelTemplateArgumentList(TemplateArgs)));
2031 if (!TempParm)
2032 return true;
2033
2034 // FIXME: TempParam is leaked.
2035 }
2036
2037 switch (Arg.getArgument().getKind()) {
2038 case TemplateArgument::Null:
2039 assert(false && "Should never see a NULL template argument here");
2040 return true;
2041
2042 case TemplateArgument::Template:
2043 if (CheckTemplateArgument(TempParm, Arg))
2044 return true;
2045
2046 Converted.Append(Arg.getArgument());
2047 break;
2048
2049 case TemplateArgument::Expression:
2050 case TemplateArgument::Type:
2051 // We have a template template parameter but the template
2052 // argument does not refer to a template.
2053 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2054 return true;
2055
2056 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002057 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002058 "Declaration argument with template template parameter");
2059 break;
2060 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002061 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002062 "Integral argument with template template parameter");
2063 break;
2064
2065 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002066 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002067 break;
2068 }
2069
2070 return false;
2071}
2072
Douglas Gregorc15cb382009-02-09 23:23:08 +00002073/// \brief Check that the given template argument list is well-formed
2074/// for specializing the given template.
2075bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2076 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002077 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002078 bool PartialTemplateArgs,
Anders Carlsson1c5976e2009-06-05 03:43:12 +00002079 TemplateArgumentListBuilder &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002080 TemplateParameterList *Params = Template->getTemplateParameters();
2081 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002082 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002083 bool Invalid = false;
2084
John McCalld5532b62009-11-23 01:53:49 +00002085 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2086
Mike Stump1eb44332009-09-09 15:08:12 +00002087 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002088 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002090 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002091 (NumArgs < Params->getMinRequiredArguments() &&
2092 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002093 // FIXME: point at either the first arg beyond what we can handle,
2094 // or the '>', depending on whether we have too many or too few
2095 // arguments.
2096 SourceRange Range;
2097 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002098 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002099 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2100 << (NumArgs > NumParams)
2101 << (isa<ClassTemplateDecl>(Template)? 0 :
2102 isa<FunctionTemplateDecl>(Template)? 1 :
2103 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2104 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002105 Diag(Template->getLocation(), diag::note_template_decl_here)
2106 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002107 Invalid = true;
2108 }
Mike Stump1eb44332009-09-09 15:08:12 +00002109
2110 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002111 // [...] The type and form of each template-argument specified in
2112 // a template-id shall match the type and form specified for the
2113 // corresponding parameter declared by the template in its
2114 // template-parameter-list.
2115 unsigned ArgIdx = 0;
2116 for (TemplateParameterList::iterator Param = Params->begin(),
2117 ParamEnd = Params->end();
2118 Param != ParamEnd; ++Param, ++ArgIdx) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002119 if (ArgIdx > NumArgs && PartialTemplateArgs)
2120 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Douglas Gregord9e15302009-11-11 19:41:09 +00002122 // If we have a template parameter pack, check every remaining template
2123 // argument against that template parameter pack.
2124 if ((*Param)->isTemplateParameterPack()) {
2125 Converted.BeginPack();
2126 for (; ArgIdx < NumArgs; ++ArgIdx) {
2127 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2128 TemplateLoc, RAngleLoc, Converted)) {
2129 Invalid = true;
2130 break;
2131 }
2132 }
2133 Converted.EndPack();
2134 continue;
2135 }
2136
Douglas Gregorf35f8282009-11-11 21:54:23 +00002137 if (ArgIdx < NumArgs) {
2138 // Check the template argument we were given.
2139 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2140 TemplateLoc, RAngleLoc, Converted))
2141 return true;
2142
2143 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002144 }
Douglas Gregore7526412009-11-11 19:31:23 +00002145
Douglas Gregorf35f8282009-11-11 21:54:23 +00002146 // We have a default template argument that we will use.
2147 TemplateArgumentLoc Arg;
2148
2149 // Retrieve the default template argument from the template
2150 // parameter. For each kind of template parameter, we substitute the
2151 // template arguments provided thus far and any "outer" template arguments
2152 // (when the template parameter was part of a nested template) into
2153 // the default argument.
2154 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2155 if (!TTP->hasDefaultArgument()) {
2156 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2157 break;
2158 }
2159
John McCalla93c9342009-12-07 02:54:59 +00002160 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002161 Template,
2162 TemplateLoc,
2163 RAngleLoc,
2164 TTP,
2165 Converted);
2166 if (!ArgType)
2167 return true;
2168
2169 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2170 ArgType);
2171 } else if (NonTypeTemplateParmDecl *NTTP
2172 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2173 if (!NTTP->hasDefaultArgument()) {
2174 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2175 break;
2176 }
2177
2178 Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template,
2179 TemplateLoc,
2180 RAngleLoc,
2181 NTTP,
2182 Converted);
2183 if (E.isInvalid())
2184 return true;
2185
2186 Expr *Ex = E.takeAs<Expr>();
2187 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2188 } else {
2189 TemplateTemplateParmDecl *TempParm
2190 = cast<TemplateTemplateParmDecl>(*Param);
2191
2192 if (!TempParm->hasDefaultArgument()) {
2193 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2194 break;
2195 }
2196
2197 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
2198 TemplateLoc,
2199 RAngleLoc,
2200 TempParm,
2201 Converted);
2202 if (Name.isNull())
2203 return true;
2204
2205 Arg = TemplateArgumentLoc(TemplateArgument(Name),
2206 TempParm->getDefaultArgument().getTemplateQualifierRange(),
2207 TempParm->getDefaultArgument().getTemplateNameLoc());
2208 }
2209
2210 // Introduce an instantiation record that describes where we are using
2211 // the default template argument.
2212 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
2213 Converted.getFlatArguments(),
2214 Converted.flatSize(),
2215 SourceRange(TemplateLoc, RAngleLoc));
2216
2217 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002218 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002219 RAngleLoc, Converted))
2220 return true;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002221 }
2222
2223 return Invalid;
2224}
2225
2226/// \brief Check a template argument against its corresponding
2227/// template type parameter.
2228///
2229/// This routine implements the semantics of C++ [temp.arg.type]. It
2230/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002231bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002232 TypeSourceInfo *ArgInfo) {
2233 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002234 QualType Arg = ArgInfo->getType();
2235
Douglas Gregorc15cb382009-02-09 23:23:08 +00002236 // C++ [temp.arg.type]p2:
2237 // A local type, a type with no linkage, an unnamed type or a type
2238 // compounded from any of these types shall not be used as a
2239 // template-argument for a template type-parameter.
2240 //
2241 // FIXME: Perform the recursive and no-linkage type checks.
2242 const TagType *Tag = 0;
John McCall183700f2009-09-21 23:43:11 +00002243 if (const EnumType *EnumT = Arg->getAs<EnumType>())
Douglas Gregorc15cb382009-02-09 23:23:08 +00002244 Tag = EnumT;
Ted Kremenek6217b802009-07-29 21:53:49 +00002245 else if (const RecordType *RecordT = Arg->getAs<RecordType>())
Douglas Gregorc15cb382009-02-09 23:23:08 +00002246 Tag = RecordT;
John McCall833ca992009-10-29 08:12:44 +00002247 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) {
2248 SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange();
2249 return Diag(SR.getBegin(), diag::err_template_arg_local_type)
2250 << QualType(Tag, 0) << SR;
2251 } else if (Tag && !Tag->getDecl()->getDeclName() &&
Douglas Gregor98137532009-03-10 18:33:27 +00002252 !Tag->getDecl()->getTypedefForAnonDecl()) {
John McCall833ca992009-10-29 08:12:44 +00002253 SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange();
2254 Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002255 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
2256 return true;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002257 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
2258 SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange();
2259 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002260 }
2261
2262 return false;
2263}
2264
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002265/// \brief Checks whether the given template argument is the address
2266/// of an object or function according to C++ [temp.arg.nontype]p1.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002267bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg,
2268 NamedDecl *&Entity) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002269 bool Invalid = false;
2270
2271 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002272 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002273 Arg = Cast->getSubExpr();
2274
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002275 // C++0x allows nullptr, and there's no further checking to be done for that.
2276 if (Arg->getType()->isNullPtrType())
2277 return false;
2278
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002279 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002280 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002281 // A template-argument for a non-type, non-template
2282 // template-parameter shall be one of: [...]
2283 //
2284 // -- the address of an object or function with external
2285 // linkage, including function templates and function
2286 // template-ids but excluding non-static class members,
2287 // expressed as & id-expression where the & is optional if
2288 // the name refers to a function or array, or if the
2289 // corresponding template-parameter is a reference; or
2290 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002291
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002292 // Ignore (and complain about) any excess parentheses.
2293 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
2294 if (!Invalid) {
Mike Stump1eb44332009-09-09 15:08:12 +00002295 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002296 diag::err_template_arg_extra_parens)
2297 << Arg->getSourceRange();
2298 Invalid = true;
2299 }
2300
2301 Arg = Parens->getSubExpr();
2302 }
2303
2304 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
2305 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
2306 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
2307 } else
2308 DRE = dyn_cast<DeclRefExpr>(Arg);
2309
Chandler Carruth038cc392010-01-31 10:01:20 +00002310 if (!DRE)
2311 return Diag(Arg->getSourceRange().getBegin(),
2312 diag::err_template_arg_not_decl_ref)
2313 << Arg->getSourceRange();
2314
2315 // Stop checking the precise nature of the argument if it is value dependent,
2316 // it should be checked when instantiated.
2317 if (Arg->isValueDependent())
2318 return false;
2319
2320 if (!isa<ValueDecl>(DRE->getDecl()))
Mike Stump1eb44332009-09-09 15:08:12 +00002321 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002322 diag::err_template_arg_not_object_or_func_form)
2323 << Arg->getSourceRange();
2324
2325 // Cannot refer to non-static data members
2326 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl()))
2327 return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
2328 << Field << Arg->getSourceRange();
2329
2330 // Cannot refer to non-static member functions
2331 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
2332 if (!Method->isStatic())
Mike Stump1eb44332009-09-09 15:08:12 +00002333 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002334 diag::err_template_arg_method)
2335 << Method << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002337 // Functions must have external linkage.
2338 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregord85b5b92009-11-25 22:24:25 +00002339 if (Func->getLinkage() != NamedDecl::ExternalLinkage) {
Mike Stump1eb44332009-09-09 15:08:12 +00002340 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002341 diag::err_template_arg_function_not_extern)
2342 << Func << Arg->getSourceRange();
2343 Diag(Func->getLocation(), diag::note_template_arg_internal_object)
2344 << true;
2345 return true;
2346 }
2347
2348 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002349 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002350 return Invalid;
2351 }
2352
2353 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregord85b5b92009-11-25 22:24:25 +00002354 if (Var->getLinkage() != NamedDecl::ExternalLinkage) {
Mike Stump1eb44332009-09-09 15:08:12 +00002355 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002356 diag::err_template_arg_object_not_extern)
2357 << Var << Arg->getSourceRange();
2358 Diag(Var->getLocation(), diag::note_template_arg_internal_object)
2359 << true;
2360 return true;
2361 }
2362
2363 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002364 Entity = Var;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002365 return Invalid;
2366 }
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002368 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00002369 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002370 diag::err_template_arg_not_object_or_func)
2371 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002372 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002373 diag::note_template_arg_refers_here);
2374 return true;
2375}
2376
2377/// \brief Checks whether the given template argument is a pointer to
2378/// member constant according to C++ [temp.arg.nontype]p1.
Douglas Gregorcaddba02009-11-12 18:38:13 +00002379bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
2380 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002381 bool Invalid = false;
2382
2383 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002384 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002385 Arg = Cast->getSubExpr();
2386
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002387 // C++0x allows nullptr, and there's no further checking to be done for that.
2388 if (Arg->getType()->isNullPtrType())
2389 return false;
2390
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002391 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002392 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002393 // A template-argument for a non-type, non-template
2394 // template-parameter shall be one of: [...]
2395 //
2396 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00002397 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002398
2399 // Ignore (and complain about) any excess parentheses.
2400 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
2401 if (!Invalid) {
Mike Stump1eb44332009-09-09 15:08:12 +00002402 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002403 diag::err_template_arg_extra_parens)
2404 << Arg->getSourceRange();
2405 Invalid = true;
2406 }
2407
2408 Arg = Parens->getSubExpr();
2409 }
2410
Douglas Gregorcaddba02009-11-12 18:38:13 +00002411 // A pointer-to-member constant written &Class::member.
2412 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00002413 if (UnOp->getOpcode() == UnaryOperator::AddrOf) {
2414 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
2415 if (DRE && !DRE->getQualifier())
2416 DRE = 0;
2417 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00002418 }
2419 // A constant of pointer-to-member type.
2420 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
2421 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
2422 if (VD->getType()->isMemberPointerType()) {
2423 if (isa<NonTypeTemplateParmDecl>(VD) ||
2424 (isa<VarDecl>(VD) &&
2425 Context.getCanonicalType(VD->getType()).isConstQualified())) {
2426 if (Arg->isTypeDependent() || Arg->isValueDependent())
2427 Converted = TemplateArgument(Arg->Retain());
2428 else
2429 Converted = TemplateArgument(VD->getCanonicalDecl());
2430 return Invalid;
2431 }
2432 }
2433 }
2434
2435 DRE = 0;
2436 }
2437
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002438 if (!DRE)
2439 return Diag(Arg->getSourceRange().getBegin(),
2440 diag::err_template_arg_not_pointer_to_member_form)
2441 << Arg->getSourceRange();
2442
2443 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
2444 assert((isa<FieldDecl>(DRE->getDecl()) ||
2445 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
2446 "Only non-static member pointers can make it here");
2447
2448 // Okay: this is the address of a non-static member, and therefore
2449 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00002450 if (Arg->isTypeDependent() || Arg->isValueDependent())
2451 Converted = TemplateArgument(Arg->Retain());
2452 else
2453 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002454 return Invalid;
2455 }
2456
2457 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00002458 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002459 diag::err_template_arg_not_pointer_to_member_form)
2460 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002461 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002462 diag::note_template_arg_refers_here);
2463 return true;
2464}
2465
Douglas Gregorc15cb382009-02-09 23:23:08 +00002466/// \brief Check a template argument against its corresponding
2467/// non-type template parameter.
2468///
Douglas Gregor2943aed2009-03-03 04:44:36 +00002469/// This routine implements the semantics of C++ [temp.arg.nontype].
2470/// It returns true if an error occurred, and false otherwise. \p
2471/// InstantiatedParamType is the type of the non-type template
2472/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002473///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002474/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00002475bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00002476 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002477 TemplateArgument &Converted) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002478 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
2479
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002480 // If either the parameter has a dependent type or the argument is
2481 // type-dependent, there's nothing we can check now.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002482 // FIXME: Add template argument to Converted!
Douglas Gregor40808ce2009-03-09 23:48:35 +00002483 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
2484 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002485 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002486 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00002487 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002488
2489 // C++ [temp.arg.nontype]p5:
2490 // The following conversions are performed on each expression used
2491 // as a non-type template-argument. If a non-type
2492 // template-argument cannot be converted to the type of the
2493 // corresponding template-parameter then the program is
2494 // ill-formed.
2495 //
2496 // -- for a non-type template-parameter of integral or
2497 // enumeration type, integral promotions (4.5) and integral
2498 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00002499 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00002500 QualType ArgType = Arg->getType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002501 if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002502 // C++ [temp.arg.nontype]p1:
2503 // A template-argument for a non-type, non-template
2504 // template-parameter shall be one of:
2505 //
2506 // -- an integral constant-expression of integral or enumeration
2507 // type; or
2508 // -- the name of a non-type template-parameter; or
2509 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002510 llvm::APSInt Value;
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002511 if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002512 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002513 diag::err_template_arg_not_integral_or_enumeral)
2514 << ArgType << Arg->getSourceRange();
2515 Diag(Param->getLocation(), diag::note_template_param_here);
2516 return true;
2517 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002518 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002519 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
2520 << ArgType << Arg->getSourceRange();
2521 return true;
2522 }
2523
2524 // FIXME: We need some way to more easily get the unqualified form
2525 // of the types without going all the way to the
2526 // canonical type.
2527 if (Context.getCanonicalType(ParamType).getCVRQualifiers())
2528 ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType();
2529 if (Context.getCanonicalType(ArgType).getCVRQualifiers())
2530 ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType();
2531
2532 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00002533 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002534 // Okay: no conversion necessary
2535 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
2536 !ParamType->isEnumeralType()) {
2537 // This is an integral promotion or conversion.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002538 ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002539 } else {
2540 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002541 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002542 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002543 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002544 Diag(Param->getLocation(), diag::note_template_param_here);
2545 return true;
2546 }
2547
Douglas Gregorf80a9d52009-03-14 00:20:21 +00002548 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00002549 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002550 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00002551
2552 if (!Arg->isValueDependent()) {
2553 // Check that an unsigned parameter does not receive a negative
2554 // value.
2555 if (IntegerType->isUnsignedIntegerType()
2556 && (Value.isSigned() && Value.isNegative())) {
2557 Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative)
2558 << Value.toString(10) << Param->getType()
2559 << Arg->getSourceRange();
2560 Diag(Param->getLocation(), diag::note_template_param_here);
2561 return true;
2562 }
2563
2564 // Check that we don't overflow the template parameter type.
2565 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Eli Friedman29f89f62009-12-23 18:44:58 +00002566 unsigned RequiredBits;
2567 if (IntegerType->isUnsignedIntegerType())
2568 RequiredBits = Value.getActiveBits();
2569 else if (Value.isUnsigned())
2570 RequiredBits = Value.getActiveBits() + 1;
2571 else
2572 RequiredBits = Value.getMinSignedBits();
2573 if (RequiredBits > AllowedBits) {
Mike Stump1eb44332009-09-09 15:08:12 +00002574 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorf80a9d52009-03-14 00:20:21 +00002575 diag::err_template_arg_too_large)
2576 << Value.toString(10) << Param->getType()
2577 << Arg->getSourceRange();
2578 Diag(Param->getLocation(), diag::note_template_param_here);
2579 return true;
2580 }
2581
2582 if (Value.getBitWidth() != AllowedBits)
2583 Value.extOrTrunc(AllowedBits);
2584 Value.setIsSigned(IntegerType->isSignedIntegerType());
2585 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002586
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002587 // Add the value of this argument to the list of converted
2588 // arguments. We use the bitwidth and signedness of the template
2589 // parameter.
2590 if (Arg->isValueDependent()) {
2591 // The argument is value-dependent. Create a new
2592 // TemplateArgument with the converted expression.
2593 Converted = TemplateArgument(Arg);
2594 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002595 }
2596
John McCall833ca992009-10-29 08:12:44 +00002597 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00002598 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00002599 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00002600 return false;
2601 }
Douglas Gregora35284b2009-02-11 00:19:33 +00002602
Douglas Gregorb86b0572009-02-11 01:18:59 +00002603 // Handle pointer-to-function, reference-to-function, and
2604 // pointer-to-member-function all in (roughly) the same way.
2605 if (// -- For a non-type template-parameter of type pointer to
2606 // function, only the function-to-pointer conversion (4.3) is
2607 // applied. If the template-argument represents a set of
2608 // overloaded functions (or a pointer to such), the matching
2609 // function is selected from the set (13.4).
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002610 // In C++0x, any std::nullptr_t value can be converted.
Douglas Gregorb86b0572009-02-11 01:18:59 +00002611 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00002612 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00002613 // -- For a non-type template-parameter of type reference to
2614 // function, no conversions apply. If the template-argument
2615 // represents a set of overloaded functions, the matching
2616 // function is selected from the set (13.4).
2617 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00002618 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00002619 // -- For a non-type template-parameter of type pointer to
2620 // member function, no conversions apply. If the
2621 // template-argument represents a set of overloaded member
2622 // functions, the matching member function is selected from
2623 // the set (13.4).
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002624 // Again, C++0x allows a std::nullptr_t value.
Douglas Gregorb86b0572009-02-11 01:18:59 +00002625 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00002626 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00002627 ->isFunctionType())) {
Mike Stump1eb44332009-09-09 15:08:12 +00002628 if (Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002629 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00002630 // We don't have to do anything: the types already match.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002631 } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() ||
2632 ParamType->isMemberPointerType())) {
2633 ArgType = ParamType;
Eli Friedman73c39ab2009-10-20 08:27:19 +00002634 if (ParamType->isMemberPointerType())
2635 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer);
2636 else
2637 ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast);
Douglas Gregorb86b0572009-02-11 01:18:59 +00002638 } else if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregora35284b2009-02-11 00:19:33 +00002639 ArgType = Context.getPointerType(ArgType);
Eli Friedman73c39ab2009-10-20 08:27:19 +00002640 ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay);
Mike Stump1eb44332009-09-09 15:08:12 +00002641 } else if (FunctionDecl *Fn
Douglas Gregora35284b2009-02-11 00:19:33 +00002642 = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
Douglas Gregor48f3bb92009-02-18 21:56:37 +00002643 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
2644 return true;
2645
Anders Carlsson96ad5332009-10-21 17:16:23 +00002646 Arg = FixOverloadedFunctionReference(Arg, Fn);
Douglas Gregora35284b2009-02-11 00:19:33 +00002647 ArgType = Arg->getType();
Douglas Gregorb86b0572009-02-11 01:18:59 +00002648 if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregora35284b2009-02-11 00:19:33 +00002649 ArgType = Context.getPointerType(Arg->getType());
Eli Friedman73c39ab2009-10-20 08:27:19 +00002650 ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay);
Douglas Gregora35284b2009-02-11 00:19:33 +00002651 }
2652 }
2653
Mike Stump1eb44332009-09-09 15:08:12 +00002654 if (!Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002655 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00002656 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002657 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00002658 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002659 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00002660 Diag(Param->getLocation(), diag::note_template_param_here);
2661 return true;
2662 }
Mike Stump1eb44332009-09-09 15:08:12 +00002663
Douglas Gregorcaddba02009-11-12 18:38:13 +00002664 if (ParamType->isMemberPointerType())
2665 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Mike Stump1eb44332009-09-09 15:08:12 +00002666
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002667 NamedDecl *Entity = 0;
2668 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2669 return true;
2670
Chandler Carruth038cc392010-01-31 10:01:20 +00002671 if (Arg->isValueDependent()) {
2672 Converted = TemplateArgument(Arg);
2673 } else {
2674 if (Entity)
2675 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
2676 Converted = TemplateArgument(Entity);
2677 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002678 return false;
Douglas Gregora35284b2009-02-11 00:19:33 +00002679 }
2680
Chris Lattnerfe90de72009-02-20 21:37:53 +00002681 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002682 // -- for a non-type template-parameter of type pointer to
2683 // object, qualification conversions (4.4) and the
2684 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002685 // C++0x also allows a value of std::nullptr_t.
Ted Kremenek6217b802009-07-29 21:53:49 +00002686 assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00002687 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002688
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002689 if (ArgType->isNullPtrType()) {
2690 ArgType = ParamType;
Eli Friedman73c39ab2009-10-20 08:27:19 +00002691 ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast);
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002692 } else if (ArgType->isArrayType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002693 ArgType = Context.getArrayDecayedType(ArgType);
Eli Friedman73c39ab2009-10-20 08:27:19 +00002694 ImpCastExprToType(Arg, ArgType, CastExpr::CK_ArrayToPointerDecay);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002695 }
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002696
Douglas Gregorb86b0572009-02-11 01:18:59 +00002697 if (IsQualificationConversion(ArgType, ParamType)) {
2698 ArgType = ParamType;
Eli Friedman73c39ab2009-10-20 08:27:19 +00002699 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp);
Douglas Gregorb86b0572009-02-11 01:18:59 +00002700 }
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002702 if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002703 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002704 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb86b0572009-02-11 01:18:59 +00002705 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002706 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregorb86b0572009-02-11 01:18:59 +00002707 Diag(Param->getLocation(), diag::note_template_param_here);
2708 return true;
2709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002711 NamedDecl *Entity = 0;
2712 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2713 return true;
2714
Chandler Carruth038cc392010-01-31 10:01:20 +00002715 if (Arg->isValueDependent()) {
2716 Converted = TemplateArgument(Arg);
2717 } else {
2718 if (Entity)
2719 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
2720 Converted = TemplateArgument(Entity);
2721 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002722 return false;
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002723 }
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Ted Kremenek6217b802009-07-29 21:53:49 +00002725 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00002726 // -- For a non-type template-parameter of type reference to
2727 // object, no conversions apply. The type referred to by the
2728 // reference may be more cv-qualified than the (otherwise
2729 // identical) type of the template-argument. The
2730 // template-parameter is bound directly to the
2731 // template-argument, which must be an lvalue.
Douglas Gregorbad0e652009-03-24 20:32:41 +00002732 assert(ParamRefType->getPointeeType()->isObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00002733 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00002734
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002735 if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002736 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb86b0572009-02-11 01:18:59 +00002737 diag::err_template_arg_no_ref_bind)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002738 << InstantiatedParamType << Arg->getType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00002739 << Arg->getSourceRange();
2740 Diag(Param->getLocation(), diag::note_template_param_here);
2741 return true;
2742 }
2743
Mike Stump1eb44332009-09-09 15:08:12 +00002744 unsigned ParamQuals
Douglas Gregorb86b0572009-02-11 01:18:59 +00002745 = Context.getCanonicalType(ParamType).getCVRQualifiers();
2746 unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers();
Mike Stump1eb44332009-09-09 15:08:12 +00002747
Douglas Gregorb86b0572009-02-11 01:18:59 +00002748 if ((ParamQuals | ArgQuals) != ParamQuals) {
2749 Diag(Arg->getSourceRange().getBegin(),
2750 diag::err_template_arg_ref_bind_ignores_quals)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002751 << InstantiatedParamType << Arg->getType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00002752 << Arg->getSourceRange();
2753 Diag(Param->getLocation(), diag::note_template_param_here);
2754 return true;
2755 }
Mike Stump1eb44332009-09-09 15:08:12 +00002756
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002757 NamedDecl *Entity = 0;
2758 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2759 return true;
2760
Chandler Carruth038cc392010-01-31 10:01:20 +00002761 if (Arg->isValueDependent()) {
2762 Converted = TemplateArgument(Arg);
2763 } else {
2764 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
2765 Converted = TemplateArgument(Entity);
2766 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002767 return false;
Douglas Gregorb86b0572009-02-11 01:18:59 +00002768 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00002769
2770 // -- For a non-type template-parameter of type pointer to data
2771 // member, qualification conversions (4.4) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002772 // C++0x allows std::nullptr_t values.
Douglas Gregor658bbb52009-02-11 16:16:59 +00002773 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
2774
Douglas Gregor8e6563b2009-02-11 18:22:40 +00002775 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00002776 // Types match exactly: nothing more to do here.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002777 } else if (ArgType->isNullPtrType()) {
Eli Friedman73c39ab2009-10-20 08:27:19 +00002778 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer);
Douglas Gregor658bbb52009-02-11 16:16:59 +00002779 } else if (IsQualificationConversion(ArgType, ParamType)) {
Eli Friedman73c39ab2009-10-20 08:27:19 +00002780 ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp);
Douglas Gregor658bbb52009-02-11 16:16:59 +00002781 } else {
2782 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00002783 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00002784 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00002785 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00002786 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002787 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00002788 }
2789
Douglas Gregorcaddba02009-11-12 18:38:13 +00002790 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002791}
2792
2793/// \brief Check a template argument against its corresponding
2794/// template template parameter.
2795///
2796/// This routine implements the semantics of C++ [temp.arg.template].
2797/// It returns true if an error occurred, and false otherwise.
2798bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00002799 const TemplateArgumentLoc &Arg) {
2800 TemplateName Name = Arg.getArgument().getAsTemplate();
2801 TemplateDecl *Template = Name.getAsTemplateDecl();
2802 if (!Template) {
2803 // Any dependent template name is fine.
2804 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
2805 return false;
2806 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00002807
2808 // C++ [temp.arg.template]p1:
2809 // A template-argument for a template template-parameter shall be
2810 // the name of a class template, expressed as id-expression. Only
2811 // primary class templates are considered when matching the
2812 // template template argument with the corresponding parameter;
2813 // partial specializations are not considered even if their
2814 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002815 //
2816 // Note that we also allow template template parameters here, which
2817 // will happen when we are dealing with, e.g., class template
2818 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002819 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00002820 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002821 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00002822 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00002823 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00002824 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00002825 << Template;
2826 }
2827
2828 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
2829 Param->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +00002830 true,
2831 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00002832 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00002833}
2834
Douglas Gregorddc29e12009-02-06 22:42:48 +00002835/// \brief Determine whether the given template parameter lists are
2836/// equivalent.
2837///
Mike Stump1eb44332009-09-09 15:08:12 +00002838/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00002839/// source code as part of a new template declaration.
2840///
2841/// \param Old The old template parameter list, typically found via
2842/// name lookup of the template declared with this template parameter
2843/// list.
2844///
2845/// \param Complain If true, this routine will produce a diagnostic if
2846/// the template parameter lists are not equivalent.
2847///
Douglas Gregorfb898e12009-11-12 16:20:59 +00002848/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00002849///
2850/// \param TemplateArgLoc If this source location is valid, then we
2851/// are actually checking the template parameter list of a template
2852/// argument (New) against the template parameter list of its
2853/// corresponding template template parameter (Old). We produce
2854/// slightly different diagnostics in this scenario.
2855///
Douglas Gregorddc29e12009-02-06 22:42:48 +00002856/// \returns True if the template parameter lists are equal, false
2857/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002858bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00002859Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
2860 TemplateParameterList *Old,
2861 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00002862 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002863 SourceLocation TemplateArgLoc) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00002864 if (Old->size() != New->size()) {
2865 if (Complain) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002866 unsigned NextDiag = diag::err_template_param_list_different_arity;
2867 if (TemplateArgLoc.isValid()) {
2868 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2869 NextDiag = diag::note_template_param_list_different_arity;
Mike Stump1eb44332009-09-09 15:08:12 +00002870 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00002871 Diag(New->getTemplateLoc(), NextDiag)
2872 << (New->size() > Old->size())
Douglas Gregorfb898e12009-11-12 16:20:59 +00002873 << (Kind != TPL_TemplateMatch)
Douglas Gregordd0574e2009-02-10 00:24:35 +00002874 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
Douglas Gregorddc29e12009-02-06 22:42:48 +00002875 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
Douglas Gregorfb898e12009-11-12 16:20:59 +00002876 << (Kind != TPL_TemplateMatch)
Douglas Gregorddc29e12009-02-06 22:42:48 +00002877 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
2878 }
2879
2880 return false;
2881 }
2882
2883 for (TemplateParameterList::iterator OldParm = Old->begin(),
2884 OldParmEnd = Old->end(), NewParm = New->begin();
2885 OldParm != OldParmEnd; ++OldParm, ++NewParm) {
2886 if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
Douglas Gregor34d1dc92009-06-24 16:50:40 +00002887 if (Complain) {
2888 unsigned NextDiag = diag::err_template_param_different_kind;
2889 if (TemplateArgLoc.isValid()) {
2890 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2891 NextDiag = diag::note_template_param_different_kind;
2892 }
2893 Diag((*NewParm)->getLocation(), NextDiag)
Douglas Gregorfb898e12009-11-12 16:20:59 +00002894 << (Kind != TPL_TemplateMatch);
Douglas Gregor34d1dc92009-06-24 16:50:40 +00002895 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
Douglas Gregorfb898e12009-11-12 16:20:59 +00002896 << (Kind != TPL_TemplateMatch);
Douglas Gregordd0574e2009-02-10 00:24:35 +00002897 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00002898 return false;
2899 }
2900
2901 if (isa<TemplateTypeParmDecl>(*OldParm)) {
2902 // Okay; all template type parameters are equivalent (since we
Douglas Gregordd0574e2009-02-10 00:24:35 +00002903 // know we're at the same index).
Mike Stump1eb44332009-09-09 15:08:12 +00002904 } else if (NonTypeTemplateParmDecl *OldNTTP
Douglas Gregorddc29e12009-02-06 22:42:48 +00002905 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
2906 // The types of non-type template parameters must agree.
2907 NonTypeTemplateParmDecl *NewNTTP
2908 = cast<NonTypeTemplateParmDecl>(*NewParm);
Douglas Gregorfb898e12009-11-12 16:20:59 +00002909
2910 // If we are matching a template template argument to a template
2911 // template parameter and one of the non-type template parameter types
2912 // is dependent, then we must wait until template instantiation time
2913 // to actually compare the arguments.
2914 if (Kind == TPL_TemplateTemplateArgumentMatch &&
2915 (OldNTTP->getType()->isDependentType() ||
2916 NewNTTP->getType()->isDependentType()))
2917 continue;
2918
Douglas Gregorddc29e12009-02-06 22:42:48 +00002919 if (Context.getCanonicalType(OldNTTP->getType()) !=
2920 Context.getCanonicalType(NewNTTP->getType())) {
2921 if (Complain) {
Douglas Gregordd0574e2009-02-10 00:24:35 +00002922 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
2923 if (TemplateArgLoc.isValid()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002924 Diag(TemplateArgLoc,
Douglas Gregordd0574e2009-02-10 00:24:35 +00002925 diag::err_template_arg_template_params_mismatch);
2926 NextDiag = diag::note_template_nontype_parm_different_type;
2927 }
2928 Diag(NewNTTP->getLocation(), NextDiag)
Douglas Gregorddc29e12009-02-06 22:42:48 +00002929 << NewNTTP->getType()
Douglas Gregorfb898e12009-11-12 16:20:59 +00002930 << (Kind != TPL_TemplateMatch);
Mike Stump1eb44332009-09-09 15:08:12 +00002931 Diag(OldNTTP->getLocation(),
Douglas Gregorddc29e12009-02-06 22:42:48 +00002932 diag::note_template_nontype_parm_prev_declaration)
2933 << OldNTTP->getType();
2934 }
2935 return false;
2936 }
2937 } else {
2938 // The template parameter lists of template template
2939 // parameters must agree.
Mike Stump1eb44332009-09-09 15:08:12 +00002940 assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
Douglas Gregorddc29e12009-02-06 22:42:48 +00002941 "Only template template parameters handled here");
Mike Stump1eb44332009-09-09 15:08:12 +00002942 TemplateTemplateParmDecl *OldTTP
Douglas Gregorddc29e12009-02-06 22:42:48 +00002943 = cast<TemplateTemplateParmDecl>(*OldParm);
2944 TemplateTemplateParmDecl *NewTTP
2945 = cast<TemplateTemplateParmDecl>(*NewParm);
2946 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
2947 OldTTP->getTemplateParameters(),
2948 Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00002949 (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind),
Douglas Gregordd0574e2009-02-10 00:24:35 +00002950 TemplateArgLoc))
Douglas Gregorddc29e12009-02-06 22:42:48 +00002951 return false;
2952 }
2953 }
2954
2955 return true;
2956}
2957
2958/// \brief Check whether a template can be declared within this scope.
2959///
2960/// If the template declaration is valid in this scope, returns
2961/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00002962bool
Douglas Gregor05396e22009-08-25 17:23:04 +00002963Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00002964 // Find the nearest enclosing declaration scope.
2965 while ((S->getFlags() & Scope::DeclScope) == 0 ||
2966 (S->getFlags() & Scope::TemplateParamScope) != 0)
2967 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00002968
Douglas Gregorddc29e12009-02-06 22:42:48 +00002969 // C++ [temp]p2:
2970 // A template-declaration can appear only as a namespace scope or
2971 // class scope declaration.
2972 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00002973 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
2974 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00002975 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00002976 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Eli Friedman1503f772009-07-31 01:43:05 +00002978 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00002979 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00002980
2981 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
2982 return false;
2983
Mike Stump1eb44332009-09-09 15:08:12 +00002984 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00002985 diag::err_template_outside_namespace_or_class_scope)
2986 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00002987}
Douglas Gregorcc636682009-02-17 23:15:12 +00002988
Douglas Gregord5cb8762009-10-07 00:13:32 +00002989/// \brief Determine what kind of template specialization the given declaration
2990/// is.
2991static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
2992 if (!D)
2993 return TSK_Undeclared;
2994
Douglas Gregorf6b11852009-10-08 15:14:33 +00002995 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
2996 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00002997 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
2998 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002999 if (VarDecl *Var = dyn_cast<VarDecl>(D))
3000 return Var->getTemplateSpecializationKind();
3001
Douglas Gregord5cb8762009-10-07 00:13:32 +00003002 return TSK_Undeclared;
3003}
3004
Douglas Gregor9302da62009-10-14 23:50:59 +00003005/// \brief Check whether a specialization is well-formed in the current
3006/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00003007///
Douglas Gregor9302da62009-10-14 23:50:59 +00003008/// This routine determines whether a template specialization can be declared
3009/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00003010///
3011/// \param S the semantic analysis object for which this check is being
3012/// performed.
3013///
3014/// \param Specialized the entity being specialized or instantiated, which
3015/// may be a kind of template (class template, function template, etc.) or
3016/// a member of a class template (member function, static data member,
3017/// member class).
3018///
3019/// \param PrevDecl the previous declaration of this entity, if any.
3020///
3021/// \param Loc the location of the explicit specialization or instantiation of
3022/// this entity.
3023///
3024/// \param IsPartialSpecialization whether this is a partial specialization of
3025/// a class template.
3026///
Douglas Gregord5cb8762009-10-07 00:13:32 +00003027/// \returns true if there was an error that we cannot recover from, false
3028/// otherwise.
3029static bool CheckTemplateSpecializationScope(Sema &S,
3030 NamedDecl *Specialized,
3031 NamedDecl *PrevDecl,
3032 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00003033 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00003034 // Keep these "kind" numbers in sync with the %select statements in the
3035 // various diagnostics emitted by this routine.
3036 int EntityKind = 0;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003037 bool isTemplateSpecialization = false;
3038 if (isa<ClassTemplateDecl>(Specialized)) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00003039 EntityKind = IsPartialSpecialization? 1 : 0;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003040 isTemplateSpecialization = true;
3041 } else if (isa<FunctionTemplateDecl>(Specialized)) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00003042 EntityKind = 2;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003043 isTemplateSpecialization = true;
3044 } else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00003045 EntityKind = 3;
3046 else if (isa<VarDecl>(Specialized))
3047 EntityKind = 4;
3048 else if (isa<RecordDecl>(Specialized))
3049 EntityKind = 5;
3050 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00003051 S.Diag(Loc, diag::err_template_spec_unknown_kind);
3052 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00003053 return true;
3054 }
3055
Douglas Gregor88b70942009-02-25 22:02:03 +00003056 // C++ [temp.expl.spec]p2:
3057 // An explicit specialization shall be declared in the namespace
3058 // of which the template is a member, or, for member templates, in
3059 // the namespace of which the enclosing class or enclosing class
3060 // template is a member. An explicit specialization of a member
3061 // function, member class or static data member of a class
3062 // template shall be declared in the namespace of which the class
3063 // template is a member. Such a declaration may also be a
3064 // definition. If the declaration is not a definition, the
3065 // specialization may be defined later in the name- space in which
3066 // the explicit specialization was declared, or in a namespace
3067 // that encloses the one in which the explicit specialization was
3068 // declared.
Douglas Gregord5cb8762009-10-07 00:13:32 +00003069 if (S.CurContext->getLookupContext()->isFunctionOrMethod()) {
3070 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00003071 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00003072 return true;
3073 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00003074
Douglas Gregor0a407472009-10-07 17:30:37 +00003075 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
3076 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00003077 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00003078 return true;
3079 }
3080
Douglas Gregor7974c3b2009-10-07 17:21:34 +00003081 // C++ [temp.class.spec]p6:
3082 // A class template partial specialization may be declared or redeclared
3083 // in any namespace scope in which its definition may be defined (14.5.1
3084 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00003085 bool ComplainedAboutScope = false;
Douglas Gregor7974c3b2009-10-07 17:21:34 +00003086 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00003087 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00003088 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
Douglas Gregor9302da62009-10-14 23:50:59 +00003089 if ((!PrevDecl ||
3090 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
3091 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
3092 // There is no prior declaration of this entity, so this
3093 // specialization must be in the same context as the template
3094 // itself.
3095 if (!DC->Equals(SpecializedContext)) {
3096 if (isa<TranslationUnitDecl>(SpecializedContext))
3097 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
3098 << EntityKind << Specialized;
3099 else if (isa<NamespaceDecl>(SpecializedContext))
3100 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope)
3101 << EntityKind << Specialized
3102 << cast<NamedDecl>(SpecializedContext);
3103
3104 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
3105 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00003106 }
Douglas Gregor88b70942009-02-25 22:02:03 +00003107 }
Douglas Gregord5cb8762009-10-07 00:13:32 +00003108
3109 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00003110 // namespace.
Douglas Gregord5cb8762009-10-07 00:13:32 +00003111 // Note that HandleDeclarator() performs this check for explicit
3112 // specializations of function templates, static data members, and member
3113 // functions, so we skip the check here for those kinds of entities.
3114 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00003115 // Should we refactor that check, so that it occurs later?
3116 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00003117 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
3118 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00003119 if (isa<TranslationUnitDecl>(SpecializedContext))
3120 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
3121 << EntityKind << Specialized;
3122 else if (isa<NamespaceDecl>(SpecializedContext))
3123 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
3124 << EntityKind << Specialized
3125 << cast<NamedDecl>(SpecializedContext);
3126
Douglas Gregor9302da62009-10-14 23:50:59 +00003127 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00003128 }
Douglas Gregord5cb8762009-10-07 00:13:32 +00003129
3130 // FIXME: check for specialization-after-instantiation errors and such.
3131
Douglas Gregor88b70942009-02-25 22:02:03 +00003132 return false;
3133}
Douglas Gregord5cb8762009-10-07 00:13:32 +00003134
Douglas Gregore94866f2009-06-12 21:21:02 +00003135/// \brief Check the non-type template arguments of a class template
3136/// partial specialization according to C++ [temp.class.spec]p9.
3137///
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003138/// \param TemplateParams the template parameters of the primary class
3139/// template.
3140///
3141/// \param TemplateArg the template arguments of the class template
3142/// partial specialization.
3143///
3144/// \param MirrorsPrimaryTemplate will be set true if the class
3145/// template partial specialization arguments are identical to the
3146/// implicit template arguments of the primary template. This is not
3147/// necessarily an error (C++0x), and it is left to the caller to diagnose
3148/// this condition when it is an error.
3149///
Douglas Gregore94866f2009-06-12 21:21:02 +00003150/// \returns true if there was an error, false otherwise.
3151bool Sema::CheckClassTemplatePartialSpecializationArgs(
3152 TemplateParameterList *TemplateParams,
Anders Carlsson6360be72009-06-13 18:20:51 +00003153 const TemplateArgumentListBuilder &TemplateArgs,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003154 bool &MirrorsPrimaryTemplate) {
Douglas Gregore94866f2009-06-12 21:21:02 +00003155 // FIXME: the interface to this function will have to change to
3156 // accommodate variadic templates.
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003157 MirrorsPrimaryTemplate = true;
Mike Stump1eb44332009-09-09 15:08:12 +00003158
Anders Carlssonfb250522009-06-23 01:26:57 +00003159 const TemplateArgument *ArgList = TemplateArgs.getFlatArguments();
Mike Stump1eb44332009-09-09 15:08:12 +00003160
Douglas Gregore94866f2009-06-12 21:21:02 +00003161 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003162 // Determine whether the template argument list of the partial
3163 // specialization is identical to the implicit argument list of
3164 // the primary template. The caller may need to diagnostic this as
3165 // an error per C++ [temp.class.spec]p9b3.
3166 if (MirrorsPrimaryTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00003167 if (TemplateTypeParmDecl *TTP
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003168 = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) {
3169 if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) !=
Anders Carlsson6360be72009-06-13 18:20:51 +00003170 Context.getCanonicalType(ArgList[I].getAsType()))
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003171 MirrorsPrimaryTemplate = false;
3172 } else if (TemplateTemplateParmDecl *TTP
3173 = dyn_cast<TemplateTemplateParmDecl>(
3174 TemplateParams->getParam(I))) {
Douglas Gregor788cd062009-11-11 01:00:40 +00003175 TemplateName Name = ArgList[I].getAsTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +00003176 TemplateTemplateParmDecl *ArgDecl
Douglas Gregor788cd062009-11-11 01:00:40 +00003177 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl());
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003178 if (!ArgDecl ||
3179 ArgDecl->getIndex() != TTP->getIndex() ||
3180 ArgDecl->getDepth() != TTP->getDepth())
3181 MirrorsPrimaryTemplate = false;
3182 }
3183 }
3184
Mike Stump1eb44332009-09-09 15:08:12 +00003185 NonTypeTemplateParmDecl *Param
Douglas Gregore94866f2009-06-12 21:21:02 +00003186 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003187 if (!Param) {
Douglas Gregore94866f2009-06-12 21:21:02 +00003188 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003189 }
3190
Anders Carlsson6360be72009-06-13 18:20:51 +00003191 Expr *ArgExpr = ArgList[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003192 if (!ArgExpr) {
3193 MirrorsPrimaryTemplate = false;
Douglas Gregore94866f2009-06-12 21:21:02 +00003194 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003195 }
Douglas Gregore94866f2009-06-12 21:21:02 +00003196
3197 // C++ [temp.class.spec]p8:
3198 // A non-type argument is non-specialized if it is the name of a
3199 // non-type parameter. All other non-type arguments are
3200 // specialized.
3201 //
3202 // Below, we check the two conditions that only apply to
3203 // specialized non-type arguments, so skip any non-specialized
3204 // arguments.
3205 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Mike Stump1eb44332009-09-09 15:08:12 +00003206 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003207 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) {
Mike Stump1eb44332009-09-09 15:08:12 +00003208 if (MirrorsPrimaryTemplate &&
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003209 (Param->getIndex() != NTTP->getIndex() ||
3210 Param->getDepth() != NTTP->getDepth()))
3211 MirrorsPrimaryTemplate = false;
3212
Douglas Gregore94866f2009-06-12 21:21:02 +00003213 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003214 }
Douglas Gregore94866f2009-06-12 21:21:02 +00003215
3216 // C++ [temp.class.spec]p9:
3217 // Within the argument list of a class template partial
3218 // specialization, the following restrictions apply:
3219 // -- A partially specialized non-type argument expression
3220 // shall not involve a template parameter of the partial
3221 // specialization except when the argument expression is a
3222 // simple identifier.
3223 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003224 Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00003225 diag::err_dependent_non_type_arg_in_partial_spec)
3226 << ArgExpr->getSourceRange();
3227 return true;
3228 }
3229
3230 // -- The type of a template parameter corresponding to a
3231 // specialized non-type argument shall not be dependent on a
3232 // parameter of the specialization.
3233 if (Param->getType()->isDependentType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003234 Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00003235 diag::err_dependent_typed_non_type_arg_in_partial_spec)
3236 << Param->getType()
3237 << ArgExpr->getSourceRange();
3238 Diag(Param->getLocation(), diag::note_template_param_here);
3239 return true;
3240 }
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003241
3242 MirrorsPrimaryTemplate = false;
Douglas Gregore94866f2009-06-12 21:21:02 +00003243 }
3244
3245 return false;
3246}
3247
Douglas Gregor212e81c2009-03-25 00:13:59 +00003248Sema::DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00003249Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
3250 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00003251 SourceLocation KWLoc,
Douglas Gregorcc636682009-02-17 23:15:12 +00003252 const CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003253 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00003254 SourceLocation TemplateNameLoc,
3255 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00003256 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00003257 SourceLocation RAngleLoc,
3258 AttributeList *Attr,
3259 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003260 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00003261
Douglas Gregorcc636682009-02-17 23:15:12 +00003262 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00003263 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00003264 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00003265 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
3266
3267 if (!ClassTemplate) {
3268 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
3269 << (Name.getAsTemplateDecl() &&
3270 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
3271 return true;
3272 }
Douglas Gregorcc636682009-02-17 23:15:12 +00003273
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003274 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003275 bool isPartialSpecialization = false;
3276
Douglas Gregor88b70942009-02-25 22:02:03 +00003277 // Check the validity of the template headers that introduce this
3278 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003279 // FIXME: We probably shouldn't complain about these headers for
3280 // friend declarations.
Douglas Gregor05396e22009-08-25 17:23:04 +00003281 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00003282 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
3283 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003284 TemplateParameterLists.size(),
3285 isExplicitSpecialization);
Douglas Gregor05396e22009-08-25 17:23:04 +00003286 if (TemplateParams && TemplateParams->size() > 0) {
3287 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00003288
Douglas Gregor05396e22009-08-25 17:23:04 +00003289 // C++ [temp.class.spec]p10:
3290 // The template parameter list of a specialization shall not
3291 // contain default template argument values.
3292 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
3293 Decl *Param = TemplateParams->getParam(I);
3294 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
3295 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003296 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003297 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00003298 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00003299 }
3300 } else if (NonTypeTemplateParmDecl *NTTP
3301 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
3302 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003303 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003304 diag::err_default_arg_in_partial_spec)
3305 << DefArg->getSourceRange();
3306 NTTP->setDefaultArgument(0);
3307 DefArg->Destroy(Context);
3308 }
3309 } else {
3310 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00003311 if (TTP->hasDefaultArgument()) {
3312 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003313 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00003314 << TTP->getDefaultArgument().getSourceRange();
3315 TTP->setDefaultArgument(TemplateArgumentLoc());
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003316 }
3317 }
3318 }
Douglas Gregora735b202009-10-13 14:39:41 +00003319 } else if (TemplateParams) {
3320 if (TUK == TUK_Friend)
3321 Diag(KWLoc, diag::err_template_spec_friend)
3322 << CodeModificationHint::CreateRemoval(
3323 SourceRange(TemplateParams->getTemplateLoc(),
3324 TemplateParams->getRAngleLoc()))
3325 << SourceRange(LAngleLoc, RAngleLoc);
3326 else
3327 isExplicitSpecialization = true;
3328 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00003329 Diag(KWLoc, diag::err_template_spec_needs_header)
3330 << CodeModificationHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003331 isExplicitSpecialization = true;
3332 }
Douglas Gregor88b70942009-02-25 22:02:03 +00003333
Douglas Gregorcc636682009-02-17 23:15:12 +00003334 // Check that the specialization uses the same tag kind as the
3335 // original template.
3336 TagDecl::TagKind Kind;
3337 switch (TagSpec) {
3338 default: assert(0 && "Unknown tag type!");
3339 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
3340 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
3341 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
3342 }
Douglas Gregor501c5ce2009-05-14 16:41:31 +00003343 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00003344 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00003345 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00003346 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00003347 << ClassTemplate
Mike Stump1eb44332009-09-09 15:08:12 +00003348 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00003349 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00003350 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00003351 diag::note_previous_use);
3352 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
3353 }
3354
Douglas Gregor40808ce2009-03-09 23:48:35 +00003355 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00003356 TemplateArgumentListInfo TemplateArgs;
3357 TemplateArgs.setLAngleLoc(LAngleLoc);
3358 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00003359 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003360
Douglas Gregorcc636682009-02-17 23:15:12 +00003361 // Check that the template argument list is well-formed for this
3362 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00003363 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
3364 TemplateArgs.size());
John McCalld5532b62009-11-23 01:53:49 +00003365 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
3366 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00003367 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00003368
Mike Stump1eb44332009-09-09 15:08:12 +00003369 assert((Converted.structuredSize() ==
Douglas Gregorcc636682009-02-17 23:15:12 +00003370 ClassTemplate->getTemplateParameters()->size()) &&
3371 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00003372
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003373 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00003374 // corresponds to these arguments.
3375 llvm::FoldingSetNodeID ID;
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003376 if (isPartialSpecialization) {
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003377 bool MirrorsPrimaryTemplate;
Douglas Gregore94866f2009-06-12 21:21:02 +00003378 if (CheckClassTemplatePartialSpecializationArgs(
3379 ClassTemplate->getTemplateParameters(),
Anders Carlssonfb250522009-06-23 01:26:57 +00003380 Converted, MirrorsPrimaryTemplate))
Douglas Gregore94866f2009-06-12 21:21:02 +00003381 return true;
3382
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003383 if (MirrorsPrimaryTemplate) {
3384 // C++ [temp.class.spec]p9b3:
3385 //
Mike Stump1eb44332009-09-09 15:08:12 +00003386 // -- The argument list of the specialization shall not be identical
3387 // to the implicit argument list of the primary template.
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003388 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
John McCall0f434ec2009-07-31 02:45:11 +00003389 << (TUK == TUK_Definition)
Mike Stump1eb44332009-09-09 15:08:12 +00003390 << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003391 RAngleLoc));
John McCall0f434ec2009-07-31 02:45:11 +00003392 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003393 ClassTemplate->getIdentifier(),
3394 TemplateNameLoc,
3395 Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +00003396 TemplateParams,
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00003397 AS_none);
3398 }
3399
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003400 // FIXME: Diagnose friend partial specializations
3401
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003402 // FIXME: Template parameter list matters, too
Mike Stump1eb44332009-09-09 15:08:12 +00003403 ClassTemplatePartialSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00003404 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00003405 Converted.flatSize(),
3406 Context);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00003407 } else
Anders Carlsson1c5976e2009-06-05 03:43:12 +00003408 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00003409 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00003410 Converted.flatSize(),
3411 Context);
Douglas Gregorcc636682009-02-17 23:15:12 +00003412 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003413 ClassTemplateSpecializationDecl *PrevDecl = 0;
3414
3415 if (isPartialSpecialization)
3416 PrevDecl
Mike Stump1eb44332009-09-09 15:08:12 +00003417 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003418 InsertPos);
3419 else
3420 PrevDecl
3421 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00003422
3423 ClassTemplateSpecializationDecl *Specialization = 0;
3424
Douglas Gregor88b70942009-02-25 22:02:03 +00003425 // Check whether we can declare a class template specialization in
3426 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003427 if (TUK != TUK_Friend &&
Douglas Gregord5cb8762009-10-07 00:13:32 +00003428 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
Douglas Gregor9302da62009-10-14 23:50:59 +00003429 TemplateNameLoc,
3430 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00003431 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003432
Douglas Gregorb88e8882009-07-30 17:40:51 +00003433 // The canonical type
3434 QualType CanonType;
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003435 if (PrevDecl &&
3436 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
3437 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00003438 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003439 // arguments was referenced but not declared, or we're only
3440 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00003441 // declaration node as our own, updating its source location to
3442 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00003443 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00003444 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00003445 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00003446 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003447 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00003448 // Build the canonical type that describes the converted template
3449 // arguments of the class template partial specialization.
3450 CanonType = Context.getTemplateSpecializationType(
3451 TemplateName(ClassTemplate),
3452 Converted.getFlatArguments(),
3453 Converted.flatSize());
3454
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003455 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003456 ClassTemplatePartialSpecializationDecl *PrevPartial
3457 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003458 ClassTemplatePartialSpecializationDecl *Partial
3459 = ClassTemplatePartialSpecializationDecl::Create(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003460 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00003461 TemplateNameLoc,
3462 TemplateParams,
3463 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00003464 Converted,
John McCalld5532b62009-11-23 01:53:49 +00003465 TemplateArgs,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00003466 PrevPartial);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003467
3468 if (PrevPartial) {
3469 ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial);
3470 ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial);
3471 } else {
3472 ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos);
3473 }
3474 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00003475
Douglas Gregored9c0f92009-10-29 00:04:11 +00003476 // If we are providing an explicit specialization of a member class
3477 // template specialization, make a note of that.
3478 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
3479 PrevPartial->setMemberSpecialization();
3480
Douglas Gregor031a5882009-06-13 00:26:55 +00003481 // Check that all of the template parameters of the class template
3482 // partial specialization are deducible from the template
3483 // arguments. If not, this class template partial specialization
3484 // will never be used.
3485 llvm::SmallVector<bool, 8> DeducibleParams;
3486 DeducibleParams.resize(TemplateParams->size());
Douglas Gregore73bb602009-09-14 21:25:05 +00003487 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003488 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003489 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00003490 unsigned NumNonDeducible = 0;
3491 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
3492 if (!DeducibleParams[I])
3493 ++NumNonDeducible;
3494
3495 if (NumNonDeducible) {
3496 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
3497 << (NumNonDeducible > 1)
3498 << SourceRange(TemplateNameLoc, RAngleLoc);
3499 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
3500 if (!DeducibleParams[I]) {
3501 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
3502 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00003503 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00003504 diag::note_partial_spec_unused_parameter)
3505 << Param->getDeclName();
3506 else
Mike Stump1eb44332009-09-09 15:08:12 +00003507 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00003508 diag::note_partial_spec_unused_parameter)
3509 << std::string("<anonymous>");
3510 }
3511 }
3512 }
Douglas Gregorcc636682009-02-17 23:15:12 +00003513 } else {
3514 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003515 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00003516 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00003517 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregorcc636682009-02-17 23:15:12 +00003518 ClassTemplate->getDeclContext(),
3519 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00003520 ClassTemplate,
Anders Carlssonfb250522009-06-23 01:26:57 +00003521 Converted,
Douglas Gregorcc636682009-02-17 23:15:12 +00003522 PrevDecl);
3523
3524 if (PrevDecl) {
3525 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
3526 ClassTemplate->getSpecializations().GetOrInsertNode(Specialization);
3527 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00003528 ClassTemplate->getSpecializations().InsertNode(Specialization,
Douglas Gregorcc636682009-02-17 23:15:12 +00003529 InsertPos);
3530 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00003531
3532 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00003533 }
3534
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003535 // C++ [temp.expl.spec]p6:
3536 // If a template, a member template or the member of a class template is
3537 // explicitly specialized then that specialization shall be declared
3538 // before the first use of that specialization that would cause an implicit
3539 // instantiation to take place, in every translation unit in which such a
3540 // use occurs; no diagnostic is required.
3541 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
3542 SourceRange Range(TemplateNameLoc, RAngleLoc);
3543 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
3544 << Context.getTypeDeclType(Specialization) << Range;
3545
3546 Diag(PrevDecl->getPointOfInstantiation(),
3547 diag::note_instantiation_required_here)
3548 << (PrevDecl->getTemplateSpecializationKind()
3549 != TSK_ImplicitInstantiation);
3550 return true;
3551 }
3552
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003553 // If this is not a friend, note that this is an explicit specialization.
3554 if (TUK != TUK_Friend)
3555 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00003556
3557 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00003558 if (TUK == TUK_Definition) {
Douglas Gregorcc636682009-02-17 23:15:12 +00003559 if (RecordDecl *Def = Specialization->getDefinition(Context)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00003560 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00003561 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00003562 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00003563 Diag(Def->getLocation(), diag::note_previous_definition);
3564 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00003565 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00003566 }
3567 }
3568
Douglas Gregorfc705b82009-02-26 22:19:44 +00003569 // Build the fully-sugared type for this class template
3570 // specialization as the user wrote in the specialization
3571 // itself. This means that we'll pretty-print the type retrieved
3572 // from the specialization's declaration the way that the user
3573 // actually wrote the specialization, rather than formatting the
3574 // name based on the "canonical" representation used to store the
3575 // template arguments in the specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00003576 QualType WrittenTy
John McCalld5532b62009-11-23 01:53:49 +00003577 = Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003578 if (TUK != TUK_Friend)
3579 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003580 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00003581
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00003582 // C++ [temp.expl.spec]p9:
3583 // A template explicit specialization is in the scope of the
3584 // namespace in which the template was defined.
3585 //
3586 // We actually implement this paragraph where we set the semantic
3587 // context (in the creation of the ClassTemplateSpecializationDecl),
3588 // but we also maintain the lexical context where the actual
3589 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00003590 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00003591
Douglas Gregorcc636682009-02-17 23:15:12 +00003592 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00003593 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00003594 Specialization->startDefinition();
3595
Douglas Gregorfc9cd612009-09-26 20:57:03 +00003596 if (TUK == TUK_Friend) {
3597 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
3598 TemplateNameLoc,
3599 WrittenTy.getTypePtr(),
3600 /*FIXME:*/KWLoc);
3601 Friend->setAccess(AS_public);
3602 CurContext->addDecl(Friend);
3603 } else {
3604 // Add the specialization into its lexical context, so that it can
3605 // be seen when iterating through the list of declarations in that
3606 // context. However, specializations are not found by name lookup.
3607 CurContext->addDecl(Specialization);
3608 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00003609 return DeclPtrTy::make(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00003610}
Douglas Gregord57959a2009-03-27 23:10:48 +00003611
Mike Stump1eb44332009-09-09 15:08:12 +00003612Sema::DeclPtrTy
3613Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00003614 MultiTemplateParamsArg TemplateParameterLists,
3615 Declarator &D) {
3616 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
3617}
3618
Mike Stump1eb44332009-09-09 15:08:12 +00003619Sema::DeclPtrTy
3620Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00003621 MultiTemplateParamsArg TemplateParameterLists,
3622 Declarator &D) {
3623 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
3624 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
3625 "Not a function declarator!");
3626 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Mike Stump1eb44332009-09-09 15:08:12 +00003627
Douglas Gregor52591bf2009-06-24 00:54:41 +00003628 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00003629 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00003630 }
Mike Stump1eb44332009-09-09 15:08:12 +00003631
Douglas Gregor52591bf2009-06-24 00:54:41 +00003632 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00003633
3634 DeclPtrTy DP = HandleDeclarator(ParentScope, D,
Douglas Gregor52591bf2009-06-24 00:54:41 +00003635 move(TemplateParameterLists),
3636 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00003637 if (FunctionTemplateDecl *FunctionTemplate
Douglas Gregorf59a56e2009-07-21 23:53:31 +00003638 = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>()))
Mike Stump1eb44332009-09-09 15:08:12 +00003639 return ActOnStartOfFunctionDef(FnBodyScope,
Douglas Gregore53060f2009-06-25 22:08:12 +00003640 DeclPtrTy::make(FunctionTemplate->getTemplatedDecl()));
Douglas Gregorf59a56e2009-07-21 23:53:31 +00003641 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>()))
3642 return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function));
Douglas Gregore53060f2009-06-25 22:08:12 +00003643 return DeclPtrTy();
Douglas Gregor52591bf2009-06-24 00:54:41 +00003644}
3645
Douglas Gregor454885e2009-10-15 15:54:05 +00003646/// \brief Diagnose cases where we have an explicit template specialization
3647/// before/after an explicit template instantiation, producing diagnostics
3648/// for those cases where they are required and determining whether the
3649/// new specialization/instantiation will have any effect.
3650///
Douglas Gregor454885e2009-10-15 15:54:05 +00003651/// \param NewLoc the location of the new explicit specialization or
3652/// instantiation.
3653///
3654/// \param NewTSK the kind of the new explicit specialization or instantiation.
3655///
3656/// \param PrevDecl the previous declaration of the entity.
3657///
3658/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
3659///
3660/// \param PrevPointOfInstantiation if valid, indicates where the previus
3661/// declaration was instantiated (either implicitly or explicitly).
3662///
3663/// \param SuppressNew will be set to true to indicate that the new
3664/// specialization or instantiation has no effect and should be ignored.
3665///
3666/// \returns true if there was an error that should prevent the introduction of
3667/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00003668bool
3669Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
3670 TemplateSpecializationKind NewTSK,
3671 NamedDecl *PrevDecl,
3672 TemplateSpecializationKind PrevTSK,
3673 SourceLocation PrevPointOfInstantiation,
3674 bool &SuppressNew) {
Douglas Gregor454885e2009-10-15 15:54:05 +00003675 SuppressNew = false;
3676
3677 switch (NewTSK) {
3678 case TSK_Undeclared:
3679 case TSK_ImplicitInstantiation:
3680 assert(false && "Don't check implicit instantiations here");
3681 return false;
3682
3683 case TSK_ExplicitSpecialization:
3684 switch (PrevTSK) {
3685 case TSK_Undeclared:
3686 case TSK_ExplicitSpecialization:
3687 // Okay, we're just specializing something that is either already
3688 // explicitly specialized or has merely been mentioned without any
3689 // instantiation.
3690 return false;
3691
3692 case TSK_ImplicitInstantiation:
3693 if (PrevPointOfInstantiation.isInvalid()) {
3694 // The declaration itself has not actually been instantiated, so it is
3695 // still okay to specialize it.
3696 return false;
3697 }
3698 // Fall through
3699
3700 case TSK_ExplicitInstantiationDeclaration:
3701 case TSK_ExplicitInstantiationDefinition:
3702 assert((PrevTSK == TSK_ImplicitInstantiation ||
3703 PrevPointOfInstantiation.isValid()) &&
3704 "Explicit instantiation without point of instantiation?");
3705
3706 // C++ [temp.expl.spec]p6:
3707 // If a template, a member template or the member of a class template
3708 // is explicitly specialized then that specialization shall be declared
3709 // before the first use of that specialization that would cause an
3710 // implicit instantiation to take place, in every translation unit in
3711 // which such a use occurs; no diagnostic is required.
Douglas Gregor0d035142009-10-27 18:42:08 +00003712 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00003713 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00003714 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00003715 << (PrevTSK != TSK_ImplicitInstantiation);
3716
3717 return true;
3718 }
3719 break;
3720
3721 case TSK_ExplicitInstantiationDeclaration:
3722 switch (PrevTSK) {
3723 case TSK_ExplicitInstantiationDeclaration:
3724 // This explicit instantiation declaration is redundant (that's okay).
3725 SuppressNew = true;
3726 return false;
3727
3728 case TSK_Undeclared:
3729 case TSK_ImplicitInstantiation:
3730 // We're explicitly instantiating something that may have already been
3731 // implicitly instantiated; that's fine.
3732 return false;
3733
3734 case TSK_ExplicitSpecialization:
3735 // C++0x [temp.explicit]p4:
3736 // For a given set of template parameters, if an explicit instantiation
3737 // of a template appears after a declaration of an explicit
3738 // specialization for that template, the explicit instantiation has no
3739 // effect.
3740 return false;
3741
3742 case TSK_ExplicitInstantiationDefinition:
3743 // C++0x [temp.explicit]p10:
3744 // If an entity is the subject of both an explicit instantiation
3745 // declaration and an explicit instantiation definition in the same
3746 // translation unit, the definition shall follow the declaration.
Douglas Gregor0d035142009-10-27 18:42:08 +00003747 Diag(NewLoc,
3748 diag::err_explicit_instantiation_declaration_after_definition);
3749 Diag(PrevPointOfInstantiation,
3750 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00003751 assert(PrevPointOfInstantiation.isValid() &&
3752 "Explicit instantiation without point of instantiation?");
3753 SuppressNew = true;
3754 return false;
3755 }
3756 break;
3757
3758 case TSK_ExplicitInstantiationDefinition:
3759 switch (PrevTSK) {
3760 case TSK_Undeclared:
3761 case TSK_ImplicitInstantiation:
3762 // We're explicitly instantiating something that may have already been
3763 // implicitly instantiated; that's fine.
3764 return false;
3765
3766 case TSK_ExplicitSpecialization:
3767 // C++ DR 259, C++0x [temp.explicit]p4:
3768 // For a given set of template parameters, if an explicit
3769 // instantiation of a template appears after a declaration of
3770 // an explicit specialization for that template, the explicit
3771 // instantiation has no effect.
3772 //
3773 // In C++98/03 mode, we only give an extension warning here, because it
3774 // is not not harmful to try to explicitly instantiate something that
3775 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00003776 if (!getLangOptions().CPlusPlus0x) {
3777 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00003778 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00003779 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00003780 diag::note_previous_template_specialization);
3781 }
3782 SuppressNew = true;
3783 return false;
3784
3785 case TSK_ExplicitInstantiationDeclaration:
3786 // We're explicity instantiating a definition for something for which we
3787 // were previously asked to suppress instantiations. That's fine.
3788 return false;
3789
3790 case TSK_ExplicitInstantiationDefinition:
3791 // C++0x [temp.spec]p5:
3792 // For a given template and a given set of template-arguments,
3793 // - an explicit instantiation definition shall appear at most once
3794 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00003795 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00003796 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00003797 Diag(PrevPointOfInstantiation,
3798 diag::note_previous_explicit_instantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00003799 SuppressNew = true;
3800 return false;
3801 }
3802 break;
3803 }
3804
3805 assert(false && "Missing specialization/instantiation case?");
3806
3807 return false;
3808}
3809
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003810/// \brief Perform semantic analysis for the given function template
3811/// specialization.
3812///
3813/// This routine performs all of the semantic analysis required for an
3814/// explicit function template specialization. On successful completion,
3815/// the function declaration \p FD will become a function template
3816/// specialization.
3817///
3818/// \param FD the function declaration, which will be updated to become a
3819/// function template specialization.
3820///
3821/// \param HasExplicitTemplateArgs whether any template arguments were
3822/// explicitly provided.
3823///
3824/// \param LAngleLoc the location of the left angle bracket ('<'), if
3825/// template arguments were explicitly provided.
3826///
3827/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
3828/// if any.
3829///
3830/// \param NumExplicitTemplateArgs the number of explicitly-provided template
3831/// arguments. This number may be zero even when HasExplicitTemplateArgs is
3832/// true as in, e.g., \c void sort<>(char*, char*);
3833///
3834/// \param RAngleLoc the location of the right angle bracket ('>'), if
3835/// template arguments were explicitly provided.
3836///
3837/// \param PrevDecl the set of declarations that
3838bool
3839Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00003840 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00003841 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003842 // The set of function template specializations that could match this
3843 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00003844 UnresolvedSet<8> Candidates;
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003845
3846 DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext();
John McCall68263142009-11-18 22:49:29 +00003847 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
3848 I != E; ++I) {
3849 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
3850 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003851 // Only consider templates found within the same semantic lookup scope as
3852 // FD.
3853 if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext()))
3854 continue;
3855
3856 // C++ [temp.expl.spec]p11:
3857 // A trailing template-argument can be left unspecified in the
3858 // template-id naming an explicit function template specialization
3859 // provided it can be deduced from the function argument type.
3860 // Perform template argument deduction to determine whether we may be
3861 // specializing this template.
3862 // FIXME: It is somewhat wasteful to build
3863 TemplateDeductionInfo Info(Context);
3864 FunctionDecl *Specialization = 0;
3865 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00003866 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003867 FD->getType(),
3868 Specialization,
3869 Info)) {
3870 // FIXME: Template argument deduction failed; record why it failed, so
3871 // that we can provide nifty diagnostics.
3872 (void)TDK;
3873 continue;
3874 }
3875
3876 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00003877 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003878 }
3879 }
3880
Douglas Gregorc5df30f2009-09-26 03:41:46 +00003881 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00003882 UnresolvedSetIterator Result
3883 = getMostSpecialized(Candidates.begin(), Candidates.end(),
3884 TPOC_Other, FD->getLocation(),
Douglas Gregorc5df30f2009-09-26 03:41:46 +00003885 PartialDiagnostic(diag::err_function_template_spec_no_match)
3886 << FD->getDeclName(),
3887 PartialDiagnostic(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00003888 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorc5df30f2009-09-26 03:41:46 +00003889 PartialDiagnostic(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00003890 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003891 return true;
John McCallc373d482010-01-27 01:50:18 +00003892
3893 // Ignore access information; it doesn't figure into redeclaration checking.
3894 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003895
3896 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003897 // If so, we have run afoul of .
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003898
Douglas Gregord5cb8762009-10-07 00:13:32 +00003899 // Check the scope of this explicit specialization.
3900 if (CheckTemplateSpecializationScope(*this,
3901 Specialization->getPrimaryTemplate(),
3902 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00003903 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00003904 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003905
3906 // C++ [temp.expl.spec]p6:
3907 // If a template, a member template or the member of a class template is
Douglas Gregor0d035142009-10-27 18:42:08 +00003908 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003909 // before the first use of that specialization that would cause an implicit
3910 // instantiation to take place, in every translation unit in which such a
3911 // use occurs; no diagnostic is required.
3912 FunctionTemplateSpecializationInfo *SpecInfo
3913 = Specialization->getTemplateSpecializationInfo();
3914 assert(SpecInfo && "Function template specialization info missing?");
3915 if (SpecInfo->getPointOfInstantiation().isValid()) {
3916 Diag(FD->getLocation(), diag::err_specialization_after_instantiation)
3917 << FD;
3918 Diag(SpecInfo->getPointOfInstantiation(),
3919 diag::note_instantiation_required_here)
3920 << (Specialization->getTemplateSpecializationKind()
3921 != TSK_ImplicitInstantiation);
3922 return true;
3923 }
Douglas Gregord5cb8762009-10-07 00:13:32 +00003924
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003925 // Mark the prior declaration as an explicit specialization, so that later
3926 // clients know that this is an explicit specialization.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003927 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003928
3929 // Turn the given function declaration into a function template
3930 // specialization, with the template arguments from the previous
3931 // specialization.
3932 FD->setFunctionTemplateSpecialization(Context,
3933 Specialization->getPrimaryTemplate(),
3934 new (Context) TemplateArgumentList(
3935 *Specialization->getTemplateSpecializationArgs()),
3936 /*InsertPos=*/0,
3937 TSK_ExplicitSpecialization);
3938
3939 // The "previous declaration" for this function template specialization is
3940 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00003941 Previous.clear();
3942 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00003943 return false;
3944}
3945
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003946/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003947/// specialization.
3948///
3949/// This routine performs all of the semantic analysis required for an
3950/// explicit member function specialization. On successful completion,
3951/// the function declaration \p FD will become a member function
3952/// specialization.
3953///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003954/// \param Member the member declaration, which will be updated to become a
3955/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003956///
John McCall68263142009-11-18 22:49:29 +00003957/// \param Previous the set of declarations, one of which may be specialized
3958/// by this function specialization; the set will be modified to contain the
3959/// redeclared member.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003960bool
John McCall68263142009-11-18 22:49:29 +00003961Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003962 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
3963
3964 // Try to find the member we are instantiating.
3965 NamedDecl *Instantiation = 0;
3966 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003967 MemberSpecializationInfo *MSInfo = 0;
3968
John McCall68263142009-11-18 22:49:29 +00003969 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003970 // Nowhere to look anyway.
3971 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00003972 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
3973 I != E; ++I) {
3974 NamedDecl *D = (*I)->getUnderlyingDecl();
3975 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003976 if (Context.hasSameType(Function->getType(), Method->getType())) {
3977 Instantiation = Method;
3978 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003979 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003980 break;
3981 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00003982 }
3983 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003984 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00003985 VarDecl *PrevVar;
3986 if (Previous.isSingleResult() &&
3987 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003988 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00003989 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003990 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003991 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003992 }
3993 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00003994 CXXRecordDecl *PrevRecord;
3995 if (Previous.isSingleResult() &&
3996 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
3997 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003998 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003999 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004000 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004001 }
4002
4003 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004004 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004005 // specializations are always out-of-line, the caller will complain about
4006 // this mismatch later.
4007 return false;
4008 }
4009
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004010 // Make sure that this is a specialization of a member.
4011 if (!InstantiatedFrom) {
4012 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
4013 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004014 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
4015 return true;
4016 }
4017
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004018 // C++ [temp.expl.spec]p6:
4019 // If a template, a member template or the member of a class template is
4020 // explicitly specialized then that spe- cialization shall be declared
4021 // before the first use of that specialization that would cause an implicit
4022 // instantiation to take place, in every translation unit in which such a
4023 // use occurs; no diagnostic is required.
4024 assert(MSInfo && "Member specialization info missing?");
4025 if (MSInfo->getPointOfInstantiation().isValid()) {
4026 Diag(Member->getLocation(), diag::err_specialization_after_instantiation)
4027 << Member;
4028 Diag(MSInfo->getPointOfInstantiation(),
4029 diag::note_instantiation_required_here)
4030 << (MSInfo->getTemplateSpecializationKind() != TSK_ImplicitInstantiation);
4031 return true;
4032 }
4033
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004034 // Check the scope of this explicit specialization.
4035 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004036 InstantiatedFrom,
4037 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00004038 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004039 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00004040
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004041 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00004042 // the original declaration to note that it is an explicit specialization
4043 // (if it was previously an implicit instantiation). This latter step
4044 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004045 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00004046 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
4047 if (InstantiationFunction->getTemplateSpecializationKind() ==
4048 TSK_ImplicitInstantiation) {
4049 InstantiationFunction->setTemplateSpecializationKind(
4050 TSK_ExplicitSpecialization);
4051 InstantiationFunction->setLocation(Member->getLocation());
4052 }
4053
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004054 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
4055 cast<CXXMethodDecl>(InstantiatedFrom),
4056 TSK_ExplicitSpecialization);
4057 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00004058 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
4059 if (InstantiationVar->getTemplateSpecializationKind() ==
4060 TSK_ImplicitInstantiation) {
4061 InstantiationVar->setTemplateSpecializationKind(
4062 TSK_ExplicitSpecialization);
4063 InstantiationVar->setLocation(Member->getLocation());
4064 }
4065
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004066 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
4067 cast<VarDecl>(InstantiatedFrom),
4068 TSK_ExplicitSpecialization);
4069 } else {
4070 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00004071 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
4072 if (InstantiationClass->getTemplateSpecializationKind() ==
4073 TSK_ImplicitInstantiation) {
4074 InstantiationClass->setTemplateSpecializationKind(
4075 TSK_ExplicitSpecialization);
4076 InstantiationClass->setLocation(Member->getLocation());
4077 }
4078
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004079 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00004080 cast<CXXRecordDecl>(InstantiatedFrom),
4081 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004082 }
4083
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004084 // Save the caller the trouble of having to figure out which declaration
4085 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00004086 Previous.clear();
4087 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004088 return false;
4089}
4090
Douglas Gregor558c0322009-10-14 23:41:34 +00004091/// \brief Check the scope of an explicit instantiation.
4092static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
4093 SourceLocation InstLoc,
4094 bool WasQualifiedName) {
4095 DeclContext *ExpectedContext
4096 = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext();
4097 DeclContext *CurContext = S.CurContext->getLookupContext();
4098
4099 // C++0x [temp.explicit]p2:
4100 // An explicit instantiation shall appear in an enclosing namespace of its
4101 // template.
4102 //
4103 // This is DR275, which we do not retroactively apply to C++98/03.
4104 if (S.getLangOptions().CPlusPlus0x &&
4105 !CurContext->Encloses(ExpectedContext)) {
4106 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext))
4107 S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope)
4108 << D << NS;
4109 else
4110 S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global)
4111 << D;
4112 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
4113 return;
4114 }
4115
4116 // C++0x [temp.explicit]p2:
4117 // If the name declared in the explicit instantiation is an unqualified
4118 // name, the explicit instantiation shall appear in the namespace where
4119 // its template is declared or, if that namespace is inline (7.3.1), any
4120 // namespace from its enclosing namespace set.
4121 if (WasQualifiedName)
4122 return;
4123
4124 if (CurContext->Equals(ExpectedContext))
4125 return;
4126
4127 S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace)
4128 << D << ExpectedContext;
4129 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
4130}
4131
4132/// \brief Determine whether the given scope specifier has a template-id in it.
4133static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
4134 if (!SS.isSet())
4135 return false;
4136
4137 // C++0x [temp.explicit]p2:
4138 // If the explicit instantiation is for a member function, a member class
4139 // or a static data member of a class template specialization, the name of
4140 // the class template specialization in the qualified-id for the member
4141 // name shall be a simple-template-id.
4142 //
4143 // C++98 has the same restriction, just worded differently.
4144 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
4145 NNS; NNS = NNS->getPrefix())
4146 if (Type *T = NNS->getAsType())
4147 if (isa<TemplateSpecializationType>(T))
4148 return true;
4149
4150 return false;
4151}
4152
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004153// Explicit instantiation of a class template specialization
Douglas Gregor45f96552009-09-04 06:33:52 +00004154// FIXME: Implement extern template semantics
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004155Sema::DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00004156Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00004157 SourceLocation ExternLoc,
4158 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004159 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004160 SourceLocation KWLoc,
4161 const CXXScopeSpec &SS,
4162 TemplateTy TemplateD,
4163 SourceLocation TemplateNameLoc,
4164 SourceLocation LAngleLoc,
4165 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004166 SourceLocation RAngleLoc,
4167 AttributeList *Attr) {
4168 // Find the class template we're specializing
4169 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004170 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004171 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
4172
4173 // Check that the specialization uses the same tag kind as the
4174 // original template.
4175 TagDecl::TagKind Kind;
4176 switch (TagSpec) {
4177 default: assert(0 && "Unknown tag type!");
4178 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
4179 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
4180 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
4181 }
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004182 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004183 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004184 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004185 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004186 << ClassTemplate
Mike Stump1eb44332009-09-09 15:08:12 +00004187 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004188 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004189 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004190 diag::note_previous_use);
4191 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4192 }
4193
Douglas Gregor558c0322009-10-14 23:41:34 +00004194 // C++0x [temp.explicit]p2:
4195 // There are two forms of explicit instantiation: an explicit instantiation
4196 // definition and an explicit instantiation declaration. An explicit
4197 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00004198 TemplateSpecializationKind TSK
4199 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
4200 : TSK_ExplicitInstantiationDeclaration;
4201
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004202 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004203 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004204 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004205
4206 // Check that the template argument list is well-formed for this
4207 // template.
Anders Carlssonfb250522009-06-23 01:26:57 +00004208 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
4209 TemplateArgs.size());
John McCalld5532b62009-11-23 01:53:49 +00004210 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4211 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004212 return true;
4213
Mike Stump1eb44332009-09-09 15:08:12 +00004214 assert((Converted.structuredSize() ==
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004215 ClassTemplate->getTemplateParameters()->size()) &&
4216 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004217
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004218 // Find the class template specialization declaration that
4219 // corresponds to these arguments.
4220 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00004221 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlssonfb250522009-06-23 01:26:57 +00004222 Converted.getFlatArguments(),
Douglas Gregor828e2262009-07-29 16:09:57 +00004223 Converted.flatSize(),
4224 Context);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004225 void *InsertPos = 0;
4226 ClassTemplateSpecializationDecl *PrevDecl
4227 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
4228
Douglas Gregord5cb8762009-10-07 00:13:32 +00004229 // C++0x [temp.explicit]p2:
4230 // [...] An explicit instantiation shall appear in an enclosing
4231 // namespace of its template. [...]
4232 //
4233 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00004234 CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
4235 SS.isSet());
Douglas Gregord5cb8762009-10-07 00:13:32 +00004236
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004237 ClassTemplateSpecializationDecl *Specialization = 0;
4238
Douglas Gregord78f5982009-11-25 06:01:46 +00004239 bool ReusedDecl = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004240 if (PrevDecl) {
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004241 bool SuppressNew = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00004242 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004243 PrevDecl,
4244 PrevDecl->getSpecializationKind(),
4245 PrevDecl->getPointOfInstantiation(),
4246 SuppressNew))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004247 return DeclPtrTy::make(PrevDecl);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004248
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004249 if (SuppressNew)
Douglas Gregor52604ab2009-09-11 21:19:12 +00004250 return DeclPtrTy::make(PrevDecl);
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004251
Douglas Gregor52604ab2009-09-11 21:19:12 +00004252 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation ||
4253 PrevDecl->getSpecializationKind() == TSK_Undeclared) {
4254 // Since the only prior class template specialization with these
4255 // arguments was referenced but not declared, reuse that
4256 // declaration node as our own, updating its source location to
4257 // reflect our new declaration.
4258 Specialization = PrevDecl;
4259 Specialization->setLocation(TemplateNameLoc);
4260 PrevDecl = 0;
Douglas Gregord78f5982009-11-25 06:01:46 +00004261 ReusedDecl = true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00004262 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004263 }
Douglas Gregor52604ab2009-09-11 21:19:12 +00004264
4265 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004266 // Create a new class template specialization declaration node for
4267 // this explicit specialization.
4268 Specialization
Mike Stump1eb44332009-09-09 15:08:12 +00004269 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004270 ClassTemplate->getDeclContext(),
4271 TemplateNameLoc,
4272 ClassTemplate,
Douglas Gregor52604ab2009-09-11 21:19:12 +00004273 Converted, PrevDecl);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004274
Douglas Gregor52604ab2009-09-11 21:19:12 +00004275 if (PrevDecl) {
4276 // Remove the previous declaration from the folding set, since we want
4277 // to introduce a new declaration.
4278 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
4279 ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
4280 }
4281
4282 // Insert the new specialization.
4283 ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004284 }
4285
4286 // Build the fully-sugared type for this explicit instantiation as
4287 // the user wrote in the explicit instantiation itself. This means
4288 // that we'll pretty-print the type retrieved from the
4289 // specialization's declaration the way that the user actually wrote
4290 // the explicit instantiation, rather than formatting the name based
4291 // on the "canonical" representation used to store the template
4292 // arguments in the specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00004293 QualType WrittenTy
John McCalld5532b62009-11-23 01:53:49 +00004294 = Context.getTemplateSpecializationType(Name, TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004295 Context.getTypeDeclType(Specialization));
4296 Specialization->setTypeAsWritten(WrittenTy);
4297 TemplateArgsIn.release();
4298
Douglas Gregord78f5982009-11-25 06:01:46 +00004299 if (!ReusedDecl) {
4300 // Add the explicit instantiation into its lexical context. However,
4301 // since explicit instantiations are never found by name lookup, we
4302 // just put it into the declaration context directly.
4303 Specialization->setLexicalDeclContext(CurContext);
4304 CurContext->addDecl(Specialization);
4305 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004306
4307 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004308 // A definition of a class template or class member template
4309 // shall be in scope at the point of the explicit instantiation of
4310 // the class template or class member template.
4311 //
4312 // This check comes when we actually try to perform the
4313 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004314 ClassTemplateSpecializationDecl *Def
4315 = cast_or_null<ClassTemplateSpecializationDecl>(
4316 Specialization->getDefinition(Context));
4317 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00004318 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00004319
4320 // Instantiate the members of this class template specialization.
4321 Def = cast_or_null<ClassTemplateSpecializationDecl>(
4322 Specialization->getDefinition(Context));
4323 if (Def)
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004324 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00004325
4326 return DeclPtrTy::make(Specialization);
4327}
4328
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004329// Explicit instantiation of a member class of a class template.
4330Sema::DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00004331Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00004332 SourceLocation ExternLoc,
4333 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004334 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004335 SourceLocation KWLoc,
4336 const CXXScopeSpec &SS,
4337 IdentifierInfo *Name,
4338 SourceLocation NameLoc,
4339 AttributeList *Attr) {
4340
Douglas Gregor402abb52009-05-28 23:31:59 +00004341 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00004342 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00004343 DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference,
Douglas Gregor7cdbc582009-07-22 23:48:44 +00004344 KWLoc, SS, Name, NameLoc, Attr, AS_none,
John McCallc4e70192009-09-11 04:59:25 +00004345 MultiTemplateParamsArg(*this, 0, 0),
4346 Owned, IsDependent);
4347 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
4348
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004349 if (!TagD)
4350 return true;
4351
4352 TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
4353 if (Tag->isEnum()) {
4354 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
4355 << Context.getTypeDeclType(Tag);
4356 return true;
4357 }
4358
Douglas Gregord0c87372009-05-27 17:30:49 +00004359 if (Tag->isInvalidDecl())
4360 return true;
Douglas Gregor558c0322009-10-14 23:41:34 +00004361
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004362 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
4363 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
4364 if (!Pattern) {
4365 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
4366 << Context.getTypeDeclType(Record);
4367 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
4368 return true;
4369 }
4370
Douglas Gregor558c0322009-10-14 23:41:34 +00004371 // C++0x [temp.explicit]p2:
4372 // If the explicit instantiation is for a class or member class, the
4373 // elaborated-type-specifier in the declaration shall include a
4374 // simple-template-id.
4375 //
4376 // C++98 has the same restriction, just worded differently.
4377 if (!ScopeSpecifierHasTemplateId(SS))
4378 Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id)
4379 << Record << SS.getRange();
4380
4381 // C++0x [temp.explicit]p2:
4382 // There are two forms of explicit instantiation: an explicit instantiation
4383 // definition and an explicit instantiation declaration. An explicit
4384 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00004385 TemplateSpecializationKind TSK
4386 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
4387 : TSK_ExplicitInstantiationDeclaration;
4388
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004389 // C++0x [temp.explicit]p2:
4390 // [...] An explicit instantiation shall appear in an enclosing
4391 // namespace of its template. [...]
4392 //
4393 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00004394 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
Douglas Gregor454885e2009-10-15 15:54:05 +00004395
4396 // Verify that it is okay to explicitly instantiate here.
Douglas Gregor583f33b2009-10-15 18:07:02 +00004397 CXXRecordDecl *PrevDecl
4398 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
4399 if (!PrevDecl && Record->getDefinition(Context))
4400 PrevDecl = Record;
4401 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00004402 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
4403 bool SuppressNew = false;
4404 assert(MSInfo && "No member specialization information?");
Douglas Gregor0d035142009-10-27 18:42:08 +00004405 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00004406 PrevDecl,
4407 MSInfo->getTemplateSpecializationKind(),
4408 MSInfo->getPointOfInstantiation(),
4409 SuppressNew))
4410 return true;
4411 if (SuppressNew)
4412 return TagD;
4413 }
4414
Douglas Gregor89a5bea2009-10-15 22:53:21 +00004415 CXXRecordDecl *RecordDef
4416 = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
4417 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00004418 // C++ [temp.explicit]p3:
4419 // A definition of a member class of a class template shall be in scope
4420 // at the point of an explicit instantiation of the member class.
4421 CXXRecordDecl *Def
4422 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
4423 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00004424 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
4425 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00004426 Diag(Pattern->getLocation(), diag::note_forward_declaration)
4427 << Pattern;
4428 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00004429 } else {
4430 if (InstantiateClass(NameLoc, Record, Def,
4431 getTemplateInstantiationArgs(Record),
4432 TSK))
4433 return true;
4434
4435 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
4436 if (!RecordDef)
4437 return true;
4438 }
4439 }
4440
4441 // Instantiate all of the members of the class.
4442 InstantiateClassMembers(NameLoc, RecordDef,
4443 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004444
Mike Stump390b4cc2009-05-16 07:39:55 +00004445 // FIXME: We don't have any representation for explicit instantiations of
4446 // member classes. Such a representation is not needed for compilation, but it
4447 // should be available for clients that want to see all of the declarations in
4448 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00004449 return TagD;
4450}
4451
Douglas Gregord5a423b2009-09-25 18:43:00 +00004452Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
4453 SourceLocation ExternLoc,
4454 SourceLocation TemplateLoc,
4455 Declarator &D) {
4456 // Explicit instantiations always require a name.
4457 DeclarationName Name = GetNameForDeclarator(D);
4458 if (!Name) {
4459 if (!D.isInvalidType())
4460 Diag(D.getDeclSpec().getSourceRange().getBegin(),
4461 diag::err_explicit_instantiation_requires_name)
4462 << D.getDeclSpec().getSourceRange()
4463 << D.getSourceRange();
4464
4465 return true;
4466 }
4467
4468 // The scope passed in may not be a decl scope. Zip up the scope tree until
4469 // we find one that is.
4470 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4471 (S->getFlags() & Scope::TemplateParamScope) != 0)
4472 S = S->getParent();
4473
4474 // Determine the type of the declaration.
4475 QualType R = GetTypeForDeclarator(D, S, 0);
4476 if (R.isNull())
4477 return true;
4478
4479 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
4480 // Cannot explicitly instantiate a typedef.
4481 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
4482 << Name;
4483 return true;
4484 }
4485
Douglas Gregor663b5a02009-10-14 20:14:33 +00004486 // C++0x [temp.explicit]p1:
4487 // [...] An explicit instantiation of a function template shall not use the
4488 // inline or constexpr specifiers.
4489 // Presumably, this also applies to member functions of class templates as
4490 // well.
4491 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
4492 Diag(D.getDeclSpec().getInlineSpecLoc(),
4493 diag::err_explicit_instantiation_inline)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00004494 <<CodeModificationHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
Douglas Gregor663b5a02009-10-14 20:14:33 +00004495
4496 // FIXME: check for constexpr specifier.
4497
Douglas Gregor558c0322009-10-14 23:41:34 +00004498 // C++0x [temp.explicit]p2:
4499 // There are two forms of explicit instantiation: an explicit instantiation
4500 // definition and an explicit instantiation declaration. An explicit
4501 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00004502 TemplateSpecializationKind TSK
4503 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
4504 : TSK_ExplicitInstantiationDeclaration;
Douglas Gregor558c0322009-10-14 23:41:34 +00004505
John McCalla24dc2e2009-11-17 02:14:36 +00004506 LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName);
4507 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00004508
4509 if (!R->isFunctionType()) {
4510 // C++ [temp.explicit]p1:
4511 // A [...] static data member of a class template can be explicitly
4512 // instantiated from the member definition associated with its class
4513 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00004514 if (Previous.isAmbiguous())
4515 return true;
Douglas Gregord5a423b2009-09-25 18:43:00 +00004516
John McCall1bcee0a2009-12-02 08:25:40 +00004517 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00004518 if (!Prev || !Prev->isStaticDataMember()) {
4519 // We expect to see a data data member here.
4520 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
4521 << Name;
4522 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
4523 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00004524 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00004525 return true;
4526 }
4527
4528 if (!Prev->getInstantiatedFromStaticDataMember()) {
4529 // FIXME: Check for explicit specialization?
4530 Diag(D.getIdentifierLoc(),
4531 diag::err_explicit_instantiation_data_member_not_instantiated)
4532 << Prev;
4533 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
4534 // FIXME: Can we provide a note showing where this was declared?
4535 return true;
4536 }
4537
Douglas Gregor558c0322009-10-14 23:41:34 +00004538 // C++0x [temp.explicit]p2:
4539 // If the explicit instantiation is for a member function, a member class
4540 // or a static data member of a class template specialization, the name of
4541 // the class template specialization in the qualified-id for the member
4542 // name shall be a simple-template-id.
4543 //
4544 // C++98 has the same restriction, just worded differently.
4545 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
4546 Diag(D.getIdentifierLoc(),
4547 diag::err_explicit_instantiation_without_qualified_id)
4548 << Prev << D.getCXXScopeSpec().getRange();
4549
4550 // Check the scope of this explicit instantiation.
4551 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
4552
Douglas Gregor454885e2009-10-15 15:54:05 +00004553 // Verify that it is okay to explicitly instantiate here.
4554 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
4555 assert(MSInfo && "Missing static data member specialization info?");
4556 bool SuppressNew = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00004557 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00004558 MSInfo->getTemplateSpecializationKind(),
4559 MSInfo->getPointOfInstantiation(),
4560 SuppressNew))
4561 return true;
4562 if (SuppressNew)
4563 return DeclPtrTy();
4564
Douglas Gregord5a423b2009-09-25 18:43:00 +00004565 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00004566 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00004567 if (TSK == TSK_ExplicitInstantiationDefinition)
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00004568 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false,
4569 /*DefinitionRequired=*/true);
Douglas Gregord5a423b2009-09-25 18:43:00 +00004570
4571 // FIXME: Create an ExplicitInstantiation node?
4572 return DeclPtrTy();
4573 }
4574
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00004575 // If the declarator is a template-id, translate the parser's template
4576 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00004577 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00004578 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004579 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
4580 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00004581 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
4582 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00004583 ASTTemplateArgsPtr TemplateArgsPtr(*this,
4584 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00004585 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00004586 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00004587 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00004588 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00004589 }
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00004590
Douglas Gregord5a423b2009-09-25 18:43:00 +00004591 // C++ [temp.explicit]p1:
4592 // A [...] function [...] can be explicitly instantiated from its template.
4593 // A member function [...] of a class template can be explicitly
4594 // instantiated from the member definition associated with its class
4595 // template.
John McCallc373d482010-01-27 01:50:18 +00004596 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00004597 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
4598 P != PEnd; ++P) {
4599 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00004600 if (!HasExplicitTemplateArgs) {
4601 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
4602 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
4603 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00004604
John McCallc373d482010-01-27 01:50:18 +00004605 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00004606 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
4607 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00004608 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00004609 }
4610 }
4611
4612 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
4613 if (!FunTmpl)
4614 continue;
4615
4616 TemplateDeductionInfo Info(Context);
4617 FunctionDecl *Specialization = 0;
4618 if (TemplateDeductionResult TDK
Douglas Gregor48026d22010-01-11 18:40:55 +00004619 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00004620 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00004621 R, Specialization, Info)) {
4622 // FIXME: Keep track of almost-matches?
4623 (void)TDK;
4624 continue;
4625 }
4626
John McCallc373d482010-01-27 01:50:18 +00004627 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00004628 }
4629
4630 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00004631 UnresolvedSetIterator Result
4632 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other,
Douglas Gregord5a423b2009-09-25 18:43:00 +00004633 D.getIdentifierLoc(),
4634 PartialDiagnostic(diag::err_explicit_instantiation_not_known) << Name,
4635 PartialDiagnostic(diag::err_explicit_instantiation_ambiguous) << Name,
4636 PartialDiagnostic(diag::note_explicit_instantiation_candidate));
4637
John McCallc373d482010-01-27 01:50:18 +00004638 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00004639 return true;
John McCallc373d482010-01-27 01:50:18 +00004640
4641 // Ignore access control bits, we don't need them for redeclaration checking.
4642 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregord5a423b2009-09-25 18:43:00 +00004643
Douglas Gregor0a897e32009-10-15 17:21:20 +00004644 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00004645 Diag(D.getIdentifierLoc(),
4646 diag::err_explicit_instantiation_member_function_not_instantiated)
4647 << Specialization
4648 << (Specialization->getTemplateSpecializationKind() ==
4649 TSK_ExplicitSpecialization);
4650 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
4651 return true;
Douglas Gregor0a897e32009-10-15 17:21:20 +00004652 }
Douglas Gregor558c0322009-10-14 23:41:34 +00004653
Douglas Gregor0a897e32009-10-15 17:21:20 +00004654 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00004655 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
4656 PrevDecl = Specialization;
4657
Douglas Gregor0a897e32009-10-15 17:21:20 +00004658 if (PrevDecl) {
4659 bool SuppressNew = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00004660 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
Douglas Gregor0a897e32009-10-15 17:21:20 +00004661 PrevDecl,
4662 PrevDecl->getTemplateSpecializationKind(),
4663 PrevDecl->getPointOfInstantiation(),
4664 SuppressNew))
4665 return true;
4666
4667 // FIXME: We may still want to build some representation of this
4668 // explicit specialization.
4669 if (SuppressNew)
4670 return DeclPtrTy();
4671 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00004672
4673 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregor0a897e32009-10-15 17:21:20 +00004674
4675 if (TSK == TSK_ExplicitInstantiationDefinition)
4676 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization,
4677 false, /*DefinitionRequired=*/true);
Douglas Gregor0a897e32009-10-15 17:21:20 +00004678
Douglas Gregor558c0322009-10-14 23:41:34 +00004679 // C++0x [temp.explicit]p2:
4680 // If the explicit instantiation is for a member function, a member class
4681 // or a static data member of a class template specialization, the name of
4682 // the class template specialization in the qualified-id for the member
4683 // name shall be a simple-template-id.
4684 //
4685 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00004686 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004687 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
Douglas Gregor558c0322009-10-14 23:41:34 +00004688 D.getCXXScopeSpec().isSet() &&
4689 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
4690 Diag(D.getIdentifierLoc(),
4691 diag::err_explicit_instantiation_without_qualified_id)
4692 << Specialization << D.getCXXScopeSpec().getRange();
4693
4694 CheckExplicitInstantiationScope(*this,
4695 FunTmpl? (NamedDecl *)FunTmpl
4696 : Specialization->getInstantiatedFromMemberFunction(),
4697 D.getIdentifierLoc(),
4698 D.getCXXScopeSpec().isSet());
4699
Douglas Gregord5a423b2009-09-25 18:43:00 +00004700 // FIXME: Create some kind of ExplicitInstantiationDecl here.
4701 return DeclPtrTy();
4702}
4703
Douglas Gregord57959a2009-03-27 23:10:48 +00004704Sema::TypeResult
John McCallc4e70192009-09-11 04:59:25 +00004705Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
4706 const CXXScopeSpec &SS, IdentifierInfo *Name,
4707 SourceLocation TagLoc, SourceLocation NameLoc) {
4708 // This has to hold, because SS is expected to be defined.
4709 assert(Name && "Expected a name in a dependent tag");
4710
4711 NestedNameSpecifier *NNS
4712 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4713 if (!NNS)
4714 return true;
4715
4716 QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc));
4717 if (T.isNull())
4718 return true;
4719
4720 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
4721 QualType ElabType = Context.getElaboratedType(T, TagKind);
4722
4723 return ElabType.getAsOpaquePtr();
4724}
4725
4726Sema::TypeResult
Douglas Gregord57959a2009-03-27 23:10:48 +00004727Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
4728 const IdentifierInfo &II, SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004729 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00004730 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
4731 if (!NNS)
4732 return true;
4733
4734 QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc));
Douglas Gregor31a19b62009-04-01 21:51:26 +00004735 if (T.isNull())
4736 return true;
Douglas Gregord57959a2009-03-27 23:10:48 +00004737 return T.getAsOpaquePtr();
4738}
4739
Douglas Gregor17343172009-04-01 00:28:59 +00004740Sema::TypeResult
4741Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
4742 SourceLocation TemplateLoc, TypeTy *Ty) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00004743 QualType T = GetTypeFromParser(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004744 NestedNameSpecifier *NNS
Douglas Gregor17343172009-04-01 00:28:59 +00004745 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Mike Stump1eb44332009-09-09 15:08:12 +00004746 const TemplateSpecializationType *TemplateId
John McCall183700f2009-09-21 23:43:11 +00004747 = T->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00004748 assert(TemplateId && "Expected a template specialization type");
4749
Douglas Gregor6946baf2009-09-02 13:05:45 +00004750 if (computeDeclContext(SS, false)) {
4751 // If we can compute a declaration context, then the "typename"
4752 // keyword was superfluous. Just build a QualifiedNameType to keep
4753 // track of the nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +00004754
Douglas Gregor6946baf2009-09-02 13:05:45 +00004755 // FIXME: Note that the QualifiedNameType had the "typename" keyword!
4756 return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr();
4757 }
Mike Stump1eb44332009-09-09 15:08:12 +00004758
Douglas Gregor6946baf2009-09-02 13:05:45 +00004759 return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr();
Douglas Gregor17343172009-04-01 00:28:59 +00004760}
4761
Douglas Gregord57959a2009-03-27 23:10:48 +00004762/// \brief Build the type that describes a C++ typename specifier,
4763/// e.g., "typename T::type".
4764QualType
4765Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II,
4766 SourceRange Range) {
Douglas Gregor42af25f2009-05-11 19:58:34 +00004767 CXXRecordDecl *CurrentInstantiation = 0;
4768 if (NNS->isDependent()) {
4769 CurrentInstantiation = getCurrentInstantiationOf(NNS);
Douglas Gregord57959a2009-03-27 23:10:48 +00004770
Douglas Gregor42af25f2009-05-11 19:58:34 +00004771 // If the nested-name-specifier does not refer to the current
4772 // instantiation, then build a typename type.
4773 if (!CurrentInstantiation)
4774 return Context.getTypenameType(NNS, &II);
Mike Stump1eb44332009-09-09 15:08:12 +00004775
Douglas Gregorde18d122009-09-02 13:12:51 +00004776 // The nested-name-specifier refers to the current instantiation, so the
4777 // "typename" keyword itself is superfluous. In C++03, the program is
Mike Stump1eb44332009-09-09 15:08:12 +00004778 // actually ill-formed. However, DR 382 (in C++0x CD1) allows such
Douglas Gregorde18d122009-09-02 13:12:51 +00004779 // extraneous "typename" keywords, and we retroactively apply this DR to
4780 // C++03 code.
Douglas Gregor42af25f2009-05-11 19:58:34 +00004781 }
Douglas Gregord57959a2009-03-27 23:10:48 +00004782
Douglas Gregor42af25f2009-05-11 19:58:34 +00004783 DeclContext *Ctx = 0;
4784
4785 if (CurrentInstantiation)
4786 Ctx = CurrentInstantiation;
4787 else {
4788 CXXScopeSpec SS;
4789 SS.setScopeRep(NNS);
4790 SS.setRange(Range);
4791 if (RequireCompleteDeclContext(SS))
4792 return QualType();
4793
4794 Ctx = computeDeclContext(SS);
4795 }
Douglas Gregord57959a2009-03-27 23:10:48 +00004796 assert(Ctx && "No declaration context?");
4797
4798 DeclarationName Name(&II);
John McCalla24dc2e2009-11-17 02:14:36 +00004799 LookupResult Result(*this, Name, Range.getEnd(), LookupOrdinaryName);
4800 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00004801 unsigned DiagID = 0;
4802 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00004803 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00004804 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00004805 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00004806 break;
Douglas Gregor7d3f5762010-01-15 01:44:47 +00004807
4808 case LookupResult::NotFoundInCurrentInstantiation:
4809 // Okay, it's a member of an unknown instantiation.
4810 return Context.getTypenameType(NNS, &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00004811
4812 case LookupResult::Found:
John McCallf36e02d2009-10-09 21:13:30 +00004813 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Douglas Gregord57959a2009-03-27 23:10:48 +00004814 // We found a type. Build a QualifiedNameType, since the
4815 // typename-specifier was just sugar. FIXME: Tell
4816 // QualifiedNameType that it has a "typename" prefix.
4817 return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type));
4818 }
4819
4820 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00004821 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00004822 break;
4823
John McCall7ba107a2009-11-18 02:36:19 +00004824 case LookupResult::FoundUnresolvedValue:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004825 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00004826 return QualType();
4827
Douglas Gregord57959a2009-03-27 23:10:48 +00004828 case LookupResult::FoundOverloaded:
4829 DiagID = diag::err_typename_nested_not_type;
4830 Referenced = *Result.begin();
4831 break;
4832
John McCall6e247262009-10-10 05:48:19 +00004833 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00004834 return QualType();
4835 }
4836
4837 // If we get here, it's because name lookup did not find a
4838 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregor3f093272009-10-13 21:16:44 +00004839 Diag(Range.getEnd(), DiagID) << Range << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00004840 if (Referenced)
4841 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
4842 << Name;
4843 return QualType();
4844}
Douglas Gregor4a959d82009-08-06 16:20:37 +00004845
4846namespace {
4847 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00004848 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00004849 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00004850 SourceLocation Loc;
4851 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00004852
Douglas Gregor4a959d82009-08-06 16:20:37 +00004853 public:
Mike Stump1eb44332009-09-09 15:08:12 +00004854 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00004855 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00004856 DeclarationName Entity)
4857 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00004858 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00004859
4860 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00004861 /// transformed.
4862 ///
4863 /// For the purposes of type reconstruction, a type has already been
4864 /// transformed if it is NULL or if it is not dependent.
4865 bool AlreadyTransformed(QualType T) {
4866 return T.isNull() || !T->isDependentType();
4867 }
Mike Stump1eb44332009-09-09 15:08:12 +00004868
4869 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00004870 /// rebuilt.
4871 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00004872
Douglas Gregor4a959d82009-08-06 16:20:37 +00004873 /// \brief Returns the name of the entity whose type is being rebuilt.
4874 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00004875
Douglas Gregor972e6ce2009-10-27 06:26:26 +00004876 /// \brief Sets the "base" location and entity when that
4877 /// information is known based on another transformation.
4878 void setBase(SourceLocation Loc, DeclarationName Entity) {
4879 this->Loc = Loc;
4880 this->Entity = Entity;
4881 }
4882
Douglas Gregor4a959d82009-08-06 16:20:37 +00004883 /// \brief Transforms an expression by returning the expression itself
4884 /// (an identity function).
4885 ///
4886 /// FIXME: This is completely unsafe; we will need to actually clone the
4887 /// expressions.
4888 Sema::OwningExprResult TransformExpr(Expr *E) {
4889 return getSema().Owned(E);
4890 }
Mike Stump1eb44332009-09-09 15:08:12 +00004891
Douglas Gregor4a959d82009-08-06 16:20:37 +00004892 /// \brief Transforms a typename type by determining whether the type now
4893 /// refers to a member of the current instantiation, and then
4894 /// type-checking and building a QualifiedNameType (when possible).
John McCalla2becad2009-10-21 00:40:46 +00004895 QualType TransformTypenameType(TypeLocBuilder &TLB, TypenameTypeLoc TL);
Douglas Gregor4a959d82009-08-06 16:20:37 +00004896 };
4897}
4898
Mike Stump1eb44332009-09-09 15:08:12 +00004899QualType
John McCalla2becad2009-10-21 00:40:46 +00004900CurrentInstantiationRebuilder::TransformTypenameType(TypeLocBuilder &TLB,
4901 TypenameTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00004902 TypenameType *T = TL.getTypePtr();
John McCalla2becad2009-10-21 00:40:46 +00004903
Douglas Gregor4a959d82009-08-06 16:20:37 +00004904 NestedNameSpecifier *NNS
4905 = TransformNestedNameSpecifier(T->getQualifier(),
4906 /*FIXME:*/SourceRange(getBaseLocation()));
4907 if (!NNS)
4908 return QualType();
4909
4910 // If the nested-name-specifier did not change, and we cannot compute the
4911 // context corresponding to the nested-name-specifier, then this
4912 // typename type will not change; exit early.
4913 CXXScopeSpec SS;
4914 SS.setRange(SourceRange(getBaseLocation()));
4915 SS.setScopeRep(NNS);
John McCall833ca992009-10-29 08:12:44 +00004916
4917 QualType Result;
Douglas Gregor4a959d82009-08-06 16:20:37 +00004918 if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0)
John McCall833ca992009-10-29 08:12:44 +00004919 Result = QualType(T, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00004920
4921 // Rebuild the typename type, which will probably turn into a
Douglas Gregor4a959d82009-08-06 16:20:37 +00004922 // QualifiedNameType.
John McCall833ca992009-10-29 08:12:44 +00004923 else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004924 QualType NewTemplateId
Douglas Gregor4a959d82009-08-06 16:20:37 +00004925 = TransformType(QualType(TemplateId, 0));
4926 if (NewTemplateId.isNull())
4927 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004928
Douglas Gregor4a959d82009-08-06 16:20:37 +00004929 if (NNS == T->getQualifier() &&
4930 NewTemplateId == QualType(TemplateId, 0))
John McCall833ca992009-10-29 08:12:44 +00004931 Result = QualType(T, 0);
4932 else
4933 Result = getDerived().RebuildTypenameType(NNS, NewTemplateId);
4934 } else
4935 Result = getDerived().RebuildTypenameType(NNS, T->getIdentifier(),
4936 SourceRange(TL.getNameLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +00004937
John McCall833ca992009-10-29 08:12:44 +00004938 TypenameTypeLoc NewTL = TLB.push<TypenameTypeLoc>(Result);
4939 NewTL.setNameLoc(TL.getNameLoc());
4940 return Result;
Douglas Gregor4a959d82009-08-06 16:20:37 +00004941}
4942
4943/// \brief Rebuilds a type within the context of the current instantiation.
4944///
Mike Stump1eb44332009-09-09 15:08:12 +00004945/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00004946/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00004947/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00004948/// partial specialization thereof). This routine will rebuild that type now
4949/// that we have entered the declarator's scope, which may produce different
4950/// canonical types, e.g.,
4951///
4952/// \code
4953/// template<typename T>
4954/// struct X {
4955/// typedef T* pointer;
4956/// pointer data();
4957/// };
4958///
4959/// template<typename T>
4960/// typename X<T>::pointer X<T>::data() { ... }
4961/// \endcode
4962///
4963/// Here, the type "typename X<T>::pointer" will be created as a TypenameType,
4964/// since we do not know that we can look into X<T> when we parsed the type.
4965/// This function will rebuild the type, performing the lookup of "pointer"
4966/// in X<T> and returning a QualifiedNameType whose canonical type is the same
4967/// as the canonical type of T*, allowing the return types of the out-of-line
4968/// definition and the declaration to match.
4969QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc,
4970 DeclarationName Name) {
4971 if (T.isNull() || !T->isDependentType())
4972 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00004973
Douglas Gregor4a959d82009-08-06 16:20:37 +00004974 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
4975 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00004976}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00004977
4978/// \brief Produces a formatted string that describes the binding of
4979/// template parameters to template arguments.
4980std::string
4981Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
4982 const TemplateArgumentList &Args) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00004983 // FIXME: For variadic templates, we'll need to get the structured list.
4984 return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(),
4985 Args.flat_size());
4986}
4987
4988std::string
4989Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
4990 const TemplateArgument *Args,
4991 unsigned NumArgs) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00004992 std::string Result;
4993
Douglas Gregor9148c3f2009-11-11 19:13:48 +00004994 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregorbf4ea562009-09-15 16:23:51 +00004995 return Result;
4996
4997 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00004998 if (I >= NumArgs)
4999 break;
5000
Douglas Gregorbf4ea562009-09-15 16:23:51 +00005001 if (I == 0)
5002 Result += "[with ";
5003 else
5004 Result += ", ";
5005
5006 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
5007 Result += Id->getName();
5008 } else {
5009 Result += '$';
5010 Result += llvm::utostr(I);
5011 }
5012
5013 Result += " = ";
5014
5015 switch (Args[I].getKind()) {
5016 case TemplateArgument::Null:
5017 Result += "<no value>";
5018 break;
5019
5020 case TemplateArgument::Type: {
5021 std::string TypeStr;
5022 Args[I].getAsType().getAsStringInternal(TypeStr,
5023 Context.PrintingPolicy);
5024 Result += TypeStr;
5025 break;
5026 }
5027
5028 case TemplateArgument::Declaration: {
5029 bool Unnamed = true;
5030 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) {
5031 if (ND->getDeclName()) {
5032 Unnamed = false;
5033 Result += ND->getNameAsString();
5034 }
5035 }
5036
5037 if (Unnamed) {
5038 Result += "<anonymous>";
5039 }
5040 break;
5041 }
5042
Douglas Gregor788cd062009-11-11 01:00:40 +00005043 case TemplateArgument::Template: {
5044 std::string Str;
5045 llvm::raw_string_ostream OS(Str);
5046 Args[I].getAsTemplate().print(OS, Context.PrintingPolicy);
5047 Result += OS.str();
5048 break;
5049 }
5050
Douglas Gregorbf4ea562009-09-15 16:23:51 +00005051 case TemplateArgument::Integral: {
5052 Result += Args[I].getAsIntegral()->toString(10);
5053 break;
5054 }
5055
5056 case TemplateArgument::Expression: {
5057 assert(false && "No expressions in deduced template arguments!");
5058 Result += "<expression>";
5059 break;
5060 }
5061
5062 case TemplateArgument::Pack:
5063 // FIXME: Format template argument packs
5064 Result += "<template argument pack>";
5065 break;
5066 }
5067 }
5068
5069 Result += ']';
5070 return Result;
5071}