aboutsummaryrefslogtreecommitdiff
path: root/lib/Parse/ParseExprCXX.cpp
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2019-05-09 03:31:27 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2019-05-09 03:31:27 +0000
commitfb98e7c13da639f34f34830a4e675367e0816cb6 (patch)
tree70e025e9b4a31559d1f061e72439b5414537ad3b /lib/Parse/ParseExprCXX.cpp
parent70cd574857c6023b8c60e325c4029a23f716c2dd (diff)
[c++20] Implement P0846R0: allow (ADL-only) calls to template-ids whose
template name is not visible to unqualified lookup. In order to support this without a severe degradation in our ability to diagnose typos in template names, this change significantly restructures the way we handle template-id-shaped syntax for which lookup of the template name finds nothing. Instead of eagerly diagnosing an undeclared template name, we now form a placeholder template-name representing a name that is known to not find any templates. When the parser sees such a name, it attempts to disambiguate whether we have a less-than comparison or a template-id. Any diagnostics or typo-correction for the name are delayed until its point of use. The upshot should be a small improvement of our diagostic quality overall: we now take more syntactic context into account when trying to resolve an undeclared identifier on the left hand side of a '<'. In fact, this works well enough that the backwards-compatible portion (for an undeclared identifier rather than a lookup that finds functions but no function templates) is enabled in all language modes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@360308 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse/ParseExprCXX.cpp')
-rw-r--r--lib/Parse/ParseExprCXX.cpp26
1 files changed, 19 insertions, 7 deletions
diff --git a/lib/Parse/ParseExprCXX.cpp b/lib/Parse/ParseExprCXX.cpp
index fbafb43688..c6f457fb14 100644
--- a/lib/Parse/ParseExprCXX.cpp
+++ b/lib/Parse/ParseExprCXX.cpp
@@ -487,6 +487,14 @@ bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
EnteringContext,
Template,
MemberOfUnknownSpecialization)) {
+ // If lookup didn't find anything, we treat the name as a template-name
+ // anyway. C++20 requires this, and in prior language modes it improves
+ // error recovery. But before we commit to this, check that we actually
+ // have something that looks like a template-argument-list next.
+ if (!IsTypename && TNK == TNK_Undeclared_template &&
+ isTemplateArgumentList(1) == TPResult::False)
+ break;
+
// We have found a template name, so annotate this token
// with a template-id annotation. We do not permit the
// template-id to be translated into a type annotation,
@@ -501,7 +509,7 @@ bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
}
if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
- (IsTypename || IsTemplateArgumentList(1))) {
+ (IsTypename || isTemplateArgumentList(1) == TPResult::True)) {
// We have something like t::getAs<T>, where getAs is a
// member of an unknown specialization. However, this will only
// parse correctly as a template, so suggest the keyword 'template'
@@ -2138,9 +2146,15 @@ bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
TemplateKWLoc.isValid(), Id,
ObjectType, EnteringContext, Template,
MemberOfUnknownSpecialization);
+ // If lookup found nothing but we're assuming that this is a template
+ // name, double-check that makes sense syntactically before committing
+ // to it.
+ if (TNK == TNK_Undeclared_template &&
+ isTemplateArgumentList(0) == TPResult::False)
+ return false;
if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
- ObjectType && IsTemplateArgumentList()) {
+ ObjectType && isTemplateArgumentList(0) == TPResult::True) {
// We have something like t->getAs<T>(), where getAs is a
// member of an unknown specialization. However, this will only
// parse correctly as a template, so suggest the keyword 'template'
@@ -2244,11 +2258,9 @@ bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
// Constructor and destructor names.
- TypeResult Type
- = Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
- Template, Name, NameLoc,
- LAngleLoc, TemplateArgsPtr, RAngleLoc,
- /*IsCtorOrDtorName=*/true);
+ TypeResult Type = Actions.ActOnTemplateIdType(
+ getCurScope(), SS, TemplateKWLoc, Template, Name, NameLoc, LAngleLoc,
+ TemplateArgsPtr, RAngleLoc, /*IsCtorOrDtorName=*/true);
if (Type.isInvalid())
return true;