template specialization (e.g., std::vector<int> would now be
well-formed, since it relies on a default argument for the Allocator
template parameter).
This is much less interesting than one might expect, since (1) we're
not actually using the default arguments for anything important, such
as naming an actual Decl, and (2) we'll often need to instantiate the
default arguments to check their well-formedness. The real fun will
come later.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64310
91177308-0d34-0410-b5e6-
96231b3b80d8
unsigned size() const { return NumParams; }
+ /// \btief Returns the minimum number of arguments needed to form a
+ /// template specialization. This may be fewer than the number of
+ /// template parameters, if some of the parameters have default
+ /// arguments.
+ unsigned getMinRequiredArguments() const;
+
SourceLocation getTemplateLoc() const { return TemplateLoc; }
SourceLocation getLAngleLoc() const { return LAngleLoc; }
SourceLocation getRAngleLoc() const { return RAngleLoc; }
+
+ SourceRange getSourceRange() const {
+ return SourceRange(TemplateLoc, RAngleLoc);
+ }
};
//===----------------------------------------------------------------------===//
// C++ Template Argument Lists
DIAG(err_template_arg_list_different_arity, ERROR,
"%select{too few|too many}0 template arguments for %select{class template|function template|template template parameter|template}1 %2")
+DIAG(note_template_decl_here, NOTE,
+ "template is declared here")
DIAG(err_template_arg_must_be_type, ERROR,
"template argument for template type parameter must be a type")
DIAG(err_template_arg_must_be_expr, ERROR,
NumParams, RAngleLoc);
}
+unsigned TemplateParameterList::getMinRequiredArguments() const {
+ unsigned NumRequiredArgs = size();
+ iterator Param = const_cast<TemplateParameterList *>(this)->end(),
+ ParamBegin = const_cast<TemplateParameterList *>(this)->begin();
+ while (Param != ParamBegin) {
+ --Param;
+ if (!(isa<TemplateTypeParmDecl>(*Param) &&
+ cast<TemplateTypeParmDecl>(*Param)->hasDefaultArgument()) &&
+ !(isa<NonTypeTemplateParmDecl>(*Param) &&
+ cast<NonTypeTemplateParmDecl>(*Param)->hasDefaultArgument()) &&
+ !(isa<TemplateTemplateParmDecl>(*Param) &&
+ cast<TemplateTemplateParmDecl>(*Param)->hasDefaultArgument()))
+ break;
+
+ --NumRequiredArgs;
+ }
+
+ return NumRequiredArgs;
+}
+
//===----------------------------------------------------------------------===//
// TemplateDecl Implementation
//===----------------------------------------------------------------------===//
bool Invalid = false;
if (NumArgs > NumParams ||
- NumArgs < NumParams /*FIXME: default arguments! */) {
+ NumArgs < Params->getMinRequiredArguments()) {
// FIXME: point at either the first arg beyond what we can handle,
// or the '>', depending on whether we have too many or too few
// arguments.
isa<FunctionTemplateDecl>(Template)? 1 :
isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
<< Template << Range;
-
+ Diag(Template->getLocation(), diag::note_template_decl_here)
+ << Params->getSourceRange();
Invalid = true;
}
--- /dev/null
+// RUN: clang -fsyntax-only -verify %s
+
+template<typename T, int N = 2> struct X; // expected-note{{template is declared here}}
+
+X<int, 1> *x1;
+X<int> *x2;
+
+X<> *x3; // expected-error{{too few template arguments for class template 'X'}} \
+ // FIXME: expected-error{{expected unqualified-id}}
+
+template<typename U = float, int M> struct X;
+
+X<> *x4;
template<typename T,
int I,
template<typename> class TT>
- class A;
+ class A; // expected-note 2 {{template is declared here}}
template<typename> class X;