blob: 74120498573b9a709f237d640ca85382b9783d3a [file] [log] [blame]
Douglas Gregor5101c242008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor5101c242008-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 Gregorfe1e1102009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor5101c242008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregorfe1e1102009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor5101c242008-12-05 18:15:24 +000011
12#include "Sema.h"
Douglas Gregor15acfb92009-08-06 16:20:37 +000013#include "TreeTransform.h"
Douglas Gregorcd72ba92009-02-06 22:42:48 +000014#include "clang/AST/ASTContext.h"
Douglas Gregor4619e432008-12-05 23:32:09 +000015#include "clang/AST/Expr.h"
Douglas Gregorccb07762009-02-11 19:52:55 +000016#include "clang/AST/ExprCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregor5101c242008-12-05 18:15:24 +000018#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregor450f00842009-09-25 18:43:00 +000020#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregor15acfb92009-08-06 16:20:37 +000021#include "llvm/Support/Compiler.h"
Douglas Gregorbe999392009-09-15 16:23:51 +000022#include "llvm/ADT/StringExtras.h"
Douglas Gregor5101c242008-12-05 18:15:24 +000023using namespace clang;
24
Douglas Gregorb7bfe792009-09-02 22:59:36 +000025/// \brief Determine whether the declaration found is acceptable as the name
26/// of a template and, if so, return that template declaration. Otherwise,
27/// returns NULL.
28static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) {
29 if (!D)
30 return 0;
Mike Stump11289f42009-09-09 15:08:12 +000031
Douglas Gregorb7bfe792009-09-02 22:59:36 +000032 if (isa<TemplateDecl>(D))
33 return D;
Mike Stump11289f42009-09-09 15:08:12 +000034
Douglas Gregorb7bfe792009-09-02 22:59:36 +000035 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
36 // C++ [temp.local]p1:
37 // Like normal (non-template) classes, class templates have an
38 // injected-class-name (Clause 9). The injected-class-name
39 // can be used with or without a template-argument-list. When
40 // it is used without a template-argument-list, it is
41 // equivalent to the injected-class-name followed by the
42 // template-parameters of the class template enclosed in
43 // <>. When it is used with a template-argument-list, it
44 // refers to the specified class template specialization,
45 // which could be the current specialization or another
46 // specialization.
47 if (Record->isInjectedClassName()) {
48 Record = cast<CXXRecordDecl>(Record->getCanonicalDecl());
49 if (Record->getDescribedClassTemplate())
50 return Record->getDescribedClassTemplate();
51
52 if (ClassTemplateSpecializationDecl *Spec
53 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
54 return Spec->getSpecializedTemplate();
55 }
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregorb7bfe792009-09-02 22:59:36 +000057 return 0;
58 }
Mike Stump11289f42009-09-09 15:08:12 +000059
Douglas Gregorb7bfe792009-09-02 22:59:36 +000060 OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D);
61 if (!Ovl)
62 return 0;
Mike Stump11289f42009-09-09 15:08:12 +000063
Douglas Gregorb7bfe792009-09-02 22:59:36 +000064 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
65 FEnd = Ovl->function_end();
66 F != FEnd; ++F) {
67 if (FunctionTemplateDecl *FuncTmpl = dyn_cast<FunctionTemplateDecl>(*F)) {
68 // We've found a function template. Determine whether there are
69 // any other function templates we need to bundle together in an
70 // OverloadedFunctionDecl
71 for (++F; F != FEnd; ++F) {
72 if (isa<FunctionTemplateDecl>(*F))
73 break;
74 }
Mike Stump11289f42009-09-09 15:08:12 +000075
Douglas Gregorb7bfe792009-09-02 22:59:36 +000076 if (F != FEnd) {
77 // Build an overloaded function decl containing only the
78 // function templates in Ovl.
Mike Stump11289f42009-09-09 15:08:12 +000079 OverloadedFunctionDecl *OvlTemplate
Douglas Gregorb7bfe792009-09-02 22:59:36 +000080 = OverloadedFunctionDecl::Create(Context,
81 Ovl->getDeclContext(),
82 Ovl->getDeclName());
83 OvlTemplate->addOverload(FuncTmpl);
84 OvlTemplate->addOverload(*F);
85 for (++F; F != FEnd; ++F) {
86 if (isa<FunctionTemplateDecl>(*F))
87 OvlTemplate->addOverload(*F);
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Douglas Gregorb7bfe792009-09-02 22:59:36 +000090 return OvlTemplate;
91 }
92
93 return FuncTmpl;
94 }
95 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Douglas Gregorb7bfe792009-09-02 22:59:36 +000097 return 0;
98}
99
100TemplateNameKind Sema::isTemplateName(Scope *S,
Mike Stump11289f42009-09-09 15:08:12 +0000101 const IdentifierInfo &II,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000102 SourceLocation IdLoc,
Douglas Gregore861bac2009-08-25 22:51:20 +0000103 const CXXScopeSpec *SS,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000104 TypeTy *ObjectTypePtr,
Douglas Gregore861bac2009-08-25 22:51:20 +0000105 bool EnteringContext,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000106 TemplateTy &TemplateResult) {
107 // Determine where to perform name lookup
108 DeclContext *LookupCtx = 0;
109 bool isDependent = false;
110 if (ObjectTypePtr) {
111 // This nested-name-specifier occurs in a member access expression, e.g.,
112 // x->B::f, and we are looking into the type of the object.
Mike Stump11289f42009-09-09 15:08:12 +0000113 assert((!SS || !SS->isSet()) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000114 "ObjectType and scope specifier cannot coexist");
115 QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr);
116 LookupCtx = computeDeclContext(ObjectType);
117 isDependent = ObjectType->isDependentType();
118 } else if (SS && SS->isSet()) {
119 // This nested-name-specifier occurs after another nested-name-specifier,
120 // so long into the context associated with the prior nested-name-specifier.
121
122 LookupCtx = computeDeclContext(*SS, EnteringContext);
123 isDependent = isDependentScopeSpecifier(*SS);
124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000126 LookupResult Found;
127 bool ObjectTypeSearchedInScope = false;
128 if (LookupCtx) {
129 // Perform "qualified" name lookup into the declaration context we
130 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000131 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000132 // nested-name-specifier.
133
134 // The declaration context must be complete.
135 if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(*SS))
136 return TNK_Non_template;
Mike Stump11289f42009-09-09 15:08:12 +0000137
John McCall9f3059a2009-10-09 21:13:30 +0000138 LookupQualifiedName(Found, LookupCtx, &II, LookupOrdinaryName);
Mike Stump11289f42009-09-09 15:08:12 +0000139
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000140 if (ObjectTypePtr && Found.getKind() == LookupResult::NotFound) {
141 // C++ [basic.lookup.classref]p1:
142 // In a class member access expression (5.2.5), if the . or -> token is
Mike Stump11289f42009-09-09 15:08:12 +0000143 // immediately followed by an identifier followed by a <, the
144 // identifier must be looked up to determine whether the < is the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000145 // beginning of a template argument list (14.2) or a less-than operator.
Mike Stump11289f42009-09-09 15:08:12 +0000146 // The identifier is first looked up in the class of the object
147 // expression. If the identifier is not found, it is then looked up in
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000148 // the context of the entire postfix-expression and shall name a class
149 // or function template.
150 //
151 // FIXME: When we're instantiating a template, do we actually have to
152 // look in the scope of the template? Seems fishy...
John McCall9f3059a2009-10-09 21:13:30 +0000153 LookupName(Found, S, &II, LookupOrdinaryName);
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000154 ObjectTypeSearchedInScope = true;
155 }
156 } else if (isDependent) {
Mike Stump11289f42009-09-09 15:08:12 +0000157 // We cannot look into a dependent object type or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000158 return TNK_Non_template;
159 } else {
160 // Perform unqualified name lookup in the current scope.
John McCall9f3059a2009-10-09 21:13:30 +0000161 LookupName(Found, S, &II, LookupOrdinaryName);
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000162 }
Mike Stump11289f42009-09-09 15:08:12 +0000163
Douglas Gregore861bac2009-08-25 22:51:20 +0000164 // FIXME: Cope with ambiguous name-lookup results.
Mike Stump11289f42009-09-09 15:08:12 +0000165 assert(!Found.isAmbiguous() &&
Douglas Gregore861bac2009-08-25 22:51:20 +0000166 "Cannot handle template name-lookup ambiguities");
Douglas Gregordc572a32009-03-30 22:58:21 +0000167
John McCall9f3059a2009-10-09 21:13:30 +0000168 NamedDecl *Template
169 = isAcceptableTemplateName(Context, Found.getAsSingleDecl(Context));
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000170 if (!Template)
171 return TNK_Non_template;
172
173 if (ObjectTypePtr && !ObjectTypeSearchedInScope) {
174 // C++ [basic.lookup.classref]p1:
Mike Stump11289f42009-09-09 15:08:12 +0000175 // [...] If the lookup in the class of the object expression finds a
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000176 // template, the name is also looked up in the context of the entire
177 // postfix-expression and [...]
178 //
John McCall9f3059a2009-10-09 21:13:30 +0000179 LookupResult FoundOuter;
180 LookupName(FoundOuter, S, &II, LookupOrdinaryName);
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000181 // FIXME: Handle ambiguities in this lookup better
John McCall9f3059a2009-10-09 21:13:30 +0000182 NamedDecl *OuterTemplate
183 = isAcceptableTemplateName(Context, FoundOuter.getAsSingleDecl(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000184
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000185 if (!OuterTemplate) {
Mike Stump11289f42009-09-09 15:08:12 +0000186 // - if the name is not found, the name found in the class of the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000187 // object expression is used, otherwise
188 } else if (!isa<ClassTemplateDecl>(OuterTemplate)) {
Mike Stump11289f42009-09-09 15:08:12 +0000189 // - if the name is found in the context of the entire
190 // postfix-expression and does not name a class template, the name
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000191 // found in the class of the object expression is used, otherwise
192 } else {
193 // - if the name found is a class template, it must refer to the same
Mike Stump11289f42009-09-09 15:08:12 +0000194 // entity as the one found in the class of the object expression,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000195 // otherwise the program is ill-formed.
196 if (OuterTemplate->getCanonicalDecl() != Template->getCanonicalDecl()) {
197 Diag(IdLoc, diag::err_nested_name_member_ref_lookup_ambiguous)
198 << &II;
199 Diag(Template->getLocation(), diag::note_ambig_member_ref_object_type)
200 << QualType::getFromOpaquePtr(ObjectTypePtr);
201 Diag(OuterTemplate->getLocation(), diag::note_ambig_member_ref_scope);
Mike Stump11289f42009-09-09 15:08:12 +0000202
203 // Recover by taking the template that we found in the object
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000204 // expression's type.
Douglas Gregor97f1f1c2009-03-26 00:10:35 +0000205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206 }
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000207 }
Mike Stump11289f42009-09-09 15:08:12 +0000208
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000209 if (SS && SS->isSet() && !SS->isInvalid()) {
Mike Stump11289f42009-09-09 15:08:12 +0000210 NestedNameSpecifier *Qualifier
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000211 = static_cast<NestedNameSpecifier *>(SS->getScopeRep());
Mike Stump11289f42009-09-09 15:08:12 +0000212 if (OverloadedFunctionDecl *Ovl
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000213 = dyn_cast<OverloadedFunctionDecl>(Template))
Mike Stump11289f42009-09-09 15:08:12 +0000214 TemplateResult
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000215 = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
216 Ovl));
217 else
Mike Stump11289f42009-09-09 15:08:12 +0000218 TemplateResult
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000219 = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false,
Mike Stump11289f42009-09-09 15:08:12 +0000220 cast<TemplateDecl>(Template)));
221 } else if (OverloadedFunctionDecl *Ovl
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000222 = dyn_cast<OverloadedFunctionDecl>(Template)) {
223 TemplateResult = TemplateTy::make(TemplateName(Ovl));
224 } else {
225 TemplateResult = TemplateTy::make(
226 TemplateName(cast<TemplateDecl>(Template)));
227 }
Mike Stump11289f42009-09-09 15:08:12 +0000228
229 if (isa<ClassTemplateDecl>(Template) ||
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000230 isa<TemplateTemplateParmDecl>(Template))
231 return TNK_Type_template;
Mike Stump11289f42009-09-09 15:08:12 +0000232
233 assert((isa<FunctionTemplateDecl>(Template) ||
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000234 isa<OverloadedFunctionDecl>(Template)) &&
235 "Unhandled template kind in Sema::isTemplateName");
236 return TNK_Function_template;
Douglas Gregor55ad91f2008-12-18 19:37:40 +0000237}
238
Douglas Gregor5101c242008-12-05 18:15:24 +0000239/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
240/// that the template parameter 'PrevDecl' is being shadowed by a new
241/// declaration at location Loc. Returns true to indicate that this is
242/// an error, and false otherwise.
243bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregor5daeee22008-12-08 18:40:42 +0000244 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor5101c242008-12-05 18:15:24 +0000245
246 // Microsoft Visual C++ permits template parameters to be shadowed.
247 if (getLangOptions().Microsoft)
248 return false;
249
250 // C++ [temp.local]p4:
251 // A template-parameter shall not be redeclared within its
252 // scope (including nested scopes).
Mike Stump11289f42009-09-09 15:08:12 +0000253 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor5101c242008-12-05 18:15:24 +0000254 << cast<NamedDecl>(PrevDecl)->getDeclName();
255 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
256 return true;
257}
258
Douglas Gregor463421d2009-03-03 04:44:36 +0000259/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000260/// the parameter D to reference the templated declaration and return a pointer
261/// to the template declaration. Otherwise, do nothing to D and return null.
Chris Lattner83f095c2009-03-28 19:18:32 +0000262TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) {
Douglas Gregor27c26e92009-10-06 21:27:51 +0000263 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000264 D = DeclPtrTy::make(Temp->getTemplatedDecl());
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000265 return Temp;
266 }
267 return 0;
268}
269
Douglas Gregor5101c242008-12-05 18:15:24 +0000270/// ActOnTypeParameter - Called when a C++ template type parameter
271/// (e.g., "typename T") has been parsed. Typename specifies whether
272/// the keyword "typename" was used to declare the type parameter
273/// (otherwise, "class" was used), and KeyLoc is the location of the
274/// "class" or "typename" keyword. ParamName is the name of the
275/// parameter (NULL indicates an unnamed template parameter) and
Mike Stump11289f42009-09-09 15:08:12 +0000276/// ParamName is the location of the parameter name (if any).
Douglas Gregor5101c242008-12-05 18:15:24 +0000277/// If the type parameter has a default argument, it will be added
278/// later via ActOnTypeParameterDefault.
Mike Stump11289f42009-09-09 15:08:12 +0000279Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
Anders Carlsson01e9e932009-06-12 19:58:00 +0000280 SourceLocation EllipsisLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000281 SourceLocation KeyLoc,
282 IdentifierInfo *ParamName,
283 SourceLocation ParamNameLoc,
284 unsigned Depth, unsigned Position) {
Mike Stump11289f42009-09-09 15:08:12 +0000285 assert(S->isTemplateParamScope() &&
286 "Template type parameter not in template parameter scope!");
Douglas Gregor5101c242008-12-05 18:15:24 +0000287 bool Invalid = false;
288
289 if (ParamName) {
John McCall9f3059a2009-10-09 21:13:30 +0000290 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName);
Douglas Gregor5daeee22008-12-08 18:40:42 +0000291 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor5101c242008-12-05 18:15:24 +0000292 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000293 PrevDecl);
Douglas Gregor5101c242008-12-05 18:15:24 +0000294 }
295
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000296 SourceLocation Loc = ParamNameLoc;
297 if (!ParamName)
298 Loc = KeyLoc;
299
Douglas Gregor5101c242008-12-05 18:15:24 +0000300 TemplateTypeParmDecl *Param
Mike Stump11289f42009-09-09 15:08:12 +0000301 = TemplateTypeParmDecl::Create(Context, CurContext, Loc,
302 Depth, Position, ParamName, Typename,
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000303 Ellipsis);
Douglas Gregor5101c242008-12-05 18:15:24 +0000304 if (Invalid)
305 Param->setInvalidDecl();
306
307 if (ParamName) {
308 // Add the template parameter into the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +0000309 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregor5101c242008-12-05 18:15:24 +0000310 IdResolver.AddDecl(Param);
311 }
312
Chris Lattner83f095c2009-03-28 19:18:32 +0000313 return DeclPtrTy::make(Param);
Douglas Gregor5101c242008-12-05 18:15:24 +0000314}
315
Douglas Gregordba32632009-02-10 19:49:53 +0000316/// ActOnTypeParameterDefault - Adds a default argument (the type
Mike Stump11289f42009-09-09 15:08:12 +0000317/// Default) to the given template type parameter (TypeParam).
318void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam,
Douglas Gregordba32632009-02-10 19:49:53 +0000319 SourceLocation EqualLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000320 SourceLocation DefaultLoc,
Douglas Gregordba32632009-02-10 19:49:53 +0000321 TypeTy *DefaultT) {
Mike Stump11289f42009-09-09 15:08:12 +0000322 TemplateTypeParmDecl *Parm
Chris Lattner83f095c2009-03-28 19:18:32 +0000323 = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>());
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +0000324 // FIXME: Preserve type source info.
325 QualType Default = GetTypeFromParser(DefaultT);
Douglas Gregordba32632009-02-10 19:49:53 +0000326
Anders Carlssond3824352009-06-12 22:30:13 +0000327 // C++0x [temp.param]p9:
328 // A default template-argument may be specified for any kind of
Mike Stump11289f42009-09-09 15:08:12 +0000329 // template-parameter that is not a template parameter pack.
Anders Carlssond3824352009-06-12 22:30:13 +0000330 if (Parm->isParameterPack()) {
331 Diag(DefaultLoc, diag::err_template_param_pack_default_arg);
Anders Carlssond3824352009-06-12 22:30:13 +0000332 return;
333 }
Mike Stump11289f42009-09-09 15:08:12 +0000334
Douglas Gregordba32632009-02-10 19:49:53 +0000335 // C++ [temp.param]p14:
336 // A template-parameter shall not be used in its own default argument.
337 // FIXME: Implement this check! Needs a recursive walk over the types.
Mike Stump11289f42009-09-09 15:08:12 +0000338
Douglas Gregordba32632009-02-10 19:49:53 +0000339 // Check the template argument itself.
340 if (CheckTemplateArgument(Parm, Default, DefaultLoc)) {
341 Parm->setInvalidDecl();
342 return;
343 }
344
345 Parm->setDefaultArgument(Default, DefaultLoc, false);
346}
347
Douglas Gregor463421d2009-03-03 04:44:36 +0000348/// \brief Check that the type of a non-type template parameter is
349/// well-formed.
350///
351/// \returns the (possibly-promoted) parameter type if valid;
352/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump11289f42009-09-09 15:08:12 +0000353QualType
Douglas Gregor463421d2009-03-03 04:44:36 +0000354Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
355 // C++ [temp.param]p4:
356 //
357 // A non-type template-parameter shall have one of the following
358 // (optionally cv-qualified) types:
359 //
360 // -- integral or enumeration type,
361 if (T->isIntegralType() || T->isEnumeralType() ||
Mike Stump11289f42009-09-09 15:08:12 +0000362 // -- pointer to object or pointer to function,
363 (T->isPointerType() &&
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000364 (T->getAs<PointerType>()->getPointeeType()->isObjectType() ||
365 T->getAs<PointerType>()->getPointeeType()->isFunctionType())) ||
Mike Stump11289f42009-09-09 15:08:12 +0000366 // -- reference to object or reference to function,
Douglas Gregor463421d2009-03-03 04:44:36 +0000367 T->isReferenceType() ||
368 // -- pointer to member.
369 T->isMemberPointerType() ||
370 // If T is a dependent type, we can't do the check now, so we
371 // assume that it is well-formed.
372 T->isDependentType())
373 return T;
374 // C++ [temp.param]p8:
375 //
376 // A non-type template-parameter of type "array of T" or
377 // "function returning T" is adjusted to be of type "pointer to
378 // T" or "pointer to function returning T", respectively.
379 else if (T->isArrayType())
380 // FIXME: Keep the type prior to promotion?
381 return Context.getArrayDecayedType(T);
382 else if (T->isFunctionType())
383 // FIXME: Keep the type prior to promotion?
384 return Context.getPointerType(T);
385
386 Diag(Loc, diag::err_template_nontype_parm_bad_type)
387 << T;
388
389 return QualType();
390}
391
Douglas Gregor5101c242008-12-05 18:15:24 +0000392/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
393/// template parameter (e.g., "int Size" in "template<int Size>
394/// class Array") has been parsed. S is the current scope and D is
395/// the parsed declarator.
Chris Lattner83f095c2009-03-28 19:18:32 +0000396Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
Mike Stump11289f42009-09-09 15:08:12 +0000397 unsigned Depth,
Chris Lattner83f095c2009-03-28 19:18:32 +0000398 unsigned Position) {
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000399 DeclaratorInfo *DInfo = 0;
400 QualType T = GetTypeForDeclarator(D, S, &DInfo);
Douglas Gregor5101c242008-12-05 18:15:24 +0000401
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000402 assert(S->isTemplateParamScope() &&
403 "Non-type template parameter not in template parameter scope!");
Douglas Gregor5101c242008-12-05 18:15:24 +0000404 bool Invalid = false;
405
406 IdentifierInfo *ParamName = D.getIdentifier();
407 if (ParamName) {
John McCall9f3059a2009-10-09 21:13:30 +0000408 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName);
Douglas Gregor5daeee22008-12-08 18:40:42 +0000409 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor5101c242008-12-05 18:15:24 +0000410 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000411 PrevDecl);
Douglas Gregor5101c242008-12-05 18:15:24 +0000412 }
413
Douglas Gregor463421d2009-03-03 04:44:36 +0000414 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
Douglas Gregorce0fc86f2009-03-09 16:46:39 +0000415 if (T.isNull()) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000416 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorce0fc86f2009-03-09 16:46:39 +0000417 Invalid = true;
418 }
Douglas Gregor81338792009-02-10 17:43:50 +0000419
Douglas Gregor5101c242008-12-05 18:15:24 +0000420 NonTypeTemplateParmDecl *Param
421 = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000422 Depth, Position, ParamName, T, DInfo);
Douglas Gregor5101c242008-12-05 18:15:24 +0000423 if (Invalid)
424 Param->setInvalidDecl();
425
426 if (D.getIdentifier()) {
427 // Add the template parameter into the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +0000428 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregor5101c242008-12-05 18:15:24 +0000429 IdResolver.AddDecl(Param);
430 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000431 return DeclPtrTy::make(Param);
Douglas Gregor5101c242008-12-05 18:15:24 +0000432}
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000433
Douglas Gregordba32632009-02-10 19:49:53 +0000434/// \brief Adds a default argument to the given non-type template
435/// parameter.
Chris Lattner83f095c2009-03-28 19:18:32 +0000436void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregordba32632009-02-10 19:49:53 +0000437 SourceLocation EqualLoc,
438 ExprArg DefaultE) {
Mike Stump11289f42009-09-09 15:08:12 +0000439 NonTypeTemplateParmDecl *TemplateParm
Chris Lattner83f095c2009-03-28 19:18:32 +0000440 = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregordba32632009-02-10 19:49:53 +0000441 Expr *Default = static_cast<Expr *>(DefaultE.get());
Mike Stump11289f42009-09-09 15:08:12 +0000442
Douglas Gregordba32632009-02-10 19:49:53 +0000443 // C++ [temp.param]p14:
444 // A template-parameter shall not be used in its own default argument.
445 // FIXME: Implement this check! Needs a recursive walk over the types.
Mike Stump11289f42009-09-09 15:08:12 +0000446
Douglas Gregordba32632009-02-10 19:49:53 +0000447 // Check the well-formedness of the default template argument.
Douglas Gregor74eba0b2009-06-11 18:10:32 +0000448 TemplateArgument Converted;
449 if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default,
450 Converted)) {
Douglas Gregordba32632009-02-10 19:49:53 +0000451 TemplateParm->setInvalidDecl();
452 return;
453 }
454
Anders Carlssonb781bcd2009-05-01 19:49:17 +0000455 TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>());
Douglas Gregordba32632009-02-10 19:49:53 +0000456}
457
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000458
459/// ActOnTemplateTemplateParameter - Called when a C++ template template
460/// parameter (e.g. T in template <template <typename> class T> class array)
461/// has been parsed. S is the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +0000462Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S,
463 SourceLocation TmpLoc,
464 TemplateParamsTy *Params,
465 IdentifierInfo *Name,
466 SourceLocation NameLoc,
467 unsigned Depth,
Mike Stump11289f42009-09-09 15:08:12 +0000468 unsigned Position) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000469 assert(S->isTemplateParamScope() &&
470 "Template template parameter not in template parameter scope!");
471
472 // Construct the parameter object.
473 TemplateTemplateParmDecl *Param =
474 TemplateTemplateParmDecl::Create(Context, CurContext, TmpLoc, Depth,
475 Position, Name,
476 (TemplateParameterList*)Params);
477
478 // Make sure the parameter is valid.
479 // FIXME: Decl object is not currently invalidated anywhere so this doesn't
480 // do anything yet. However, if the template parameter list or (eventual)
481 // default value is ever invalidated, that will propagate here.
482 bool Invalid = false;
483 if (Invalid) {
484 Param->setInvalidDecl();
485 }
486
487 // If the tt-param has a name, then link the identifier into the scope
488 // and lookup mechanisms.
489 if (Name) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000490 S->AddDecl(DeclPtrTy::make(Param));
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000491 IdResolver.AddDecl(Param);
492 }
493
Chris Lattner83f095c2009-03-28 19:18:32 +0000494 return DeclPtrTy::make(Param);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000495}
496
Douglas Gregordba32632009-02-10 19:49:53 +0000497/// \brief Adds a default argument to the given template template
498/// parameter.
Chris Lattner83f095c2009-03-28 19:18:32 +0000499void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD,
Douglas Gregordba32632009-02-10 19:49:53 +0000500 SourceLocation EqualLoc,
501 ExprArg DefaultE) {
Mike Stump11289f42009-09-09 15:08:12 +0000502 TemplateTemplateParmDecl *TemplateParm
Chris Lattner83f095c2009-03-28 19:18:32 +0000503 = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>());
Douglas Gregordba32632009-02-10 19:49:53 +0000504
505 // Since a template-template parameter's default argument is an
506 // id-expression, it must be a DeclRefExpr.
Mike Stump11289f42009-09-09 15:08:12 +0000507 DeclRefExpr *Default
Douglas Gregordba32632009-02-10 19:49:53 +0000508 = cast<DeclRefExpr>(static_cast<Expr *>(DefaultE.get()));
509
510 // C++ [temp.param]p14:
511 // A template-parameter shall not be used in its own default argument.
512 // FIXME: Implement this check! Needs a recursive walk over the types.
513
514 // Check the well-formedness of the template argument.
515 if (!isa<TemplateDecl>(Default->getDecl())) {
Mike Stump11289f42009-09-09 15:08:12 +0000516 Diag(Default->getSourceRange().getBegin(),
Douglas Gregordba32632009-02-10 19:49:53 +0000517 diag::err_template_arg_must_be_template)
518 << Default->getSourceRange();
519 TemplateParm->setInvalidDecl();
520 return;
Mike Stump11289f42009-09-09 15:08:12 +0000521 }
Douglas Gregordba32632009-02-10 19:49:53 +0000522 if (CheckTemplateArgument(TemplateParm, Default)) {
523 TemplateParm->setInvalidDecl();
524 return;
525 }
526
527 DefaultE.release();
528 TemplateParm->setDefaultArgument(Default);
529}
530
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000531/// ActOnTemplateParameterList - Builds a TemplateParameterList that
532/// contains the template parameters in Params/NumParams.
533Sema::TemplateParamsTy *
534Sema::ActOnTemplateParameterList(unsigned Depth,
535 SourceLocation ExportLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000536 SourceLocation TemplateLoc,
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000537 SourceLocation LAngleLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +0000538 DeclPtrTy *Params, unsigned NumParams,
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000539 SourceLocation RAngleLoc) {
540 if (ExportLoc.isValid())
541 Diag(ExportLoc, diag::note_template_export_unsupported);
542
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000543 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +0000544 (NamedDecl**)Params, NumParams,
545 RAngleLoc);
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000546}
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000547
Douglas Gregorc08f4892009-03-25 00:13:59 +0000548Sema::DeclResult
John McCall9bb74a52009-07-31 02:45:11 +0000549Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000550 SourceLocation KWLoc, const CXXScopeSpec &SS,
551 IdentifierInfo *Name, SourceLocation NameLoc,
552 AttributeList *Attr,
Douglas Gregor1d5e9f92009-08-25 17:23:04 +0000553 TemplateParameterList *TemplateParams,
Anders Carlssondfbbdf62009-03-26 00:52:18 +0000554 AccessSpecifier AS) {
Mike Stump11289f42009-09-09 15:08:12 +0000555 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor1d5e9f92009-08-25 17:23:04 +0000556 "No template parameters");
John McCall9bb74a52009-07-31 02:45:11 +0000557 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregordba32632009-02-10 19:49:53 +0000558 bool Invalid = false;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000559
560 // Check that we can declare a template here.
Douglas Gregor1d5e9f92009-08-25 17:23:04 +0000561 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregorc08f4892009-03-25 00:13:59 +0000562 return true;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000563
John McCall27b5c252009-09-14 21:59:20 +0000564 TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec);
565 assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type");
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000566
567 // There is no such thing as an unnamed class template.
568 if (!Name) {
569 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregorc08f4892009-03-25 00:13:59 +0000570 return true;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000571 }
572
573 // Find any previous declaration with this name.
Douglas Gregor1d5e9f92009-08-25 17:23:04 +0000574 DeclContext *SemanticContext;
575 LookupResult Previous;
576 if (SS.isNotEmpty() && !SS.isInvalid()) {
Douglas Gregoref06ccf2009-10-12 23:11:44 +0000577 if (RequireCompleteDeclContext(SS))
578 return true;
579
Douglas Gregor1d5e9f92009-08-25 17:23:04 +0000580 SemanticContext = computeDeclContext(SS, true);
581 if (!SemanticContext) {
582 // FIXME: Produce a reasonable diagnostic here
583 return true;
584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
John McCall9f3059a2009-10-09 21:13:30 +0000586 LookupQualifiedName(Previous, SemanticContext, Name, LookupOrdinaryName,
Douglas Gregor1d5e9f92009-08-25 17:23:04 +0000587 true);
588 } else {
589 SemanticContext = CurContext;
John McCall9f3059a2009-10-09 21:13:30 +0000590 LookupName(Previous, S, Name, LookupOrdinaryName, true);
Douglas Gregor1d5e9f92009-08-25 17:23:04 +0000591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000593 assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?");
594 NamedDecl *PrevDecl = 0;
595 if (Previous.begin() != Previous.end())
596 PrevDecl = *Previous.begin();
597
Douglas Gregor9acb6902009-09-26 07:05:09 +0000598 if (PrevDecl && TUK == TUK_Friend) {
599 // C++ [namespace.memdef]p3:
600 // [...] When looking for a prior declaration of a class or a function
601 // declared as a friend, and when the name of the friend class or
602 // function is neither a qualified name nor a template-id, scopes outside
603 // the innermost enclosing namespace scope are not considered.
604 DeclContext *OutermostContext = CurContext;
605 while (!OutermostContext->isFileContext())
606 OutermostContext = OutermostContext->getLookupParent();
607
608 if (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
609 OutermostContext->Encloses(PrevDecl->getDeclContext())) {
610 SemanticContext = PrevDecl->getDeclContext();
611 } else {
612 // Declarations in outer scopes don't matter. However, the outermost
613 // context we computed is the semntic context for our new
614 // declaration.
615 PrevDecl = 0;
616 SemanticContext = OutermostContext;
617 }
618 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
Douglas Gregorf187420f2009-06-17 23:37:01 +0000619 PrevDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000620
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000621 // If there is a previous declaration with the same name, check
622 // whether this is a valid redeclaration.
Mike Stump11289f42009-09-09 15:08:12 +0000623 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000624 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregor7f34bae2009-10-09 21:11:42 +0000625
626 // We may have found the injected-class-name of a class template,
627 // class template partial specialization, or class template specialization.
628 // In these cases, grab the template that is being defined or specialized.
629 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
630 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
631 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
632 PrevClassTemplate
633 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
634 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
635 PrevClassTemplate
636 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
637 ->getSpecializedTemplate();
638 }
639 }
640
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000641 if (PrevClassTemplate) {
642 // Ensure that the template parameter lists are compatible.
643 if (!TemplateParameterListsAreEqual(TemplateParams,
644 PrevClassTemplate->getTemplateParameters(),
645 /*Complain=*/true))
Douglas Gregorc08f4892009-03-25 00:13:59 +0000646 return true;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000647
648 // C++ [temp.class]p4:
649 // In a redeclaration, partial specialization, explicit
650 // specialization or explicit instantiation of a class template,
651 // the class-key shall agree in kind with the original class
652 // template declaration (7.1.5.3).
653 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregord9034f02009-05-14 16:41:31 +0000654 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump11289f42009-09-09 15:08:12 +0000655 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor170512f2009-04-01 23:51:29 +0000656 << Name
Mike Stump11289f42009-09-09 15:08:12 +0000657 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregor170512f2009-04-01 23:51:29 +0000658 PrevRecordDecl->getKindName());
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000659 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregor170512f2009-04-01 23:51:29 +0000660 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000661 }
662
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000663 // Check for redefinition of this class template.
John McCall9bb74a52009-07-31 02:45:11 +0000664 if (TUK == TUK_Definition) {
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000665 if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) {
666 Diag(NameLoc, diag::err_redefinition) << Name;
667 Diag(Def->getLocation(), diag::note_previous_definition);
668 // FIXME: Would it make sense to try to "forget" the previous
669 // definition, as part of error recovery?
Douglas Gregorc08f4892009-03-25 00:13:59 +0000670 return true;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000671 }
672 }
673 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
674 // Maybe we will complain about the shadowed template parameter.
675 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
676 // Just pretend that we didn't see the previous declaration.
677 PrevDecl = 0;
678 } else if (PrevDecl) {
679 // C++ [temp]p5:
680 // A class template shall not have the same name as any other
681 // template, class, function, object, enumeration, enumerator,
682 // namespace, or type in the same scope (3.3), except as specified
683 // in (14.5.4).
684 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
685 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregorc08f4892009-03-25 00:13:59 +0000686 return true;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000687 }
688
Douglas Gregordba32632009-02-10 19:49:53 +0000689 // Check the template parameter list of this declaration, possibly
690 // merging in the template parameter list from the previous class
691 // template declaration.
692 if (CheckTemplateParameterList(TemplateParams,
693 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0))
694 Invalid = true;
Mike Stump11289f42009-09-09 15:08:12 +0000695
Douglas Gregore362cea2009-05-10 22:57:19 +0000696 // FIXME: If we had a scope specifier, we better have a previous template
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000697 // declaration!
698
Mike Stump11289f42009-09-09 15:08:12 +0000699 CXXRecordDecl *NewClass =
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000700 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000701 PrevClassTemplate?
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000702 PrevClassTemplate->getTemplatedDecl() : 0,
703 /*DelayTypeCreation=*/true);
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000704
705 ClassTemplateDecl *NewTemplate
706 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
707 DeclarationName(Name), TemplateParams,
Douglas Gregor90a1a652009-03-19 17:26:29 +0000708 NewClass, PrevClassTemplate);
Douglas Gregor97f1f1c2009-03-26 00:10:35 +0000709 NewClass->setDescribedClassTemplate(NewTemplate);
710
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000711 // Build the type for the class template declaration now.
Mike Stump11289f42009-09-09 15:08:12 +0000712 QualType T =
713 Context.getTypeDeclType(NewClass,
714 PrevClassTemplate?
715 PrevClassTemplate->getTemplatedDecl() : 0);
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000716 assert(T->isDependentType() && "Class template type is not dependent?");
717 (void)T;
718
Anders Carlsson137108d2009-03-26 01:24:28 +0000719 // Set the access specifier.
Douglas Gregor3dad8422009-09-26 06:47:28 +0000720 if (!Invalid && TUK != TUK_Friend)
John McCall27b5c252009-09-14 21:59:20 +0000721 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump11289f42009-09-09 15:08:12 +0000722
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000723 // Set the lexical context of these templates
724 NewClass->setLexicalDeclContext(CurContext);
725 NewTemplate->setLexicalDeclContext(CurContext);
726
John McCall9bb74a52009-07-31 02:45:11 +0000727 if (TUK == TUK_Definition)
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000728 NewClass->startDefinition();
729
730 if (Attr)
Douglas Gregor758a8692009-06-17 21:51:59 +0000731 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000732
John McCall27b5c252009-09-14 21:59:20 +0000733 if (TUK != TUK_Friend)
734 PushOnScopeChains(NewTemplate, S);
735 else {
Douglas Gregor3dad8422009-09-26 06:47:28 +0000736 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall27b5c252009-09-14 21:59:20 +0000737 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregor3dad8422009-09-26 06:47:28 +0000738 NewClass->setAccess(PrevClassTemplate->getAccess());
739 }
John McCall27b5c252009-09-14 21:59:20 +0000740
Douglas Gregor3dad8422009-09-26 06:47:28 +0000741 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
742 PrevClassTemplate != NULL);
743
John McCall27b5c252009-09-14 21:59:20 +0000744 // Friend templates are visible in fairly strange ways.
745 if (!CurContext->isDependentContext()) {
746 DeclContext *DC = SemanticContext->getLookupContext();
747 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
748 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
749 PushOnScopeChains(NewTemplate, EnclosingScope,
750 /* AddToContext = */ false);
751 }
Douglas Gregor3dad8422009-09-26 06:47:28 +0000752
753 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
754 NewClass->getLocation(),
755 NewTemplate,
756 /*FIXME:*/NewClass->getLocation());
757 Friend->setAccess(AS_public);
758 CurContext->addDecl(Friend);
John McCall27b5c252009-09-14 21:59:20 +0000759 }
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000760
Douglas Gregordba32632009-02-10 19:49:53 +0000761 if (Invalid) {
762 NewTemplate->setInvalidDecl();
763 NewClass->setInvalidDecl();
764 }
Chris Lattner83f095c2009-03-28 19:18:32 +0000765 return DeclPtrTy::make(NewTemplate);
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000766}
767
Douglas Gregordba32632009-02-10 19:49:53 +0000768/// \brief Checks the validity of a template parameter list, possibly
769/// considering the template parameter list from a previous
770/// declaration.
771///
772/// If an "old" template parameter list is provided, it must be
773/// equivalent (per TemplateParameterListsAreEqual) to the "new"
774/// template parameter list.
775///
776/// \param NewParams Template parameter list for a new template
777/// declaration. This template parameter list will be updated with any
778/// default arguments that are carried through from the previous
779/// template parameter list.
780///
781/// \param OldParams If provided, template parameter list from a
782/// previous declaration of the same template. Default template
783/// arguments will be merged from the old template parameter list to
784/// the new template parameter list.
785///
786/// \returns true if an error occurred, false otherwise.
787bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
788 TemplateParameterList *OldParams) {
789 bool Invalid = false;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Douglas Gregordba32632009-02-10 19:49:53 +0000791 // C++ [temp.param]p10:
792 // The set of default template-arguments available for use with a
793 // template declaration or definition is obtained by merging the
794 // default arguments from the definition (if in scope) and all
795 // declarations in scope in the same way default function
796 // arguments are (8.3.6).
797 bool SawDefaultArgument = false;
798 SourceLocation PreviousDefaultArgLoc;
Douglas Gregord32e0282009-02-09 23:23:08 +0000799
Anders Carlsson327865d2009-06-12 23:20:15 +0000800 bool SawParameterPack = false;
801 SourceLocation ParameterPackLoc;
802
Mike Stumpc89c8e32009-02-11 23:03:27 +0000803 // Dummy initialization to avoid warnings.
Douglas Gregor5bd22da2009-02-11 20:46:19 +0000804 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregordba32632009-02-10 19:49:53 +0000805 if (OldParams)
806 OldParam = OldParams->begin();
807
808 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
809 NewParamEnd = NewParams->end();
810 NewParam != NewParamEnd; ++NewParam) {
811 // Variables used to diagnose redundant default arguments
812 bool RedundantDefaultArg = false;
813 SourceLocation OldDefaultLoc;
814 SourceLocation NewDefaultLoc;
815
816 // Variables used to diagnose missing default arguments
817 bool MissingDefaultArg = false;
818
Anders Carlsson327865d2009-06-12 23:20:15 +0000819 // C++0x [temp.param]p11:
820 // If a template parameter of a class template is a template parameter pack,
821 // it must be the last template parameter.
822 if (SawParameterPack) {
Mike Stump11289f42009-09-09 15:08:12 +0000823 Diag(ParameterPackLoc,
Anders Carlsson327865d2009-06-12 23:20:15 +0000824 diag::err_template_param_pack_must_be_last_template_parameter);
825 Invalid = true;
826 }
827
Douglas Gregordba32632009-02-10 19:49:53 +0000828 // Merge default arguments for template type parameters.
829 if (TemplateTypeParmDecl *NewTypeParm
830 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Mike Stump11289f42009-09-09 15:08:12 +0000831 TemplateTypeParmDecl *OldTypeParm
Douglas Gregordba32632009-02-10 19:49:53 +0000832 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000833
Anders Carlsson327865d2009-06-12 23:20:15 +0000834 if (NewTypeParm->isParameterPack()) {
835 assert(!NewTypeParm->hasDefaultArgument() &&
836 "Parameter packs can't have a default argument!");
837 SawParameterPack = true;
838 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000839 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
Douglas Gregordba32632009-02-10 19:49:53 +0000840 NewTypeParm->hasDefaultArgument()) {
841 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
842 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
843 SawDefaultArgument = true;
844 RedundantDefaultArg = true;
845 PreviousDefaultArgLoc = NewDefaultLoc;
846 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
847 // Merge the default argument from the old declaration to the
848 // new declaration.
849 SawDefaultArgument = true;
850 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(),
851 OldTypeParm->getDefaultArgumentLoc(),
852 true);
853 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
854 } else if (NewTypeParm->hasDefaultArgument()) {
855 SawDefaultArgument = true;
856 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
857 } else if (SawDefaultArgument)
858 MissingDefaultArg = true;
Mike Stump12b8ce12009-08-04 21:02:39 +0000859 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregordba32632009-02-10 19:49:53 +0000860 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Mike Stump12b8ce12009-08-04 21:02:39 +0000861 // Merge default arguments for non-type template parameters
Douglas Gregordba32632009-02-10 19:49:53 +0000862 NonTypeTemplateParmDecl *OldNonTypeParm
863 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000864 if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregordba32632009-02-10 19:49:53 +0000865 NewNonTypeParm->hasDefaultArgument()) {
866 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
867 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
868 SawDefaultArgument = true;
869 RedundantDefaultArg = true;
870 PreviousDefaultArgLoc = NewDefaultLoc;
871 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
872 // Merge the default argument from the old declaration to the
873 // new declaration.
874 SawDefaultArgument = true;
875 // FIXME: We need to create a new kind of "default argument"
876 // expression that points to a previous template template
877 // parameter.
878 NewNonTypeParm->setDefaultArgument(
879 OldNonTypeParm->getDefaultArgument());
880 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
881 } else if (NewNonTypeParm->hasDefaultArgument()) {
882 SawDefaultArgument = true;
883 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
884 } else if (SawDefaultArgument)
Mike Stump11289f42009-09-09 15:08:12 +0000885 MissingDefaultArg = true;
Mike Stump12b8ce12009-08-04 21:02:39 +0000886 } else {
Douglas Gregordba32632009-02-10 19:49:53 +0000887 // Merge default arguments for template template parameters
Douglas Gregordba32632009-02-10 19:49:53 +0000888 TemplateTemplateParmDecl *NewTemplateParm
889 = cast<TemplateTemplateParmDecl>(*NewParam);
890 TemplateTemplateParmDecl *OldTemplateParm
891 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000892 if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregordba32632009-02-10 19:49:53 +0000893 NewTemplateParm->hasDefaultArgument()) {
894 OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc();
895 NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc();
896 SawDefaultArgument = true;
897 RedundantDefaultArg = true;
898 PreviousDefaultArgLoc = NewDefaultLoc;
899 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
900 // Merge the default argument from the old declaration to the
901 // new declaration.
902 SawDefaultArgument = true;
Mike Stump87c57ac2009-05-16 07:39:55 +0000903 // FIXME: We need to create a new kind of "default argument" expression
904 // that points to a previous template template parameter.
Douglas Gregordba32632009-02-10 19:49:53 +0000905 NewTemplateParm->setDefaultArgument(
906 OldTemplateParm->getDefaultArgument());
907 PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc();
908 } else if (NewTemplateParm->hasDefaultArgument()) {
909 SawDefaultArgument = true;
910 PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc();
911 } else if (SawDefaultArgument)
Mike Stump11289f42009-09-09 15:08:12 +0000912 MissingDefaultArg = true;
Douglas Gregordba32632009-02-10 19:49:53 +0000913 }
914
915 if (RedundantDefaultArg) {
916 // C++ [temp.param]p12:
917 // A template-parameter shall not be given default arguments
918 // by two different declarations in the same scope.
919 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
920 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
921 Invalid = true;
922 } else if (MissingDefaultArg) {
923 // C++ [temp.param]p11:
924 // If a template-parameter has a default template-argument,
925 // all subsequent template-parameters shall have a default
926 // template-argument supplied.
Mike Stump11289f42009-09-09 15:08:12 +0000927 Diag((*NewParam)->getLocation(),
Douglas Gregordba32632009-02-10 19:49:53 +0000928 diag::err_template_param_default_arg_missing);
929 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
930 Invalid = true;
931 }
932
933 // If we have an old template parameter list that we're merging
934 // in, move on to the next parameter.
935 if (OldParams)
936 ++OldParam;
937 }
938
939 return Invalid;
940}
Douglas Gregord32e0282009-02-09 23:23:08 +0000941
Mike Stump11289f42009-09-09 15:08:12 +0000942/// \brief Match the given template parameter lists to the given scope
Douglas Gregord8d297c2009-07-21 23:53:31 +0000943/// specifier, returning the template parameter list that applies to the
944/// name.
945///
946/// \param DeclStartLoc the start of the declaration that has a scope
947/// specifier or a template parameter list.
Mike Stump11289f42009-09-09 15:08:12 +0000948///
Douglas Gregord8d297c2009-07-21 23:53:31 +0000949/// \param SS the scope specifier that will be matched to the given template
950/// parameter lists. This scope specifier precedes a qualified name that is
951/// being declared.
952///
953/// \param ParamLists the template parameter lists, from the outermost to the
954/// innermost template parameter lists.
955///
956/// \param NumParamLists the number of template parameter lists in ParamLists.
957///
Douglas Gregor5c0405d2009-10-07 22:35:40 +0000958/// \param IsExplicitSpecialization will be set true if the entity being
959/// declared is an explicit specialization, false otherwise.
960///
Mike Stump11289f42009-09-09 15:08:12 +0000961/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregord8d297c2009-07-21 23:53:31 +0000962/// name that is preceded by the scope specifier @p SS. This template
963/// parameter list may be have template parameters (if we're declaring a
Mike Stump11289f42009-09-09 15:08:12 +0000964/// template) or may have no template parameters (if we're declaring a
Douglas Gregord8d297c2009-07-21 23:53:31 +0000965/// template specialization), or may be NULL (if we were's declaring isn't
966/// itself a template).
967TemplateParameterList *
968Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
969 const CXXScopeSpec &SS,
970 TemplateParameterList **ParamLists,
Douglas Gregor5c0405d2009-10-07 22:35:40 +0000971 unsigned NumParamLists,
972 bool &IsExplicitSpecialization) {
973 IsExplicitSpecialization = false;
974
Douglas Gregord8d297c2009-07-21 23:53:31 +0000975 // Find the template-ids that occur within the nested-name-specifier. These
976 // template-ids will match up with the template parameter lists.
977 llvm::SmallVector<const TemplateSpecializationType *, 4>
978 TemplateIdsInSpecifier;
979 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
980 NNS; NNS = NNS->getPrefix()) {
Mike Stump11289f42009-09-09 15:08:12 +0000981 if (const TemplateSpecializationType *SpecType
Douglas Gregord8d297c2009-07-21 23:53:31 +0000982 = dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) {
983 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
984 if (!Template)
985 continue; // FIXME: should this be an error? probably...
Mike Stump11289f42009-09-09 15:08:12 +0000986
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000987 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregord8d297c2009-07-21 23:53:31 +0000988 ClassTemplateSpecializationDecl *SpecDecl
989 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
990 // If the nested name specifier refers to an explicit specialization,
991 // we don't need a template<> header.
Douglas Gregor82e22862009-09-16 00:01:48 +0000992 // FIXME: revisit this approach once we cope with specializations
Douglas Gregor15301382009-07-30 17:40:51 +0000993 // properly.
Douglas Gregord8d297c2009-07-21 23:53:31 +0000994 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization)
995 continue;
996 }
Mike Stump11289f42009-09-09 15:08:12 +0000997
Douglas Gregord8d297c2009-07-21 23:53:31 +0000998 TemplateIdsInSpecifier.push_back(SpecType);
999 }
1000 }
Mike Stump11289f42009-09-09 15:08:12 +00001001
Douglas Gregord8d297c2009-07-21 23:53:31 +00001002 // Reverse the list of template-ids in the scope specifier, so that we can
1003 // more easily match up the template-ids and the template parameter lists.
1004 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump11289f42009-09-09 15:08:12 +00001005
Douglas Gregord8d297c2009-07-21 23:53:31 +00001006 SourceLocation FirstTemplateLoc = DeclStartLoc;
1007 if (NumParamLists)
1008 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump11289f42009-09-09 15:08:12 +00001009
Douglas Gregord8d297c2009-07-21 23:53:31 +00001010 // Match the template-ids found in the specifier to the template parameter
1011 // lists.
1012 unsigned Idx = 0;
1013 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
1014 Idx != NumTemplateIds; ++Idx) {
Douglas Gregor15301382009-07-30 17:40:51 +00001015 QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0);
1016 bool DependentTemplateId = TemplateId->isDependentType();
Douglas Gregord8d297c2009-07-21 23:53:31 +00001017 if (Idx >= NumParamLists) {
1018 // We have a template-id without a corresponding template parameter
1019 // list.
1020 if (DependentTemplateId) {
Mike Stump11289f42009-09-09 15:08:12 +00001021 // FIXME: the location information here isn't great.
1022 Diag(SS.getRange().getBegin(),
Douglas Gregord8d297c2009-07-21 23:53:31 +00001023 diag::err_template_spec_needs_template_parameters)
Douglas Gregor15301382009-07-30 17:40:51 +00001024 << TemplateId
Douglas Gregord8d297c2009-07-21 23:53:31 +00001025 << SS.getRange();
1026 } else {
1027 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1028 << SS.getRange()
1029 << CodeModificationHint::CreateInsertion(FirstTemplateLoc,
1030 "template<> ");
Douglas Gregor5c0405d2009-10-07 22:35:40 +00001031 IsExplicitSpecialization = true;
Douglas Gregord8d297c2009-07-21 23:53:31 +00001032 }
1033 return 0;
1034 }
Mike Stump11289f42009-09-09 15:08:12 +00001035
Douglas Gregord8d297c2009-07-21 23:53:31 +00001036 // Check the template parameter list against its corresponding template-id.
Douglas Gregor15301382009-07-30 17:40:51 +00001037 if (DependentTemplateId) {
Mike Stump11289f42009-09-09 15:08:12 +00001038 TemplateDecl *Template
Douglas Gregor15301382009-07-30 17:40:51 +00001039 = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl();
1040
Mike Stump11289f42009-09-09 15:08:12 +00001041 if (ClassTemplateDecl *ClassTemplate
Douglas Gregor15301382009-07-30 17:40:51 +00001042 = dyn_cast<ClassTemplateDecl>(Template)) {
1043 TemplateParameterList *ExpectedTemplateParams = 0;
1044 // Is this template-id naming the primary template?
1045 if (Context.hasSameType(TemplateId,
1046 ClassTemplate->getInjectedClassNameType(Context)))
1047 ExpectedTemplateParams = ClassTemplate->getTemplateParameters();
1048 // ... or a partial specialization?
1049 else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1050 = ClassTemplate->findPartialSpecialization(TemplateId))
1051 ExpectedTemplateParams = PartialSpec->getTemplateParameters();
1052
1053 if (ExpectedTemplateParams)
Mike Stump11289f42009-09-09 15:08:12 +00001054 TemplateParameterListsAreEqual(ParamLists[Idx],
Douglas Gregor15301382009-07-30 17:40:51 +00001055 ExpectedTemplateParams,
1056 true);
Mike Stump11289f42009-09-09 15:08:12 +00001057 }
Douglas Gregor15301382009-07-30 17:40:51 +00001058 } else if (ParamLists[Idx]->size() > 0)
Mike Stump11289f42009-09-09 15:08:12 +00001059 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregor15301382009-07-30 17:40:51 +00001060 diag::err_template_param_list_matches_nontemplate)
1061 << TemplateId
1062 << ParamLists[Idx]->getSourceRange();
Douglas Gregor5c0405d2009-10-07 22:35:40 +00001063 else
1064 IsExplicitSpecialization = true;
Douglas Gregord8d297c2009-07-21 23:53:31 +00001065 }
Mike Stump11289f42009-09-09 15:08:12 +00001066
Douglas Gregord8d297c2009-07-21 23:53:31 +00001067 // If there were at least as many template-ids as there were template
1068 // parameter lists, then there are no template parameter lists remaining for
1069 // the declaration itself.
1070 if (Idx >= NumParamLists)
1071 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001072
Douglas Gregord8d297c2009-07-21 23:53:31 +00001073 // If there were too many template parameter lists, complain about that now.
1074 if (Idx != NumParamLists - 1) {
1075 while (Idx < NumParamLists - 1) {
Mike Stump11289f42009-09-09 15:08:12 +00001076 Diag(ParamLists[Idx]->getTemplateLoc(),
Douglas Gregord8d297c2009-07-21 23:53:31 +00001077 diag::err_template_spec_extra_headers)
1078 << SourceRange(ParamLists[Idx]->getTemplateLoc(),
1079 ParamLists[Idx]->getRAngleLoc());
1080 ++Idx;
1081 }
1082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Douglas Gregord8d297c2009-07-21 23:53:31 +00001084 // Return the last template parameter list, which corresponds to the
1085 // entity being declared.
1086 return ParamLists[NumParamLists - 1];
1087}
1088
Douglas Gregorc40290e2009-03-09 23:48:35 +00001089/// \brief Translates template arguments as provided by the parser
1090/// into template arguments used by semantic analysis.
Douglas Gregor0e876e02009-09-25 23:53:26 +00001091void Sema::translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn,
1092 SourceLocation *TemplateArgLocs,
Douglas Gregorc40290e2009-03-09 23:48:35 +00001093 llvm::SmallVector<TemplateArgument, 16> &TemplateArgs) {
1094 TemplateArgs.reserve(TemplateArgsIn.size());
1095
1096 void **Args = TemplateArgsIn.getArgs();
1097 bool *ArgIsType = TemplateArgsIn.getArgIsType();
1098 for (unsigned Arg = 0, Last = TemplateArgsIn.size(); Arg != Last; ++Arg) {
1099 TemplateArgs.push_back(
1100 ArgIsType[Arg]? TemplateArgument(TemplateArgLocs[Arg],
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001101 //FIXME: Preserve type source info.
1102 Sema::GetTypeFromParser(Args[Arg]))
Douglas Gregorc40290e2009-03-09 23:48:35 +00001103 : TemplateArgument(reinterpret_cast<Expr *>(Args[Arg])));
1104 }
1105}
1106
Douglas Gregordc572a32009-03-30 22:58:21 +00001107QualType Sema::CheckTemplateIdType(TemplateName Name,
1108 SourceLocation TemplateLoc,
1109 SourceLocation LAngleLoc,
1110 const TemplateArgument *TemplateArgs,
1111 unsigned NumTemplateArgs,
1112 SourceLocation RAngleLoc) {
1113 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorb67535d2009-03-31 00:43:58 +00001114 if (!Template) {
1115 // The template name does not resolve to a template, so we just
1116 // build a dependent template-id type.
Douglas Gregorb67535d2009-03-31 00:43:58 +00001117 return Context.getTemplateSpecializationType(Name, TemplateArgs,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001118 NumTemplateArgs);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001119 }
Douglas Gregordc572a32009-03-30 22:58:21 +00001120
Douglas Gregorc40290e2009-03-09 23:48:35 +00001121 // Check that the template argument list is well-formed for this
1122 // template.
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001123 TemplateArgumentListBuilder Converted(Template->getTemplateParameters(),
1124 NumTemplateArgs);
Mike Stump11289f42009-09-09 15:08:12 +00001125 if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc,
Douglas Gregorc40290e2009-03-09 23:48:35 +00001126 TemplateArgs, NumTemplateArgs, RAngleLoc,
Douglas Gregore3f1f352009-07-01 00:28:38 +00001127 false, Converted))
Douglas Gregorc40290e2009-03-09 23:48:35 +00001128 return QualType();
1129
Mike Stump11289f42009-09-09 15:08:12 +00001130 assert((Converted.structuredSize() ==
Douglas Gregordc572a32009-03-30 22:58:21 +00001131 Template->getTemplateParameters()->size()) &&
Douglas Gregorc40290e2009-03-09 23:48:35 +00001132 "Converted template argument list is too short!");
1133
1134 QualType CanonType;
1135
Douglas Gregordc572a32009-03-30 22:58:21 +00001136 if (TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregorc40290e2009-03-09 23:48:35 +00001137 TemplateArgs,
1138 NumTemplateArgs)) {
1139 // This class template specialization is a dependent
1140 // type. Therefore, its canonical type is another class template
1141 // specialization type that contains all of the converted
1142 // arguments in canonical form. This ensures that, e.g., A<T> and
1143 // A<T, T> have identical types when A is declared as:
1144 //
1145 // template<typename T, typename U = T> struct A;
Douglas Gregor6bc50582009-05-07 06:41:52 +00001146 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001147 CanonType = Context.getTemplateSpecializationType(CanonName,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001148 Converted.getFlatArguments(),
1149 Converted.flatSize());
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregora8e02e72009-07-28 23:00:59 +00001151 // FIXME: CanonType is not actually the canonical type, and unfortunately
1152 // it is a TemplateTypeSpecializationType that we will never use again.
1153 // In the future, we need to teach getTemplateSpecializationType to only
1154 // build the canonical type and return that to us.
1155 CanonType = Context.getCanonicalType(CanonType);
Mike Stump11289f42009-09-09 15:08:12 +00001156 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregordc572a32009-03-30 22:58:21 +00001157 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregorc40290e2009-03-09 23:48:35 +00001158 // Find the class template specialization declaration that
1159 // corresponds to these arguments.
1160 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001161 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001162 Converted.getFlatArguments(),
Douglas Gregor00044172009-07-29 16:09:57 +00001163 Converted.flatSize(),
1164 Context);
Douglas Gregorc40290e2009-03-09 23:48:35 +00001165 void *InsertPos = 0;
1166 ClassTemplateSpecializationDecl *Decl
1167 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
1168 if (!Decl) {
1169 // This is the first time we have referenced this class template
1170 // specialization. Create the canonical declaration and add it to
1171 // the set of specializations.
Mike Stump11289f42009-09-09 15:08:12 +00001172 Decl = ClassTemplateSpecializationDecl::Create(Context,
Anders Carlsson8aa89d42009-06-05 03:43:12 +00001173 ClassTemplate->getDeclContext(),
John McCall1806c272009-09-11 07:25:08 +00001174 ClassTemplate->getLocation(),
Anders Carlsson8aa89d42009-06-05 03:43:12 +00001175 ClassTemplate,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001176 Converted, 0);
Douglas Gregorc40290e2009-03-09 23:48:35 +00001177 ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos);
1178 Decl->setLexicalDeclContext(CurContext);
1179 }
1180
1181 CanonType = Context.getTypeDeclType(Decl);
1182 }
Mike Stump11289f42009-09-09 15:08:12 +00001183
Douglas Gregorc40290e2009-03-09 23:48:35 +00001184 // Build the fully-sugared type for this class template
1185 // specialization, which refers back to the class template
1186 // specialization we created or found.
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00001187 //FIXME: Preserve type source info.
Douglas Gregordc572a32009-03-30 22:58:21 +00001188 return Context.getTemplateSpecializationType(Name, TemplateArgs,
1189 NumTemplateArgs, CanonType);
Douglas Gregorc40290e2009-03-09 23:48:35 +00001190}
1191
Douglas Gregor67a65642009-02-17 23:15:12 +00001192Action::TypeResult
Douglas Gregordc572a32009-03-30 22:58:21 +00001193Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001194 SourceLocation LAngleLoc,
Douglas Gregordc572a32009-03-30 22:58:21 +00001195 ASTTemplateArgsPtr TemplateArgsIn,
1196 SourceLocation *TemplateArgLocs,
John McCalld8fe9af2009-09-08 17:47:29 +00001197 SourceLocation RAngleLoc) {
Douglas Gregordc572a32009-03-30 22:58:21 +00001198 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor8bf42052009-02-09 18:46:07 +00001199
Douglas Gregorc40290e2009-03-09 23:48:35 +00001200 // Translate the parser's template argument list in our AST format.
1201 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1202 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregord32e0282009-02-09 23:23:08 +00001203
Douglas Gregordc572a32009-03-30 22:58:21 +00001204 QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001205 TemplateArgs.data(),
1206 TemplateArgs.size(),
Douglas Gregordc572a32009-03-30 22:58:21 +00001207 RAngleLoc);
Douglas Gregorc40290e2009-03-09 23:48:35 +00001208 TemplateArgsIn.release();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001209
1210 if (Result.isNull())
1211 return true;
1212
John McCalld8fe9af2009-09-08 17:47:29 +00001213 return Result.getAsOpaquePtr();
1214}
John McCall06f6fe8d2009-09-04 01:14:41 +00001215
John McCalld8fe9af2009-09-08 17:47:29 +00001216Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult,
1217 TagUseKind TUK,
1218 DeclSpec::TST TagSpec,
1219 SourceLocation TagLoc) {
1220 if (TypeResult.isInvalid())
1221 return Sema::TypeResult();
John McCall06f6fe8d2009-09-04 01:14:41 +00001222
John McCalld8fe9af2009-09-08 17:47:29 +00001223 QualType Type = QualType::getFromOpaquePtr(TypeResult.get());
John McCall06f6fe8d2009-09-04 01:14:41 +00001224
John McCalld8fe9af2009-09-08 17:47:29 +00001225 // Verify the tag specifier.
1226 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
Mike Stump11289f42009-09-09 15:08:12 +00001227
John McCalld8fe9af2009-09-08 17:47:29 +00001228 if (const RecordType *RT = Type->getAs<RecordType>()) {
1229 RecordDecl *D = RT->getDecl();
1230
1231 IdentifierInfo *Id = D->getIdentifier();
1232 assert(Id && "templated class must have an identifier");
1233
1234 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1235 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCall7f41d982009-09-11 04:59:25 +00001236 << Type
John McCalld8fe9af2009-09-08 17:47:29 +00001237 << CodeModificationHint::CreateReplacement(SourceRange(TagLoc),
1238 D->getKindName());
John McCall7f41d982009-09-11 04:59:25 +00001239 Diag(D->getLocation(), diag::note_previous_use);
John McCall06f6fe8d2009-09-04 01:14:41 +00001240 }
1241 }
1242
John McCalld8fe9af2009-09-08 17:47:29 +00001243 QualType ElabType = Context.getElaboratedType(Type, TagKind);
1244
1245 return ElabType.getAsOpaquePtr();
Douglas Gregor8bf42052009-02-09 18:46:07 +00001246}
1247
Douglas Gregora727cb92009-06-30 22:34:41 +00001248Sema::OwningExprResult Sema::BuildTemplateIdExpr(TemplateName Template,
1249 SourceLocation TemplateNameLoc,
1250 SourceLocation LAngleLoc,
1251 const TemplateArgument *TemplateArgs,
1252 unsigned NumTemplateArgs,
1253 SourceLocation RAngleLoc) {
1254 // FIXME: Can we do any checking at this point? I guess we could check the
1255 // template arguments that we have against the template name, if the template
Mike Stump11289f42009-09-09 15:08:12 +00001256 // name refers to a single template. That's not a terribly common case,
Douglas Gregora727cb92009-06-30 22:34:41 +00001257 // though.
Mike Stump11289f42009-09-09 15:08:12 +00001258 return Owned(TemplateIdRefExpr::Create(Context,
Douglas Gregora727cb92009-06-30 22:34:41 +00001259 /*FIXME: New type?*/Context.OverloadTy,
1260 /*FIXME: Necessary?*/0,
1261 /*FIXME: Necessary?*/SourceRange(),
1262 Template, TemplateNameLoc, LAngleLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001263 TemplateArgs,
Douglas Gregora727cb92009-06-30 22:34:41 +00001264 NumTemplateArgs, RAngleLoc));
1265}
1266
1267Sema::OwningExprResult Sema::ActOnTemplateIdExpr(TemplateTy TemplateD,
1268 SourceLocation TemplateNameLoc,
1269 SourceLocation LAngleLoc,
1270 ASTTemplateArgsPtr TemplateArgsIn,
1271 SourceLocation *TemplateArgLocs,
1272 SourceLocation RAngleLoc) {
1273 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump11289f42009-09-09 15:08:12 +00001274
Douglas Gregora727cb92009-06-30 22:34:41 +00001275 // Translate the parser's template argument list in our AST format.
1276 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1277 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
Douglas Gregorb77af8f2009-07-22 20:55:49 +00001278 TemplateArgsIn.release();
Mike Stump11289f42009-09-09 15:08:12 +00001279
Douglas Gregora727cb92009-06-30 22:34:41 +00001280 return BuildTemplateIdExpr(Template, TemplateNameLoc, LAngleLoc,
1281 TemplateArgs.data(), TemplateArgs.size(),
1282 RAngleLoc);
1283}
1284
Douglas Gregor84f14dd2009-09-01 00:37:14 +00001285Sema::OwningExprResult
1286Sema::ActOnMemberTemplateIdReferenceExpr(Scope *S, ExprArg Base,
1287 SourceLocation OpLoc,
1288 tok::TokenKind OpKind,
1289 const CXXScopeSpec &SS,
1290 TemplateTy TemplateD,
1291 SourceLocation TemplateNameLoc,
1292 SourceLocation LAngleLoc,
1293 ASTTemplateArgsPtr TemplateArgsIn,
1294 SourceLocation *TemplateArgLocs,
1295 SourceLocation RAngleLoc) {
1296 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Mike Stump11289f42009-09-09 15:08:12 +00001297
Douglas Gregor84f14dd2009-09-01 00:37:14 +00001298 // FIXME: We're going to end up looking up the template based on its name,
1299 // twice!
1300 DeclarationName Name;
1301 if (TemplateDecl *ActualTemplate = Template.getAsTemplateDecl())
1302 Name = ActualTemplate->getDeclName();
1303 else if (OverloadedFunctionDecl *Ovl = Template.getAsOverloadedFunctionDecl())
1304 Name = Ovl->getDeclName();
1305 else
Douglas Gregor308047d2009-09-09 00:23:06 +00001306 Name = Template.getAsDependentTemplateName()->getName();
Mike Stump11289f42009-09-09 15:08:12 +00001307
Douglas Gregor84f14dd2009-09-01 00:37:14 +00001308 // Translate the parser's template argument list in our AST format.
1309 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
1310 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
1311 TemplateArgsIn.release();
Mike Stump11289f42009-09-09 15:08:12 +00001312
Douglas Gregor84f14dd2009-09-01 00:37:14 +00001313 // Do we have the save the actual template name? We might need it...
1314 return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, TemplateNameLoc,
1315 Name, true, LAngleLoc,
1316 TemplateArgs.data(), TemplateArgs.size(),
Mike Stump11289f42009-09-09 15:08:12 +00001317 RAngleLoc, DeclPtrTy(), &SS);
Douglas Gregor84f14dd2009-09-01 00:37:14 +00001318}
1319
Douglas Gregorb67535d2009-03-31 00:43:58 +00001320/// \brief Form a dependent template name.
1321///
1322/// This action forms a dependent template name given the template
1323/// name and its (presumably dependent) scope specifier. For
1324/// example, given "MetaFun::template apply", the scope specifier \p
1325/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1326/// of the "template" keyword, and "apply" is the \p Name.
Mike Stump11289f42009-09-09 15:08:12 +00001327Sema::TemplateTy
Douglas Gregorb67535d2009-03-31 00:43:58 +00001328Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc,
1329 const IdentifierInfo &Name,
1330 SourceLocation NameLoc,
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001331 const CXXScopeSpec &SS,
1332 TypeTy *ObjectType) {
Mike Stump11289f42009-09-09 15:08:12 +00001333 if ((ObjectType &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001334 computeDeclContext(QualType::getFromOpaquePtr(ObjectType))) ||
1335 (SS.isSet() && computeDeclContext(SS, false))) {
Douglas Gregorb67535d2009-03-31 00:43:58 +00001336 // C++0x [temp.names]p5:
1337 // If a name prefixed by the keyword template is not the name of
1338 // a template, the program is ill-formed. [Note: the keyword
1339 // template may not be applied to non-template members of class
1340 // templates. -end note ] [ Note: as is the case with the
1341 // typename prefix, the template prefix is allowed in cases
1342 // where it is not strictly necessary; i.e., when the
1343 // nested-name-specifier or the expression on the left of the ->
1344 // or . is not dependent on a template-parameter, or the use
1345 // does not appear in the scope of a template. -end note]
1346 //
1347 // Note: C++03 was more strict here, because it banned the use of
1348 // the "template" keyword prior to a template-name that was not a
1349 // dependent name. C++ DR468 relaxed this requirement (the
1350 // "template" keyword is now permitted). We follow the C++0x
1351 // rules, even in C++03 mode, retroactively applying the DR.
1352 TemplateTy Template;
Mike Stump11289f42009-09-09 15:08:12 +00001353 TemplateNameKind TNK = isTemplateName(0, Name, NameLoc, &SS, ObjectType,
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001354 false, Template);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001355 if (TNK == TNK_Non_template) {
1356 Diag(NameLoc, diag::err_template_kw_refers_to_non_template)
1357 << &Name;
1358 return TemplateTy();
1359 }
1360
1361 return Template;
1362 }
1363
Mike Stump11289f42009-09-09 15:08:12 +00001364 NestedNameSpecifier *Qualifier
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001365 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001366 return TemplateTy::make(Context.getDependentTemplateName(Qualifier, &Name));
1367}
1368
Mike Stump11289f42009-09-09 15:08:12 +00001369bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
Anders Carlssonc8cbb2d2009-06-13 00:33:33 +00001370 const TemplateArgument &Arg,
1371 TemplateArgumentListBuilder &Converted) {
1372 // Check template type parameter.
1373 if (Arg.getKind() != TemplateArgument::Type) {
1374 // C++ [temp.arg.type]p1:
1375 // A template-argument for a template-parameter which is a
1376 // type shall be a type-id.
1377
1378 // We have a template type parameter but the template argument
1379 // is not a type.
1380 Diag(Arg.getLocation(), diag::err_template_arg_must_be_type);
1381 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump11289f42009-09-09 15:08:12 +00001382
Anders Carlssonc8cbb2d2009-06-13 00:33:33 +00001383 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001384 }
Anders Carlssonc8cbb2d2009-06-13 00:33:33 +00001385
1386 if (CheckTemplateArgument(Param, Arg.getAsType(), Arg.getLocation()))
1387 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001388
Anders Carlssonc8cbb2d2009-06-13 00:33:33 +00001389 // Add the converted template type argument.
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001390 Converted.Append(
Anders Carlssonc8cbb2d2009-06-13 00:33:33 +00001391 TemplateArgument(Arg.getLocation(),
1392 Context.getCanonicalType(Arg.getAsType())));
1393 return false;
1394}
1395
Douglas Gregord32e0282009-02-09 23:23:08 +00001396/// \brief Check that the given template argument list is well-formed
1397/// for specializing the given template.
1398bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
1399 SourceLocation TemplateLoc,
1400 SourceLocation LAngleLoc,
Douglas Gregorc40290e2009-03-09 23:48:35 +00001401 const TemplateArgument *TemplateArgs,
1402 unsigned NumTemplateArgs,
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001403 SourceLocation RAngleLoc,
Douglas Gregore3f1f352009-07-01 00:28:38 +00001404 bool PartialTemplateArgs,
Anders Carlsson8aa89d42009-06-05 03:43:12 +00001405 TemplateArgumentListBuilder &Converted) {
Douglas Gregord32e0282009-02-09 23:23:08 +00001406 TemplateParameterList *Params = Template->getTemplateParameters();
1407 unsigned NumParams = Params->size();
Douglas Gregorc40290e2009-03-09 23:48:35 +00001408 unsigned NumArgs = NumTemplateArgs;
Douglas Gregord32e0282009-02-09 23:23:08 +00001409 bool Invalid = false;
1410
Mike Stump11289f42009-09-09 15:08:12 +00001411 bool HasParameterPack =
Anders Carlsson15201f12009-06-13 02:08:00 +00001412 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump11289f42009-09-09 15:08:12 +00001413
Anders Carlsson15201f12009-06-13 02:08:00 +00001414 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregore3f1f352009-07-01 00:28:38 +00001415 (NumArgs < Params->getMinRequiredArguments() &&
1416 !PartialTemplateArgs)) {
Douglas Gregord32e0282009-02-09 23:23:08 +00001417 // FIXME: point at either the first arg beyond what we can handle,
1418 // or the '>', depending on whether we have too many or too few
1419 // arguments.
1420 SourceRange Range;
1421 if (NumArgs > NumParams)
Douglas Gregorc40290e2009-03-09 23:48:35 +00001422 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregord32e0282009-02-09 23:23:08 +00001423 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
1424 << (NumArgs > NumParams)
1425 << (isa<ClassTemplateDecl>(Template)? 0 :
1426 isa<FunctionTemplateDecl>(Template)? 1 :
1427 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
1428 << Template << Range;
Douglas Gregorf8f86832009-02-11 18:16:40 +00001429 Diag(Template->getLocation(), diag::note_template_decl_here)
1430 << Params->getSourceRange();
Douglas Gregord32e0282009-02-09 23:23:08 +00001431 Invalid = true;
1432 }
Mike Stump11289f42009-09-09 15:08:12 +00001433
1434 // C++ [temp.arg]p1:
Douglas Gregord32e0282009-02-09 23:23:08 +00001435 // [...] The type and form of each template-argument specified in
1436 // a template-id shall match the type and form specified for the
1437 // corresponding parameter declared by the template in its
1438 // template-parameter-list.
1439 unsigned ArgIdx = 0;
1440 for (TemplateParameterList::iterator Param = Params->begin(),
1441 ParamEnd = Params->end();
1442 Param != ParamEnd; ++Param, ++ArgIdx) {
Douglas Gregore3f1f352009-07-01 00:28:38 +00001443 if (ArgIdx > NumArgs && PartialTemplateArgs)
1444 break;
Mike Stump11289f42009-09-09 15:08:12 +00001445
Douglas Gregord32e0282009-02-09 23:23:08 +00001446 // Decode the template argument
Douglas Gregorc40290e2009-03-09 23:48:35 +00001447 TemplateArgument Arg;
Douglas Gregord32e0282009-02-09 23:23:08 +00001448 if (ArgIdx >= NumArgs) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001449 // Retrieve the default template argument from the template
1450 // parameter.
1451 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson15201f12009-06-13 02:08:00 +00001452 if (TTP->isParameterPack()) {
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001453 // We have an empty argument pack.
1454 Converted.BeginPack();
1455 Converted.EndPack();
Anders Carlsson15201f12009-06-13 02:08:00 +00001456 break;
1457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001459 if (!TTP->hasDefaultArgument())
1460 break;
1461
Douglas Gregorc40290e2009-03-09 23:48:35 +00001462 QualType ArgType = TTP->getDefaultArgument();
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001463
1464 // If the argument type is dependent, instantiate it now based
1465 // on the previously-computed template arguments.
Douglas Gregor79cf6032009-03-10 20:44:00 +00001466 if (ArgType->isDependentType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001467 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001468 Template, Converted.getFlatArguments(),
Anders Carlsson8aa89d42009-06-05 03:43:12 +00001469 Converted.flatSize(),
Douglas Gregor79cf6032009-03-10 20:44:00 +00001470 SourceRange(TemplateLoc, RAngleLoc));
Douglas Gregord002c7b2009-05-11 23:53:27 +00001471
Anders Carlssonc8e71132009-06-05 04:47:51 +00001472 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001473 /*TakeArgs=*/false);
Mike Stump11289f42009-09-09 15:08:12 +00001474 ArgType = SubstType(ArgType,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001475 MultiLevelTemplateArgumentList(TemplateArgs),
John McCall76d824f2009-08-25 22:02:44 +00001476 TTP->getDefaultArgumentLoc(),
1477 TTP->getDeclName());
Douglas Gregor79cf6032009-03-10 20:44:00 +00001478 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001479
1480 if (ArgType.isNull())
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001481 return true;
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001482
Douglas Gregorc40290e2009-03-09 23:48:35 +00001483 Arg = TemplateArgument(TTP->getLocation(), ArgType);
Mike Stump11289f42009-09-09 15:08:12 +00001484 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001485 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1486 if (!NTTP->hasDefaultArgument())
1487 break;
1488
Mike Stump11289f42009-09-09 15:08:12 +00001489 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001490 Template, Converted.getFlatArguments(),
Anders Carlsson40ed3442009-06-11 16:06:49 +00001491 Converted.flatSize(),
1492 SourceRange(TemplateLoc, RAngleLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001493
Anders Carlsson40ed3442009-06-11 16:06:49 +00001494 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001495 /*TakeArgs=*/false);
Anders Carlsson40ed3442009-06-11 16:06:49 +00001496
Mike Stump11289f42009-09-09 15:08:12 +00001497 Sema::OwningExprResult E
1498 = SubstExpr(NTTP->getDefaultArgument(),
Douglas Gregor01afeef2009-08-28 20:31:08 +00001499 MultiLevelTemplateArgumentList(TemplateArgs));
Anders Carlsson40ed3442009-06-11 16:06:49 +00001500 if (E.isInvalid())
1501 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001502
Anders Carlsson40ed3442009-06-11 16:06:49 +00001503 Arg = TemplateArgument(E.takeAs<Expr>());
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001504 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001505 TemplateTemplateParmDecl *TempParm
1506 = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001507
1508 if (!TempParm->hasDefaultArgument())
1509 break;
1510
John McCall76d824f2009-08-25 22:02:44 +00001511 // FIXME: Subst default argument
Douglas Gregorc40290e2009-03-09 23:48:35 +00001512 Arg = TemplateArgument(TempParm->getDefaultArgument());
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001513 }
1514 } else {
1515 // Retrieve the template argument produced by the user.
Douglas Gregorc40290e2009-03-09 23:48:35 +00001516 Arg = TemplateArgs[ArgIdx];
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001517 }
1518
Douglas Gregord32e0282009-02-09 23:23:08 +00001519
1520 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Anders Carlsson15201f12009-06-13 02:08:00 +00001521 if (TTP->isParameterPack()) {
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001522 Converted.BeginPack();
Anders Carlsson15201f12009-06-13 02:08:00 +00001523 // Check all the remaining arguments (if any).
1524 for (; ArgIdx < NumArgs; ++ArgIdx) {
1525 if (CheckTemplateTypeArgument(TTP, TemplateArgs[ArgIdx], Converted))
1526 Invalid = true;
1527 }
Mike Stump11289f42009-09-09 15:08:12 +00001528
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001529 Converted.EndPack();
Anders Carlsson15201f12009-06-13 02:08:00 +00001530 } else {
1531 if (CheckTemplateTypeArgument(TTP, Arg, Converted))
1532 Invalid = true;
1533 }
Mike Stump11289f42009-09-09 15:08:12 +00001534 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregord32e0282009-02-09 23:23:08 +00001535 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
1536 // Check non-type template parameters.
Douglas Gregor463421d2009-03-03 04:44:36 +00001537
John McCall76d824f2009-08-25 22:02:44 +00001538 // Do substitution on the type of the non-type template parameter
1539 // with the template arguments we've seen thus far.
Douglas Gregor463421d2009-03-03 04:44:36 +00001540 QualType NTTPType = NTTP->getType();
1541 if (NTTPType->isDependentType()) {
John McCall76d824f2009-08-25 22:02:44 +00001542 // Do substitution on the type of the non-type template parameter.
Mike Stump11289f42009-09-09 15:08:12 +00001543 InstantiatingTemplate Inst(*this, TemplateLoc,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001544 Template, Converted.getFlatArguments(),
Anders Carlsson8aa89d42009-06-05 03:43:12 +00001545 Converted.flatSize(),
Douglas Gregor79cf6032009-03-10 20:44:00 +00001546 SourceRange(TemplateLoc, RAngleLoc));
1547
Anders Carlssonc8e71132009-06-05 04:47:51 +00001548 TemplateArgumentList TemplateArgs(Context, Converted,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001549 /*TakeArgs=*/false);
Mike Stump11289f42009-09-09 15:08:12 +00001550 NTTPType = SubstType(NTTPType,
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001551 MultiLevelTemplateArgumentList(TemplateArgs),
John McCall76d824f2009-08-25 22:02:44 +00001552 NTTP->getLocation(),
1553 NTTP->getDeclName());
Douglas Gregor463421d2009-03-03 04:44:36 +00001554 // If that worked, check the non-type template parameter type
1555 // for validity.
1556 if (!NTTPType.isNull())
Mike Stump11289f42009-09-09 15:08:12 +00001557 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
Douglas Gregor463421d2009-03-03 04:44:36 +00001558 NTTP->getLocation());
Douglas Gregor463421d2009-03-03 04:44:36 +00001559 if (NTTPType.isNull()) {
1560 Invalid = true;
1561 break;
1562 }
1563 }
1564
Douglas Gregorc40290e2009-03-09 23:48:35 +00001565 switch (Arg.getKind()) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001566 case TemplateArgument::Null:
1567 assert(false && "Should never see a NULL template argument here");
1568 break;
Mike Stump11289f42009-09-09 15:08:12 +00001569
Douglas Gregorc40290e2009-03-09 23:48:35 +00001570 case TemplateArgument::Expression: {
1571 Expr *E = Arg.getAsExpr();
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001572 TemplateArgument Result;
1573 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
Douglas Gregord32e0282009-02-09 23:23:08 +00001574 Invalid = true;
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001575 else
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001576 Converted.Append(Result);
Douglas Gregorc40290e2009-03-09 23:48:35 +00001577 break;
Douglas Gregord32e0282009-02-09 23:23:08 +00001578 }
1579
Douglas Gregorc40290e2009-03-09 23:48:35 +00001580 case TemplateArgument::Declaration:
1581 case TemplateArgument::Integral:
1582 // We've already checked this template argument, so just copy
1583 // it to the list of converted arguments.
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001584 Converted.Append(Arg);
Douglas Gregorc40290e2009-03-09 23:48:35 +00001585 break;
Douglas Gregord32e0282009-02-09 23:23:08 +00001586
Douglas Gregorc40290e2009-03-09 23:48:35 +00001587 case TemplateArgument::Type:
1588 // We have a non-type template parameter but the template
1589 // argument is a type.
Mike Stump11289f42009-09-09 15:08:12 +00001590
Douglas Gregorc40290e2009-03-09 23:48:35 +00001591 // C++ [temp.arg]p2:
1592 // In a template-argument, an ambiguity between a type-id and
1593 // an expression is resolved to a type-id, regardless of the
1594 // form of the corresponding template-parameter.
1595 //
1596 // We warn specifically about this case, since it can be rather
1597 // confusing for users.
1598 if (Arg.getAsType()->isFunctionType())
1599 Diag(Arg.getLocation(), diag::err_template_arg_nontype_ambig)
1600 << Arg.getAsType();
1601 else
1602 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr);
1603 Diag((*Param)->getLocation(), diag::note_template_param_here);
1604 Invalid = true;
Anders Carlssonbc343912009-06-15 17:04:53 +00001605 break;
Mike Stump11289f42009-09-09 15:08:12 +00001606
Anders Carlssonbc343912009-06-15 17:04:53 +00001607 case TemplateArgument::Pack:
1608 assert(0 && "FIXME: Implement!");
1609 break;
Douglas Gregorc40290e2009-03-09 23:48:35 +00001610 }
Mike Stump11289f42009-09-09 15:08:12 +00001611 } else {
Douglas Gregord32e0282009-02-09 23:23:08 +00001612 // Check template template parameters.
Mike Stump11289f42009-09-09 15:08:12 +00001613 TemplateTemplateParmDecl *TempParm
Douglas Gregord32e0282009-02-09 23:23:08 +00001614 = cast<TemplateTemplateParmDecl>(*Param);
Mike Stump11289f42009-09-09 15:08:12 +00001615
Douglas Gregorc40290e2009-03-09 23:48:35 +00001616 switch (Arg.getKind()) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001617 case TemplateArgument::Null:
1618 assert(false && "Should never see a NULL template argument here");
1619 break;
Mike Stump11289f42009-09-09 15:08:12 +00001620
Douglas Gregorc40290e2009-03-09 23:48:35 +00001621 case TemplateArgument::Expression: {
1622 Expr *ArgExpr = Arg.getAsExpr();
1623 if (ArgExpr && isa<DeclRefExpr>(ArgExpr) &&
1624 isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) {
1625 if (CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr)))
1626 Invalid = true;
Mike Stump11289f42009-09-09 15:08:12 +00001627
Douglas Gregorc40290e2009-03-09 23:48:35 +00001628 // Add the converted template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001629 Decl *D
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001630 = cast<DeclRefExpr>(ArgExpr)->getDecl()->getCanonicalDecl();
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001631 Converted.Append(TemplateArgument(Arg.getLocation(), D));
Douglas Gregorc40290e2009-03-09 23:48:35 +00001632 continue;
1633 }
1634 }
1635 // fall through
Mike Stump11289f42009-09-09 15:08:12 +00001636
Douglas Gregorc40290e2009-03-09 23:48:35 +00001637 case TemplateArgument::Type: {
1638 // We have a template template parameter but the template
1639 // argument does not refer to a template.
1640 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
1641 Invalid = true;
1642 break;
Douglas Gregord32e0282009-02-09 23:23:08 +00001643 }
1644
Douglas Gregorc40290e2009-03-09 23:48:35 +00001645 case TemplateArgument::Declaration:
1646 // We've already checked this template argument, so just copy
1647 // it to the list of converted arguments.
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001648 Converted.Append(Arg);
Douglas Gregorc40290e2009-03-09 23:48:35 +00001649 break;
Mike Stump11289f42009-09-09 15:08:12 +00001650
Douglas Gregorc40290e2009-03-09 23:48:35 +00001651 case TemplateArgument::Integral:
1652 assert(false && "Integral argument with template template parameter");
1653 break;
Mike Stump11289f42009-09-09 15:08:12 +00001654
Anders Carlssonbc343912009-06-15 17:04:53 +00001655 case TemplateArgument::Pack:
1656 assert(0 && "FIXME: Implement!");
1657 break;
Douglas Gregorc40290e2009-03-09 23:48:35 +00001658 }
Douglas Gregord32e0282009-02-09 23:23:08 +00001659 }
1660 }
1661
1662 return Invalid;
1663}
1664
1665/// \brief Check a template argument against its corresponding
1666/// template type parameter.
1667///
1668/// This routine implements the semantics of C++ [temp.arg.type]. It
1669/// returns true if an error occurred, and false otherwise.
Mike Stump11289f42009-09-09 15:08:12 +00001670bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
Douglas Gregord32e0282009-02-09 23:23:08 +00001671 QualType Arg, SourceLocation ArgLoc) {
1672 // C++ [temp.arg.type]p2:
1673 // A local type, a type with no linkage, an unnamed type or a type
1674 // compounded from any of these types shall not be used as a
1675 // template-argument for a template type-parameter.
1676 //
1677 // FIXME: Perform the recursive and no-linkage type checks.
1678 const TagType *Tag = 0;
John McCall9dd450b2009-09-21 23:43:11 +00001679 if (const EnumType *EnumT = Arg->getAs<EnumType>())
Douglas Gregord32e0282009-02-09 23:23:08 +00001680 Tag = EnumT;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001681 else if (const RecordType *RecordT = Arg->getAs<RecordType>())
Douglas Gregord32e0282009-02-09 23:23:08 +00001682 Tag = RecordT;
1683 if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod())
1684 return Diag(ArgLoc, diag::err_template_arg_local_type)
1685 << QualType(Tag, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001686 else if (Tag && !Tag->getDecl()->getDeclName() &&
Douglas Gregor65b2c4c2009-03-10 18:33:27 +00001687 !Tag->getDecl()->getTypedefForAnonDecl()) {
Douglas Gregord32e0282009-02-09 23:23:08 +00001688 Diag(ArgLoc, diag::err_template_arg_unnamed_type);
1689 Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here);
1690 return true;
1691 }
1692
1693 return false;
1694}
1695
Douglas Gregorccb07762009-02-11 19:52:55 +00001696/// \brief Checks whether the given template argument is the address
1697/// of an object or function according to C++ [temp.arg.nontype]p1.
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001698bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg,
1699 NamedDecl *&Entity) {
Douglas Gregorccb07762009-02-11 19:52:55 +00001700 bool Invalid = false;
1701
1702 // See through any implicit casts we added to fix the type.
1703 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1704 Arg = Cast->getSubExpr();
1705
Sebastian Redl576fd422009-05-10 18:38:11 +00001706 // C++0x allows nullptr, and there's no further checking to be done for that.
1707 if (Arg->getType()->isNullPtrType())
1708 return false;
1709
Douglas Gregorccb07762009-02-11 19:52:55 +00001710 // C++ [temp.arg.nontype]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001711 //
Douglas Gregorccb07762009-02-11 19:52:55 +00001712 // A template-argument for a non-type, non-template
1713 // template-parameter shall be one of: [...]
1714 //
1715 // -- the address of an object or function with external
1716 // linkage, including function templates and function
1717 // template-ids but excluding non-static class members,
1718 // expressed as & id-expression where the & is optional if
1719 // the name refers to a function or array, or if the
1720 // corresponding template-parameter is a reference; or
1721 DeclRefExpr *DRE = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001722
Douglas Gregorccb07762009-02-11 19:52:55 +00001723 // Ignore (and complain about) any excess parentheses.
1724 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1725 if (!Invalid) {
Mike Stump11289f42009-09-09 15:08:12 +00001726 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001727 diag::err_template_arg_extra_parens)
1728 << Arg->getSourceRange();
1729 Invalid = true;
1730 }
1731
1732 Arg = Parens->getSubExpr();
1733 }
1734
1735 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
1736 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1737 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
1738 } else
1739 DRE = dyn_cast<DeclRefExpr>(Arg);
1740
1741 if (!DRE || !isa<ValueDecl>(DRE->getDecl()))
Mike Stump11289f42009-09-09 15:08:12 +00001742 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001743 diag::err_template_arg_not_object_or_func_form)
1744 << Arg->getSourceRange();
1745
1746 // Cannot refer to non-static data members
1747 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl()))
1748 return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
1749 << Field << Arg->getSourceRange();
1750
1751 // Cannot refer to non-static member functions
1752 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
1753 if (!Method->isStatic())
Mike Stump11289f42009-09-09 15:08:12 +00001754 return Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001755 diag::err_template_arg_method)
1756 << Method << Arg->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001757
Douglas Gregorccb07762009-02-11 19:52:55 +00001758 // Functions must have external linkage.
1759 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
1760 if (Func->getStorageClass() == FunctionDecl::Static) {
Mike Stump11289f42009-09-09 15:08:12 +00001761 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001762 diag::err_template_arg_function_not_extern)
1763 << Func << Arg->getSourceRange();
1764 Diag(Func->getLocation(), diag::note_template_arg_internal_object)
1765 << true;
1766 return true;
1767 }
1768
1769 // Okay: we've named a function with external linkage.
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001770 Entity = Func;
Douglas Gregorccb07762009-02-11 19:52:55 +00001771 return Invalid;
1772 }
1773
1774 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
1775 if (!Var->hasGlobalStorage()) {
Mike Stump11289f42009-09-09 15:08:12 +00001776 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001777 diag::err_template_arg_object_not_extern)
1778 << Var << Arg->getSourceRange();
1779 Diag(Var->getLocation(), diag::note_template_arg_internal_object)
1780 << true;
1781 return true;
1782 }
1783
1784 // Okay: we've named an object with external linkage
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001785 Entity = Var;
Douglas Gregorccb07762009-02-11 19:52:55 +00001786 return Invalid;
1787 }
Mike Stump11289f42009-09-09 15:08:12 +00001788
Douglas Gregorccb07762009-02-11 19:52:55 +00001789 // We found something else, but we don't know specifically what it is.
Mike Stump11289f42009-09-09 15:08:12 +00001790 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001791 diag::err_template_arg_not_object_or_func)
1792 << Arg->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001793 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001794 diag::note_template_arg_refers_here);
1795 return true;
1796}
1797
1798/// \brief Checks whether the given template argument is a pointer to
1799/// member constant according to C++ [temp.arg.nontype]p1.
Mike Stump11289f42009-09-09 15:08:12 +00001800bool
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001801Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) {
Douglas Gregorccb07762009-02-11 19:52:55 +00001802 bool Invalid = false;
1803
1804 // See through any implicit casts we added to fix the type.
1805 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1806 Arg = Cast->getSubExpr();
1807
Sebastian Redl576fd422009-05-10 18:38:11 +00001808 // C++0x allows nullptr, and there's no further checking to be done for that.
1809 if (Arg->getType()->isNullPtrType())
1810 return false;
1811
Douglas Gregorccb07762009-02-11 19:52:55 +00001812 // C++ [temp.arg.nontype]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001813 //
Douglas Gregorccb07762009-02-11 19:52:55 +00001814 // A template-argument for a non-type, non-template
1815 // template-parameter shall be one of: [...]
1816 //
1817 // -- a pointer to member expressed as described in 5.3.1.
1818 QualifiedDeclRefExpr *DRE = 0;
1819
1820 // Ignore (and complain about) any excess parentheses.
1821 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
1822 if (!Invalid) {
Mike Stump11289f42009-09-09 15:08:12 +00001823 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001824 diag::err_template_arg_extra_parens)
1825 << Arg->getSourceRange();
1826 Invalid = true;
1827 }
1828
1829 Arg = Parens->getSubExpr();
1830 }
1831
1832 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg))
1833 if (UnOp->getOpcode() == UnaryOperator::AddrOf)
1834 DRE = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr());
1835
1836 if (!DRE)
1837 return Diag(Arg->getSourceRange().getBegin(),
1838 diag::err_template_arg_not_pointer_to_member_form)
1839 << Arg->getSourceRange();
1840
1841 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
1842 assert((isa<FieldDecl>(DRE->getDecl()) ||
1843 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
1844 "Only non-static member pointers can make it here");
1845
1846 // Okay: this is the address of a non-static member, and therefore
1847 // a member pointer constant.
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001848 Member = DRE->getDecl();
Douglas Gregorccb07762009-02-11 19:52:55 +00001849 return Invalid;
1850 }
1851
1852 // We found something else, but we don't know specifically what it is.
Mike Stump11289f42009-09-09 15:08:12 +00001853 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001854 diag::err_template_arg_not_pointer_to_member_form)
1855 << Arg->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001856 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorccb07762009-02-11 19:52:55 +00001857 diag::note_template_arg_refers_here);
1858 return true;
1859}
1860
Douglas Gregord32e0282009-02-09 23:23:08 +00001861/// \brief Check a template argument against its corresponding
1862/// non-type template parameter.
1863///
Douglas Gregor463421d2009-03-03 04:44:36 +00001864/// This routine implements the semantics of C++ [temp.arg.nontype].
1865/// It returns true if an error occurred, and false otherwise. \p
1866/// InstantiatedParamType is the type of the non-type template
1867/// parameter after it has been instantiated.
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001868///
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001869/// If no error was detected, Converted receives the converted template argument.
Douglas Gregord32e0282009-02-09 23:23:08 +00001870bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump11289f42009-09-09 15:08:12 +00001871 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001872 TemplateArgument &Converted) {
Douglas Gregorc40290e2009-03-09 23:48:35 +00001873 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
1874
Douglas Gregor86560402009-02-10 23:36:10 +00001875 // If either the parameter has a dependent type or the argument is
1876 // type-dependent, there's nothing we can check now.
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001877 // FIXME: Add template argument to Converted!
Douglas Gregorc40290e2009-03-09 23:48:35 +00001878 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
1879 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001880 Converted = TemplateArgument(Arg);
Douglas Gregor86560402009-02-10 23:36:10 +00001881 return false;
Douglas Gregorc40290e2009-03-09 23:48:35 +00001882 }
Douglas Gregor86560402009-02-10 23:36:10 +00001883
1884 // C++ [temp.arg.nontype]p5:
1885 // The following conversions are performed on each expression used
1886 // as a non-type template-argument. If a non-type
1887 // template-argument cannot be converted to the type of the
1888 // corresponding template-parameter then the program is
1889 // ill-formed.
1890 //
1891 // -- for a non-type template-parameter of integral or
1892 // enumeration type, integral promotions (4.5) and integral
1893 // conversions (4.7) are applied.
Douglas Gregor463421d2009-03-03 04:44:36 +00001894 QualType ParamType = InstantiatedParamType;
Douglas Gregor3a7796b2009-02-11 00:19:33 +00001895 QualType ArgType = Arg->getType();
Douglas Gregor86560402009-02-10 23:36:10 +00001896 if (ParamType->isIntegralType() || ParamType->isEnumeralType()) {
Douglas Gregor86560402009-02-10 23:36:10 +00001897 // C++ [temp.arg.nontype]p1:
1898 // A template-argument for a non-type, non-template
1899 // template-parameter shall be one of:
1900 //
1901 // -- an integral constant-expression of integral or enumeration
1902 // type; or
1903 // -- the name of a non-type template-parameter; or
1904 SourceLocation NonConstantLoc;
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001905 llvm::APSInt Value;
Douglas Gregor86560402009-02-10 23:36:10 +00001906 if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001907 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor86560402009-02-10 23:36:10 +00001908 diag::err_template_arg_not_integral_or_enumeral)
1909 << ArgType << Arg->getSourceRange();
1910 Diag(Param->getLocation(), diag::note_template_param_here);
1911 return true;
1912 } else if (!Arg->isValueDependent() &&
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001913 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor86560402009-02-10 23:36:10 +00001914 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
1915 << ArgType << Arg->getSourceRange();
1916 return true;
1917 }
1918
1919 // FIXME: We need some way to more easily get the unqualified form
1920 // of the types without going all the way to the
1921 // canonical type.
1922 if (Context.getCanonicalType(ParamType).getCVRQualifiers())
1923 ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType();
1924 if (Context.getCanonicalType(ArgType).getCVRQualifiers())
1925 ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType();
1926
1927 // Try to convert the argument to the parameter's type.
1928 if (ParamType == ArgType) {
1929 // Okay: no conversion necessary
1930 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
1931 !ParamType->isEnumeralType()) {
1932 // This is an integral promotion or conversion.
1933 ImpCastExprToType(Arg, ParamType);
1934 } else {
1935 // We can't perform this conversion.
Mike Stump11289f42009-09-09 15:08:12 +00001936 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor86560402009-02-10 23:36:10 +00001937 diag::err_template_arg_not_convertible)
Douglas Gregor463421d2009-03-03 04:44:36 +00001938 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor86560402009-02-10 23:36:10 +00001939 Diag(Param->getLocation(), diag::note_template_param_here);
1940 return true;
1941 }
1942
Douglas Gregor52aba872009-03-14 00:20:21 +00001943 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall9dd450b2009-09-21 23:43:11 +00001944 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001945 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregor52aba872009-03-14 00:20:21 +00001946
1947 if (!Arg->isValueDependent()) {
1948 // Check that an unsigned parameter does not receive a negative
1949 // value.
1950 if (IntegerType->isUnsignedIntegerType()
1951 && (Value.isSigned() && Value.isNegative())) {
1952 Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative)
1953 << Value.toString(10) << Param->getType()
1954 << Arg->getSourceRange();
1955 Diag(Param->getLocation(), diag::note_template_param_here);
1956 return true;
1957 }
1958
1959 // Check that we don't overflow the template parameter type.
1960 unsigned AllowedBits = Context.getTypeSize(IntegerType);
1961 if (Value.getActiveBits() > AllowedBits) {
Mike Stump11289f42009-09-09 15:08:12 +00001962 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor52aba872009-03-14 00:20:21 +00001963 diag::err_template_arg_too_large)
1964 << Value.toString(10) << Param->getType()
1965 << Arg->getSourceRange();
1966 Diag(Param->getLocation(), diag::note_template_param_here);
1967 return true;
1968 }
1969
1970 if (Value.getBitWidth() != AllowedBits)
1971 Value.extOrTrunc(AllowedBits);
1972 Value.setIsSigned(IntegerType->isSignedIntegerType());
1973 }
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001974
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001975 // Add the value of this argument to the list of converted
1976 // arguments. We use the bitwidth and signedness of the template
1977 // parameter.
1978 if (Arg->isValueDependent()) {
1979 // The argument is value-dependent. Create a new
1980 // TemplateArgument with the converted expression.
1981 Converted = TemplateArgument(Arg);
1982 return false;
Douglas Gregor264ec4f2009-02-17 01:05:43 +00001983 }
1984
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001985 Converted = TemplateArgument(StartLoc, Value,
Mike Stump11289f42009-09-09 15:08:12 +00001986 ParamType->isEnumeralType() ? ParamType
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001987 : IntegerType);
Douglas Gregor86560402009-02-10 23:36:10 +00001988 return false;
1989 }
Douglas Gregor3a7796b2009-02-11 00:19:33 +00001990
Douglas Gregor6f233ef2009-02-11 01:18:59 +00001991 // Handle pointer-to-function, reference-to-function, and
1992 // pointer-to-member-function all in (roughly) the same way.
1993 if (// -- For a non-type template-parameter of type pointer to
1994 // function, only the function-to-pointer conversion (4.3) is
1995 // applied. If the template-argument represents a set of
1996 // overloaded functions (or a pointer to such), the matching
1997 // function is selected from the set (13.4).
Sebastian Redl576fd422009-05-10 18:38:11 +00001998 // In C++0x, any std::nullptr_t value can be converted.
Douglas Gregor6f233ef2009-02-11 01:18:59 +00001999 (ParamType->isPointerType() &&
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002000 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002001 // -- For a non-type template-parameter of type reference to
2002 // function, no conversions apply. If the template-argument
2003 // represents a set of overloaded functions, the matching
2004 // function is selected from the set (13.4).
2005 (ParamType->isReferenceType() &&
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002006 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002007 // -- For a non-type template-parameter of type pointer to
2008 // member function, no conversions apply. If the
2009 // template-argument represents a set of overloaded member
2010 // functions, the matching member function is selected from
2011 // the set (13.4).
Sebastian Redl576fd422009-05-10 18:38:11 +00002012 // Again, C++0x allows a std::nullptr_t value.
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002013 (ParamType->isMemberPointerType() &&
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002014 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002015 ->isFunctionType())) {
Mike Stump11289f42009-09-09 15:08:12 +00002016 if (Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorccb07762009-02-11 19:52:55 +00002017 ParamType.getNonReferenceType())) {
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002018 // We don't have to do anything: the types already match.
Sebastian Redl576fd422009-05-10 18:38:11 +00002019 } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() ||
2020 ParamType->isMemberPointerType())) {
2021 ArgType = ParamType;
2022 ImpCastExprToType(Arg, ParamType);
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002023 } else if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002024 ArgType = Context.getPointerType(ArgType);
2025 ImpCastExprToType(Arg, ArgType);
Mike Stump11289f42009-09-09 15:08:12 +00002026 } else if (FunctionDecl *Fn
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002027 = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) {
Douglas Gregor171c45a2009-02-18 21:56:37 +00002028 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
2029 return true;
2030
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002031 FixOverloadedFunctionReference(Arg, Fn);
2032 ArgType = Arg->getType();
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002033 if (ArgType->isFunctionType() && ParamType->isPointerType()) {
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002034 ArgType = Context.getPointerType(Arg->getType());
2035 ImpCastExprToType(Arg, ArgType);
2036 }
2037 }
2038
Mike Stump11289f42009-09-09 15:08:12 +00002039 if (!Context.hasSameUnqualifiedType(ArgType,
Douglas Gregorccb07762009-02-11 19:52:55 +00002040 ParamType.getNonReferenceType())) {
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002041 // We can't perform this conversion.
Mike Stump11289f42009-09-09 15:08:12 +00002042 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002043 diag::err_template_arg_not_convertible)
Douglas Gregor463421d2009-03-03 04:44:36 +00002044 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002045 Diag(Param->getLocation(), diag::note_template_param_here);
2046 return true;
2047 }
Mike Stump11289f42009-09-09 15:08:12 +00002048
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002049 if (ParamType->isMemberPointerType()) {
2050 NamedDecl *Member = 0;
2051 if (CheckTemplateArgumentPointerToMember(Arg, Member))
2052 return true;
2053
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002054 if (Member)
2055 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor74eba0b2009-06-11 18:10:32 +00002056 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002057 return false;
2058 }
Mike Stump11289f42009-09-09 15:08:12 +00002059
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002060 NamedDecl *Entity = 0;
2061 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2062 return true;
2063
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002064 if (Entity)
2065 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor74eba0b2009-06-11 18:10:32 +00002066 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002067 return false;
Douglas Gregor3a7796b2009-02-11 00:19:33 +00002068 }
2069
Chris Lattner696197c2009-02-20 21:37:53 +00002070 if (ParamType->isPointerType()) {
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002071 // -- for a non-type template-parameter of type pointer to
2072 // object, qualification conversions (4.4) and the
2073 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl576fd422009-05-10 18:38:11 +00002074 // C++0x also allows a value of std::nullptr_t.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002075 assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() &&
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002076 "Only object pointers allowed here");
Douglas Gregora9faa442009-02-11 00:44:29 +00002077
Sebastian Redl576fd422009-05-10 18:38:11 +00002078 if (ArgType->isNullPtrType()) {
2079 ArgType = ParamType;
2080 ImpCastExprToType(Arg, ParamType);
2081 } else if (ArgType->isArrayType()) {
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002082 ArgType = Context.getArrayDecayedType(ArgType);
2083 ImpCastExprToType(Arg, ArgType);
Douglas Gregora9faa442009-02-11 00:44:29 +00002084 }
Sebastian Redl576fd422009-05-10 18:38:11 +00002085
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002086 if (IsQualificationConversion(ArgType, ParamType)) {
2087 ArgType = ParamType;
2088 ImpCastExprToType(Arg, ParamType);
2089 }
Mike Stump11289f42009-09-09 15:08:12 +00002090
Douglas Gregor1515f762009-02-11 18:22:40 +00002091 if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) {
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002092 // We can't perform this conversion.
Mike Stump11289f42009-09-09 15:08:12 +00002093 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002094 diag::err_template_arg_not_convertible)
Douglas Gregor463421d2009-03-03 04:44:36 +00002095 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002096 Diag(Param->getLocation(), diag::note_template_param_here);
2097 return true;
2098 }
Mike Stump11289f42009-09-09 15:08:12 +00002099
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002100 NamedDecl *Entity = 0;
2101 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2102 return true;
2103
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002104 if (Entity)
2105 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor74eba0b2009-06-11 18:10:32 +00002106 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002107 return false;
Douglas Gregora9faa442009-02-11 00:44:29 +00002108 }
Mike Stump11289f42009-09-09 15:08:12 +00002109
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002110 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002111 // -- For a non-type template-parameter of type reference to
2112 // object, no conversions apply. The type referred to by the
2113 // reference may be more cv-qualified than the (otherwise
2114 // identical) type of the template-argument. The
2115 // template-parameter is bound directly to the
2116 // template-argument, which must be an lvalue.
Douglas Gregor64259f52009-03-24 20:32:41 +00002117 assert(ParamRefType->getPointeeType()->isObjectType() &&
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002118 "Only object references allowed here");
Douglas Gregora9faa442009-02-11 00:44:29 +00002119
Douglas Gregor1515f762009-02-11 18:22:40 +00002120 if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) {
Mike Stump11289f42009-09-09 15:08:12 +00002121 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002122 diag::err_template_arg_no_ref_bind)
Douglas Gregor463421d2009-03-03 04:44:36 +00002123 << InstantiatedParamType << Arg->getType()
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002124 << Arg->getSourceRange();
2125 Diag(Param->getLocation(), diag::note_template_param_here);
2126 return true;
2127 }
2128
Mike Stump11289f42009-09-09 15:08:12 +00002129 unsigned ParamQuals
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002130 = Context.getCanonicalType(ParamType).getCVRQualifiers();
2131 unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers();
Mike Stump11289f42009-09-09 15:08:12 +00002132
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002133 if ((ParamQuals | ArgQuals) != ParamQuals) {
2134 Diag(Arg->getSourceRange().getBegin(),
2135 diag::err_template_arg_ref_bind_ignores_quals)
Douglas Gregor463421d2009-03-03 04:44:36 +00002136 << InstantiatedParamType << Arg->getType()
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002137 << Arg->getSourceRange();
2138 Diag(Param->getLocation(), diag::note_template_param_here);
2139 return true;
2140 }
Mike Stump11289f42009-09-09 15:08:12 +00002141
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002142 NamedDecl *Entity = 0;
2143 if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity))
2144 return true;
2145
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002146 Entity = cast<NamedDecl>(Entity->getCanonicalDecl());
Douglas Gregor74eba0b2009-06-11 18:10:32 +00002147 Converted = TemplateArgument(StartLoc, Entity);
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002148 return false;
Douglas Gregor6f233ef2009-02-11 01:18:59 +00002149 }
Douglas Gregor0e558532009-02-11 16:16:59 +00002150
2151 // -- For a non-type template-parameter of type pointer to data
2152 // member, qualification conversions (4.4) are applied.
Sebastian Redl576fd422009-05-10 18:38:11 +00002153 // C++0x allows std::nullptr_t values.
Douglas Gregor0e558532009-02-11 16:16:59 +00002154 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
2155
Douglas Gregor1515f762009-02-11 18:22:40 +00002156 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor0e558532009-02-11 16:16:59 +00002157 // Types match exactly: nothing more to do here.
Sebastian Redl576fd422009-05-10 18:38:11 +00002158 } else if (ArgType->isNullPtrType()) {
2159 ImpCastExprToType(Arg, ParamType);
Douglas Gregor0e558532009-02-11 16:16:59 +00002160 } else if (IsQualificationConversion(ArgType, ParamType)) {
2161 ImpCastExprToType(Arg, ParamType);
2162 } else {
2163 // We can't perform this conversion.
Mike Stump11289f42009-09-09 15:08:12 +00002164 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor0e558532009-02-11 16:16:59 +00002165 diag::err_template_arg_not_convertible)
Douglas Gregor463421d2009-03-03 04:44:36 +00002166 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor0e558532009-02-11 16:16:59 +00002167 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump11289f42009-09-09 15:08:12 +00002168 return true;
Douglas Gregor0e558532009-02-11 16:16:59 +00002169 }
2170
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002171 NamedDecl *Member = 0;
2172 if (CheckTemplateArgumentPointerToMember(Arg, Member))
2173 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002174
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002175 if (Member)
2176 Member = cast<NamedDecl>(Member->getCanonicalDecl());
Douglas Gregor74eba0b2009-06-11 18:10:32 +00002177 Converted = TemplateArgument(StartLoc, Member);
Douglas Gregor264ec4f2009-02-17 01:05:43 +00002178 return false;
Douglas Gregord32e0282009-02-09 23:23:08 +00002179}
2180
2181/// \brief Check a template argument against its corresponding
2182/// template template parameter.
2183///
2184/// This routine implements the semantics of C++ [temp.arg.template].
2185/// It returns true if an error occurred, and false otherwise.
2186bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
2187 DeclRefExpr *Arg) {
Douglas Gregor85e0f662009-02-10 00:24:35 +00002188 assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed");
2189 TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl());
2190
2191 // C++ [temp.arg.template]p1:
2192 // A template-argument for a template template-parameter shall be
2193 // the name of a class template, expressed as id-expression. Only
2194 // primary class templates are considered when matching the
2195 // template template argument with the corresponding parameter;
2196 // partial specializations are not considered even if their
2197 // parameter lists match that of the template template parameter.
Douglas Gregord5222052009-06-12 19:43:02 +00002198 //
2199 // Note that we also allow template template parameters here, which
2200 // will happen when we are dealing with, e.g., class template
2201 // partial specializations.
Mike Stump11289f42009-09-09 15:08:12 +00002202 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregord5222052009-06-12 19:43:02 +00002203 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002204 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregor85e0f662009-02-10 00:24:35 +00002205 "Only function templates are possible here");
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00002206 Diag(Arg->getLocStart(), diag::err_template_arg_not_class_template);
2207 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregor85e0f662009-02-10 00:24:35 +00002208 << Template;
2209 }
2210
2211 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
2212 Param->getTemplateParameters(),
2213 true, true,
2214 Arg->getSourceRange().getBegin());
Douglas Gregord32e0282009-02-09 23:23:08 +00002215}
2216
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002217/// \brief Determine whether the given template parameter lists are
2218/// equivalent.
2219///
Mike Stump11289f42009-09-09 15:08:12 +00002220/// \param New The new template parameter list, typically written in the
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002221/// source code as part of a new template declaration.
2222///
2223/// \param Old The old template parameter list, typically found via
2224/// name lookup of the template declared with this template parameter
2225/// list.
2226///
2227/// \param Complain If true, this routine will produce a diagnostic if
2228/// the template parameter lists are not equivalent.
2229///
Douglas Gregor85e0f662009-02-10 00:24:35 +00002230/// \param IsTemplateTemplateParm If true, this routine is being
2231/// called to compare the template parameter lists of a template
2232/// template parameter.
2233///
2234/// \param TemplateArgLoc If this source location is valid, then we
2235/// are actually checking the template parameter list of a template
2236/// argument (New) against the template parameter list of its
2237/// corresponding template template parameter (Old). We produce
2238/// slightly different diagnostics in this scenario.
2239///
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002240/// \returns True if the template parameter lists are equal, false
2241/// otherwise.
Mike Stump11289f42009-09-09 15:08:12 +00002242bool
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002243Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
2244 TemplateParameterList *Old,
2245 bool Complain,
Douglas Gregor85e0f662009-02-10 00:24:35 +00002246 bool IsTemplateTemplateParm,
2247 SourceLocation TemplateArgLoc) {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002248 if (Old->size() != New->size()) {
2249 if (Complain) {
Douglas Gregor85e0f662009-02-10 00:24:35 +00002250 unsigned NextDiag = diag::err_template_param_list_different_arity;
2251 if (TemplateArgLoc.isValid()) {
2252 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2253 NextDiag = diag::note_template_param_list_different_arity;
Mike Stump11289f42009-09-09 15:08:12 +00002254 }
Douglas Gregor85e0f662009-02-10 00:24:35 +00002255 Diag(New->getTemplateLoc(), NextDiag)
2256 << (New->size() > Old->size())
2257 << IsTemplateTemplateParm
2258 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002259 Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
2260 << IsTemplateTemplateParm
2261 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
2262 }
2263
2264 return false;
2265 }
2266
2267 for (TemplateParameterList::iterator OldParm = Old->begin(),
2268 OldParmEnd = Old->end(), NewParm = New->begin();
2269 OldParm != OldParmEnd; ++OldParm, ++NewParm) {
2270 if ((*OldParm)->getKind() != (*NewParm)->getKind()) {
Douglas Gregor23061de2009-06-24 16:50:40 +00002271 if (Complain) {
2272 unsigned NextDiag = diag::err_template_param_different_kind;
2273 if (TemplateArgLoc.isValid()) {
2274 Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
2275 NextDiag = diag::note_template_param_different_kind;
2276 }
2277 Diag((*NewParm)->getLocation(), NextDiag)
2278 << IsTemplateTemplateParm;
2279 Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration)
2280 << IsTemplateTemplateParm;
Douglas Gregor85e0f662009-02-10 00:24:35 +00002281 }
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002282 return false;
2283 }
2284
2285 if (isa<TemplateTypeParmDecl>(*OldParm)) {
2286 // Okay; all template type parameters are equivalent (since we
Douglas Gregor85e0f662009-02-10 00:24:35 +00002287 // know we're at the same index).
2288#if 0
Mike Stump87c57ac2009-05-16 07:39:55 +00002289 // FIXME: Enable this code in debug mode *after* we properly go through
2290 // and "instantiate" the template parameter lists of template template
2291 // parameters. It's only after this instantiation that (1) any dependent
2292 // types within the template parameter list of the template template
2293 // parameter can be checked, and (2) the template type parameter depths
Douglas Gregor85e0f662009-02-10 00:24:35 +00002294 // will match up.
Mike Stump11289f42009-09-09 15:08:12 +00002295 QualType OldParmType
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002296 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm));
Mike Stump11289f42009-09-09 15:08:12 +00002297 QualType NewParmType
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002298 = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm));
Mike Stump11289f42009-09-09 15:08:12 +00002299 assert(Context.getCanonicalType(OldParmType) ==
2300 Context.getCanonicalType(NewParmType) &&
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002301 "type parameter mismatch?");
2302#endif
Mike Stump11289f42009-09-09 15:08:12 +00002303 } else if (NonTypeTemplateParmDecl *OldNTTP
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002304 = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) {
2305 // The types of non-type template parameters must agree.
2306 NonTypeTemplateParmDecl *NewNTTP
2307 = cast<NonTypeTemplateParmDecl>(*NewParm);
2308 if (Context.getCanonicalType(OldNTTP->getType()) !=
2309 Context.getCanonicalType(NewNTTP->getType())) {
2310 if (Complain) {
Douglas Gregor85e0f662009-02-10 00:24:35 +00002311 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
2312 if (TemplateArgLoc.isValid()) {
Mike Stump11289f42009-09-09 15:08:12 +00002313 Diag(TemplateArgLoc,
Douglas Gregor85e0f662009-02-10 00:24:35 +00002314 diag::err_template_arg_template_params_mismatch);
2315 NextDiag = diag::note_template_nontype_parm_different_type;
2316 }
2317 Diag(NewNTTP->getLocation(), NextDiag)
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002318 << NewNTTP->getType()
2319 << IsTemplateTemplateParm;
Mike Stump11289f42009-09-09 15:08:12 +00002320 Diag(OldNTTP->getLocation(),
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002321 diag::note_template_nontype_parm_prev_declaration)
2322 << OldNTTP->getType();
2323 }
2324 return false;
2325 }
2326 } else {
2327 // The template parameter lists of template template
2328 // parameters must agree.
2329 // FIXME: Could we perform a faster "type" comparison here?
Mike Stump11289f42009-09-09 15:08:12 +00002330 assert(isa<TemplateTemplateParmDecl>(*OldParm) &&
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002331 "Only template template parameters handled here");
Mike Stump11289f42009-09-09 15:08:12 +00002332 TemplateTemplateParmDecl *OldTTP
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002333 = cast<TemplateTemplateParmDecl>(*OldParm);
2334 TemplateTemplateParmDecl *NewTTP
2335 = cast<TemplateTemplateParmDecl>(*NewParm);
2336 if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
2337 OldTTP->getTemplateParameters(),
2338 Complain,
Douglas Gregor85e0f662009-02-10 00:24:35 +00002339 /*IsTemplateTemplateParm=*/true,
2340 TemplateArgLoc))
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002341 return false;
2342 }
2343 }
2344
2345 return true;
2346}
2347
2348/// \brief Check whether a template can be declared within this scope.
2349///
2350/// If the template declaration is valid in this scope, returns
2351/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump11289f42009-09-09 15:08:12 +00002352bool
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002353Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002354 // Find the nearest enclosing declaration scope.
2355 while ((S->getFlags() & Scope::DeclScope) == 0 ||
2356 (S->getFlags() & Scope::TemplateParamScope) != 0)
2357 S = S->getParent();
Mike Stump11289f42009-09-09 15:08:12 +00002358
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002359 // C++ [temp]p2:
2360 // A template-declaration can appear only as a namespace scope or
2361 // class scope declaration.
2362 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedmandfbd0c42009-07-31 01:43:05 +00002363 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
2364 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump11289f42009-09-09 15:08:12 +00002365 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002366 << TemplateParams->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002367
Eli Friedmandfbd0c42009-07-31 01:43:05 +00002368 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002369 Ctx = Ctx->getParent();
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002370
2371 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
2372 return false;
2373
Mike Stump11289f42009-09-09 15:08:12 +00002374 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002375 diag::err_template_outside_namespace_or_class_scope)
2376 << TemplateParams->getSourceRange();
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002377}
Douglas Gregor67a65642009-02-17 23:15:12 +00002378
Douglas Gregor54888652009-10-07 00:13:32 +00002379/// \brief Determine what kind of template specialization the given declaration
2380/// is.
2381static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
2382 if (!D)
2383 return TSK_Undeclared;
2384
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002385 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
2386 return Record->getTemplateSpecializationKind();
Douglas Gregor54888652009-10-07 00:13:32 +00002387 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
2388 return Function->getTemplateSpecializationKind();
Douglas Gregor86d142a2009-10-08 07:24:58 +00002389 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2390 return Var->getTemplateSpecializationKind();
2391
Douglas Gregor54888652009-10-07 00:13:32 +00002392 return TSK_Undeclared;
2393}
2394
2395/// \brief Check whether a specialization or explicit instantiation is
2396/// well-formed in the current context.
Douglas Gregorf47b9112009-02-25 22:02:03 +00002397///
Douglas Gregor54888652009-10-07 00:13:32 +00002398/// This routine determines whether a template specialization or
Mike Stump11289f42009-09-09 15:08:12 +00002399/// explicit instantiation can be declared in the current context
Douglas Gregor54888652009-10-07 00:13:32 +00002400/// (C++ [temp.expl.spec]p2, C++0x [temp.explicit]p2).
2401///
2402/// \param S the semantic analysis object for which this check is being
2403/// performed.
2404///
2405/// \param Specialized the entity being specialized or instantiated, which
2406/// may be a kind of template (class template, function template, etc.) or
2407/// a member of a class template (member function, static data member,
2408/// member class).
2409///
2410/// \param PrevDecl the previous declaration of this entity, if any.
2411///
2412/// \param Loc the location of the explicit specialization or instantiation of
2413/// this entity.
2414///
2415/// \param IsPartialSpecialization whether this is a partial specialization of
2416/// a class template.
2417///
2418/// \param TSK the kind of specialization or implicit instantiation being
2419/// performed.
2420///
2421/// \returns true if there was an error that we cannot recover from, false
2422/// otherwise.
2423static bool CheckTemplateSpecializationScope(Sema &S,
2424 NamedDecl *Specialized,
2425 NamedDecl *PrevDecl,
2426 SourceLocation Loc,
2427 bool IsPartialSpecialization,
2428 TemplateSpecializationKind TSK) {
2429 // Keep these "kind" numbers in sync with the %select statements in the
2430 // various diagnostics emitted by this routine.
2431 int EntityKind = 0;
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002432 bool isTemplateSpecialization = false;
2433 if (isa<ClassTemplateDecl>(Specialized)) {
Douglas Gregor54888652009-10-07 00:13:32 +00002434 EntityKind = IsPartialSpecialization? 1 : 0;
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002435 isTemplateSpecialization = true;
2436 } else if (isa<FunctionTemplateDecl>(Specialized)) {
Douglas Gregor54888652009-10-07 00:13:32 +00002437 EntityKind = 2;
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002438 isTemplateSpecialization = true;
2439 } else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregor54888652009-10-07 00:13:32 +00002440 EntityKind = 3;
2441 else if (isa<VarDecl>(Specialized))
2442 EntityKind = 4;
2443 else if (isa<RecordDecl>(Specialized))
2444 EntityKind = 5;
2445 else {
2446 S.Diag(Loc, diag::err_template_spec_unknown_kind) << TSK;
2447 S.Diag(Specialized->getLocation(), diag::note_specialized_entity) << TSK;
2448 return true;
2449 }
2450
Douglas Gregorf47b9112009-02-25 22:02:03 +00002451 // C++ [temp.expl.spec]p2:
2452 // An explicit specialization shall be declared in the namespace
2453 // of which the template is a member, or, for member templates, in
2454 // the namespace of which the enclosing class or enclosing class
2455 // template is a member. An explicit specialization of a member
2456 // function, member class or static data member of a class
2457 // template shall be declared in the namespace of which the class
2458 // template is a member. Such a declaration may also be a
2459 // definition. If the declaration is not a definition, the
2460 // specialization may be defined later in the name- space in which
2461 // the explicit specialization was declared, or in a namespace
2462 // that encloses the one in which the explicit specialization was
2463 // declared.
Douglas Gregor54888652009-10-07 00:13:32 +00002464 if (S.CurContext->getLookupContext()->isFunctionOrMethod()) {
2465 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
2466 << TSK << Specialized;
Douglas Gregorf47b9112009-02-25 22:02:03 +00002467 return true;
2468 }
Douglas Gregore4b05162009-10-07 17:21:34 +00002469
Douglas Gregor40fb7442009-10-07 17:30:37 +00002470 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
2471 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
2472 << TSK << Specialized;
2473 return true;
2474 }
2475
Douglas Gregore4b05162009-10-07 17:21:34 +00002476 // C++ [temp.class.spec]p6:
2477 // A class template partial specialization may be declared or redeclared
2478 // in any namespace scope in which its definition may be defined (14.5.1
2479 // and 14.5.2).
Douglas Gregor54888652009-10-07 00:13:32 +00002480 bool ComplainedAboutScope = false;
Douglas Gregore4b05162009-10-07 17:21:34 +00002481 DeclContext *SpecializedContext
Douglas Gregor54888652009-10-07 00:13:32 +00002482 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregore4b05162009-10-07 17:21:34 +00002483 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
Douglas Gregor54888652009-10-07 00:13:32 +00002484 if (TSK == TSK_ExplicitSpecialization) {
Douglas Gregor54888652009-10-07 00:13:32 +00002485 if ((!PrevDecl ||
2486 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
2487 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
2488 // There is no prior declaration of this entity, so this
2489 // specialization must be in the same context as the template
2490 // itself.
2491 if (!DC->Equals(SpecializedContext)) {
2492 if (isa<TranslationUnitDecl>(SpecializedContext))
2493 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
2494 << EntityKind << Specialized;
2495 else if (isa<NamespaceDecl>(SpecializedContext))
2496 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope)
2497 << EntityKind << Specialized
2498 << cast<NamedDecl>(SpecializedContext);
2499
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002500 S.Diag(Specialized->getLocation(), diag::note_specialized_entity)
2501 << TSK;
Douglas Gregor54888652009-10-07 00:13:32 +00002502 ComplainedAboutScope = true;
2503 }
Douglas Gregorf47b9112009-02-25 22:02:03 +00002504 }
Douglas Gregorf47b9112009-02-25 22:02:03 +00002505 }
Douglas Gregor54888652009-10-07 00:13:32 +00002506
2507 // Make sure that this redeclaration (or definition) occurs in an enclosing
2508 // namespace. We perform this check for explicit specializations and, in
2509 // C++0x, for explicit instantiations as well (per DR275).
2510 // FIXME: -Wc++0x should make these warnings.
2511 // Note that HandleDeclarator() performs this check for explicit
2512 // specializations of function templates, static data members, and member
2513 // functions, so we skip the check here for those kinds of entities.
2514 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregore4b05162009-10-07 17:21:34 +00002515 // Should we refactor that check, so that it occurs later?
2516 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor54888652009-10-07 00:13:32 +00002517 ((TSK == TSK_ExplicitSpecialization &&
2518 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
2519 isa<FunctionDecl>(Specialized))) ||
2520 S.getLangOptions().CPlusPlus0x)) {
2521 if (isa<TranslationUnitDecl>(SpecializedContext))
2522 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
2523 << EntityKind << Specialized;
2524 else if (isa<NamespaceDecl>(SpecializedContext))
2525 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
2526 << EntityKind << Specialized
2527 << cast<NamedDecl>(SpecializedContext);
2528
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002529 S.Diag(Specialized->getLocation(), diag::note_specialized_entity) << TSK;
Douglas Gregorf47b9112009-02-25 22:02:03 +00002530 }
Douglas Gregor54888652009-10-07 00:13:32 +00002531
2532 // FIXME: check for specialization-after-instantiation errors and such.
2533
Douglas Gregorf47b9112009-02-25 22:02:03 +00002534 return false;
2535}
Douglas Gregor54888652009-10-07 00:13:32 +00002536
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002537/// \brief Check the non-type template arguments of a class template
2538/// partial specialization according to C++ [temp.class.spec]p9.
2539///
Douglas Gregor09a30232009-06-12 22:08:06 +00002540/// \param TemplateParams the template parameters of the primary class
2541/// template.
2542///
2543/// \param TemplateArg the template arguments of the class template
2544/// partial specialization.
2545///
2546/// \param MirrorsPrimaryTemplate will be set true if the class
2547/// template partial specialization arguments are identical to the
2548/// implicit template arguments of the primary template. This is not
2549/// necessarily an error (C++0x), and it is left to the caller to diagnose
2550/// this condition when it is an error.
2551///
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002552/// \returns true if there was an error, false otherwise.
2553bool Sema::CheckClassTemplatePartialSpecializationArgs(
2554 TemplateParameterList *TemplateParams,
Anders Carlsson40c1d492009-06-13 18:20:51 +00002555 const TemplateArgumentListBuilder &TemplateArgs,
Douglas Gregor09a30232009-06-12 22:08:06 +00002556 bool &MirrorsPrimaryTemplate) {
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002557 // FIXME: the interface to this function will have to change to
2558 // accommodate variadic templates.
Douglas Gregor09a30232009-06-12 22:08:06 +00002559 MirrorsPrimaryTemplate = true;
Mike Stump11289f42009-09-09 15:08:12 +00002560
Anders Carlsson5947ddf2009-06-23 01:26:57 +00002561 const TemplateArgument *ArgList = TemplateArgs.getFlatArguments();
Mike Stump11289f42009-09-09 15:08:12 +00002562
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002563 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
Douglas Gregor09a30232009-06-12 22:08:06 +00002564 // Determine whether the template argument list of the partial
2565 // specialization is identical to the implicit argument list of
2566 // the primary template. The caller may need to diagnostic this as
2567 // an error per C++ [temp.class.spec]p9b3.
2568 if (MirrorsPrimaryTemplate) {
Mike Stump11289f42009-09-09 15:08:12 +00002569 if (TemplateTypeParmDecl *TTP
Douglas Gregor09a30232009-06-12 22:08:06 +00002570 = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) {
2571 if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) !=
Anders Carlsson40c1d492009-06-13 18:20:51 +00002572 Context.getCanonicalType(ArgList[I].getAsType()))
Douglas Gregor09a30232009-06-12 22:08:06 +00002573 MirrorsPrimaryTemplate = false;
2574 } else if (TemplateTemplateParmDecl *TTP
2575 = dyn_cast<TemplateTemplateParmDecl>(
2576 TemplateParams->getParam(I))) {
2577 // FIXME: We should settle on either Declaration storage or
2578 // Expression storage for template template parameters.
Mike Stump11289f42009-09-09 15:08:12 +00002579 TemplateTemplateParmDecl *ArgDecl
Douglas Gregor09a30232009-06-12 22:08:06 +00002580 = dyn_cast_or_null<TemplateTemplateParmDecl>(
Anders Carlsson40c1d492009-06-13 18:20:51 +00002581 ArgList[I].getAsDecl());
Douglas Gregor09a30232009-06-12 22:08:06 +00002582 if (!ArgDecl)
Mike Stump11289f42009-09-09 15:08:12 +00002583 if (DeclRefExpr *DRE
Anders Carlsson40c1d492009-06-13 18:20:51 +00002584 = dyn_cast_or_null<DeclRefExpr>(ArgList[I].getAsExpr()))
Douglas Gregor09a30232009-06-12 22:08:06 +00002585 ArgDecl = dyn_cast<TemplateTemplateParmDecl>(DRE->getDecl());
2586
2587 if (!ArgDecl ||
2588 ArgDecl->getIndex() != TTP->getIndex() ||
2589 ArgDecl->getDepth() != TTP->getDepth())
2590 MirrorsPrimaryTemplate = false;
2591 }
2592 }
2593
Mike Stump11289f42009-09-09 15:08:12 +00002594 NonTypeTemplateParmDecl *Param
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002595 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
Douglas Gregor09a30232009-06-12 22:08:06 +00002596 if (!Param) {
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002597 continue;
Douglas Gregor09a30232009-06-12 22:08:06 +00002598 }
2599
Anders Carlsson40c1d492009-06-13 18:20:51 +00002600 Expr *ArgExpr = ArgList[I].getAsExpr();
Douglas Gregor09a30232009-06-12 22:08:06 +00002601 if (!ArgExpr) {
2602 MirrorsPrimaryTemplate = false;
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002603 continue;
Douglas Gregor09a30232009-06-12 22:08:06 +00002604 }
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002605
2606 // C++ [temp.class.spec]p8:
2607 // A non-type argument is non-specialized if it is the name of a
2608 // non-type parameter. All other non-type arguments are
2609 // specialized.
2610 //
2611 // Below, we check the two conditions that only apply to
2612 // specialized non-type arguments, so skip any non-specialized
2613 // arguments.
2614 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Mike Stump11289f42009-09-09 15:08:12 +00002615 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor09a30232009-06-12 22:08:06 +00002616 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) {
Mike Stump11289f42009-09-09 15:08:12 +00002617 if (MirrorsPrimaryTemplate &&
Douglas Gregor09a30232009-06-12 22:08:06 +00002618 (Param->getIndex() != NTTP->getIndex() ||
2619 Param->getDepth() != NTTP->getDepth()))
2620 MirrorsPrimaryTemplate = false;
2621
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002622 continue;
Douglas Gregor09a30232009-06-12 22:08:06 +00002623 }
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002624
2625 // C++ [temp.class.spec]p9:
2626 // Within the argument list of a class template partial
2627 // specialization, the following restrictions apply:
2628 // -- A partially specialized non-type argument expression
2629 // shall not involve a template parameter of the partial
2630 // specialization except when the argument expression is a
2631 // simple identifier.
2632 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Mike Stump11289f42009-09-09 15:08:12 +00002633 Diag(ArgExpr->getLocStart(),
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002634 diag::err_dependent_non_type_arg_in_partial_spec)
2635 << ArgExpr->getSourceRange();
2636 return true;
2637 }
2638
2639 // -- The type of a template parameter corresponding to a
2640 // specialized non-type argument shall not be dependent on a
2641 // parameter of the specialization.
2642 if (Param->getType()->isDependentType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002643 Diag(ArgExpr->getLocStart(),
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002644 diag::err_dependent_typed_non_type_arg_in_partial_spec)
2645 << Param->getType()
2646 << ArgExpr->getSourceRange();
2647 Diag(Param->getLocation(), diag::note_template_param_here);
2648 return true;
2649 }
Douglas Gregor09a30232009-06-12 22:08:06 +00002650
2651 MirrorsPrimaryTemplate = false;
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002652 }
2653
2654 return false;
2655}
2656
Douglas Gregorc08f4892009-03-25 00:13:59 +00002657Sema::DeclResult
John McCall9bb74a52009-07-31 02:45:11 +00002658Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
2659 TagUseKind TUK,
Mike Stump11289f42009-09-09 15:08:12 +00002660 SourceLocation KWLoc,
Douglas Gregor67a65642009-02-17 23:15:12 +00002661 const CXXScopeSpec &SS,
Douglas Gregordc572a32009-03-30 22:58:21 +00002662 TemplateTy TemplateD,
Douglas Gregor67a65642009-02-17 23:15:12 +00002663 SourceLocation TemplateNameLoc,
2664 SourceLocation LAngleLoc,
Douglas Gregorc40290e2009-03-09 23:48:35 +00002665 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor67a65642009-02-17 23:15:12 +00002666 SourceLocation *TemplateArgLocs,
2667 SourceLocation RAngleLoc,
2668 AttributeList *Attr,
2669 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregor2208a292009-09-26 20:57:03 +00002670 assert(TUK != TUK_Reference && "References are not specializations");
John McCall06f6fe8d2009-09-04 01:14:41 +00002671
Douglas Gregor67a65642009-02-17 23:15:12 +00002672 // Find the class template we're specializing
Douglas Gregordc572a32009-03-30 22:58:21 +00002673 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump11289f42009-09-09 15:08:12 +00002674 ClassTemplateDecl *ClassTemplate
Douglas Gregordc572a32009-03-30 22:58:21 +00002675 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
Douglas Gregor67a65642009-02-17 23:15:12 +00002676
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002677 bool isExplicitSpecialization = false;
Douglas Gregor2373c592009-05-31 09:31:02 +00002678 bool isPartialSpecialization = false;
2679
Douglas Gregorf47b9112009-02-25 22:02:03 +00002680 // Check the validity of the template headers that introduce this
2681 // template.
Douglas Gregor2208a292009-09-26 20:57:03 +00002682 // FIXME: We probably shouldn't complain about these headers for
2683 // friend declarations.
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002684 TemplateParameterList *TemplateParams
Mike Stump11289f42009-09-09 15:08:12 +00002685 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
2686 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002687 TemplateParameterLists.size(),
2688 isExplicitSpecialization);
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002689 if (TemplateParams && TemplateParams->size() > 0) {
2690 isPartialSpecialization = true;
Douglas Gregorf47b9112009-02-25 22:02:03 +00002691
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002692 // C++ [temp.class.spec]p10:
2693 // The template parameter list of a specialization shall not
2694 // contain default template argument values.
2695 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2696 Decl *Param = TemplateParams->getParam(I);
2697 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
2698 if (TTP->hasDefaultArgument()) {
Mike Stump11289f42009-09-09 15:08:12 +00002699 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002700 diag::err_default_arg_in_partial_spec);
2701 TTP->setDefaultArgument(QualType(), SourceLocation(), false);
2702 }
2703 } else if (NonTypeTemplateParmDecl *NTTP
2704 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2705 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump11289f42009-09-09 15:08:12 +00002706 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002707 diag::err_default_arg_in_partial_spec)
2708 << DefArg->getSourceRange();
2709 NTTP->setDefaultArgument(0);
2710 DefArg->Destroy(Context);
2711 }
2712 } else {
2713 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
2714 if (Expr *DefArg = TTP->getDefaultArgument()) {
Mike Stump11289f42009-09-09 15:08:12 +00002715 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002716 diag::err_default_arg_in_partial_spec)
2717 << DefArg->getSourceRange();
2718 TTP->setDefaultArgument(0);
2719 DefArg->Destroy(Context);
Douglas Gregord5222052009-06-12 19:43:02 +00002720 }
2721 }
2722 }
Douglas Gregor3a88c1d2009-10-13 14:39:41 +00002723 } else if (TemplateParams) {
2724 if (TUK == TUK_Friend)
2725 Diag(KWLoc, diag::err_template_spec_friend)
2726 << CodeModificationHint::CreateRemoval(
2727 SourceRange(TemplateParams->getTemplateLoc(),
2728 TemplateParams->getRAngleLoc()))
2729 << SourceRange(LAngleLoc, RAngleLoc);
2730 else
2731 isExplicitSpecialization = true;
2732 } else if (TUK != TUK_Friend) {
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002733 Diag(KWLoc, diag::err_template_spec_needs_header)
2734 << CodeModificationHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor5c0405d2009-10-07 22:35:40 +00002735 isExplicitSpecialization = true;
2736 }
Douglas Gregorf47b9112009-02-25 22:02:03 +00002737
Douglas Gregor67a65642009-02-17 23:15:12 +00002738 // Check that the specialization uses the same tag kind as the
2739 // original template.
2740 TagDecl::TagKind Kind;
2741 switch (TagSpec) {
2742 default: assert(0 && "Unknown tag type!");
2743 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
2744 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
2745 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
2746 }
Douglas Gregord9034f02009-05-14 16:41:31 +00002747 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump11289f42009-09-09 15:08:12 +00002748 Kind, KWLoc,
Douglas Gregord9034f02009-05-14 16:41:31 +00002749 *ClassTemplate->getIdentifier())) {
Mike Stump11289f42009-09-09 15:08:12 +00002750 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor170512f2009-04-01 23:51:29 +00002751 << ClassTemplate
Mike Stump11289f42009-09-09 15:08:12 +00002752 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregor170512f2009-04-01 23:51:29 +00002753 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump11289f42009-09-09 15:08:12 +00002754 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor67a65642009-02-17 23:15:12 +00002755 diag::note_previous_use);
2756 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
2757 }
2758
Douglas Gregorc40290e2009-03-09 23:48:35 +00002759 // Translate the parser's template argument list in our AST format.
2760 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
2761 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
2762
Douglas Gregor67a65642009-02-17 23:15:12 +00002763 // Check that the template argument list is well-formed for this
2764 // template.
Anders Carlsson5947ddf2009-06-23 01:26:57 +00002765 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
2766 TemplateArgs.size());
Mike Stump11289f42009-09-09 15:08:12 +00002767 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlsson40c1d492009-06-13 18:20:51 +00002768 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregore3f1f352009-07-01 00:28:38 +00002769 RAngleLoc, false, Converted))
Douglas Gregorc08f4892009-03-25 00:13:59 +00002770 return true;
Douglas Gregor67a65642009-02-17 23:15:12 +00002771
Mike Stump11289f42009-09-09 15:08:12 +00002772 assert((Converted.structuredSize() ==
Douglas Gregor67a65642009-02-17 23:15:12 +00002773 ClassTemplate->getTemplateParameters()->size()) &&
2774 "Converted template argument list is too short!");
Mike Stump11289f42009-09-09 15:08:12 +00002775
Douglas Gregor2373c592009-05-31 09:31:02 +00002776 // Find the class template (partial) specialization declaration that
Douglas Gregor67a65642009-02-17 23:15:12 +00002777 // corresponds to these arguments.
2778 llvm::FoldingSetNodeID ID;
Douglas Gregord5222052009-06-12 19:43:02 +00002779 if (isPartialSpecialization) {
Douglas Gregor09a30232009-06-12 22:08:06 +00002780 bool MirrorsPrimaryTemplate;
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002781 if (CheckClassTemplatePartialSpecializationArgs(
2782 ClassTemplate->getTemplateParameters(),
Anders Carlsson5947ddf2009-06-23 01:26:57 +00002783 Converted, MirrorsPrimaryTemplate))
Douglas Gregor8cfd2ba2009-06-12 21:21:02 +00002784 return true;
2785
Douglas Gregor09a30232009-06-12 22:08:06 +00002786 if (MirrorsPrimaryTemplate) {
2787 // C++ [temp.class.spec]p9b3:
2788 //
Mike Stump11289f42009-09-09 15:08:12 +00002789 // -- The argument list of the specialization shall not be identical
2790 // to the implicit argument list of the primary template.
Douglas Gregor09a30232009-06-12 22:08:06 +00002791 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
John McCall9bb74a52009-07-31 02:45:11 +00002792 << (TUK == TUK_Definition)
Mike Stump11289f42009-09-09 15:08:12 +00002793 << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc,
Douglas Gregor09a30232009-06-12 22:08:06 +00002794 RAngleLoc));
John McCall9bb74a52009-07-31 02:45:11 +00002795 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
Douglas Gregor09a30232009-06-12 22:08:06 +00002796 ClassTemplate->getIdentifier(),
2797 TemplateNameLoc,
2798 Attr,
Douglas Gregor1d5e9f92009-08-25 17:23:04 +00002799 TemplateParams,
Douglas Gregor09a30232009-06-12 22:08:06 +00002800 AS_none);
2801 }
2802
Douglas Gregor2208a292009-09-26 20:57:03 +00002803 // FIXME: Diagnose friend partial specializations
2804
Douglas Gregor2373c592009-05-31 09:31:02 +00002805 // FIXME: Template parameter list matters, too
Mike Stump11289f42009-09-09 15:08:12 +00002806 ClassTemplatePartialSpecializationDecl::Profile(ID,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00002807 Converted.getFlatArguments(),
Douglas Gregor00044172009-07-29 16:09:57 +00002808 Converted.flatSize(),
2809 Context);
Mike Stump12b8ce12009-08-04 21:02:39 +00002810 } else
Anders Carlsson8aa89d42009-06-05 03:43:12 +00002811 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00002812 Converted.getFlatArguments(),
Douglas Gregor00044172009-07-29 16:09:57 +00002813 Converted.flatSize(),
2814 Context);
Douglas Gregor67a65642009-02-17 23:15:12 +00002815 void *InsertPos = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00002816 ClassTemplateSpecializationDecl *PrevDecl = 0;
2817
2818 if (isPartialSpecialization)
2819 PrevDecl
Mike Stump11289f42009-09-09 15:08:12 +00002820 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor2373c592009-05-31 09:31:02 +00002821 InsertPos);
2822 else
2823 PrevDecl
2824 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor67a65642009-02-17 23:15:12 +00002825
2826 ClassTemplateSpecializationDecl *Specialization = 0;
2827
Douglas Gregorf47b9112009-02-25 22:02:03 +00002828 // Check whether we can declare a class template specialization in
2829 // the current scope.
Douglas Gregor2208a292009-09-26 20:57:03 +00002830 if (TUK != TUK_Friend &&
Douglas Gregor54888652009-10-07 00:13:32 +00002831 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
2832 TemplateNameLoc, isPartialSpecialization,
2833 TSK_ExplicitSpecialization))
Douglas Gregorc08f4892009-03-25 00:13:59 +00002834 return true;
Douglas Gregor06db9f52009-10-12 20:18:28 +00002835
Douglas Gregor15301382009-07-30 17:40:51 +00002836 // The canonical type
2837 QualType CanonType;
Douglas Gregor2208a292009-09-26 20:57:03 +00002838 if (PrevDecl &&
2839 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
2840 TUK == TUK_Friend)) {
Douglas Gregor67a65642009-02-17 23:15:12 +00002841 // Since the only prior class template specialization with these
Douglas Gregor2208a292009-09-26 20:57:03 +00002842 // arguments was referenced but not declared, or we're only
2843 // referencing this specialization as a friend, reuse that
Douglas Gregor67a65642009-02-17 23:15:12 +00002844 // declaration node as our own, updating its source location to
2845 // reflect our new declaration.
Douglas Gregor67a65642009-02-17 23:15:12 +00002846 Specialization = PrevDecl;
Douglas Gregor1e249f82009-02-25 22:18:32 +00002847 Specialization->setLocation(TemplateNameLoc);
Douglas Gregor67a65642009-02-17 23:15:12 +00002848 PrevDecl = 0;
Douglas Gregor15301382009-07-30 17:40:51 +00002849 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregor2373c592009-05-31 09:31:02 +00002850 } else if (isPartialSpecialization) {
Douglas Gregor15301382009-07-30 17:40:51 +00002851 // Build the canonical type that describes the converted template
2852 // arguments of the class template partial specialization.
2853 CanonType = Context.getTemplateSpecializationType(
2854 TemplateName(ClassTemplate),
2855 Converted.getFlatArguments(),
2856 Converted.flatSize());
2857
Douglas Gregor2373c592009-05-31 09:31:02 +00002858 // Create a new class template partial specialization declaration node.
Mike Stump11289f42009-09-09 15:08:12 +00002859 TemplateParameterList *TemplateParams
Douglas Gregor2373c592009-05-31 09:31:02 +00002860 = static_cast<TemplateParameterList*>(*TemplateParameterLists.get());
2861 ClassTemplatePartialSpecializationDecl *PrevPartial
2862 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002863 ClassTemplatePartialSpecializationDecl *Partial
2864 = ClassTemplatePartialSpecializationDecl::Create(Context,
Douglas Gregor2373c592009-05-31 09:31:02 +00002865 ClassTemplate->getDeclContext(),
Anders Carlsson1b28c3e2009-06-05 04:06:48 +00002866 TemplateNameLoc,
2867 TemplateParams,
2868 ClassTemplate,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00002869 Converted,
Anders Carlsson1b28c3e2009-06-05 04:06:48 +00002870 PrevPartial);
Douglas Gregor2373c592009-05-31 09:31:02 +00002871
2872 if (PrevPartial) {
2873 ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial);
2874 ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial);
2875 } else {
2876 ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos);
2877 }
2878 Specialization = Partial;
Douglas Gregor91772d12009-06-13 00:26:55 +00002879
2880 // Check that all of the template parameters of the class template
2881 // partial specialization are deducible from the template
2882 // arguments. If not, this class template partial specialization
2883 // will never be used.
2884 llvm::SmallVector<bool, 8> DeducibleParams;
2885 DeducibleParams.resize(TemplateParams->size());
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002886 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
2887 DeducibleParams);
Douglas Gregor91772d12009-06-13 00:26:55 +00002888 unsigned NumNonDeducible = 0;
2889 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
2890 if (!DeducibleParams[I])
2891 ++NumNonDeducible;
2892
2893 if (NumNonDeducible) {
2894 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
2895 << (NumNonDeducible > 1)
2896 << SourceRange(TemplateNameLoc, RAngleLoc);
2897 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
2898 if (!DeducibleParams[I]) {
2899 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
2900 if (Param->getDeclName())
Mike Stump11289f42009-09-09 15:08:12 +00002901 Diag(Param->getLocation(),
Douglas Gregor91772d12009-06-13 00:26:55 +00002902 diag::note_partial_spec_unused_parameter)
2903 << Param->getDeclName();
2904 else
Mike Stump11289f42009-09-09 15:08:12 +00002905 Diag(Param->getLocation(),
Douglas Gregor91772d12009-06-13 00:26:55 +00002906 diag::note_partial_spec_unused_parameter)
2907 << std::string("<anonymous>");
2908 }
2909 }
2910 }
Douglas Gregor67a65642009-02-17 23:15:12 +00002911 } else {
2912 // Create a new class template specialization declaration node for
Douglas Gregor2208a292009-09-26 20:57:03 +00002913 // this explicit specialization or friend declaration.
Douglas Gregor67a65642009-02-17 23:15:12 +00002914 Specialization
Mike Stump11289f42009-09-09 15:08:12 +00002915 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor67a65642009-02-17 23:15:12 +00002916 ClassTemplate->getDeclContext(),
2917 TemplateNameLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002918 ClassTemplate,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00002919 Converted,
Douglas Gregor67a65642009-02-17 23:15:12 +00002920 PrevDecl);
2921
2922 if (PrevDecl) {
2923 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
2924 ClassTemplate->getSpecializations().GetOrInsertNode(Specialization);
2925 } else {
Mike Stump11289f42009-09-09 15:08:12 +00002926 ClassTemplate->getSpecializations().InsertNode(Specialization,
Douglas Gregor67a65642009-02-17 23:15:12 +00002927 InsertPos);
2928 }
Douglas Gregor15301382009-07-30 17:40:51 +00002929
2930 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregor67a65642009-02-17 23:15:12 +00002931 }
2932
Douglas Gregor06db9f52009-10-12 20:18:28 +00002933 // C++ [temp.expl.spec]p6:
2934 // If a template, a member template or the member of a class template is
2935 // explicitly specialized then that specialization shall be declared
2936 // before the first use of that specialization that would cause an implicit
2937 // instantiation to take place, in every translation unit in which such a
2938 // use occurs; no diagnostic is required.
2939 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
2940 SourceRange Range(TemplateNameLoc, RAngleLoc);
2941 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
2942 << Context.getTypeDeclType(Specialization) << Range;
2943
2944 Diag(PrevDecl->getPointOfInstantiation(),
2945 diag::note_instantiation_required_here)
2946 << (PrevDecl->getTemplateSpecializationKind()
2947 != TSK_ImplicitInstantiation);
2948 return true;
2949 }
2950
Douglas Gregor2208a292009-09-26 20:57:03 +00002951 // If this is not a friend, note that this is an explicit specialization.
2952 if (TUK != TUK_Friend)
2953 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregor67a65642009-02-17 23:15:12 +00002954
2955 // Check that this isn't a redefinition of this specialization.
John McCall9bb74a52009-07-31 02:45:11 +00002956 if (TUK == TUK_Definition) {
Douglas Gregor67a65642009-02-17 23:15:12 +00002957 if (RecordDecl *Def = Specialization->getDefinition(Context)) {
Douglas Gregor67a65642009-02-17 23:15:12 +00002958 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002959 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregor2373c592009-05-31 09:31:02 +00002960 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregor67a65642009-02-17 23:15:12 +00002961 Diag(Def->getLocation(), diag::note_previous_definition);
2962 Specialization->setInvalidDecl();
Douglas Gregorc08f4892009-03-25 00:13:59 +00002963 return true;
Douglas Gregor67a65642009-02-17 23:15:12 +00002964 }
2965 }
2966
Douglas Gregord56a91e2009-02-26 22:19:44 +00002967 // Build the fully-sugared type for this class template
2968 // specialization as the user wrote in the specialization
2969 // itself. This means that we'll pretty-print the type retrieved
2970 // from the specialization's declaration the way that the user
2971 // actually wrote the specialization, rather than formatting the
2972 // name based on the "canonical" representation used to store the
2973 // template arguments in the specialization.
Mike Stump11289f42009-09-09 15:08:12 +00002974 QualType WrittenTy
2975 = Context.getTemplateSpecializationType(Name,
Anders Carlsson40c1d492009-06-13 18:20:51 +00002976 TemplateArgs.data(),
Douglas Gregordc572a32009-03-30 22:58:21 +00002977 TemplateArgs.size(),
Douglas Gregor15301382009-07-30 17:40:51 +00002978 CanonType);
Douglas Gregor2208a292009-09-26 20:57:03 +00002979 if (TUK != TUK_Friend)
2980 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregorc40290e2009-03-09 23:48:35 +00002981 TemplateArgsIn.release();
Douglas Gregor67a65642009-02-17 23:15:12 +00002982
Douglas Gregor1e249f82009-02-25 22:18:32 +00002983 // C++ [temp.expl.spec]p9:
2984 // A template explicit specialization is in the scope of the
2985 // namespace in which the template was defined.
2986 //
2987 // We actually implement this paragraph where we set the semantic
2988 // context (in the creation of the ClassTemplateSpecializationDecl),
2989 // but we also maintain the lexical context where the actual
2990 // definition occurs.
Douglas Gregor67a65642009-02-17 23:15:12 +00002991 Specialization->setLexicalDeclContext(CurContext);
Mike Stump11289f42009-09-09 15:08:12 +00002992
Douglas Gregor67a65642009-02-17 23:15:12 +00002993 // We may be starting the definition of this specialization.
John McCall9bb74a52009-07-31 02:45:11 +00002994 if (TUK == TUK_Definition)
Douglas Gregor67a65642009-02-17 23:15:12 +00002995 Specialization->startDefinition();
2996
Douglas Gregor2208a292009-09-26 20:57:03 +00002997 if (TUK == TUK_Friend) {
2998 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
2999 TemplateNameLoc,
3000 WrittenTy.getTypePtr(),
3001 /*FIXME:*/KWLoc);
3002 Friend->setAccess(AS_public);
3003 CurContext->addDecl(Friend);
3004 } else {
3005 // Add the specialization into its lexical context, so that it can
3006 // be seen when iterating through the list of declarations in that
3007 // context. However, specializations are not found by name lookup.
3008 CurContext->addDecl(Specialization);
3009 }
Chris Lattner83f095c2009-03-28 19:18:32 +00003010 return DeclPtrTy::make(Specialization);
Douglas Gregor67a65642009-02-17 23:15:12 +00003011}
Douglas Gregor333489b2009-03-27 23:10:48 +00003012
Mike Stump11289f42009-09-09 15:08:12 +00003013Sema::DeclPtrTy
3014Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00003015 MultiTemplateParamsArg TemplateParameterLists,
3016 Declarator &D) {
3017 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
3018}
3019
Mike Stump11289f42009-09-09 15:08:12 +00003020Sema::DeclPtrTy
3021Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor17a7c122009-06-24 00:54:41 +00003022 MultiTemplateParamsArg TemplateParameterLists,
3023 Declarator &D) {
3024 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
3025 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
3026 "Not a function declarator!");
3027 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
Mike Stump11289f42009-09-09 15:08:12 +00003028
Douglas Gregor17a7c122009-06-24 00:54:41 +00003029 if (FTI.hasPrototype) {
Mike Stump11289f42009-09-09 15:08:12 +00003030 // FIXME: Diagnose arguments without names in C.
Douglas Gregor17a7c122009-06-24 00:54:41 +00003031 }
Mike Stump11289f42009-09-09 15:08:12 +00003032
Douglas Gregor17a7c122009-06-24 00:54:41 +00003033 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump11289f42009-09-09 15:08:12 +00003034
3035 DeclPtrTy DP = HandleDeclarator(ParentScope, D,
Douglas Gregor17a7c122009-06-24 00:54:41 +00003036 move(TemplateParameterLists),
3037 /*IsFunctionDefinition=*/true);
Mike Stump11289f42009-09-09 15:08:12 +00003038 if (FunctionTemplateDecl *FunctionTemplate
Douglas Gregord8d297c2009-07-21 23:53:31 +00003039 = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>()))
Mike Stump11289f42009-09-09 15:08:12 +00003040 return ActOnStartOfFunctionDef(FnBodyScope,
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003041 DeclPtrTy::make(FunctionTemplate->getTemplatedDecl()));
Douglas Gregord8d297c2009-07-21 23:53:31 +00003042 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>()))
3043 return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function));
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003044 return DeclPtrTy();
Douglas Gregor17a7c122009-06-24 00:54:41 +00003045}
3046
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003047/// \brief Perform semantic analysis for the given function template
3048/// specialization.
3049///
3050/// This routine performs all of the semantic analysis required for an
3051/// explicit function template specialization. On successful completion,
3052/// the function declaration \p FD will become a function template
3053/// specialization.
3054///
3055/// \param FD the function declaration, which will be updated to become a
3056/// function template specialization.
3057///
3058/// \param HasExplicitTemplateArgs whether any template arguments were
3059/// explicitly provided.
3060///
3061/// \param LAngleLoc the location of the left angle bracket ('<'), if
3062/// template arguments were explicitly provided.
3063///
3064/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
3065/// if any.
3066///
3067/// \param NumExplicitTemplateArgs the number of explicitly-provided template
3068/// arguments. This number may be zero even when HasExplicitTemplateArgs is
3069/// true as in, e.g., \c void sort<>(char*, char*);
3070///
3071/// \param RAngleLoc the location of the right angle bracket ('>'), if
3072/// template arguments were explicitly provided.
3073///
3074/// \param PrevDecl the set of declarations that
3075bool
3076Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
3077 bool HasExplicitTemplateArgs,
3078 SourceLocation LAngleLoc,
3079 const TemplateArgument *ExplicitTemplateArgs,
3080 unsigned NumExplicitTemplateArgs,
3081 SourceLocation RAngleLoc,
3082 NamedDecl *&PrevDecl) {
3083 // The set of function template specializations that could match this
3084 // explicit function template specialization.
3085 typedef llvm::SmallVector<FunctionDecl *, 8> CandidateSet;
3086 CandidateSet Candidates;
3087
3088 DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext();
3089 for (OverloadIterator Ovl(PrevDecl), OvlEnd; Ovl != OvlEnd; ++Ovl) {
3090 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(*Ovl)) {
3091 // Only consider templates found within the same semantic lookup scope as
3092 // FD.
3093 if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext()))
3094 continue;
3095
3096 // C++ [temp.expl.spec]p11:
3097 // A trailing template-argument can be left unspecified in the
3098 // template-id naming an explicit function template specialization
3099 // provided it can be deduced from the function argument type.
3100 // Perform template argument deduction to determine whether we may be
3101 // specializing this template.
3102 // FIXME: It is somewhat wasteful to build
3103 TemplateDeductionInfo Info(Context);
3104 FunctionDecl *Specialization = 0;
3105 if (TemplateDeductionResult TDK
3106 = DeduceTemplateArguments(FunTmpl, HasExplicitTemplateArgs,
3107 ExplicitTemplateArgs,
3108 NumExplicitTemplateArgs,
3109 FD->getType(),
3110 Specialization,
3111 Info)) {
3112 // FIXME: Template argument deduction failed; record why it failed, so
3113 // that we can provide nifty diagnostics.
3114 (void)TDK;
3115 continue;
3116 }
3117
3118 // Record this candidate.
3119 Candidates.push_back(Specialization);
3120 }
3121 }
3122
Douglas Gregor5de279c2009-09-26 03:41:46 +00003123 // Find the most specialized function template.
3124 FunctionDecl *Specialization = getMostSpecialized(Candidates.data(),
3125 Candidates.size(),
3126 TPOC_Other,
3127 FD->getLocation(),
3128 PartialDiagnostic(diag::err_function_template_spec_no_match)
3129 << FD->getDeclName(),
3130 PartialDiagnostic(diag::err_function_template_spec_ambiguous)
3131 << FD->getDeclName() << HasExplicitTemplateArgs,
3132 PartialDiagnostic(diag::note_function_template_spec_matched));
3133 if (!Specialization)
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003134 return true;
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003135
3136 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregor06db9f52009-10-12 20:18:28 +00003137 // If so, we have run afoul of .
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003138
Douglas Gregor54888652009-10-07 00:13:32 +00003139 // Check the scope of this explicit specialization.
3140 if (CheckTemplateSpecializationScope(*this,
3141 Specialization->getPrimaryTemplate(),
3142 Specialization, FD->getLocation(),
3143 false, TSK_ExplicitSpecialization))
3144 return true;
Douglas Gregor06db9f52009-10-12 20:18:28 +00003145
3146 // C++ [temp.expl.spec]p6:
3147 // If a template, a member template or the member of a class template is
3148 // explicitly specialized then that spe- cialization shall be declared
3149 // before the first use of that specialization that would cause an implicit
3150 // instantiation to take place, in every translation unit in which such a
3151 // use occurs; no diagnostic is required.
3152 FunctionTemplateSpecializationInfo *SpecInfo
3153 = Specialization->getTemplateSpecializationInfo();
3154 assert(SpecInfo && "Function template specialization info missing?");
3155 if (SpecInfo->getPointOfInstantiation().isValid()) {
3156 Diag(FD->getLocation(), diag::err_specialization_after_instantiation)
3157 << FD;
3158 Diag(SpecInfo->getPointOfInstantiation(),
3159 diag::note_instantiation_required_here)
3160 << (Specialization->getTemplateSpecializationKind()
3161 != TSK_ImplicitInstantiation);
3162 return true;
3163 }
Douglas Gregor54888652009-10-07 00:13:32 +00003164
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003165 // Mark the prior declaration as an explicit specialization, so that later
3166 // clients know that this is an explicit specialization.
Douglas Gregor06db9f52009-10-12 20:18:28 +00003167 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00003168
3169 // Turn the given function declaration into a function template
3170 // specialization, with the template arguments from the previous
3171 // specialization.
3172 FD->setFunctionTemplateSpecialization(Context,
3173 Specialization->getPrimaryTemplate(),
3174 new (Context) TemplateArgumentList(
3175 *Specialization->getTemplateSpecializationArgs()),
3176 /*InsertPos=*/0,
3177 TSK_ExplicitSpecialization);
3178
3179 // The "previous declaration" for this function template specialization is
3180 // the prior function template specialization.
3181 PrevDecl = Specialization;
3182 return false;
3183}
3184
Douglas Gregor86d142a2009-10-08 07:24:58 +00003185/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003186/// specialization.
3187///
3188/// This routine performs all of the semantic analysis required for an
3189/// explicit member function specialization. On successful completion,
3190/// the function declaration \p FD will become a member function
3191/// specialization.
3192///
Douglas Gregor86d142a2009-10-08 07:24:58 +00003193/// \param Member the member declaration, which will be updated to become a
3194/// specialization.
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003195///
3196/// \param PrevDecl the set of declarations, one of which may be specialized
3197/// by this function specialization.
3198bool
Douglas Gregor86d142a2009-10-08 07:24:58 +00003199Sema::CheckMemberSpecialization(NamedDecl *Member, NamedDecl *&PrevDecl) {
3200 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
3201
3202 // Try to find the member we are instantiating.
3203 NamedDecl *Instantiation = 0;
3204 NamedDecl *InstantiatedFrom = 0;
Douglas Gregor06db9f52009-10-12 20:18:28 +00003205 MemberSpecializationInfo *MSInfo = 0;
3206
Douglas Gregor86d142a2009-10-08 07:24:58 +00003207 if (!PrevDecl) {
3208 // Nowhere to look anyway.
3209 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
3210 for (OverloadIterator Ovl(PrevDecl), OvlEnd; Ovl != OvlEnd; ++Ovl) {
3211 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Ovl)) {
3212 if (Context.hasSameType(Function->getType(), Method->getType())) {
3213 Instantiation = Method;
3214 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregor06db9f52009-10-12 20:18:28 +00003215 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00003216 break;
3217 }
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003218 }
3219 }
Douglas Gregor86d142a2009-10-08 07:24:58 +00003220 } else if (isa<VarDecl>(Member)) {
3221 if (VarDecl *PrevVar = dyn_cast<VarDecl>(PrevDecl))
3222 if (PrevVar->isStaticDataMember()) {
3223 Instantiation = PrevDecl;
3224 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregor06db9f52009-10-12 20:18:28 +00003225 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00003226 }
3227 } else if (isa<RecordDecl>(Member)) {
3228 if (CXXRecordDecl *PrevRecord = dyn_cast<CXXRecordDecl>(PrevDecl)) {
3229 Instantiation = PrevDecl;
3230 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregor06db9f52009-10-12 20:18:28 +00003231 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00003232 }
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003233 }
3234
3235 if (!Instantiation) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00003236 // There is no previous declaration that matches. Since member
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003237 // specializations are always out-of-line, the caller will complain about
3238 // this mismatch later.
3239 return false;
3240 }
3241
Douglas Gregor86d142a2009-10-08 07:24:58 +00003242 // Make sure that this is a specialization of a member.
3243 if (!InstantiatedFrom) {
3244 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
3245 << Member;
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003246 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
3247 return true;
3248 }
3249
Douglas Gregor06db9f52009-10-12 20:18:28 +00003250 // C++ [temp.expl.spec]p6:
3251 // If a template, a member template or the member of a class template is
3252 // explicitly specialized then that spe- cialization shall be declared
3253 // before the first use of that specialization that would cause an implicit
3254 // instantiation to take place, in every translation unit in which such a
3255 // use occurs; no diagnostic is required.
3256 assert(MSInfo && "Member specialization info missing?");
3257 if (MSInfo->getPointOfInstantiation().isValid()) {
3258 Diag(Member->getLocation(), diag::err_specialization_after_instantiation)
3259 << Member;
3260 Diag(MSInfo->getPointOfInstantiation(),
3261 diag::note_instantiation_required_here)
3262 << (MSInfo->getTemplateSpecializationKind() != TSK_ImplicitInstantiation);
3263 return true;
3264 }
3265
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003266 // Check the scope of this explicit specialization.
3267 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor86d142a2009-10-08 07:24:58 +00003268 InstantiatedFrom,
3269 Instantiation, Member->getLocation(),
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003270 false, TSK_ExplicitSpecialization))
3271 return true;
Douglas Gregord801b062009-10-07 23:56:10 +00003272
Douglas Gregor86d142a2009-10-08 07:24:58 +00003273 // Note that this is an explicit instantiation of a member.
Douglas Gregorbbe8f462009-10-08 15:14:33 +00003274 // the original declaration to note that it is an explicit specialization
3275 // (if it was previously an implicit instantiation). This latter step
3276 // makes bookkeeping easier.
Douglas Gregor86d142a2009-10-08 07:24:58 +00003277 if (isa<FunctionDecl>(Member)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00003278 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
3279 if (InstantiationFunction->getTemplateSpecializationKind() ==
3280 TSK_ImplicitInstantiation) {
3281 InstantiationFunction->setTemplateSpecializationKind(
3282 TSK_ExplicitSpecialization);
3283 InstantiationFunction->setLocation(Member->getLocation());
3284 }
3285
Douglas Gregor86d142a2009-10-08 07:24:58 +00003286 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
3287 cast<CXXMethodDecl>(InstantiatedFrom),
3288 TSK_ExplicitSpecialization);
3289 } else if (isa<VarDecl>(Member)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00003290 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
3291 if (InstantiationVar->getTemplateSpecializationKind() ==
3292 TSK_ImplicitInstantiation) {
3293 InstantiationVar->setTemplateSpecializationKind(
3294 TSK_ExplicitSpecialization);
3295 InstantiationVar->setLocation(Member->getLocation());
3296 }
3297
Douglas Gregor86d142a2009-10-08 07:24:58 +00003298 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
3299 cast<VarDecl>(InstantiatedFrom),
3300 TSK_ExplicitSpecialization);
3301 } else {
3302 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00003303 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
3304 if (InstantiationClass->getTemplateSpecializationKind() ==
3305 TSK_ImplicitInstantiation) {
3306 InstantiationClass->setTemplateSpecializationKind(
3307 TSK_ExplicitSpecialization);
3308 InstantiationClass->setLocation(Member->getLocation());
3309 }
3310
Douglas Gregor86d142a2009-10-08 07:24:58 +00003311 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorbbe8f462009-10-08 15:14:33 +00003312 cast<CXXRecordDecl>(InstantiatedFrom),
3313 TSK_ExplicitSpecialization);
Douglas Gregor86d142a2009-10-08 07:24:58 +00003314 }
3315
Douglas Gregor5c0405d2009-10-07 22:35:40 +00003316 // Save the caller the trouble of having to figure out which declaration
3317 // this specialization matches.
3318 PrevDecl = Instantiation;
3319 return false;
3320}
3321
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003322// Explicit instantiation of a class template specialization
Douglas Gregor43e75172009-09-04 06:33:52 +00003323// FIXME: Implement extern template semantics
Douglas Gregora1f49972009-05-13 00:25:59 +00003324Sema::DeclResult
Mike Stump11289f42009-09-09 15:08:12 +00003325Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor43e75172009-09-04 06:33:52 +00003326 SourceLocation ExternLoc,
3327 SourceLocation TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +00003328 unsigned TagSpec,
Douglas Gregora1f49972009-05-13 00:25:59 +00003329 SourceLocation KWLoc,
3330 const CXXScopeSpec &SS,
3331 TemplateTy TemplateD,
3332 SourceLocation TemplateNameLoc,
3333 SourceLocation LAngleLoc,
3334 ASTTemplateArgsPtr TemplateArgsIn,
3335 SourceLocation *TemplateArgLocs,
3336 SourceLocation RAngleLoc,
3337 AttributeList *Attr) {
3338 // Find the class template we're specializing
3339 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump11289f42009-09-09 15:08:12 +00003340 ClassTemplateDecl *ClassTemplate
Douglas Gregora1f49972009-05-13 00:25:59 +00003341 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
3342
3343 // Check that the specialization uses the same tag kind as the
3344 // original template.
3345 TagDecl::TagKind Kind;
3346 switch (TagSpec) {
3347 default: assert(0 && "Unknown tag type!");
3348 case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break;
3349 case DeclSpec::TST_union: Kind = TagDecl::TK_union; break;
3350 case DeclSpec::TST_class: Kind = TagDecl::TK_class; break;
3351 }
Douglas Gregord9034f02009-05-14 16:41:31 +00003352 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump11289f42009-09-09 15:08:12 +00003353 Kind, KWLoc,
Douglas Gregord9034f02009-05-14 16:41:31 +00003354 *ClassTemplate->getIdentifier())) {
Mike Stump11289f42009-09-09 15:08:12 +00003355 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora1f49972009-05-13 00:25:59 +00003356 << ClassTemplate
Mike Stump11289f42009-09-09 15:08:12 +00003357 << CodeModificationHint::CreateReplacement(KWLoc,
Douglas Gregora1f49972009-05-13 00:25:59 +00003358 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump11289f42009-09-09 15:08:12 +00003359 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregora1f49972009-05-13 00:25:59 +00003360 diag::note_previous_use);
3361 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
3362 }
3363
Douglas Gregor54888652009-10-07 00:13:32 +00003364 TemplateSpecializationKind TSK
3365 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
3366 : TSK_ExplicitInstantiationDeclaration;
3367
Douglas Gregora1f49972009-05-13 00:25:59 +00003368 // Translate the parser's template argument list in our AST format.
3369 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
3370 translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs);
3371
3372 // Check that the template argument list is well-formed for this
3373 // template.
Anders Carlsson5947ddf2009-06-23 01:26:57 +00003374 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
3375 TemplateArgs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003376 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc,
Anders Carlssondd096d82009-06-05 02:12:32 +00003377 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregore3f1f352009-07-01 00:28:38 +00003378 RAngleLoc, false, Converted))
Douglas Gregora1f49972009-05-13 00:25:59 +00003379 return true;
3380
Mike Stump11289f42009-09-09 15:08:12 +00003381 assert((Converted.structuredSize() ==
Douglas Gregora1f49972009-05-13 00:25:59 +00003382 ClassTemplate->getTemplateParameters()->size()) &&
3383 "Converted template argument list is too short!");
Mike Stump11289f42009-09-09 15:08:12 +00003384
Douglas Gregora1f49972009-05-13 00:25:59 +00003385 // Find the class template specialization declaration that
3386 // corresponds to these arguments.
3387 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00003388 ClassTemplateSpecializationDecl::Profile(ID,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00003389 Converted.getFlatArguments(),
Douglas Gregor00044172009-07-29 16:09:57 +00003390 Converted.flatSize(),
3391 Context);
Douglas Gregora1f49972009-05-13 00:25:59 +00003392 void *InsertPos = 0;
3393 ClassTemplateSpecializationDecl *PrevDecl
3394 = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
3395
Douglas Gregor54888652009-10-07 00:13:32 +00003396 // C++0x [temp.explicit]p2:
3397 // [...] An explicit instantiation shall appear in an enclosing
3398 // namespace of its template. [...]
3399 //
3400 // This is C++ DR 275.
3401 if (CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
3402 TemplateNameLoc, false,
3403 TSK))
3404 return true;
3405
Douglas Gregora1f49972009-05-13 00:25:59 +00003406 ClassTemplateSpecializationDecl *Specialization = 0;
3407
Douglas Gregorf61eca92009-05-13 18:28:20 +00003408 bool SpecializationRequiresInstantiation = true;
Douglas Gregora1f49972009-05-13 00:25:59 +00003409 if (PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003410 if (PrevDecl->getSpecializationKind()
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003411 == TSK_ExplicitInstantiationDefinition) {
Douglas Gregora1f49972009-05-13 00:25:59 +00003412 // This particular specialization has already been declared or
3413 // instantiated. We cannot explicitly instantiate it.
Douglas Gregorf61eca92009-05-13 18:28:20 +00003414 Diag(TemplateNameLoc, diag::err_explicit_instantiation_duplicate)
3415 << Context.getTypeDeclType(PrevDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003416 Diag(PrevDecl->getLocation(),
Douglas Gregorf61eca92009-05-13 18:28:20 +00003417 diag::note_previous_explicit_instantiation);
Douglas Gregora1f49972009-05-13 00:25:59 +00003418 return DeclPtrTy::make(PrevDecl);
3419 }
3420
Douglas Gregorf61eca92009-05-13 18:28:20 +00003421 if (PrevDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003422 // C++ DR 259, C++0x [temp.explicit]p4:
Douglas Gregorf61eca92009-05-13 18:28:20 +00003423 // For a given set of template parameters, if an explicit
3424 // instantiation of a template appears after a declaration of
3425 // an explicit specialization for that template, the explicit
3426 // instantiation has no effect.
3427 if (!getLangOptions().CPlusPlus0x) {
Mike Stump11289f42009-09-09 15:08:12 +00003428 Diag(TemplateNameLoc,
Douglas Gregorf61eca92009-05-13 18:28:20 +00003429 diag::ext_explicit_instantiation_after_specialization)
3430 << Context.getTypeDeclType(PrevDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003431 Diag(PrevDecl->getLocation(),
Douglas Gregorf61eca92009-05-13 18:28:20 +00003432 diag::note_previous_template_specialization);
3433 }
3434
3435 // Create a new class template specialization declaration node
3436 // for this explicit specialization. This node is only used to
3437 // record the existence of this explicit instantiation for
3438 // accurate reproduction of the source code; we don't actually
3439 // use it for anything, since it is semantically irrelevant.
3440 Specialization
Mike Stump11289f42009-09-09 15:08:12 +00003441 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregorf61eca92009-05-13 18:28:20 +00003442 ClassTemplate->getDeclContext(),
3443 TemplateNameLoc,
3444 ClassTemplate,
Anders Carlsson5947ddf2009-06-23 01:26:57 +00003445 Converted, 0);
Douglas Gregorf61eca92009-05-13 18:28:20 +00003446 Specialization->setLexicalDeclContext(CurContext);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003447 CurContext->addDecl(Specialization);
Douglas Gregor4aa04b12009-09-11 21:19:12 +00003448 return DeclPtrTy::make(PrevDecl);
Douglas Gregorf61eca92009-05-13 18:28:20 +00003449 }
3450
3451 // If we have already (implicitly) instantiated this
3452 // specialization, there is less work to do.
3453 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation)
3454 SpecializationRequiresInstantiation = false;
3455
Douglas Gregor4aa04b12009-09-11 21:19:12 +00003456 if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation ||
3457 PrevDecl->getSpecializationKind() == TSK_Undeclared) {
3458 // Since the only prior class template specialization with these
3459 // arguments was referenced but not declared, reuse that
3460 // declaration node as our own, updating its source location to
3461 // reflect our new declaration.
3462 Specialization = PrevDecl;
3463 Specialization->setLocation(TemplateNameLoc);
3464 PrevDecl = 0;
3465 }
3466 }
3467
3468 if (!Specialization) {
Douglas Gregora1f49972009-05-13 00:25:59 +00003469 // Create a new class template specialization declaration node for
3470 // this explicit specialization.
3471 Specialization
Mike Stump11289f42009-09-09 15:08:12 +00003472 = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregora1f49972009-05-13 00:25:59 +00003473 ClassTemplate->getDeclContext(),
3474 TemplateNameLoc,
3475 ClassTemplate,
Douglas Gregor4aa04b12009-09-11 21:19:12 +00003476 Converted, PrevDecl);
Douglas Gregora1f49972009-05-13 00:25:59 +00003477
Douglas Gregor4aa04b12009-09-11 21:19:12 +00003478 if (PrevDecl) {
3479 // Remove the previous declaration from the folding set, since we want
3480 // to introduce a new declaration.
3481 ClassTemplate->getSpecializations().RemoveNode(PrevDecl);
3482 ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
3483 }
3484
3485 // Insert the new specialization.
3486 ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos);
Douglas Gregora1f49972009-05-13 00:25:59 +00003487 }
3488
3489 // Build the fully-sugared type for this explicit instantiation as
3490 // the user wrote in the explicit instantiation itself. This means
3491 // that we'll pretty-print the type retrieved from the
3492 // specialization's declaration the way that the user actually wrote
3493 // the explicit instantiation, rather than formatting the name based
3494 // on the "canonical" representation used to store the template
3495 // arguments in the specialization.
Mike Stump11289f42009-09-09 15:08:12 +00003496 QualType WrittenTy
3497 = Context.getTemplateSpecializationType(Name,
Anders Carlsson03c9e872009-06-05 02:45:24 +00003498 TemplateArgs.data(),
Douglas Gregora1f49972009-05-13 00:25:59 +00003499 TemplateArgs.size(),
3500 Context.getTypeDeclType(Specialization));
3501 Specialization->setTypeAsWritten(WrittenTy);
3502 TemplateArgsIn.release();
3503
3504 // Add the explicit instantiation into its lexical context. However,
3505 // since explicit instantiations are never found by name lookup, we
3506 // just put it into the declaration context directly.
3507 Specialization->setLexicalDeclContext(CurContext);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003508 CurContext->addDecl(Specialization);
Douglas Gregora1f49972009-05-13 00:25:59 +00003509
John McCall1806c272009-09-11 07:25:08 +00003510 Specialization->setPointOfInstantiation(TemplateNameLoc);
3511
Douglas Gregora1f49972009-05-13 00:25:59 +00003512 // C++ [temp.explicit]p3:
Douglas Gregora1f49972009-05-13 00:25:59 +00003513 // A definition of a class template or class member template
3514 // shall be in scope at the point of the explicit instantiation of
3515 // the class template or class member template.
3516 //
3517 // This check comes when we actually try to perform the
3518 // instantiation.
Douglas Gregor67da0d92009-05-15 17:59:04 +00003519 if (SpecializationRequiresInstantiation)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003520 InstantiateClassTemplateSpecialization(Specialization, TSK);
Douglas Gregor85673582009-05-18 17:01:57 +00003521 else // Instantiate the members of this class template specialization.
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003522 InstantiateClassTemplateSpecializationMembers(TemplateLoc, Specialization,
3523 TSK);
Douglas Gregora1f49972009-05-13 00:25:59 +00003524
3525 return DeclPtrTy::make(Specialization);
3526}
3527
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003528// Explicit instantiation of a member class of a class template.
3529Sema::DeclResult
Mike Stump11289f42009-09-09 15:08:12 +00003530Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor43e75172009-09-04 06:33:52 +00003531 SourceLocation ExternLoc,
3532 SourceLocation TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +00003533 unsigned TagSpec,
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003534 SourceLocation KWLoc,
3535 const CXXScopeSpec &SS,
3536 IdentifierInfo *Name,
3537 SourceLocation NameLoc,
3538 AttributeList *Attr) {
3539
Douglas Gregord6ab8742009-05-28 23:31:59 +00003540 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003541 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00003542 DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference,
Douglas Gregore93e46c2009-07-22 23:48:44 +00003543 KWLoc, SS, Name, NameLoc, Attr, AS_none,
John McCall7f41d982009-09-11 04:59:25 +00003544 MultiTemplateParamsArg(*this, 0, 0),
3545 Owned, IsDependent);
3546 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
3547
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003548 if (!TagD)
3549 return true;
3550
3551 TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>());
3552 if (Tag->isEnum()) {
3553 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
3554 << Context.getTypeDeclType(Tag);
3555 return true;
3556 }
3557
Douglas Gregorb8006faf2009-05-27 17:30:49 +00003558 if (Tag->isInvalidDecl())
3559 return true;
3560
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003561 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
3562 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
3563 if (!Pattern) {
3564 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
3565 << Context.getTypeDeclType(Record);
3566 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
3567 return true;
3568 }
3569
3570 // C++0x [temp.explicit]p2:
3571 // [...] An explicit instantiation shall appear in an enclosing
3572 // namespace of its template. [...]
3573 //
3574 // This is C++ DR 275.
3575 if (getLangOptions().CPlusPlus0x) {
Mike Stump87c57ac2009-05-16 07:39:55 +00003576 // FIXME: In C++98, we would like to turn these errors into warnings,
3577 // dependent on a -Wc++0x flag.
Mike Stump11289f42009-09-09 15:08:12 +00003578 DeclContext *PatternContext
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003579 = Pattern->getDeclContext()->getEnclosingNamespaceContext();
3580 if (!CurContext->Encloses(PatternContext)) {
3581 Diag(TemplateLoc, diag::err_explicit_instantiation_out_of_scope)
3582 << Record << cast<NamedDecl>(PatternContext) << SS.getRange();
3583 Diag(Pattern->getLocation(), diag::note_previous_declaration);
3584 }
3585 }
3586
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003587 TemplateSpecializationKind TSK
Mike Stump11289f42009-09-09 15:08:12 +00003588 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003589 : TSK_ExplicitInstantiationDeclaration;
Mike Stump11289f42009-09-09 15:08:12 +00003590
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003591 if (!Record->getDefinition(Context)) {
3592 // If the class has a definition, instantiate it (and all of its
3593 // members, recursively).
3594 Pattern = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
Mike Stump11289f42009-09-09 15:08:12 +00003595 if (Pattern && InstantiateClass(TemplateLoc, Record, Pattern,
Douglas Gregorb4850462009-05-14 23:26:13 +00003596 getTemplateInstantiationArgs(Record),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003597 TSK))
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003598 return true;
John McCall76d824f2009-08-25 22:02:44 +00003599 } else // Instantiate all of the members of the class.
Mike Stump11289f42009-09-09 15:08:12 +00003600 InstantiateClassMembers(TemplateLoc, Record,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00003601 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003602
Mike Stump87c57ac2009-05-16 07:39:55 +00003603 // FIXME: We don't have any representation for explicit instantiations of
3604 // member classes. Such a representation is not needed for compilation, but it
3605 // should be available for clients that want to see all of the declarations in
3606 // the source code.
Douglas Gregor2ec748c2009-05-14 00:28:11 +00003607 return TagD;
3608}
3609
Douglas Gregor450f00842009-09-25 18:43:00 +00003610Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
3611 SourceLocation ExternLoc,
3612 SourceLocation TemplateLoc,
3613 Declarator &D) {
3614 // Explicit instantiations always require a name.
3615 DeclarationName Name = GetNameForDeclarator(D);
3616 if (!Name) {
3617 if (!D.isInvalidType())
3618 Diag(D.getDeclSpec().getSourceRange().getBegin(),
3619 diag::err_explicit_instantiation_requires_name)
3620 << D.getDeclSpec().getSourceRange()
3621 << D.getSourceRange();
3622
3623 return true;
3624 }
3625
3626 // The scope passed in may not be a decl scope. Zip up the scope tree until
3627 // we find one that is.
3628 while ((S->getFlags() & Scope::DeclScope) == 0 ||
3629 (S->getFlags() & Scope::TemplateParamScope) != 0)
3630 S = S->getParent();
3631
3632 // Determine the type of the declaration.
3633 QualType R = GetTypeForDeclarator(D, S, 0);
3634 if (R.isNull())
3635 return true;
3636
3637 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
3638 // Cannot explicitly instantiate a typedef.
3639 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
3640 << Name;
3641 return true;
3642 }
3643
3644 // Determine what kind of explicit instantiation we have.
3645 TemplateSpecializationKind TSK
3646 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
3647 : TSK_ExplicitInstantiationDeclaration;
3648
John McCall9f3059a2009-10-09 21:13:30 +00003649 LookupResult Previous;
3650 LookupParsedName(Previous, S, &D.getCXXScopeSpec(),
3651 Name, LookupOrdinaryName);
Douglas Gregor450f00842009-09-25 18:43:00 +00003652
3653 if (!R->isFunctionType()) {
3654 // C++ [temp.explicit]p1:
3655 // A [...] static data member of a class template can be explicitly
3656 // instantiated from the member definition associated with its class
3657 // template.
3658 if (Previous.isAmbiguous()) {
3659 return DiagnoseAmbiguousLookup(Previous, Name, D.getIdentifierLoc(),
3660 D.getSourceRange());
3661 }
3662
John McCall9f3059a2009-10-09 21:13:30 +00003663 VarDecl *Prev = dyn_cast_or_null<VarDecl>(
3664 Previous.getAsSingleDecl(Context));
Douglas Gregor450f00842009-09-25 18:43:00 +00003665 if (!Prev || !Prev->isStaticDataMember()) {
3666 // We expect to see a data data member here.
3667 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
3668 << Name;
3669 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
3670 P != PEnd; ++P)
John McCall9f3059a2009-10-09 21:13:30 +00003671 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor450f00842009-09-25 18:43:00 +00003672 return true;
3673 }
3674
3675 if (!Prev->getInstantiatedFromStaticDataMember()) {
3676 // FIXME: Check for explicit specialization?
3677 Diag(D.getIdentifierLoc(),
3678 diag::err_explicit_instantiation_data_member_not_instantiated)
3679 << Prev;
3680 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
3681 // FIXME: Can we provide a note showing where this was declared?
3682 return true;
3683 }
3684
3685 // Instantiate static data member.
Douglas Gregor86d142a2009-10-08 07:24:58 +00003686 // FIXME: Check for prior specializations and such.
3687 Prev->setTemplateSpecializationKind(TSK);
Douglas Gregor450f00842009-09-25 18:43:00 +00003688 if (TSK == TSK_ExplicitInstantiationDefinition)
3689 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false);
3690
3691 // FIXME: Create an ExplicitInstantiation node?
3692 return DeclPtrTy();
3693 }
3694
Douglas Gregor0e876e02009-09-25 23:53:26 +00003695 // If the declarator is a template-id, translate the parser's template
3696 // argument list into our AST format.
Douglas Gregord90fd522009-09-25 21:45:23 +00003697 bool HasExplicitTemplateArgs = false;
3698 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
3699 if (D.getKind() == Declarator::DK_TemplateId) {
3700 TemplateIdAnnotation *TemplateId = D.getTemplateId();
3701 ASTTemplateArgsPtr TemplateArgsPtr(*this,
3702 TemplateId->getTemplateArgs(),
3703 TemplateId->getTemplateArgIsType(),
3704 TemplateId->NumArgs);
3705 translateTemplateArguments(TemplateArgsPtr,
3706 TemplateId->getTemplateArgLocations(),
3707 TemplateArgs);
3708 HasExplicitTemplateArgs = true;
Douglas Gregorf343fd82009-10-01 23:51:25 +00003709 TemplateArgsPtr.release();
Douglas Gregord90fd522009-09-25 21:45:23 +00003710 }
Douglas Gregor0e876e02009-09-25 23:53:26 +00003711
Douglas Gregor450f00842009-09-25 18:43:00 +00003712 // C++ [temp.explicit]p1:
3713 // A [...] function [...] can be explicitly instantiated from its template.
3714 // A member function [...] of a class template can be explicitly
3715 // instantiated from the member definition associated with its class
3716 // template.
Douglas Gregor450f00842009-09-25 18:43:00 +00003717 llvm::SmallVector<FunctionDecl *, 8> Matches;
3718 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
3719 P != PEnd; ++P) {
3720 NamedDecl *Prev = *P;
Douglas Gregord90fd522009-09-25 21:45:23 +00003721 if (!HasExplicitTemplateArgs) {
3722 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
3723 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
3724 Matches.clear();
3725 Matches.push_back(Method);
3726 break;
3727 }
Douglas Gregor450f00842009-09-25 18:43:00 +00003728 }
3729 }
3730
3731 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
3732 if (!FunTmpl)
3733 continue;
3734
3735 TemplateDeductionInfo Info(Context);
3736 FunctionDecl *Specialization = 0;
3737 if (TemplateDeductionResult TDK
Douglas Gregord90fd522009-09-25 21:45:23 +00003738 = DeduceTemplateArguments(FunTmpl, HasExplicitTemplateArgs,
3739 TemplateArgs.data(), TemplateArgs.size(),
Douglas Gregor450f00842009-09-25 18:43:00 +00003740 R, Specialization, Info)) {
3741 // FIXME: Keep track of almost-matches?
3742 (void)TDK;
3743 continue;
3744 }
3745
3746 Matches.push_back(Specialization);
3747 }
3748
3749 // Find the most specialized function template specialization.
3750 FunctionDecl *Specialization
3751 = getMostSpecialized(Matches.data(), Matches.size(), TPOC_Other,
3752 D.getIdentifierLoc(),
3753 PartialDiagnostic(diag::err_explicit_instantiation_not_known) << Name,
3754 PartialDiagnostic(diag::err_explicit_instantiation_ambiguous) << Name,
3755 PartialDiagnostic(diag::note_explicit_instantiation_candidate));
3756
3757 if (!Specialization)
3758 return true;
3759
3760 switch (Specialization->getTemplateSpecializationKind()) {
3761 case TSK_Undeclared:
3762 Diag(D.getIdentifierLoc(),
3763 diag::err_explicit_instantiation_member_function_not_instantiated)
3764 << Specialization
3765 << (Specialization->getTemplateSpecializationKind() ==
3766 TSK_ExplicitSpecialization);
3767 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
3768 return true;
3769
3770 case TSK_ExplicitSpecialization:
3771 // C++ [temp.explicit]p4:
3772 // For a given set of template parameters, if an explicit instantiation
3773 // of a template appears after a declaration of an explicit
3774 // specialization for that template, the explicit instantiation has no
3775 // effect.
3776 break;
3777
3778 case TSK_ExplicitInstantiationDefinition:
3779 // FIXME: Check that we aren't trying to perform an explicit instantiation
3780 // declaration now.
3781 // Fall through
3782
3783 case TSK_ImplicitInstantiation:
3784 case TSK_ExplicitInstantiationDeclaration:
3785 // Instantiate the function, if this is an explicit instantiation
3786 // definition.
3787 if (TSK == TSK_ExplicitInstantiationDefinition)
3788 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization,
3789 false);
3790
Douglas Gregor450f00842009-09-25 18:43:00 +00003791 Specialization->setTemplateSpecializationKind(TSK);
3792 break;
3793 }
3794
3795 // FIXME: Create some kind of ExplicitInstantiationDecl here.
3796 return DeclPtrTy();
3797}
3798
Douglas Gregor333489b2009-03-27 23:10:48 +00003799Sema::TypeResult
John McCall7f41d982009-09-11 04:59:25 +00003800Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
3801 const CXXScopeSpec &SS, IdentifierInfo *Name,
3802 SourceLocation TagLoc, SourceLocation NameLoc) {
3803 // This has to hold, because SS is expected to be defined.
3804 assert(Name && "Expected a name in a dependent tag");
3805
3806 NestedNameSpecifier *NNS
3807 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3808 if (!NNS)
3809 return true;
3810
3811 QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc));
3812 if (T.isNull())
3813 return true;
3814
3815 TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec);
3816 QualType ElabType = Context.getElaboratedType(T, TagKind);
3817
3818 return ElabType.getAsOpaquePtr();
3819}
3820
3821Sema::TypeResult
Douglas Gregor333489b2009-03-27 23:10:48 +00003822Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3823 const IdentifierInfo &II, SourceLocation IdLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00003824 NestedNameSpecifier *NNS
Douglas Gregor333489b2009-03-27 23:10:48 +00003825 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3826 if (!NNS)
3827 return true;
3828
3829 QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc));
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00003830 if (T.isNull())
3831 return true;
Douglas Gregor333489b2009-03-27 23:10:48 +00003832 return T.getAsOpaquePtr();
3833}
3834
Douglas Gregordce2b622009-04-01 00:28:59 +00003835Sema::TypeResult
3836Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS,
3837 SourceLocation TemplateLoc, TypeTy *Ty) {
Argyrios Kyrtzidisc7148c92009-08-19 01:28:28 +00003838 QualType T = GetTypeFromParser(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00003839 NestedNameSpecifier *NNS
Douglas Gregordce2b622009-04-01 00:28:59 +00003840 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Mike Stump11289f42009-09-09 15:08:12 +00003841 const TemplateSpecializationType *TemplateId
John McCall9dd450b2009-09-21 23:43:11 +00003842 = T->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00003843 assert(TemplateId && "Expected a template specialization type");
3844
Douglas Gregor12bbfe12009-09-02 13:05:45 +00003845 if (computeDeclContext(SS, false)) {
3846 // If we can compute a declaration context, then the "typename"
3847 // keyword was superfluous. Just build a QualifiedNameType to keep
3848 // track of the nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +00003849
Douglas Gregor12bbfe12009-09-02 13:05:45 +00003850 // FIXME: Note that the QualifiedNameType had the "typename" keyword!
3851 return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr();
3852 }
Mike Stump11289f42009-09-09 15:08:12 +00003853
Douglas Gregor12bbfe12009-09-02 13:05:45 +00003854 return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr();
Douglas Gregordce2b622009-04-01 00:28:59 +00003855}
3856
Douglas Gregor333489b2009-03-27 23:10:48 +00003857/// \brief Build the type that describes a C++ typename specifier,
3858/// e.g., "typename T::type".
3859QualType
3860Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II,
3861 SourceRange Range) {
Douglas Gregorc9f9b862009-05-11 19:58:34 +00003862 CXXRecordDecl *CurrentInstantiation = 0;
3863 if (NNS->isDependent()) {
3864 CurrentInstantiation = getCurrentInstantiationOf(NNS);
Douglas Gregor333489b2009-03-27 23:10:48 +00003865
Douglas Gregorc9f9b862009-05-11 19:58:34 +00003866 // If the nested-name-specifier does not refer to the current
3867 // instantiation, then build a typename type.
3868 if (!CurrentInstantiation)
3869 return Context.getTypenameType(NNS, &II);
Mike Stump11289f42009-09-09 15:08:12 +00003870
Douglas Gregorc707da62009-09-02 13:12:51 +00003871 // The nested-name-specifier refers to the current instantiation, so the
3872 // "typename" keyword itself is superfluous. In C++03, the program is
Mike Stump11289f42009-09-09 15:08:12 +00003873 // actually ill-formed. However, DR 382 (in C++0x CD1) allows such
Douglas Gregorc707da62009-09-02 13:12:51 +00003874 // extraneous "typename" keywords, and we retroactively apply this DR to
3875 // C++03 code.
Douglas Gregorc9f9b862009-05-11 19:58:34 +00003876 }
Douglas Gregor333489b2009-03-27 23:10:48 +00003877
Douglas Gregorc9f9b862009-05-11 19:58:34 +00003878 DeclContext *Ctx = 0;
3879
3880 if (CurrentInstantiation)
3881 Ctx = CurrentInstantiation;
3882 else {
3883 CXXScopeSpec SS;
3884 SS.setScopeRep(NNS);
3885 SS.setRange(Range);
3886 if (RequireCompleteDeclContext(SS))
3887 return QualType();
3888
3889 Ctx = computeDeclContext(SS);
3890 }
Douglas Gregor333489b2009-03-27 23:10:48 +00003891 assert(Ctx && "No declaration context?");
3892
3893 DeclarationName Name(&II);
John McCall9f3059a2009-10-09 21:13:30 +00003894 LookupResult Result;
3895 LookupQualifiedName(Result, Ctx, Name, LookupOrdinaryName, false);
Douglas Gregor333489b2009-03-27 23:10:48 +00003896 unsigned DiagID = 0;
3897 Decl *Referenced = 0;
3898 switch (Result.getKind()) {
3899 case LookupResult::NotFound:
3900 if (Ctx->isTranslationUnit())
3901 DiagID = diag::err_typename_nested_not_found_global;
3902 else
3903 DiagID = diag::err_typename_nested_not_found;
3904 break;
3905
3906 case LookupResult::Found:
John McCall9f3059a2009-10-09 21:13:30 +00003907 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Douglas Gregor333489b2009-03-27 23:10:48 +00003908 // We found a type. Build a QualifiedNameType, since the
3909 // typename-specifier was just sugar. FIXME: Tell
3910 // QualifiedNameType that it has a "typename" prefix.
3911 return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type));
3912 }
3913
3914 DiagID = diag::err_typename_nested_not_type;
John McCall9f3059a2009-10-09 21:13:30 +00003915 Referenced = Result.getFoundDecl();
Douglas Gregor333489b2009-03-27 23:10:48 +00003916 break;
3917
3918 case LookupResult::FoundOverloaded:
3919 DiagID = diag::err_typename_nested_not_type;
3920 Referenced = *Result.begin();
3921 break;
3922
John McCall6538c932009-10-10 05:48:19 +00003923 case LookupResult::Ambiguous:
Douglas Gregor333489b2009-03-27 23:10:48 +00003924 DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range);
3925 return QualType();
3926 }
3927
3928 // If we get here, it's because name lookup did not find a
3929 // type. Emit an appropriate diagnostic and return an error.
3930 if (NamedDecl *NamedCtx = dyn_cast<NamedDecl>(Ctx))
3931 Diag(Range.getEnd(), DiagID) << Range << Name << NamedCtx;
3932 else
3933 Diag(Range.getEnd(), DiagID) << Range << Name;
3934 if (Referenced)
3935 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
3936 << Name;
3937 return QualType();
3938}
Douglas Gregor15acfb92009-08-06 16:20:37 +00003939
3940namespace {
3941 // See Sema::RebuildTypeInCurrentInstantiation
Mike Stump11289f42009-09-09 15:08:12 +00003942 class VISIBILITY_HIDDEN CurrentInstantiationRebuilder
3943 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor15acfb92009-08-06 16:20:37 +00003944 SourceLocation Loc;
3945 DeclarationName Entity;
Mike Stump11289f42009-09-09 15:08:12 +00003946
Douglas Gregor15acfb92009-08-06 16:20:37 +00003947 public:
Mike Stump11289f42009-09-09 15:08:12 +00003948 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor15acfb92009-08-06 16:20:37 +00003949 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +00003950 DeclarationName Entity)
3951 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor15acfb92009-08-06 16:20:37 +00003952 Loc(Loc), Entity(Entity) { }
Mike Stump11289f42009-09-09 15:08:12 +00003953
3954 /// \brief Determine whether the given type \p T has already been
Douglas Gregor15acfb92009-08-06 16:20:37 +00003955 /// transformed.
3956 ///
3957 /// For the purposes of type reconstruction, a type has already been
3958 /// transformed if it is NULL or if it is not dependent.
3959 bool AlreadyTransformed(QualType T) {
3960 return T.isNull() || !T->isDependentType();
3961 }
Mike Stump11289f42009-09-09 15:08:12 +00003962
3963 /// \brief Returns the location of the entity whose type is being
Douglas Gregor15acfb92009-08-06 16:20:37 +00003964 /// rebuilt.
3965 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +00003966
Douglas Gregor15acfb92009-08-06 16:20:37 +00003967 /// \brief Returns the name of the entity whose type is being rebuilt.
3968 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +00003969
Douglas Gregor15acfb92009-08-06 16:20:37 +00003970 /// \brief Transforms an expression by returning the expression itself
3971 /// (an identity function).
3972 ///
3973 /// FIXME: This is completely unsafe; we will need to actually clone the
3974 /// expressions.
3975 Sema::OwningExprResult TransformExpr(Expr *E) {
3976 return getSema().Owned(E);
3977 }
Mike Stump11289f42009-09-09 15:08:12 +00003978
Douglas Gregor15acfb92009-08-06 16:20:37 +00003979 /// \brief Transforms a typename type by determining whether the type now
3980 /// refers to a member of the current instantiation, and then
3981 /// type-checking and building a QualifiedNameType (when possible).
3982 QualType TransformTypenameType(const TypenameType *T);
3983 };
3984}
3985
Mike Stump11289f42009-09-09 15:08:12 +00003986QualType
Douglas Gregor15acfb92009-08-06 16:20:37 +00003987CurrentInstantiationRebuilder::TransformTypenameType(const TypenameType *T) {
3988 NestedNameSpecifier *NNS
3989 = TransformNestedNameSpecifier(T->getQualifier(),
3990 /*FIXME:*/SourceRange(getBaseLocation()));
3991 if (!NNS)
3992 return QualType();
3993
3994 // If the nested-name-specifier did not change, and we cannot compute the
3995 // context corresponding to the nested-name-specifier, then this
3996 // typename type will not change; exit early.
3997 CXXScopeSpec SS;
3998 SS.setRange(SourceRange(getBaseLocation()));
3999 SS.setScopeRep(NNS);
4000 if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0)
4001 return QualType(T, 0);
Mike Stump11289f42009-09-09 15:08:12 +00004002
4003 // Rebuild the typename type, which will probably turn into a
Douglas Gregor15acfb92009-08-06 16:20:37 +00004004 // QualifiedNameType.
4005 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
Mike Stump11289f42009-09-09 15:08:12 +00004006 QualType NewTemplateId
Douglas Gregor15acfb92009-08-06 16:20:37 +00004007 = TransformType(QualType(TemplateId, 0));
4008 if (NewTemplateId.isNull())
4009 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004010
Douglas Gregor15acfb92009-08-06 16:20:37 +00004011 if (NNS == T->getQualifier() &&
4012 NewTemplateId == QualType(TemplateId, 0))
4013 return QualType(T, 0);
Mike Stump11289f42009-09-09 15:08:12 +00004014
Douglas Gregor15acfb92009-08-06 16:20:37 +00004015 return getDerived().RebuildTypenameType(NNS, NewTemplateId);
4016 }
Mike Stump11289f42009-09-09 15:08:12 +00004017
Douglas Gregor15acfb92009-08-06 16:20:37 +00004018 return getDerived().RebuildTypenameType(NNS, T->getIdentifier());
4019}
4020
4021/// \brief Rebuilds a type within the context of the current instantiation.
4022///
Mike Stump11289f42009-09-09 15:08:12 +00004023/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor15acfb92009-08-06 16:20:37 +00004024/// a class template (or class template partial specialization) that was parsed
Mike Stump11289f42009-09-09 15:08:12 +00004025/// and constructed before we entered the scope of the class template (or
Douglas Gregor15acfb92009-08-06 16:20:37 +00004026/// partial specialization thereof). This routine will rebuild that type now
4027/// that we have entered the declarator's scope, which may produce different
4028/// canonical types, e.g.,
4029///
4030/// \code
4031/// template<typename T>
4032/// struct X {
4033/// typedef T* pointer;
4034/// pointer data();
4035/// };
4036///
4037/// template<typename T>
4038/// typename X<T>::pointer X<T>::data() { ... }
4039/// \endcode
4040///
4041/// Here, the type "typename X<T>::pointer" will be created as a TypenameType,
4042/// since we do not know that we can look into X<T> when we parsed the type.
4043/// This function will rebuild the type, performing the lookup of "pointer"
4044/// in X<T> and returning a QualifiedNameType whose canonical type is the same
4045/// as the canonical type of T*, allowing the return types of the out-of-line
4046/// definition and the declaration to match.
4047QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc,
4048 DeclarationName Name) {
4049 if (T.isNull() || !T->isDependentType())
4050 return T;
Mike Stump11289f42009-09-09 15:08:12 +00004051
Douglas Gregor15acfb92009-08-06 16:20:37 +00004052 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
4053 return Rebuilder.TransformType(T);
Benjamin Kramer854d7de2009-08-11 22:33:06 +00004054}
Douglas Gregorbe999392009-09-15 16:23:51 +00004055
4056/// \brief Produces a formatted string that describes the binding of
4057/// template parameters to template arguments.
4058std::string
4059Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
4060 const TemplateArgumentList &Args) {
4061 std::string Result;
4062
4063 if (!Params || Params->size() == 0)
4064 return Result;
4065
4066 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
4067 if (I == 0)
4068 Result += "[with ";
4069 else
4070 Result += ", ";
4071
4072 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
4073 Result += Id->getName();
4074 } else {
4075 Result += '$';
4076 Result += llvm::utostr(I);
4077 }
4078
4079 Result += " = ";
4080
4081 switch (Args[I].getKind()) {
4082 case TemplateArgument::Null:
4083 Result += "<no value>";
4084 break;
4085
4086 case TemplateArgument::Type: {
4087 std::string TypeStr;
4088 Args[I].getAsType().getAsStringInternal(TypeStr,
4089 Context.PrintingPolicy);
4090 Result += TypeStr;
4091 break;
4092 }
4093
4094 case TemplateArgument::Declaration: {
4095 bool Unnamed = true;
4096 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) {
4097 if (ND->getDeclName()) {
4098 Unnamed = false;
4099 Result += ND->getNameAsString();
4100 }
4101 }
4102
4103 if (Unnamed) {
4104 Result += "<anonymous>";
4105 }
4106 break;
4107 }
4108
4109 case TemplateArgument::Integral: {
4110 Result += Args[I].getAsIntegral()->toString(10);
4111 break;
4112 }
4113
4114 case TemplateArgument::Expression: {
4115 assert(false && "No expressions in deduced template arguments!");
4116 Result += "<expression>";
4117 break;
4118 }
4119
4120 case TemplateArgument::Pack:
4121 // FIXME: Format template argument packs
4122 Result += "<template argument pack>";
4123 break;
4124 }
4125 }
4126
4127 Result += ']';
4128 return Result;
4129}