#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/ExprCXX.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/RAIIObjectsForParser.h"
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaDiagnostic.h"
#include "llvm/Support/TimeProfiler.h"
using namespace clang;
unsigned Parser::ReenterTemplateScopes(MultiParseScope &S, Decl *D) {
return Actions.ActOnReenterTemplateScope(D, [&] {
S.Enter(Scope::TemplateParamScope);
return Actions.getCurScope();
});
}
Decl *Parser::ParseDeclarationStartingWithTemplate(
DeclaratorContext Context, SourceLocation &DeclEnd,
ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
ObjCDeclContextSwitch ObjCDC(*this);
if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(),
DeclEnd, AccessAttrs, AS);
}
return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs,
AS);
}
Decl *Parser::ParseTemplateDeclarationOrSpecialization(
DeclaratorContext Context, SourceLocation &DeclEnd,
ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
assert(Tok.isOneOf(tok::kw_export, tok::kw_template) &&
"Token does not start a template declaration.");
MultiParseScope TemplateParamScopes(*this);
ParsingDeclRAIIObject
ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
bool isSpecialization = true;
bool LastParamListWasEmpty = false;
TemplateParameterLists ParamLists;
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
do {
SourceLocation ExportLoc;
TryConsumeToken(tok::kw_export, ExportLoc);
SourceLocation TemplateLoc;
if (!TryConsumeToken(tok::kw_template, TemplateLoc)) {
Diag(Tok.getLocation(), diag::err_expected_template);
return nullptr;
}
SourceLocation LAngleLoc, RAngleLoc;
SmallVector<NamedDecl*, 4> TemplateParams;
if (ParseTemplateParameters(TemplateParamScopes,
CurTemplateDepthTracker.getDepth(),
TemplateParams, LAngleLoc, RAngleLoc)) {
SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
TryConsumeToken(tok::semi);
return nullptr;
}
ExprResult OptionalRequiresClauseConstraintER;
if (!TemplateParams.empty()) {
isSpecialization = false;
++CurTemplateDepthTracker;
if (TryConsumeToken(tok::kw_requires)) {
OptionalRequiresClauseConstraintER =
Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression(
false));
if (!OptionalRequiresClauseConstraintER.isUsable()) {
SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
TryConsumeToken(tok::semi);
return nullptr;
}
}
} else {
LastParamListWasEmpty = true;
}
ParamLists.push_back(Actions.ActOnTemplateParameterList(
CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc,
TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get()));
} while (Tok.isOneOf(tok::kw_export, tok::kw_template));
if (Tok.is(tok::kw_concept))
return ParseConceptDefinition(
ParsedTemplateInfo(&ParamLists, isSpecialization,
LastParamListWasEmpty),
DeclEnd);
return ParseSingleDeclarationAfterTemplate(
Context,
ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty),
ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
}
Decl *Parser::ParseSingleDeclarationAfterTemplate(
DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd,
ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
"Template information required");
if (Tok.is(tok::kw_static_assert)) {
Diag(Tok.getLocation(), diag::err_templated_invalid_declaration)
<< TemplateInfo.getSourceRange();
return ParseStaticAssertDeclaration(DeclEnd);
}
if (Context == DeclaratorContext::Member) {
DeclGroupPtrTy D = ParseCXXClassMemberDeclaration(
AS, AccessAttrs, TemplateInfo, &DiagsFromTParams);
if (!D || !D.get().isSingleDecl())
return nullptr;
return D.get().getSingleDecl();
}
ParsedAttributes prefixAttrs(AttrFactory);
MaybeParseCXX11Attributes(prefixAttrs);
if (Tok.is(tok::kw_using)) {
auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
prefixAttrs);
if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl())
return nullptr;
return usingDeclPtr.get().getSingleDecl();
}
ParsingDeclSpec DS(*this, &DiagsFromTParams);
ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
getDeclSpecContextFromDeclaratorContext(Context));
if (Tok.is(tok::semi)) {
ProhibitAttributes(prefixAttrs);
DeclEnd = ConsumeToken();
RecordDecl *AnonRecord = nullptr;
Decl *Decl = Actions.ParsedFreeStandingDeclSpec(
getCurScope(), AS, DS, ParsedAttributesView::none(),
TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams
: MultiTemplateParamsArg(),
TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation,
AnonRecord);
assert(!AnonRecord &&
"Anonymous unions/structs should not be valid with template");
DS.complete(Decl);
return Decl;
}
if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
ProhibitAttributes(prefixAttrs);
ParsingDeclarator DeclaratorInfo(*this, DS, prefixAttrs,
(DeclaratorContext)Context);
if (TemplateInfo.TemplateParams)
DeclaratorInfo.setTemplateParameterLists(*TemplateInfo.TemplateParams);
bool IsTemplateSpecOrInst =
(TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
ParseDeclarator(DeclaratorInfo);
if (IsTemplateSpecOrInst)
SAC.done();
if (!DeclaratorInfo.hasName()) {
SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
if (Tok.is(tok::semi))
ConsumeToken();
return nullptr;
}
llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() {
return std::string(DeclaratorInfo.getIdentifier() != nullptr
? DeclaratorInfo.getIdentifier()->getName()
: "<unknown>");
});
LateParsedAttrList LateParsedAttrs(true);
if (DeclaratorInfo.isFunctionDeclarator()) {
if (Tok.is(tok::kw_requires))
ParseTrailingRequiresClause(DeclaratorInfo);
MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
}
if (DeclaratorInfo.isFunctionDeclarator() &&
isStartOfFunctionDefinition(DeclaratorInfo)) {
if (Context != DeclaratorContext::File) {
Diag(Tok, diag::err_function_definition_not_allowed);
SkipMalformedDecl();
return nullptr;
}
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef)
<< FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
DS.ClearStorageClassSpecs();
}
if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
if (DeclaratorInfo.getName().getKind() !=
UnqualifiedIdKind::IK_TemplateId) {
Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0;
return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(),
&LateParsedAttrs);
} else {
SourceLocation LAngleLoc
= PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Diag(DeclaratorInfo.getIdentifierLoc(),
diag::err_explicit_instantiation_with_definition)
<< SourceRange(TemplateInfo.TemplateLoc)
<< FixItHint::CreateInsertion(LAngleLoc, "<>");
TemplateParameterLists FakedParamLists;
FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
LAngleLoc, nullptr));
return ParseFunctionDefinition(
DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists,
true,
true),
&LateParsedAttrs);
}
}
return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo,
&LateParsedAttrs);
}
Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
TemplateInfo);
if (Tok.is(tok::comma)) {
Diag(Tok, diag::err_multiple_template_declarators)
<< (int)TemplateInfo.Kind;
SkipUntil(tok::semi);
return ThisDecl;
}
ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
if (LateParsedAttrs.size() > 0)
ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false);
DeclaratorInfo.complete(ThisDecl);
return ThisDecl;
}
Decl *
Parser::ParseConceptDefinition(const ParsedTemplateInfo &TemplateInfo,
SourceLocation &DeclEnd) {
assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
"Template information required");
assert(Tok.is(tok::kw_concept) &&
"ParseConceptDefinition must be called when at a 'concept' keyword");
ConsumeToken();
SourceLocation BoolKWLoc;
if (TryConsumeToken(tok::kw_bool, BoolKWLoc))
Diag(Tok.getLocation(), diag::ext_concept_legacy_bool_keyword) <<
FixItHint::CreateRemoval(SourceLocation(BoolKWLoc));
DiagnoseAndSkipCXX11Attributes();
CXXScopeSpec SS;
if (ParseOptionalCXXScopeSpecifier(
SS, nullptr,
false, false,
nullptr,
false, nullptr, true) ||
SS.isInvalid()) {
SkipUntil(tok::semi);
return nullptr;
}
if (SS.isNotEmpty())
Diag(SS.getBeginLoc(),
diag::err_concept_definition_not_identifier);
UnqualifiedId Result;
if (ParseUnqualifiedId(SS, nullptr,
false, false,
false,
false,
false,
nullptr, Result)) {
SkipUntil(tok::semi);
return nullptr;
}
if (Result.getKind() != UnqualifiedIdKind::IK_Identifier) {
Diag(Result.getBeginLoc(), diag::err_concept_definition_not_identifier);
SkipUntil(tok::semi);
return nullptr;
}
IdentifierInfo *Id = Result.Identifier;
SourceLocation IdLoc = Result.getBeginLoc();
DiagnoseAndSkipCXX11Attributes();
if (!TryConsumeToken(tok::equal)) {
Diag(Tok.getLocation(), diag::err_expected) << tok::equal;
SkipUntil(tok::semi);
return nullptr;
}
ExprResult ConstraintExprResult =
Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression());
if (ConstraintExprResult.isInvalid()) {
SkipUntil(tok::semi);
return nullptr;
}
DeclEnd = Tok.getLocation();
ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Expr *ConstraintExpr = ConstraintExprResult.get();
return Actions.ActOnConceptDefinition(getCurScope(),
*TemplateInfo.TemplateParams,
Id, IdLoc, ConstraintExpr);
}
bool Parser::ParseTemplateParameters(
MultiParseScope &TemplateScopes, unsigned Depth,
SmallVectorImpl<NamedDecl *> &TemplateParams, SourceLocation &LAngleLoc,
SourceLocation &RAngleLoc) {
if (!TryConsumeToken(tok::less, LAngleLoc)) {
Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
return true;
}
bool Failed = false;
if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) {
TemplateScopes.Enter(Scope::TemplateParamScope);
Failed = ParseTemplateParameterList(Depth, TemplateParams);
}
if (Tok.is(tok::greatergreater)) {
Tok.setKind(tok::greater);
RAngleLoc = Tok.getLocation();
Tok.setLocation(Tok.getLocation().getLocWithOffset(1));
} else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) {
Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
return true;
}
return false;
}
bool
Parser::ParseTemplateParameterList(const unsigned Depth,
SmallVectorImpl<NamedDecl*> &TemplateParams) {
while (true) {
if (NamedDecl *TmpParam
= ParseTemplateParameter(Depth, TemplateParams.size())) {
TemplateParams.push_back(TmpParam);
} else {
SkipUntil(tok::comma, tok::greater, tok::greatergreater,
StopAtSemi | StopBeforeMatch);
}
if (Tok.is(tok::comma)) {
ConsumeToken();
} else if (Tok.isOneOf(tok::greater, tok::greatergreater)) {
break;
} else {
Diag(Tok.getLocation(), diag::err_expected_comma_greater);
SkipUntil(tok::comma, tok::greater, tok::greatergreater,
StopAtSemi | StopBeforeMatch);
return false;
}
}
return true;
}
Parser::TPResult Parser::isStartOfTemplateTypeParameter() {
if (Tok.is(tok::kw_class)) {
switch (NextToken().getKind()) {
case tok::equal:
case tok::comma:
case tok::greater:
case tok::greatergreater:
case tok::ellipsis:
return TPResult::True;
case tok::identifier:
break;
default:
return TPResult::False;
}
switch (GetLookAheadToken(2).getKind()) {
case tok::equal:
case tok::comma:
case tok::greater:
case tok::greatergreater:
return TPResult::True;
default:
return TPResult::False;
}
}
if (TryAnnotateTypeConstraint())
return TPResult::Error;
if (isTypeConstraintAnnotation() &&
!GetLookAheadToken(Tok.is(tok::annot_cxxscope) ? 2 : 1)
.isOneOf(tok::kw_auto, tok::kw_decltype))
return TPResult::True;
if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef))
return TPResult::False;
Token Next = NextToken();
if (Next.getKind() == tok::identifier)
Next = GetLookAheadToken(2);
switch (Next.getKind()) {
case tok::equal:
case tok::comma:
case tok::greater:
case tok::greatergreater:
case tok::ellipsis:
return TPResult::True;
case tok::kw_typename:
case tok::kw_typedef:
case tok::kw_class:
return TPResult::True;
default:
return TPResult::False;
}
}
NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
switch (isStartOfTemplateTypeParameter()) {
case TPResult::True:
if (Tok.is(tok::kw_typedef)) {
Diag(Tok.getLocation(), diag::err_expected_template_parameter);
Diag(Tok.getLocation(), diag::note_meant_to_use_typename)
<< FixItHint::CreateReplacement(CharSourceRange::getCharRange(
Tok.getLocation(),
Tok.getEndLoc()),
"typename");
Tok.setKind(tok::kw_typename);
}
return ParseTypeParameter(Depth, Position);
case TPResult::False:
break;
case TPResult::Error: {
DeclSpec DS(getAttrFactory());
DS.SetTypeSpecError();
Declarator D(DS, ParsedAttributesView::none(),
DeclaratorContext::TemplateParam);
D.SetIdentifier(nullptr, Tok.getLocation());
D.setInvalidType(true);
NamedDecl *ErrorParam = Actions.ActOnNonTypeTemplateParameter(
getCurScope(), D, Depth, Position, SourceLocation(),
nullptr);
ErrorParam->setInvalidDecl(true);
SkipUntil(tok::comma, tok::greater, tok::greatergreater,
StopAtSemi | StopBeforeMatch);
return ErrorParam;
}
case TPResult::Ambiguous:
llvm_unreachable("template param classification can't be ambiguous");
}
if (Tok.is(tok::kw_template))
return ParseTemplateTemplateParameter(Depth, Position);
return ParseNonTypeTemplateParameter(Depth, Position);
}
bool Parser::isTypeConstraintAnnotation() {
const Token &T = Tok.is(tok::annot_cxxscope) ? NextToken() : Tok;
if (T.isNot(tok::annot_template_id))
return false;
const auto *ExistingAnnot =
static_cast<TemplateIdAnnotation *>(T.getAnnotationValue());
return ExistingAnnot->Kind == TNK_Concept_template;
}
bool Parser::TryAnnotateTypeConstraint() {
if (!getLangOpts().CPlusPlus20)
return false;
CXXScopeSpec SS;
bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
if (ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false,
nullptr,
true, nullptr,
false))
return true;
if (Tok.is(tok::identifier)) {
UnqualifiedId PossibleConceptName;
PossibleConceptName.setIdentifier(Tok.getIdentifierInfo(),
Tok.getLocation());
TemplateTy PossibleConcept;
bool MemberOfUnknownSpecialization = false;
auto TNK = Actions.isTemplateName(getCurScope(), SS,
false,
PossibleConceptName,
ParsedType(),
false,
PossibleConcept,
MemberOfUnknownSpecialization,
true);
if (MemberOfUnknownSpecialization || !PossibleConcept ||
TNK != TNK_Concept_template) {
if (SS.isNotEmpty())
AnnotateScopeToken(SS, !WasScopeAnnotation);
return false;
}
if (AnnotateTemplateIdToken(PossibleConcept, TNK, SS,
SourceLocation(),
PossibleConceptName,
false,
true))
return true;
}
if (SS.isNotEmpty())
AnnotateScopeToken(SS, !WasScopeAnnotation);
return false;
}
NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
assert((Tok.isOneOf(tok::kw_class, tok::kw_typename) ||
isTypeConstraintAnnotation()) &&
"A type-parameter starts with 'class', 'typename' or a "
"type-constraint");
CXXScopeSpec TypeConstraintSS;
TemplateIdAnnotation *TypeConstraint = nullptr;
bool TypenameKeyword = false;
SourceLocation KeyLoc;
ParseOptionalCXXScopeSpecifier(TypeConstraintSS, nullptr,
false,
false);
if (Tok.is(tok::annot_template_id)) {
TypeConstraint =
static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
assert(TypeConstraint->Kind == TNK_Concept_template &&
"stray non-concept template-id annotation");
KeyLoc = ConsumeAnnotationToken();
} else {
assert(TypeConstraintSS.isEmpty() &&
"expected type constraint after scope specifier");
TypenameKeyword = Tok.is(tok::kw_typename);
KeyLoc = ConsumeToken();
}
SourceLocation EllipsisLoc;
if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
Diag(EllipsisLoc,
getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_variadic_templates
: diag::ext_variadic_templates);
}
SourceLocation NameLoc = Tok.getLocation();
IdentifierInfo *ParamName = nullptr;
if (Tok.is(tok::identifier)) {
ParamName = Tok.getIdentifierInfo();
ConsumeToken();
} else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
tok::greatergreater)) {
} else {
Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
return nullptr;
}
bool AlreadyHasEllipsis = EllipsisLoc.isValid();
if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
SourceLocation EqualLoc;
ParsedType DefaultArg;
if (TryConsumeToken(tok::equal, EqualLoc))
DefaultArg =
ParseTypeName(nullptr, DeclaratorContext::TemplateTypeArg)
.get();
NamedDecl *NewDecl = Actions.ActOnTypeParameter(getCurScope(),
TypenameKeyword, EllipsisLoc,
KeyLoc, ParamName, NameLoc,
Depth, Position, EqualLoc,
DefaultArg,
TypeConstraint != nullptr);
if (TypeConstraint) {
Actions.ActOnTypeConstraint(TypeConstraintSS, TypeConstraint,
cast<TemplateTypeParmDecl>(NewDecl),
EllipsisLoc);
}
return NewDecl;
}
NamedDecl *
Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
SourceLocation TemplateLoc = ConsumeToken();
SmallVector<NamedDecl*,8> TemplateParams;
SourceLocation LAngleLoc, RAngleLoc;
{
MultiParseScope TemplateParmScope(*this);
if (ParseTemplateParameters(TemplateParmScope, Depth + 1, TemplateParams,
LAngleLoc, RAngleLoc)) {
return nullptr;
}
}
if (!TryConsumeToken(tok::kw_class)) {
bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct);
const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok;
if (Tok.is(tok::kw_typename)) {
Diag(Tok.getLocation(),
getLangOpts().CPlusPlus17
? diag::warn_cxx14_compat_template_template_param_typename
: diag::ext_template_template_param_typename)
<< (!getLangOpts().CPlusPlus17
? FixItHint::CreateReplacement(Tok.getLocation(), "class")
: FixItHint());
} else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater,
tok::greatergreater, tok::ellipsis)) {
Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
<< getLangOpts().CPlusPlus17
<< (Replace
? FixItHint::CreateReplacement(Tok.getLocation(), "class")
: FixItHint::CreateInsertion(Tok.getLocation(), "class "));
} else
Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
<< getLangOpts().CPlusPlus17;
if (Replace)
ConsumeToken();
}
SourceLocation EllipsisLoc;
if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Diag(EllipsisLoc,
getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_variadic_templates
: diag::ext_variadic_templates);
SourceLocation NameLoc = Tok.getLocation();
IdentifierInfo *ParamName = nullptr;
if (Tok.is(tok::identifier)) {
ParamName = Tok.getIdentifierInfo();
ConsumeToken();
} else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
tok::greatergreater)) {
} else {
Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
return nullptr;
}
bool AlreadyHasEllipsis = EllipsisLoc.isValid();
if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
TemplateParameterList *ParamList =
Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
TemplateLoc, LAngleLoc,
TemplateParams,
RAngleLoc, nullptr);
SourceLocation EqualLoc;
ParsedTemplateArgument DefaultArg;
if (TryConsumeToken(tok::equal, EqualLoc)) {
DefaultArg = ParseTemplateTemplateArgument();
if (DefaultArg.isInvalid()) {
Diag(Tok.getLocation(),
diag::err_default_template_template_parameter_not_template);
SkipUntil(tok::comma, tok::greater, tok::greatergreater,
StopAtSemi | StopBeforeMatch);
}
}
return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
ParamList, EllipsisLoc,
ParamName, NameLoc, Depth,
Position, EqualLoc, DefaultArg);
}
NamedDecl *
Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
DeclSpec DS(AttrFactory);
ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
DeclSpecContext::DSC_template_param);
Declarator ParamDecl(DS, ParsedAttributesView::none(),
DeclaratorContext::TemplateParam);
ParseDeclarator(ParamDecl);
if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
Diag(Tok.getLocation(), diag::err_expected_template_parameter);
return nullptr;
}
SourceLocation EllipsisLoc;
if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl);
SourceLocation EqualLoc;
ExprResult DefaultArg;
if (TryConsumeToken(tok::equal, EqualLoc)) {
if (Tok.is(tok::l_paren) && NextToken().is(tok::l_brace)) {
Diag(Tok.getLocation(), diag::err_stmt_expr_in_default_arg) << 1;
SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
} else {
GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
EnterExpressionEvaluationContext ConstantEvaluated(
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
DefaultArg =
Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
if (DefaultArg.isInvalid())
SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
}
}
return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
Depth, Position, EqualLoc,
DefaultArg.get());
}
void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,
SourceLocation CorrectLoc,
bool AlreadyHasEllipsis,
bool IdentifierHasName) {
FixItHint Insertion;
if (!AlreadyHasEllipsis)
Insertion = FixItHint::CreateInsertion(CorrectLoc, "...");
Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
<< FixItHint::CreateRemoval(EllipsisLoc) << Insertion
<< !IdentifierHasName;
}
void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,
Declarator &D) {
assert(EllipsisLoc.isValid());
bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid();
if (!AlreadyHasEllipsis)
D.setEllipsisLoc(EllipsisLoc);
DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(),
AlreadyHasEllipsis, D.hasName());
}
bool Parser::ParseGreaterThanInTemplateList(SourceLocation LAngleLoc,
SourceLocation &RAngleLoc,
bool ConsumeLastToken,
bool ObjCGenericList) {
tok::TokenKind RemainingToken;
const char *ReplacementStr = "> >";
bool MergeWithNextToken = false;
switch (Tok.getKind()) {
default:
Diag(getEndOfPreviousToken(), diag::err_expected) << tok::greater;
Diag(LAngleLoc, diag::note_matching) << tok::less;
return true;
case tok::greater:
RAngleLoc = Tok.getLocation();
if (ConsumeLastToken)
ConsumeToken();
return false;
case tok::greatergreater:
RemainingToken = tok::greater;
break;
case tok::greatergreatergreater:
RemainingToken = tok::greatergreater;
break;
case tok::greaterequal:
RemainingToken = tok::equal;
ReplacementStr = "> =";
if (NextToken().is(tok::equal) &&
areTokensAdjacent(Tok, NextToken())) {
RemainingToken = tok::equalequal;
MergeWithNextToken = true;
}
break;
case tok::greatergreaterequal:
RemainingToken = tok::greaterequal;
break;
}
SourceLocation TokBeforeGreaterLoc = PrevTokLocation;
SourceLocation TokLoc = Tok.getLocation();
Token Next = NextToken();
bool PreventMergeWithNextToken =
(RemainingToken == tok::greater ||
RemainingToken == tok::greatergreater) &&
(Next.isOneOf(tok::greater, tok::greatergreater,
tok::greatergreatergreater, tok::equal, tok::greaterequal,
tok::greatergreaterequal, tok::equalequal)) &&
areTokensAdjacent(Tok, Next);
if (!ObjCGenericList) {
CharSourceRange ReplacementRange = CharSourceRange::getCharRange(
TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(),
getLangOpts()));
FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange,
ReplacementStr);
FixItHint Hint2;
if (PreventMergeWithNextToken)
Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");
unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
if (getLangOpts().CPlusPlus11 &&
(Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
else if (Tok.is(tok::greaterequal))
DiagId = diag::err_right_angle_bracket_equal_needs_space;
Diag(TokLoc, DiagId) << Hint1 << Hint2;
}
unsigned GreaterLength = Lexer::getTokenPrefixLength(
TokLoc, 1, PP.getSourceManager(), getLangOpts());
RAngleLoc = PP.SplitToken(TokLoc, GreaterLength);
bool CachingTokens = PP.IsPreviousCachedToken(Tok);
Token Greater = Tok;
Greater.setLocation(RAngleLoc);
Greater.setKind(tok::greater);
Greater.setLength(GreaterLength);
unsigned OldLength = Tok.getLength();
if (MergeWithNextToken) {
ConsumeToken();
OldLength += Tok.getLength();
}
Tok.setKind(RemainingToken);
Tok.setLength(OldLength - GreaterLength);
SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength);
if (PreventMergeWithNextToken)
AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength());
Tok.setLocation(AfterGreaterLoc);
if (CachingTokens) {
if (MergeWithNextToken)
PP.ReplacePreviousCachedToken({});
if (ConsumeLastToken)
PP.ReplacePreviousCachedToken({Greater, Tok});
else
PP.ReplacePreviousCachedToken({Greater});
}
if (ConsumeLastToken) {
PrevTokLocation = RAngleLoc;
} else {
PrevTokLocation = TokBeforeGreaterLoc;
PP.EnterToken(Tok, true);
Tok = Greater;
}
return false;
}
bool Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken,
SourceLocation &LAngleLoc,
TemplateArgList &TemplateArgs,
SourceLocation &RAngleLoc,
TemplateTy Template) {
assert(Tok.is(tok::less) && "Must have already parsed the template-name");
LAngleLoc = ConsumeToken();
bool Invalid = false;
{
GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
if (!Tok.isOneOf(tok::greater, tok::greatergreater,
tok::greatergreatergreater, tok::greaterequal,
tok::greatergreaterequal))
Invalid = ParseTemplateArgumentList(TemplateArgs, Template, LAngleLoc);
if (Invalid) {
if (getLangOpts().CPlusPlus11)
SkipUntil(tok::greater, tok::greatergreater,
tok::greatergreatergreater, StopAtSemi | StopBeforeMatch);
else
SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch);
}
}
return ParseGreaterThanInTemplateList(LAngleLoc, RAngleLoc, ConsumeLastToken,
false) ||
Invalid;
}
bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
CXXScopeSpec &SS,
SourceLocation TemplateKWLoc,
UnqualifiedId &TemplateName,
bool AllowTypeAnnotation,
bool TypeConstraint) {
assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++");
assert((Tok.is(tok::less) || TypeConstraint) &&
"Parser isn't at the beginning of a template-id");
assert(!(TypeConstraint && AllowTypeAnnotation) && "type-constraint can't be "
"a type annotation");
assert((!TypeConstraint || TNK == TNK_Concept_template) && "type-constraint "
"must accompany a concept name");
assert((Template || TNK == TNK_Non_template) && "missing template name");
SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
SourceLocation LAngleLoc, RAngleLoc;
TemplateArgList TemplateArgs;
bool ArgsInvalid = false;
if (!TypeConstraint || Tok.is(tok::less)) {
ArgsInvalid = ParseTemplateIdAfterTemplateName(
false, LAngleLoc, TemplateArgs, RAngleLoc, Template);
if (RAngleLoc.isInvalid())
return true;
}
ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
if (TNK == TNK_Type_template && AllowTypeAnnotation) {
TypeResult Type = ArgsInvalid
? TypeError()
: Actions.ActOnTemplateIdType(
getCurScope(), SS, TemplateKWLoc, Template,
TemplateName.Identifier, TemplateNameLoc,
LAngleLoc, TemplateArgsPtr, RAngleLoc);
Tok.setKind(tok::annot_typename);
setTypeAnnotation(Tok, Type);
if (SS.isNotEmpty())
Tok.setLocation(SS.getBeginLoc());
else if (TemplateKWLoc.isValid())
Tok.setLocation(TemplateKWLoc);
else
Tok.setLocation(TemplateNameLoc);
} else {
Tok.setKind(tok::annot_template_id);
IdentifierInfo *TemplateII =
TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
? TemplateName.Identifier
: nullptr;
OverloadedOperatorKind OpKind =
TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
? OO_None
: TemplateName.OperatorFunctionId.Operator;
TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create(
TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK,
LAngleLoc, RAngleLoc, TemplateArgs, ArgsInvalid, TemplateIds);
Tok.setAnnotationValue(TemplateId);
if (TemplateKWLoc.isValid())
Tok.setLocation(TemplateKWLoc);
else
Tok.setLocation(TemplateNameLoc);
}
Tok.setAnnotationEndLoc(RAngleLoc);
PP.AnnotateCachedTokens(Tok);
return false;
}
void Parser::AnnotateTemplateIdTokenAsType(CXXScopeSpec &SS,
bool IsClassName) {
assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
assert(TemplateId->mightBeType() &&
"Only works for type and dependent templates");
ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
TemplateId->NumArgs);
TypeResult Type =
TemplateId->isInvalid()
? TypeError()
: Actions.ActOnTemplateIdType(
getCurScope(), SS, TemplateId->TemplateKWLoc,
TemplateId->Template, TemplateId->Name,
TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
TemplateArgsPtr, TemplateId->RAngleLoc,
false, IsClassName);
Tok.setKind(tok::annot_typename);
setTypeAnnotation(Tok, Type);
if (SS.isNotEmpty()) Tok.setLocation(SS.getBeginLoc());
PP.AnnotateCachedTokens(Tok);
}
static bool isEndOfTemplateArgument(Token Tok) {
return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater,
tok::greatergreatergreater);
}
ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
!Tok.is(tok::annot_cxxscope))
return ParsedTemplateArgument();
CXXScopeSpec SS; ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false);
ParsedTemplateArgument Result;
SourceLocation EllipsisLoc;
if (SS.isSet() && Tok.is(tok::kw_template)) {
SourceLocation TemplateKWLoc = ConsumeToken();
if (Tok.is(tok::identifier)) {
UnqualifiedId Name;
Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
TryConsumeToken(tok::ellipsis, EllipsisLoc);
TemplateTy Template;
if (isEndOfTemplateArgument(Tok) &&
Actions.ActOnTemplateName(getCurScope(), SS, TemplateKWLoc, Name,
nullptr,
false, Template))
Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
}
} else if (Tok.is(tok::identifier)) {
TemplateTy Template;
UnqualifiedId Name;
Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
TryConsumeToken(tok::ellipsis, EllipsisLoc);
if (isEndOfTemplateArgument(Tok)) {
bool MemberOfUnknownSpecialization;
TemplateNameKind TNK = Actions.isTemplateName(
getCurScope(), SS,
false, Name,
nullptr,
false, Template, MemberOfUnknownSpecialization);
if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
}
}
}
if (EllipsisLoc.isValid() && !Result.isInvalid())
Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
return Result;
}
ParsedTemplateArgument Parser::ParseTemplateArgument() {
EnterExpressionEvaluationContext EnterConstantEvaluated(
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
nullptr,
Sema::ExpressionEvaluationContextRecord::EK_TemplateArgument);
if (isCXXTypeId(TypeIdAsTemplateArgument)) {
TypeResult TypeArg = ParseTypeName(
nullptr, DeclaratorContext::TemplateArg);
return Actions.ActOnTemplateTypeArgument(TypeArg);
}
{
TentativeParsingAction TPA(*this);
ParsedTemplateArgument TemplateTemplateArgument
= ParseTemplateTemplateArgument();
if (!TemplateTemplateArgument.isInvalid()) {
TPA.Commit();
return TemplateTemplateArgument;
}
TPA.Revert();
}
SourceLocation Loc = Tok.getLocation();
ExprResult ExprArg = ParseConstantExpressionInExprEvalContext(MaybeTypeCast);
if (ExprArg.isInvalid() || !ExprArg.get()) {
return ParsedTemplateArgument();
}
return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
ExprArg.get(), Loc);
}
bool Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs,
TemplateTy Template,
SourceLocation OpenLoc) {
ColonProtectionRAIIObject ColonProtection(*this, false);
auto RunSignatureHelp = [&] {
if (!Template)
return QualType();
CalledSignatureHelp = true;
return Actions.ProduceTemplateArgumentSignatureHelp(Template, TemplateArgs,
OpenLoc);
};
do {
PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
ParsedTemplateArgument Arg = ParseTemplateArgument();
SourceLocation EllipsisLoc;
if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
if (Arg.isInvalid()) {
if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
RunSignatureHelp();
return true;
}
TemplateArgs.push_back(Arg);
} while (TryConsumeToken(tok::comma));
return false;
}
Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context,
SourceLocation ExternLoc,
SourceLocation TemplateLoc,
SourceLocation &DeclEnd,
ParsedAttributes &AccessAttrs,
AccessSpecifier AS) {
ParsingDeclRAIIObject
ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
return ParseSingleDeclarationAfterTemplate(
Context, ParsedTemplateInfo(ExternLoc, TemplateLoc),
ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
}
SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
if (TemplateParams)
return getTemplateParamsRange(TemplateParams->data(),
TemplateParams->size());
SourceRange R(TemplateLoc);
if (ExternLoc.isValid())
R.setBegin(ExternLoc);
return R;
}
void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) {
((Parser *)P)->ParseLateTemplatedFuncDef(LPT);
}
void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
if (!LPT.D)
return;
DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
FunctionDecl *FunD = LPT.D->getAsFunction();
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
Sema::ContextRAII GlobalSavedContext(
Actions, Actions.Context.getTranslationUnitDecl());
MultiParseScope Scopes(*this);
SmallVector<DeclContext*, 4> DeclContextsToReenter;
for (DeclContext *DC = FunD; DC && !DC->isTranslationUnit();
DC = DC->getLexicalParent())
DeclContextsToReenter.push_back(DC);
for (DeclContext *DC : reverse(DeclContextsToReenter)) {
CurTemplateDepthTracker.addDepth(
ReenterTemplateScopes(Scopes, cast<Decl>(DC)));
Scopes.Enter(Scope::DeclScope);
if (DC != FunD)
Actions.PushDeclContext(Actions.getCurScope(), DC);
}
assert(!LPT.Toks.empty() && "Empty body!");
LPT.Toks.push_back(Tok);
PP.EnterTokenStream(LPT.Toks, true, true);
ConsumeAnyToken(true);
assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) &&
"Inline method not starting with '{', ':' or 'try'");
ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
Scope::CompoundStmtScope);
Sema::ContextRAII FunctionSavedContext(Actions, FunD->getLexicalParent());
Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
if (Tok.is(tok::kw_try)) {
ParseFunctionTryBlock(LPT.D, FnScope);
} else {
if (Tok.is(tok::colon))
ParseConstructorInitializer(LPT.D);
else
Actions.ActOnDefaultCtorInitializers(LPT.D);
if (Tok.is(tok::l_brace)) {
assert((!isa<FunctionTemplateDecl>(LPT.D) ||
cast<FunctionTemplateDecl>(LPT.D)
->getTemplateParameters()
->getDepth() == TemplateParameterDepth - 1) &&
"TemplateParameterDepth should be greater than the depth of "
"current template being instantiated!");
ParseFunctionStatementBody(LPT.D, FnScope);
Actions.UnmarkAsLateParsedTemplate(FunD);
} else
Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
}
}
void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
tok::TokenKind kind = Tok.getKind();
if (!ConsumeAndStoreFunctionPrologue(Toks)) {
ConsumeAndStoreUntil(tok::r_brace, Toks, false);
}
if (kind == tok::kw_try) {
while (Tok.is(tok::kw_catch)) {
ConsumeAndStoreUntil(tok::l_brace, Toks, false);
ConsumeAndStoreUntil(tok::r_brace, Toks, false);
}
}
}
bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) {
TentativeParsingAction TPA(*this);
if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater,
StopAtSemi | StopBeforeMatch)) {
TPA.Commit();
SourceLocation Greater;
ParseGreaterThanInTemplateList(Less, Greater, true, false);
Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS,
Less, Greater);
return true;
}
TPA.Revert();
return false;
}
void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) {
assert(Tok.is(tok::less) && "not at a potential angle bracket");
bool DependentTemplateName = false;
if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName,
DependentTemplateName))
return;
if (NextToken().is(tok::greater) ||
(getLangOpts().CPlusPlus11 &&
NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) {
SourceLocation Less = ConsumeToken();
SourceLocation Greater;
ParseGreaterThanInTemplateList(Less, Greater, true, false);
Actions.diagnoseExprIntendedAsTemplateName(
getCurScope(), PotentialTemplateName, Less, Greater);
PotentialTemplateName = ExprError();
return;
}
{
TentativeParsingAction TPA(*this);
SourceLocation Less = ConsumeToken();
if (isTypeIdUnambiguously() &&
diagnoseUnknownTemplateId(PotentialTemplateName, Less)) {
TPA.Commit();
PotentialTemplateName = ExprError();
return;
}
TPA.Revert();
}
AngleBracketTracker::Priority Priority =
(DependentTemplateName ? AngleBracketTracker::DependentName
: AngleBracketTracker::PotentialTypo) |
(Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess
: AngleBracketTracker::NoSpaceBeforeLess);
AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(),
Priority);
}
bool Parser::checkPotentialAngleBracketDelimiter(
const AngleBracketTracker::Loc &LAngle, const Token &OpToken) {
if (OpToken.is(tok::comma) && isTypeIdUnambiguously() &&
diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) {
AngleBrackets.clear(*this);
return true;
}
if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) &&
NextToken().is(tok::r_paren)) {
Actions.diagnoseExprIntendedAsTemplateName(
getCurScope(), LAngle.TemplateName, LAngle.LessLoc,
OpToken.getLocation());
AngleBrackets.clear(*this);
return true;
}
if (OpToken.is(tok::greater) ||
(getLangOpts().CPlusPlus11 &&
OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater)))
AngleBrackets.clear(*this);
return false;
}