Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,9 @@ void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec &DS,
}

SourceLocation Parser::ParsePackIndexingType(DeclSpec &DS) {
assert(Tok.isOneOf(tok::annot_pack_indexing_type, tok::identifier) &&
"Expected an identifier");
assert(Tok.isOneOf(tok::annot_pack_indexing_type, tok::identifier,
tok::annot_template_id) &&
"Expected an identifier or template-id");

TypeResult Type;
SourceLocation StartLoc;
Expand Down Expand Up @@ -1180,15 +1181,41 @@ SourceLocation Parser::ParsePackIndexingType(DeclSpec &DS) {
return Tok.getEndLoc();
}

ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
Tok.getLocation(), getCurScope());
if (!Ty) {
DS.SetTypeSpecError();
return Tok.getEndLoc();
}
Type = Ty;
if (Tok.is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
StartLoc = TemplateId->TemplateNameLoc;

StartLoc = ConsumeToken();
ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
TemplateId->NumArgs);
CXXScopeSpec SS;
TypeResult Ty =
TemplateId->isInvalid()
? TypeError()
: Actions.ActOnTemplateIdType(
getCurScope(), ElaboratedTypeKeyword::None,
/*ElaboratedKeywordLoc=*/SourceLocation(), SS,
TemplateId->TemplateKWLoc, TemplateId->Template,
TemplateId->Name, TemplateId->TemplateNameLoc,
TemplateId->LAngleLoc, TemplateArgsPtr, TemplateId->RAngleLoc,
/*IsCtorOrDtorName=*/false, /*IsClassName=*/false,
ImplicitTypenameContext::No);
Comment on lines +1195 to +1201
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs rewiring for nested name and elaborated keyword support, with additional tests covering those cases.

AST dump tests and ast-print roundtrip tests would make good confirmation this works.


if (Ty.isInvalid()) {
DS.SetTypeSpecError();
return TemplateId->RAngleLoc;
}
Type = Ty;
ConsumeAnnotationToken();
} else {
ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
Tok.getLocation(), getCurScope());
if (!Ty) {
DS.SetTypeSpecError();
return Tok.getEndLoc();
}
Type = Ty;
StartLoc = ConsumeToken();
}
EllipsisLoc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
Expand Down
31 changes: 31 additions & 0 deletions clang/lib/Parse/ParseExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,37 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
continue;
}

// Handle template-id pack-index-specifier
if (Tok.is(tok::annot_template_id) && NextToken().is(tok::ellipsis) &&
GetLookAheadToken(2).is(tok::l_square) &&
!GetLookAheadToken(3).is(tok::r_square)) {
SourceLocation Start = Tok.getLocation();
DeclSpec DS(AttrFactory);
SourceLocation CCLoc;
SourceLocation EndLoc = ParsePackIndexingType(DS);
if (DS.getTypeSpecType() == DeclSpec::TST_error)
return false;

QualType Pattern = Sema::GetTypeFromParser(DS.getRepAsType());
QualType Type =
Actions.ActOnPackIndexingType(Pattern, DS.getPackIndexingExpr(),
DS.getBeginLoc(), DS.getEllipsisLoc());

if (Type.isNull())
return false;

if (!TryConsumeToken(tok::coloncolon, CCLoc)) {
AnnotateExistingIndexedTypeNamePack(ParsedType::make(Type), Start,
EndLoc);
return false;
}
if (Actions.ActOnCXXNestedNameSpecifierIndexedPack(SS, DS, CCLoc,
std::move(Type)))
SS.SetInvalid(SourceRange(Start, CCLoc));
HasScopeSpecifier = true;
continue;
}

if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
// We have
//
Expand Down
27 changes: 27 additions & 0 deletions clang/test/Parser/cxx2c-pack-indexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,30 @@ void foo() {
}

}

namespace TemplateIdPackIndexing {

template <typename T>
struct Inner {
using type = T;
static constexpr int value = 42;
};

template <template<typename> typename... TT>
struct Test {
using type = TT<int>...[0]::type;
static constexpr auto value = TT<int>...[0]::value;
};

static_assert(Test<Inner>::value == 42);

template <template<typename> typename... TT>
struct Test2 {
template <typename U>
using type = typename TT<U>...[0]::template rebind<U>;
};

template <template<typename> typename... TT, typename U>
auto test_unannotated() -> typename TT<U>...[0]::type;

}