#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/PrettyDeclStackTrace.h"
#include "clang/Basic/AttributeCommonInfo.h"
#include "clang/Basic/Attributes.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/OperatorKinds.h"
#include "clang/Basic/TargetInfo.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 "llvm/ADT/SmallString.h"
#include "llvm/Support/TimeProfiler.h"
using namespace clang;
Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
SourceLocation &DeclEnd,
SourceLocation InlineLoc) {
assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
SourceLocation NamespaceLoc = ConsumeToken(); ObjCDeclContextSwitch ObjCDC(*this);
if (Tok.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteNamespaceDecl(getCurScope());
return nullptr;
}
SourceLocation IdentLoc;
IdentifierInfo *Ident = nullptr;
InnerNamespaceInfoList ExtraNSs;
SourceLocation FirstNestedInlineLoc;
ParsedAttributes attrs(AttrFactory);
auto ReadAttributes = [&] {
bool MoreToParse;
do {
MoreToParse = false;
if (Tok.is(tok::kw___attribute)) {
ParseGNUAttributes(attrs);
MoreToParse = true;
}
if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
? diag::warn_cxx14_compat_ns_enum_attribute
: diag::ext_ns_enum_attribute)
<< 0 ;
ParseCXX11Attributes(attrs);
MoreToParse = true;
}
} while (MoreToParse);
};
ReadAttributes();
if (Tok.is(tok::identifier)) {
Ident = Tok.getIdentifierInfo();
IdentLoc = ConsumeToken(); while (Tok.is(tok::coloncolon) &&
(NextToken().is(tok::identifier) ||
(NextToken().is(tok::kw_inline) &&
GetLookAheadToken(2).is(tok::identifier)))) {
InnerNamespaceInfo Info;
Info.NamespaceLoc = ConsumeToken();
if (Tok.is(tok::kw_inline)) {
Info.InlineLoc = ConsumeToken();
if (FirstNestedInlineLoc.isInvalid())
FirstNestedInlineLoc = Info.InlineLoc;
}
Info.Ident = Tok.getIdentifierInfo();
Info.IdentLoc = ConsumeToken();
ExtraNSs.push_back(Info);
}
}
ReadAttributes();
SourceLocation attrLoc = attrs.Range.getBegin();
if (!ExtraNSs.empty() && attrLoc.isValid())
Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
if (Tok.is(tok::equal)) {
if (!Ident) {
Diag(Tok, diag::err_expected) << tok::identifier;
SkipUntil(tok::semi);
return nullptr;
}
if (attrLoc.isValid())
Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
if (InlineLoc.isValid())
Diag(InlineLoc, diag::err_inline_namespace_alias)
<< FixItHint::CreateRemoval(InlineLoc);
Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
return Actions.ConvertDeclToDeclGroup(NSAlias);
}
BalancedDelimiterTracker T(*this, tok::l_brace);
if (T.consumeOpen()) {
if (Ident)
Diag(Tok, diag::err_expected) << tok::l_brace;
else
Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
return nullptr;
}
if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
getCurScope()->getFnParent()) {
Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
SkipUntil(tok::r_brace);
return nullptr;
}
if (ExtraNSs.empty()) {
} else if (InlineLoc.isValid()) {
Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
} else if (getLangOpts().CPlusPlus20) {
Diag(ExtraNSs[0].NamespaceLoc,
diag::warn_cxx14_compat_nested_namespace_definition);
if (FirstNestedInlineLoc.isValid())
Diag(FirstNestedInlineLoc,
diag::warn_cxx17_compat_inline_nested_namespace_definition);
} else if (getLangOpts().CPlusPlus17) {
Diag(ExtraNSs[0].NamespaceLoc,
diag::warn_cxx14_compat_nested_namespace_definition);
if (FirstNestedInlineLoc.isValid())
Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
} else {
TentativeParsingAction TPA(*this);
SkipUntil(tok::r_brace, StopBeforeMatch);
Token rBraceToken = Tok;
TPA.Revert();
if (!rBraceToken.is(tok::r_brace)) {
Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
<< SourceRange(ExtraNSs.front().NamespaceLoc,
ExtraNSs.back().IdentLoc);
} else {
std::string NamespaceFix;
for (const auto &ExtraNS : ExtraNSs) {
NamespaceFix += " { ";
if (ExtraNS.InlineLoc.isValid())
NamespaceFix += "inline ";
NamespaceFix += "namespace ";
NamespaceFix += ExtraNS.Ident->getName();
}
std::string RBraces;
for (unsigned i = 0, e = ExtraNSs.size(); i != e; ++i)
RBraces += "} ";
Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
<< FixItHint::CreateReplacement(
SourceRange(ExtraNSs.front().NamespaceLoc,
ExtraNSs.back().IdentLoc),
NamespaceFix)
<< FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
}
if (FirstNestedInlineLoc.isValid())
Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
}
if (InlineLoc.isValid())
Diag(InlineLoc, getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_inline_namespace
: diag::ext_inline_namespace);
ParseScope NamespaceScope(this, Scope::DeclScope);
UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
Decl *NamespcDecl = Actions.ActOnStartNamespaceDef(
getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident,
T.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl);
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl,
NamespaceLoc, "parsing namespace");
ParseInnerNamespace(ExtraNSs, 0, InlineLoc, attrs, T);
NamespaceScope.Exit();
DeclEnd = T.getCloseLocation();
Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
return Actions.ConvertDeclToDeclGroup(NamespcDecl,
ImplicitUsingDirectiveDecl);
}
void Parser::ParseInnerNamespace(const InnerNamespaceInfoList &InnerNSs,
unsigned int index, SourceLocation &InlineLoc,
ParsedAttributes &attrs,
BalancedDelimiterTracker &Tracker) {
if (index == InnerNSs.size()) {
while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
Tok.isNot(tok::eof)) {
ParsedAttributes Attrs(AttrFactory);
MaybeParseCXX11Attributes(Attrs);
ParseExternalDeclaration(Attrs);
}
Tracker.consumeClose();
return;
}
ParseScope NamespaceScope(this, Scope::DeclScope);
UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
Decl *NamespcDecl = Actions.ActOnStartNamespaceDef(
getCurScope(), InnerNSs[index].InlineLoc, InnerNSs[index].NamespaceLoc,
InnerNSs[index].IdentLoc, InnerNSs[index].Ident,
Tracker.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl);
assert(!ImplicitUsingDirectiveDecl &&
"nested namespace definition cannot define anonymous namespace");
ParseInnerNamespace(InnerNSs, ++index, InlineLoc, attrs, Tracker);
NamespaceScope.Exit();
Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
}
Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
SourceLocation AliasLoc,
IdentifierInfo *Alias,
SourceLocation &DeclEnd) {
assert(Tok.is(tok::equal) && "Not equal token");
ConsumeToken();
if (Tok.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
return nullptr;
}
CXXScopeSpec SS;
ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false,
nullptr,
false,
nullptr,
true);
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_namespace_name);
SkipUntil(tok::semi);
return nullptr;
}
if (SS.isInvalid()) {
SkipUntil(tok::semi);
return nullptr;
}
IdentifierInfo *Ident = Tok.getIdentifierInfo();
SourceLocation IdentLoc = ConsumeToken();
DeclEnd = Tok.getLocation();
if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
SkipUntil(tok::semi);
return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc,
Alias, SS, IdentLoc, Ident);
}
Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
assert(isTokenStringLiteral() && "Not a string literal!");
ExprResult Lang = ParseStringLiteralExpression(false);
ParseScope LinkageScope(this, Scope::DeclScope);
Decl *LinkageSpec =
Lang.isInvalid()
? nullptr
: Actions.ActOnStartLinkageSpecification(
getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
ParsedAttributes DeclAttrs(AttrFactory);
MaybeParseCXX11Attributes(DeclAttrs);
if (Tok.isNot(tok::l_brace)) {
DS.SetRangeStart(SourceLocation());
DS.SetRangeEnd(SourceLocation());
DS.setExternInLinkageSpec(true);
ParseExternalDeclaration(DeclAttrs, &DS);
return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
getCurScope(), LinkageSpec, SourceLocation())
: nullptr;
}
DS.abort();
ProhibitAttributes(DeclAttrs);
BalancedDelimiterTracker T(*this, tok::l_brace);
T.consumeOpen();
unsigned NestedModules = 0;
while (true) {
switch (Tok.getKind()) {
case tok::annot_module_begin:
++NestedModules;
ParseTopLevelDecl();
continue;
case tok::annot_module_end:
if (!NestedModules)
break;
--NestedModules;
ParseTopLevelDecl();
continue;
case tok::annot_module_include:
ParseTopLevelDecl();
continue;
case tok::eof:
break;
case tok::r_brace:
if (!NestedModules)
break;
LLVM_FALLTHROUGH;
default:
ParsedAttributes Attrs(AttrFactory);
MaybeParseCXX11Attributes(Attrs);
ParseExternalDeclaration(Attrs);
continue;
}
break;
}
T.consumeClose();
return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
getCurScope(), LinkageSpec, T.getCloseLocation())
: nullptr;
}
Decl *Parser::ParseExportDeclaration() {
assert(Tok.is(tok::kw_export));
SourceLocation ExportLoc = ConsumeToken();
ParseScope ExportScope(this, Scope::DeclScope);
Decl *ExportDecl = Actions.ActOnStartExportDecl(
getCurScope(), ExportLoc,
Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
if (Tok.isNot(tok::l_brace)) {
ParsedAttributes Attrs(AttrFactory);
MaybeParseCXX11Attributes(Attrs);
ParseExternalDeclaration(Attrs);
return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl,
SourceLocation());
}
BalancedDelimiterTracker T(*this, tok::l_brace);
T.consumeOpen();
if (Tok.is(tok::r_brace) && getLangOpts().ModulesTS) {
Diag(ExportLoc, diag::err_export_empty)
<< SourceRange(ExportLoc, Tok.getLocation());
}
while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
Tok.isNot(tok::eof)) {
ParsedAttributes Attrs(AttrFactory);
MaybeParseCXX11Attributes(Attrs);
ParseExternalDeclaration(Attrs);
}
T.consumeClose();
return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl,
T.getCloseLocation());
}
Parser::DeclGroupPtrTy Parser::ParseUsingDirectiveOrDeclaration(
DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
SourceLocation &DeclEnd, ParsedAttributes &Attrs) {
assert(Tok.is(tok::kw_using) && "Not using token");
ObjCDeclContextSwitch ObjCDC(*this);
SourceLocation UsingLoc = ConsumeToken();
if (Tok.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteUsing(getCurScope());
return nullptr;
}
while (Tok.is(tok::kw_template)) {
SourceLocation TemplateLoc = ConsumeToken();
Diag(TemplateLoc, diag::err_unexpected_template_after_using)
<< FixItHint::CreateRemoval(TemplateLoc);
}
if (Tok.is(tok::kw_namespace)) {
if (TemplateInfo.Kind) {
SourceRange R = TemplateInfo.getSourceRange();
Diag(UsingLoc, diag::err_templated_using_directive_declaration)
<< 0 << R << FixItHint::CreateRemoval(R);
}
Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, Attrs);
return Actions.ConvertDeclToDeclGroup(UsingDir);
}
return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, Attrs,
AS_none);
}
Decl *Parser::ParseUsingDirective(DeclaratorContext Context,
SourceLocation UsingLoc,
SourceLocation &DeclEnd,
ParsedAttributes &attrs) {
assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
SourceLocation NamespcLoc = ConsumeToken();
if (Tok.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteUsingDirective(getCurScope());
return nullptr;
}
CXXScopeSpec SS;
ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false,
nullptr,
false,
nullptr,
true);
IdentifierInfo *NamespcName = nullptr;
SourceLocation IdentLoc = SourceLocation();
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_namespace_name);
SkipUntil(tok::semi);
return nullptr;
}
if (SS.isInvalid()) {
SkipUntil(tok::semi);
return nullptr;
}
NamespcName = Tok.getIdentifierInfo();
IdentLoc = ConsumeToken();
bool GNUAttr = false;
if (Tok.is(tok::kw___attribute)) {
GNUAttr = true;
ParseGNUAttributes(attrs);
}
DeclEnd = Tok.getLocation();
if (ExpectAndConsume(tok::semi,
GNUAttr ? diag::err_expected_semi_after_attribute_list
: diag::err_expected_semi_after_namespace_name))
SkipUntil(tok::semi);
return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
IdentLoc, NamespcName, attrs);
}
bool Parser::ParseUsingDeclarator(DeclaratorContext Context,
UsingDeclarator &D) {
D.clear();
TryConsumeToken(tok::kw_typename, D.TypenameLoc);
if (Tok.is(tok::kw___super)) {
Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
return true;
}
IdentifierInfo *LastII = nullptr;
if (ParseOptionalCXXScopeSpecifier(D.SS, nullptr,
false,
false,
nullptr,
false,
&LastII,
false,
true))
return true;
if (D.SS.isInvalid())
return true;
if (getLangOpts().CPlusPlus11 && Context == DeclaratorContext::Member &&
Tok.is(tok::identifier) &&
(NextToken().is(tok::semi) || NextToken().is(tok::comma) ||
NextToken().is(tok::ellipsis) || NextToken().is(tok::l_square) ||
NextToken().is(tok::kw___attribute)) &&
D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
!D.SS.getScopeRep()->getAsNamespace() &&
!D.SS.getScopeRep()->getAsNamespaceAlias()) {
SourceLocation IdLoc = ConsumeToken();
ParsedType Type =
Actions.getInheritingConstructorName(D.SS, IdLoc, *LastII);
D.Name.setConstructorName(Type, IdLoc, IdLoc);
} else {
if (ParseUnqualifiedId(
D.SS, nullptr,
false, false,
true,
!(Tok.is(tok::identifier) && NextToken().is(tok::equal)),
false, nullptr, D.Name))
return true;
}
if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc))
Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
? diag::warn_cxx17_compat_using_declaration_pack
: diag::ext_using_declaration_pack);
return false;
}
Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
SourceLocation UsingLoc, SourceLocation &DeclEnd,
ParsedAttributes &PrefixAttrs, AccessSpecifier AS) {
SourceLocation UELoc;
bool InInitStatement = Context == DeclaratorContext::SelectionInit ||
Context == DeclaratorContext::ForInit;
if (TryConsumeToken(tok::kw_enum, UELoc) && !InInitStatement) {
Diag(UELoc, getLangOpts().CPlusPlus20
? diag::warn_cxx17_compat_using_enum_declaration
: diag::ext_using_enum_declaration);
DiagnoseCXX11AttributeExtension(PrefixAttrs);
DeclSpec DS(AttrFactory);
ParseEnumSpecifier(UELoc, DS, TemplateInfo, AS,
DeclSpecContext::DSC_trailing);
if (TemplateInfo.Kind) {
SourceRange R = TemplateInfo.getSourceRange();
Diag(UsingLoc, diag::err_templated_using_directive_declaration)
<< 1 << R << FixItHint::CreateRemoval(R);
return nullptr;
}
Decl *UED = Actions.ActOnUsingEnumDeclaration(getCurScope(), AS, UsingLoc,
UELoc, DS);
DeclEnd = Tok.getLocation();
if (ExpectAndConsume(tok::semi, diag::err_expected_after,
"using-enum declaration"))
SkipUntil(tok::semi);
return Actions.ConvertDeclToDeclGroup(UED);
}
ParsedAttributes MisplacedAttrs(AttrFactory);
MaybeParseCXX11Attributes(MisplacedAttrs);
if (InInitStatement && Tok.isNot(tok::identifier))
return nullptr;
UsingDeclarator D;
bool InvalidDeclarator = ParseUsingDeclarator(Context, D);
ParsedAttributes Attrs(AttrFactory);
MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs);
if (MisplacedAttrs.Range.isValid()) {
Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
<< FixItHint::CreateInsertionFromRange(
Tok.getLocation(),
CharSourceRange::getTokenRange(MisplacedAttrs.Range))
<< FixItHint::CreateRemoval(MisplacedAttrs.Range);
Attrs.takeAllFrom(MisplacedAttrs);
}
if (Tok.is(tok::equal) || InInitStatement) {
if (InvalidDeclarator) {
SkipUntil(tok::semi);
return nullptr;
}
ProhibitAttributes(PrefixAttrs);
Decl *DeclFromDeclSpec = nullptr;
Decl *AD = ParseAliasDeclarationAfterDeclarator(
TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec);
return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec);
}
DiagnoseCXX11AttributeExtension(PrefixAttrs);
if (TemplateInfo.Kind) {
SourceRange R = TemplateInfo.getSourceRange();
Diag(UsingLoc, diag::err_templated_using_directive_declaration)
<< 1 << R << FixItHint::CreateRemoval(R);
return nullptr;
}
SmallVector<Decl *, 8> DeclsInGroup;
while (true) {
MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs);
DiagnoseCXX11AttributeExtension(Attrs);
Attrs.addAll(PrefixAttrs.begin(), PrefixAttrs.end());
if (InvalidDeclarator)
SkipUntil(tok::comma, tok::semi, StopBeforeMatch);
else {
if (D.TypenameLoc.isValid() &&
D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) {
Diag(D.Name.getSourceRange().getBegin(),
diag::err_typename_identifiers_only)
<< FixItHint::CreateRemoval(SourceRange(D.TypenameLoc));
D.TypenameLoc = SourceLocation();
}
Decl *UD = Actions.ActOnUsingDeclaration(getCurScope(), AS, UsingLoc,
D.TypenameLoc, D.SS, D.Name,
D.EllipsisLoc, Attrs);
if (UD)
DeclsInGroup.push_back(UD);
}
if (!TryConsumeToken(tok::comma))
break;
Attrs.clear();
InvalidDeclarator = ParseUsingDeclarator(Context, D);
}
if (DeclsInGroup.size() > 1)
Diag(Tok.getLocation(),
getLangOpts().CPlusPlus17
? diag::warn_cxx17_compat_multi_using_declaration
: diag::ext_multi_using_declaration);
DeclEnd = Tok.getLocation();
if (ExpectAndConsume(tok::semi, diag::err_expected_after,
!Attrs.empty() ? "attributes list"
: UELoc.isValid() ? "using-enum declaration"
: "using declaration"))
SkipUntil(tok::semi);
return Actions.BuildDeclaratorGroup(DeclsInGroup);
}
Decl *Parser::ParseAliasDeclarationAfterDeclarator(
const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc,
UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS,
ParsedAttributes &Attrs, Decl **OwnedType) {
if (ExpectAndConsume(tok::equal)) {
SkipUntil(tok::semi);
return nullptr;
}
Diag(Tok.getLocation(), getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_alias_declaration
: diag::ext_alias_declaration);
int SpecKind = -1;
if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
D.Name.getKind() == UnqualifiedIdKind::IK_TemplateId)
SpecKind = 0;
if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
SpecKind = 1;
if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
SpecKind = 2;
if (SpecKind != -1) {
SourceRange Range;
if (SpecKind == 0)
Range = SourceRange(D.Name.TemplateId->LAngleLoc,
D.Name.TemplateId->RAngleLoc);
else
Range = TemplateInfo.getSourceRange();
Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
<< SpecKind << Range;
SkipUntil(tok::semi);
return nullptr;
}
if (D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) {
Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier);
SkipUntil(tok::semi);
return nullptr;
} else if (D.TypenameLoc.isValid())
Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier)
<< FixItHint::CreateRemoval(
SourceRange(D.TypenameLoc, D.SS.isNotEmpty() ? D.SS.getEndLoc()
: D.TypenameLoc));
else if (D.SS.isNotEmpty())
Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
<< FixItHint::CreateRemoval(D.SS.getRange());
if (D.EllipsisLoc.isValid())
Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion)
<< FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc));
Decl *DeclFromDeclSpec = nullptr;
TypeResult TypeAlias =
ParseTypeName(nullptr,
TemplateInfo.Kind ? DeclaratorContext::AliasTemplate
: DeclaratorContext::AliasDecl,
AS, &DeclFromDeclSpec, &Attrs);
if (OwnedType)
*OwnedType = DeclFromDeclSpec;
DeclEnd = Tok.getLocation();
if (ExpectAndConsume(tok::semi, diag::err_expected_after,
!Attrs.empty() ? "attributes list"
: "alias declaration"))
SkipUntil(tok::semi);
TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
MultiTemplateParamsArg TemplateParamsArg(
TemplateParams ? TemplateParams->data() : nullptr,
TemplateParams ? TemplateParams->size() : 0);
return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
UsingLoc, D.Name, Attrs, TypeAlias,
DeclFromDeclSpec);
}
static FixItHint getStaticAssertNoMessageFixIt(const Expr *AssertExpr,
SourceLocation EndExprLoc) {
if (const auto *BO = dyn_cast_or_null<BinaryOperator>(AssertExpr)) {
if (BO->getOpcode() == BO_LAnd &&
isa<StringLiteral>(BO->getRHS()->IgnoreImpCasts()))
return FixItHint::CreateReplacement(BO->getOperatorLoc(), ",");
}
return FixItHint::CreateInsertion(EndExprLoc, ", \"\"");
}
Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd) {
assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
"Not a static_assert declaration");
Token SavedTok = Tok;
if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Diag(Tok, diag::ext_c11_feature) << Tok.getName();
if (Tok.is(tok::kw_static_assert)) {
if (!getLangOpts().CPlusPlus)
Diag(Tok, diag::ext_ms_static_assert)
<< FixItHint::CreateReplacement(Tok.getLocation(), "_Static_assert");
else
Diag(Tok, diag::warn_cxx98_compat_static_assert);
}
SourceLocation StaticAssertLoc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.consumeOpen()) {
Diag(Tok, diag::err_expected) << tok::l_paren;
SkipMalformedDecl();
return nullptr;
}
EnterExpressionEvaluationContext ConstantEvaluated(
Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext());
if (AssertExpr.isInvalid()) {
SkipMalformedDecl();
return nullptr;
}
ExprResult AssertMessage;
if (Tok.is(tok::r_paren)) {
unsigned DiagVal;
if (getLangOpts().CPlusPlus17)
DiagVal = diag::warn_cxx14_compat_static_assert_no_message;
else if (getLangOpts().CPlusPlus)
DiagVal = diag::ext_cxx_static_assert_no_message;
else if (getLangOpts().C2x)
DiagVal = diag::warn_c17_compat_static_assert_no_message;
else
DiagVal = diag::ext_c_static_assert_no_message;
Diag(Tok, DiagVal) << getStaticAssertNoMessageFixIt(AssertExpr.get(),
Tok.getLocation());
} else {
if (ExpectAndConsume(tok::comma)) {
SkipUntil(tok::semi);
return nullptr;
}
if (!isTokenStringLiteral()) {
Diag(Tok, diag::err_expected_string_literal)
<< 1;
SkipMalformedDecl();
return nullptr;
}
AssertMessage = ParseStringLiteralExpression();
if (AssertMessage.isInvalid()) {
SkipMalformedDecl();
return nullptr;
}
}
T.consumeClose();
DeclEnd = Tok.getLocation();
ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert,
SavedTok.getName());
return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, AssertExpr.get(),
AssertMessage.get(),
T.getCloseLocation());
}
SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype) &&
"Not a decltype specifier");
ExprResult Result;
SourceLocation StartLoc = Tok.getLocation();
SourceLocation EndLoc;
if (Tok.is(tok::annot_decltype)) {
Result = getExprAnnotation(Tok);
EndLoc = Tok.getAnnotationEndLoc();
DS.setTypeofParensRange(SourceRange(SourceLocation(), EndLoc));
ConsumeAnnotationToken();
if (Result.isInvalid()) {
DS.SetTypeSpecError();
return EndLoc;
}
} else {
if (Tok.getIdentifierInfo()->isStr("decltype"))
Diag(Tok, diag::warn_cxx98_compat_decltype);
ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, "decltype",
tok::r_paren)) {
DS.SetTypeSpecError();
return T.getOpenLocation() == Tok.getLocation() ? StartLoc
: T.getOpenLocation();
}
if (Tok.is(tok::kw_auto) && NextToken().is(tok::r_paren)) {
Diag(Tok.getLocation(),
getLangOpts().CPlusPlus14
? diag::warn_cxx11_compat_decltype_auto_type_specifier
: diag::ext_decltype_auto_type_specifier);
ConsumeToken();
} else {
EnterExpressionEvaluationContext Unevaluated(
Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr,
Sema::ExpressionEvaluationContextRecord::EK_Decltype);
Result = Actions.CorrectDelayedTyposInExpr(
ParseExpression(), nullptr,
false,
[](Expr *E) { return E->hasPlaceholderType() ? ExprError() : E; });
if (Result.isInvalid()) {
DS.SetTypeSpecError();
if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
EndLoc = ConsumeParen();
} else {
if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
PP.RevertCachedTokens(2);
ConsumeToken(); EndLoc = ConsumeAnyToken();
assert(Tok.is(tok::semi));
} else {
EndLoc = Tok.getLocation();
}
}
return EndLoc;
}
Result = Actions.ActOnDecltypeExpression(Result.get());
}
T.consumeClose();
DS.setTypeofParensRange(T.getRange());
if (T.getCloseLocation().isInvalid()) {
DS.SetTypeSpecError();
return T.getCloseLocation();
}
if (Result.isInvalid()) {
DS.SetTypeSpecError();
return T.getCloseLocation();
}
EndLoc = T.getCloseLocation();
}
assert(!Result.isInvalid());
const char *PrevSpec = nullptr;
unsigned DiagID;
const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
if (Result.get() ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc,
PrevSpec, DiagID, Result.get(), Policy)
: DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc,
PrevSpec, DiagID, Policy)) {
Diag(StartLoc, DiagID) << PrevSpec;
DS.SetTypeSpecError();
}
return EndLoc;
}
void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec &DS,
SourceLocation StartLoc,
SourceLocation EndLoc) {
if (PP.isBacktrackEnabled()) {
PP.RevertCachedTokens(1);
if (DS.getTypeSpecType() == TST_error) {
EndLoc = PP.getLastCachedTokenLocation();
}
} else
PP.EnterToken(Tok, true);
Tok.setKind(tok::annot_decltype);
setExprAnnotation(Tok,
DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr()
: DS.getTypeSpecType() == TST_decltype_auto ? ExprResult()
: ExprError());
Tok.setAnnotationEndLoc(EndLoc);
Tok.setLocation(StartLoc);
PP.AnnotateCachedTokens(Tok);
}
void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
assert(Tok.is(tok::kw___underlying_type) &&
"Not an underlying type specifier");
SourceLocation StartLoc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, "__underlying_type",
tok::r_paren)) {
return;
}
TypeResult Result = ParseTypeName();
if (Result.isInvalid()) {
SkipUntil(tok::r_paren, StopAtSemi);
return;
}
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return;
const char *PrevSpec = nullptr;
unsigned DiagID;
if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
DiagID, Result.get(),
Actions.getASTContext().getPrintingPolicy()))
Diag(StartLoc, DiagID) << PrevSpec;
DS.setTypeofParensRange(T.getRange());
}
TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
SourceLocation &EndLocation) {
if (Tok.is(tok::kw_typename)) {
Diag(Tok, diag::err_expected_class_name_not_template)
<< FixItHint::CreateRemoval(Tok.getLocation());
ConsumeToken();
}
CXXScopeSpec SS;
if (ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false))
return true;
BaseLoc = Tok.getLocation();
if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
if (SS.isNotEmpty())
Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
<< FixItHint::CreateRemoval(SS.getRange());
DeclSpec DS(AttrFactory);
EndLocation = ParseDecltypeSpecifier(DS);
Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
DeclaratorContext::TypeName);
return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
}
if (Tok.is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
if (TemplateId->mightBeType()) {
AnnotateTemplateIdTokenAsType(SS, true);
assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
TypeResult Type = getTypeAnnotation(Tok);
EndLocation = Tok.getAnnotationEndLoc();
ConsumeAnnotationToken();
return Type;
}
}
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_class_name);
return true;
}
IdentifierInfo *Id = Tok.getIdentifierInfo();
SourceLocation IdLoc = ConsumeToken();
if (Tok.is(tok::less)) {
TemplateNameKind TNK = TNK_Non_template;
TemplateTy Template;
if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), &SS,
Template, TNK)) {
Diag(IdLoc, diag::err_unknown_template_name) << Id;
}
UnqualifiedId TemplateName;
TemplateName.setIdentifier(Id, IdLoc);
if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
TemplateName))
return true;
if (Tok.is(tok::annot_template_id) &&
takeTemplateIdAnnotation(Tok)->mightBeType())
AnnotateTemplateIdTokenAsType(SS, true);
if (Tok.isNot(tok::annot_typename))
return true;
EndLocation = Tok.getAnnotationEndLoc();
TypeResult Type = getTypeAnnotation(Tok);
ConsumeAnnotationToken();
return Type;
}
IdentifierInfo *CorrectedII = nullptr;
ParsedType Type = Actions.getTypeName(
*Id, IdLoc, getCurScope(), &SS, true, false, nullptr,
false,
true,
false, &CorrectedII);
if (!Type) {
Diag(IdLoc, diag::err_expected_class_name);
return true;
}
EndLocation = IdLoc;
DeclSpec DS(AttrFactory);
DS.SetRangeStart(IdLoc);
DS.SetRangeEnd(EndLocation);
DS.getTypeSpecScope() = SS;
const char *PrevSpec = nullptr;
unsigned DiagID;
DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
Actions.getASTContext().getPrintingPolicy());
Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
DeclaratorContext::TypeName);
return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
}
void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
while (Tok.isOneOf(tok::kw___single_inheritance,
tok::kw___multiple_inheritance,
tok::kw___virtual_inheritance)) {
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
SourceLocation AttrNameLoc = ConsumeToken();
attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
ParsedAttr::AS_Keyword);
}
}
bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
switch (Tok.getKind()) {
default:
break;
case tok::semi: case tok::star: case tok::amp: case tok::ampamp: case tok::identifier: case tok::r_paren: case tok::coloncolon: case tok::annot_cxxscope: case tok::annot_typename: case tok::annot_template_id: case tok::kw_decltype: case tok::l_paren: case tok::comma: case tok::kw_operator: case tok::kw___declspec: case tok::l_square: case tok::ellipsis: case tok::kw___attribute: case tok::annot_pragma_pack: case tok::annot_pragma_ms_pragma:
case tok::annot_pragma_ms_vtordisp:
case tok::annot_pragma_ms_pointers_to_members:
return true;
case tok::colon:
return CouldBeBitfield || ColonIsSacred; case tok::kw___cdecl: case tok::kw___fastcall: case tok::kw___stdcall: case tok::kw___thiscall: case tok::kw___vectorcall: return getLangOpts().MicrosoftExt;
case tok::kw_const: case tok::kw_volatile: case tok::kw_restrict: case tok::kw__Atomic: case tok::kw___unaligned: case tok::kw_inline: case tok::kw_virtual: case tok::kw_friend: case tok::kw_static: case tok::kw_extern: case tok::kw_typedef: case tok::kw_register: case tok::kw_auto: case tok::kw_mutable: case tok::kw_thread_local: case tok::kw_constexpr: case tok::kw_consteval: case tok::kw_constinit: if (!isKnownToBeTypeSpecifier(NextToken()))
return true;
break;
case tok::r_brace: if (!getLangOpts().CPlusPlus)
return true;
break;
case tok::greater:
return getLangOpts().CPlusPlus;
}
return false;
}
void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
SourceLocation StartLoc, DeclSpec &DS,
const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS, bool EnteringContext,
DeclSpecContext DSC,
ParsedAttributes &Attributes) {
DeclSpec::TST TagType;
if (TagTokKind == tok::kw_struct)
TagType = DeclSpec::TST_struct;
else if (TagTokKind == tok::kw___interface)
TagType = DeclSpec::TST_interface;
else if (TagTokKind == tok::kw_class)
TagType = DeclSpec::TST_class;
else {
assert(TagTokKind == tok::kw_union && "Not a class specifier");
TagType = DeclSpec::TST_union;
}
if (Tok.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteTag(getCurScope(), TagType);
return;
}
const bool shouldDelayDiagsInTag =
(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate);
SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
ParsedAttributes attrs(AttrFactory);
MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs);
if (Tok.isOneOf(tok::kw___single_inheritance, tok::kw___multiple_inheritance,
tok::kw___virtual_inheritance))
ParseMicrosoftInheritanceClassAttributes(attrs);
MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs);
SourceLocation AttrFixitLoc = Tok.getLocation();
if (TagType == DeclSpec::TST_struct && Tok.isNot(tok::identifier) &&
!Tok.isAnnotation() && Tok.getIdentifierInfo() &&
Tok.isOneOf(
tok::kw___is_abstract, tok::kw___is_aggregate,
tok::kw___is_arithmetic, tok::kw___is_array, tok::kw___is_assignable,
tok::kw___is_base_of, tok::kw___is_class, tok::kw___is_complete_type,
tok::kw___is_compound, tok::kw___is_const, tok::kw___is_constructible,
tok::kw___is_convertible, tok::kw___is_convertible_to,
tok::kw___is_destructible, tok::kw___is_empty, tok::kw___is_enum,
tok::kw___is_floating_point, tok::kw___is_final,
tok::kw___is_function, tok::kw___is_fundamental,
tok::kw___is_integral, tok::kw___is_interface_class,
tok::kw___is_literal, tok::kw___is_lvalue_expr,
tok::kw___is_lvalue_reference, tok::kw___is_member_function_pointer,
tok::kw___is_member_object_pointer, tok::kw___is_member_pointer,
tok::kw___is_nothrow_assignable, tok::kw___is_nothrow_constructible,
tok::kw___is_nothrow_destructible, tok::kw___is_object,
tok::kw___is_pod, tok::kw___is_pointer, tok::kw___is_polymorphic,
tok::kw___is_reference, tok::kw___is_rvalue_expr,
tok::kw___is_rvalue_reference, tok::kw___is_same, tok::kw___is_scalar,
tok::kw___is_sealed, tok::kw___is_signed,
tok::kw___is_standard_layout, tok::kw___is_trivial,
tok::kw___is_trivially_assignable,
tok::kw___is_trivially_constructible, tok::kw___is_trivially_copyable,
tok::kw___is_union, tok::kw___is_unsigned, tok::kw___is_void,
tok::kw___is_volatile))
TryKeywordIdentFallback(true);
struct PreserveAtomicIdentifierInfoRAII {
PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled)
: AtomicII(nullptr) {
if (!Enabled)
return;
assert(Tok.is(tok::kw__Atomic));
AtomicII = Tok.getIdentifierInfo();
AtomicII->revertTokenIDToIdentifier();
Tok.setKind(tok::identifier);
}
~PreserveAtomicIdentifierInfoRAII() {
if (!AtomicII)
return;
AtomicII->revertIdentifierToTokenID(tok::kw__Atomic);
}
IdentifierInfo *AtomicII;
};
bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat &&
Tok.is(tok::kw__Atomic) &&
TagType == DeclSpec::TST_struct;
PreserveAtomicIdentifierInfoRAII AtomicTokenGuard(
Tok, ShouldChangeAtomicToIdentifier);
CXXScopeSpec &SS = DS.getTypeSpecScope();
if (getLangOpts().CPlusPlus) {
ColonProtectionRAIIObject X(*this);
CXXScopeSpec Spec;
bool HasValidSpec = true;
if (ParseOptionalCXXScopeSpecifier(Spec, nullptr,
false,
EnteringContext)) {
DS.SetTypeSpecError();
HasValidSpec = false;
}
if (Spec.isSet())
if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
Diag(Tok, diag::err_expected) << tok::identifier;
HasValidSpec = false;
}
if (HasValidSpec)
SS = Spec;
}
TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
auto RecoverFromUndeclaredTemplateName = [&](IdentifierInfo *Name,
SourceLocation NameLoc,
SourceRange TemplateArgRange,
bool KnownUndeclared) {
Diag(NameLoc, diag::err_explicit_spec_non_template)
<< (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
<< TagTokKind << Name << TemplateArgRange << KnownUndeclared;
if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
if (TemplateParams->size() > 1) {
TemplateParams->pop_back();
} else {
TemplateParams = nullptr;
const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind =
ParsedTemplateInfo::NonTemplate;
}
} else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
TemplateParams = nullptr;
const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind =
ParsedTemplateInfo::NonTemplate;
const_cast<ParsedTemplateInfo &>(TemplateInfo).TemplateLoc =
SourceLocation();
const_cast<ParsedTemplateInfo &>(TemplateInfo).ExternLoc =
SourceLocation();
}
};
IdentifierInfo *Name = nullptr;
SourceLocation NameLoc;
TemplateIdAnnotation *TemplateId = nullptr;
if (Tok.is(tok::identifier)) {
Name = Tok.getIdentifierInfo();
NameLoc = ConsumeToken();
if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
TemplateArgList TemplateArgs;
SourceLocation LAngleLoc, RAngleLoc;
if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs,
RAngleLoc)) {
LAngleLoc = RAngleLoc = SourceLocation();
}
RecoverFromUndeclaredTemplateName(
Name, NameLoc, SourceRange(LAngleLoc, RAngleLoc), false);
}
} else if (Tok.is(tok::annot_template_id)) {
TemplateId = takeTemplateIdAnnotation(Tok);
NameLoc = ConsumeAnnotationToken();
if (TemplateId->Kind == TNK_Undeclared_template) {
Actions.ActOnUndeclaredTypeTemplateName(
getCurScope(), TemplateId->Template, TemplateId->Kind, NameLoc, Name);
if (TemplateId->Kind == TNK_Undeclared_template) {
RecoverFromUndeclaredTemplateName(
Name, NameLoc,
SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc), true);
TemplateId = nullptr;
}
}
if (TemplateId && !TemplateId->mightBeType()) {
SourceRange Range(NameLoc);
if (SS.isNotEmpty())
Range.setBegin(SS.getBeginLoc());
Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
<< TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
DS.SetTypeSpecError();
SkipUntil(tok::semi, StopBeforeMatch);
return;
}
}
MaybeParseCXX11Attributes(Attributes);
const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Sema::TagUseKind TUK;
if (isDefiningTypeSpecifierContext(DSC, getLangOpts().CPlusPlus) ==
AllowDefiningTypeSpec::No ||
(getLangOpts().OpenMP && OpenMPDirectiveParsing))
TUK = Sema::TUK_Reference;
else if (Tok.is(tok::l_brace) ||
(DSC != DeclSpecContext::DSC_association &&
getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
(isClassCompatibleKeyword() &&
(NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
if (DS.isFriendSpecified()) {
Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
<< SourceRange(DS.getFriendSpecLoc());
SkipUntil(tok::semi, StopBeforeMatch);
TUK = Sema::TUK_Friend;
} else {
TUK = Sema::TUK_Definition;
}
} else if (isClassCompatibleKeyword() &&
(NextToken().is(tok::l_square) ||
NextToken().is(tok::kw_alignas) ||
isCXX11VirtSpecifier(NextToken()) != VirtSpecifiers::VS_None)) {
TentativeParsingAction PA(*this);
while (isClassCompatibleKeyword()) {
ConsumeToken();
}
while (true) {
if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
ConsumeBracket();
if (!SkipUntil(tok::r_square, StopAtSemi))
break;
} else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
ConsumeToken();
ConsumeParen();
if (!SkipUntil(tok::r_paren, StopAtSemi))
break;
} else {
break;
}
}
if (Tok.isOneOf(tok::l_brace, tok::colon))
TUK = Sema::TUK_Definition;
else
TUK = Sema::TUK_Reference;
PA.Revert();
} else if (!isTypeSpecifier(DSC) &&
(Tok.is(tok::semi) ||
(Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
if (Tok.isNot(tok::semi)) {
const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
ExpectAndConsume(tok::semi, diag::err_expected_after,
DeclSpec::getSpecifierName(TagType, PPol));
PP.EnterToken(Tok, true);
Tok.setKind(tok::semi);
}
} else
TUK = Sema::TUK_Reference;
if (TUK != Sema::TUK_Reference) {
SourceRange AttrRange = Attributes.Range;
if (AttrRange.isValid()) {
Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
<< AttrRange
<< FixItHint::CreateInsertionFromRange(
AttrFixitLoc, CharSourceRange(AttrRange, true))
<< FixItHint::CreateRemoval(AttrRange);
attrs.takeAllFrom(Attributes);
}
}
if (!Name && !TemplateId &&
(DS.getTypeSpecType() == DeclSpec::TST_error ||
TUK != Sema::TUK_Definition)) {
if (DS.getTypeSpecType() != DeclSpec::TST_error) {
Diag(StartLoc, diag::err_anon_type_definition)
<< DeclSpec::getSpecifierName(TagType, Policy);
}
if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
SkipUntil(tok::semi, StopBeforeMatch);
else
SkipUntil(tok::comma, StopAtSemi);
return;
}
DeclResult TagOrTempResult = true; TypeResult TypeResult = true;
bool Owned = false;
Sema::SkipBodyInfo SkipBody;
if (TemplateId) {
ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
TemplateId->NumArgs);
if (TemplateId->isInvalid()) {
} else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
TUK == Sema::TUK_Declaration) {
ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
true);
TagOrTempResult = Actions.ActOnExplicitInstantiation(
getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
TagType, StartLoc, SS, TemplateId->Template,
TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, TemplateArgsPtr,
TemplateId->RAngleLoc, attrs);
} else if (TUK == Sema::TUK_Reference ||
(TUK == Sema::TUK_Friend &&
TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
true);
TypeResult = Actions.ActOnTagTemplateIdType(
TUK, TagType, StartLoc, SS, TemplateId->TemplateKWLoc,
TemplateId->Template, TemplateId->TemplateNameLoc,
TemplateId->LAngleLoc, TemplateArgsPtr, TemplateId->RAngleLoc);
} else {
TemplateParameterLists FakedParamLists;
if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
"Expected a definition here");
if (TUK == Sema::TUK_Friend) {
Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
TemplateParams = nullptr;
} else {
SourceLocation LAngleLoc =
PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Diag(TemplateId->TemplateNameLoc,
diag::err_explicit_instantiation_with_definition)
<< SourceRange(TemplateInfo.TemplateLoc)
<< FixItHint::CreateInsertion(LAngleLoc, "<>");
FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
LAngleLoc, nullptr));
TemplateParams = &FakedParamLists;
}
}
TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
SS, *TemplateId, attrs,
MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
: nullptr,
TemplateParams ? TemplateParams->size() : 0),
&SkipBody);
}
} else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
TUK == Sema::TUK_Declaration) {
ProhibitAttributes(attrs);
TagOrTempResult = Actions.ActOnExplicitInstantiation(
getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
TagType, StartLoc, SS, Name, NameLoc, attrs);
} else if (TUK == Sema::TUK_Friend &&
TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
true);
TagOrTempResult = Actions.ActOnTemplatedFriendTag(
getCurScope(), DS.getFriendSpecLoc(), TagType, StartLoc, SS, Name,
NameLoc, attrs,
MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] : nullptr,
TemplateParams ? TemplateParams->size() : 0));
} else {
if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
true);
if (TUK == Sema::TUK_Definition &&
TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
Diag(Tok, diag::err_template_defn_explicit_instantiation)
<< 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
TemplateParams = nullptr;
}
bool IsDependent = false;
MultiTemplateParamsArg TParams;
if (TUK != Sema::TUK_Reference && TemplateParams)
TParams =
MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
TagOrTempResult = Actions.ActOnTag(
getCurScope(), TagType, TUK, StartLoc, SS, Name, NameLoc, attrs, AS,
DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent,
SourceLocation(), false, clang::TypeResult(),
DSC == DeclSpecContext::DSC_type_specifier,
DSC == DeclSpecContext::DSC_template_param ||
DSC == DeclSpecContext::DSC_template_type_arg,
&SkipBody);
if (IsDependent) {
assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, SS,
Name, StartLoc, NameLoc);
}
}
if (shouldDelayDiagsInTag) {
diagsFromTag.done();
if (TUK == Sema::TUK_Reference &&
TemplateInfo.Kind == ParsedTemplateInfo::Template)
diagsFromTag.redelay();
}
if (TUK == Sema::TUK_Definition) {
assert(Tok.is(tok::l_brace) ||
(getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
isClassCompatibleKeyword());
if (SkipBody.ShouldSkip)
SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
TagOrTempResult.get());
else if (getLangOpts().CPlusPlus)
ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
TagOrTempResult.get());
else {
Decl *D =
SkipBody.CheckSameAsPrevious ? SkipBody.New : TagOrTempResult.get();
ParseStructUnionBody(StartLoc, TagType, cast<RecordDecl>(D));
if (SkipBody.CheckSameAsPrevious &&
!Actions.ActOnDuplicateDefinition(TagOrTempResult.get(), SkipBody)) {
DS.SetTypeSpecError();
return;
}
}
}
if (!TagOrTempResult.isInvalid())
Actions.ProcessDeclAttributeDelayed(TagOrTempResult.get(), attrs);
const char *PrevSpec = nullptr;
unsigned DiagID;
bool Result;
if (!TypeResult.isInvalid()) {
Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
NameLoc.isValid() ? NameLoc : StartLoc,
PrevSpec, DiagID, TypeResult.get(), Policy);
} else if (!TagOrTempResult.isInvalid()) {
Result = DS.SetTypeSpecType(
TagType, StartLoc, NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec,
DiagID, TagOrTempResult.get(), Owned, Policy);
} else {
DS.SetTypeSpecError();
return;
}
if (Result)
Diag(StartLoc, DiagID) << PrevSpec;
if (TUK == Sema::TUK_Definition &&
(getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
(TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
if (Tok.isNot(tok::semi)) {
const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
ExpectAndConsume(tok::semi, diag::err_expected_after,
DeclSpec::getSpecifierName(TagType, PPol));
PP.EnterToken(Tok, true);
Tok.setKind(tok::semi);
}
}
}
void Parser::ParseBaseClause(Decl *ClassDecl) {
assert(Tok.is(tok::colon) && "Not a base clause");
ConsumeToken();
SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
while (true) {
BaseResult Result = ParseBaseSpecifier(ClassDecl);
if (Result.isInvalid()) {
SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
} else {
BaseInfo.push_back(Result.get());
}
if (!TryConsumeToken(tok::comma))
break;
}
Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo);
}
BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
bool IsVirtual = false;
SourceLocation StartLoc = Tok.getLocation();
ParsedAttributes Attributes(AttrFactory);
MaybeParseCXX11Attributes(Attributes);
if (TryConsumeToken(tok::kw_virtual))
IsVirtual = true;
CheckMisplacedCXX11Attribute(Attributes, StartLoc);
AccessSpecifier Access = getAccessSpecifierIfPresent();
if (Access != AS_none) {
ConsumeToken();
if (getLangOpts().HLSL)
Diag(Tok.getLocation(), diag::ext_hlsl_access_specifiers);
}
CheckMisplacedCXX11Attribute(Attributes, StartLoc);
if (Tok.is(tok::kw_virtual)) {
SourceLocation VirtualLoc = ConsumeToken();
if (IsVirtual) {
Diag(VirtualLoc, diag::err_dup_virtual)
<< FixItHint::CreateRemoval(VirtualLoc);
}
IsVirtual = true;
}
CheckMisplacedCXX11Attribute(Attributes, StartLoc);
if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
NextToken().is(tok::less))
Tok.setKind(tok::identifier);
SourceLocation EndLocation;
SourceLocation BaseLoc;
TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
if (BaseType.isInvalid())
return true;
SourceLocation EllipsisLoc;
TryConsumeToken(tok::ellipsis, EllipsisLoc);
SourceRange Range(StartLoc, EndLocation);
return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
Access, BaseType.get(), BaseLoc,
EllipsisLoc);
}
AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
switch (Tok.getKind()) {
default:
return AS_none;
case tok::kw_private:
return AS_private;
case tok::kw_protected:
return AS_protected;
case tok::kw_public:
return AS_public;
}
}
void Parser::HandleMemberFunctionDeclDelays(Declarator &DeclaratorInfo,
Decl *ThisDecl) {
DeclaratorChunk::FunctionTypeInfo &FTI = DeclaratorInfo.getFunctionTypeInfo();
bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
if (!NeedLateParse) {
for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
if (Param->hasUnparsedDefaultArg()) {
NeedLateParse = true;
break;
}
}
}
if (NeedLateParse) {
auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
LateMethod->DefaultArgs.reserve(FTI.NumParams);
for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
FTI.Params[ParamIdx].Param,
std::move(FTI.Params[ParamIdx].DefaultArgTokens)));
if (FTI.getExceptionSpecType() == EST_Unparsed) {
LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
FTI.ExceptionSpecTokens = nullptr;
}
}
}
VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
return VirtSpecifiers::VS_None;
IdentifierInfo *II = Tok.getIdentifierInfo();
if (!Ident_final) {
Ident_final = &PP.getIdentifierTable().get("final");
if (getLangOpts().GNUKeywords)
Ident_GNU_final = &PP.getIdentifierTable().get("__final");
if (getLangOpts().MicrosoftExt) {
Ident_sealed = &PP.getIdentifierTable().get("sealed");
Ident_abstract = &PP.getIdentifierTable().get("abstract");
}
Ident_override = &PP.getIdentifierTable().get("override");
}
if (II == Ident_override)
return VirtSpecifiers::VS_Override;
if (II == Ident_sealed)
return VirtSpecifiers::VS_Sealed;
if (II == Ident_abstract)
return VirtSpecifiers::VS_Abstract;
if (II == Ident_final)
return VirtSpecifiers::VS_Final;
if (II == Ident_GNU_final)
return VirtSpecifiers::VS_GNU_Final;
return VirtSpecifiers::VS_None;
}
void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
bool IsInterface,
SourceLocation FriendLoc) {
while (true) {
VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
if (Specifier == VirtSpecifiers::VS_None)
return;
if (FriendLoc.isValid()) {
Diag(Tok.getLocation(), diag::err_friend_decl_spec)
<< VirtSpecifiers::getSpecifierName(Specifier)
<< FixItHint::CreateRemoval(Tok.getLocation())
<< SourceRange(FriendLoc, FriendLoc);
ConsumeToken();
continue;
}
const char *PrevSpec = nullptr;
if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
<< PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
Specifier == VirtSpecifiers::VS_Sealed)) {
Diag(Tok.getLocation(), diag::err_override_control_interface)
<< VirtSpecifiers::getSpecifierName(Specifier);
} else if (Specifier == VirtSpecifiers::VS_Sealed) {
Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
} else if (Specifier == VirtSpecifiers::VS_Abstract) {
Diag(Tok.getLocation(), diag::ext_ms_abstract_keyword);
} else if (Specifier == VirtSpecifiers::VS_GNU_Final) {
Diag(Tok.getLocation(), diag::ext_warn_gnu_final);
} else {
Diag(Tok.getLocation(),
getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_override_control_keyword
: diag::ext_override_control_keyword)
<< VirtSpecifiers::getSpecifierName(Specifier);
}
ConsumeToken();
}
}
bool Parser::isCXX11FinalKeyword() const {
VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
return Specifier == VirtSpecifiers::VS_Final ||
Specifier == VirtSpecifiers::VS_GNU_Final ||
Specifier == VirtSpecifiers::VS_Sealed;
}
bool Parser::isClassCompatibleKeyword() const {
VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
return Specifier == VirtSpecifiers::VS_Final ||
Specifier == VirtSpecifiers::VS_GNU_Final ||
Specifier == VirtSpecifiers::VS_Sealed ||
Specifier == VirtSpecifiers::VS_Abstract;
}
bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
LateParsedAttrList &LateParsedAttrs) {
if (Tok.isNot(tok::colon))
ParseDeclarator(DeclaratorInfo);
else
DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
assert(DeclaratorInfo.isPastIdentifier() &&
"don't know where identifier would go yet?");
BitfieldSize = ParseConstantExpression();
if (BitfieldSize.isInvalid())
SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
} else if (Tok.is(tok::kw_requires)) {
ParseTrailingRequiresClause(DeclaratorInfo);
} else {
ParseOptionalCXX11VirtSpecifierSeq(
VS, getCurrentClass().IsInterface,
DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
if (!VS.isUnset())
MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo,
VS);
}
if (Tok.is(tok::kw_asm)) {
SourceLocation Loc;
ExprResult AsmLabel(ParseSimpleAsm( true, &Loc));
if (AsmLabel.isInvalid())
SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
DeclaratorInfo.setAsmLabel(AsmLabel.get());
DeclaratorInfo.SetRangeEnd(Loc);
}
DiagnoseAndSkipCXX11Attributes();
MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
DiagnoseAndSkipCXX11Attributes();
if (BitfieldSize.isUnset() && VS.isUnset()) {
ParseOptionalCXX11VirtSpecifierSeq(
VS, getCurrentClass().IsInterface,
DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
if (!VS.isUnset()) {
for (const ParsedAttr &AL : DeclaratorInfo.getAttributes())
if (AL.isKnownToGCC() && !AL.isCXX11Attribute())
Diag(AL.getLoc(), diag::warn_gcc_attribute_location);
MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo,
VS);
}
}
if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
return true;
}
return false;
}
void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
Declarator &D, VirtSpecifiers &VS) {
DeclSpec DS(AttrFactory);
ParseTypeQualifierListOpt(
DS, AR_NoAttributesParsed, false,
false, llvm::function_ref<void()>([&]() {
Actions.CodeCompleteFunctionQualifiers(DS, D, &VS);
}));
D.ExtendWithDeclSpec(DS);
if (D.isFunctionDeclarator()) {
auto &Function = D.getFunctionTypeInfo();
if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
auto DeclSpecCheck = [&](DeclSpec::TQ TypeQual, StringRef FixItName,
SourceLocation SpecLoc) {
FixItHint Insertion;
auto &MQ = Function.getOrCreateMethodQualifiers();
if (!(MQ.getTypeQualifiers() & TypeQual)) {
std::string Name(FixItName.data());
Name += " ";
Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
MQ.SetTypeQual(TypeQual, SpecLoc);
}
Diag(SpecLoc, diag::err_declspec_after_virtspec)
<< FixItName
<< VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
<< FixItHint::CreateRemoval(SpecLoc) << Insertion;
};
DS.forEachQualifier(DeclSpecCheck);
}
bool RefQualifierIsLValueRef = true;
SourceLocation RefQualifierLoc;
if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
FixItHint Insertion =
FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
Function.RefQualifierLoc = RefQualifierLoc;
Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
<< (RefQualifierIsLValueRef ? "&" : "&&")
<< VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
<< FixItHint::CreateRemoval(RefQualifierLoc) << Insertion;
D.SetRangeEnd(RefQualifierLoc);
}
}
}
Parser::DeclGroupPtrTy
Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
ParsedAttributes &AccessAttrs,
const ParsedTemplateInfo &TemplateInfo,
ParsingDeclRAIIObject *TemplateDiags) {
if (Tok.is(tok::at)) {
if (getLangOpts().ObjC && NextToken().isObjCAtKeyword(tok::objc_defs))
Diag(Tok, diag::err_at_defs_cxx);
else
Diag(Tok, diag::err_at_in_class);
ConsumeToken();
SkipUntil(tok::r_brace, StopAtSemi);
return nullptr;
}
ColonProtectionRAIIObject X(*this);
bool MalformedTypeSpec = false;
if (!TemplateInfo.Kind &&
Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
if (TryAnnotateCXXScopeToken())
MalformedTypeSpec = true;
bool isAccessDecl;
if (Tok.isNot(tok::annot_cxxscope))
isAccessDecl = false;
else if (NextToken().is(tok::identifier))
isAccessDecl = GetLookAheadToken(2).is(tok::semi);
else
isAccessDecl = NextToken().is(tok::kw_operator);
if (isAccessDecl) {
CXXScopeSpec SS;
ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false);
if (SS.isInvalid()) {
SkipUntil(tok::semi);
return nullptr;
}
SourceLocation TemplateKWLoc;
UnqualifiedId Name;
if (ParseUnqualifiedId(SS, nullptr,
false, false, true, true,
false, &TemplateKWLoc, Name)) {
SkipUntil(tok::semi);
return nullptr;
}
if (ExpectAndConsume(tok::semi, diag::err_expected_after,
"access declaration")) {
SkipUntil(tok::semi);
return nullptr;
}
return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration(
getCurScope(), AS, SourceLocation(),
SourceLocation(), SS, Name,
SourceLocation(),
ParsedAttributesView())));
}
}
if (!TemplateInfo.Kind &&
Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
SourceLocation DeclEnd;
return DeclGroupPtrTy::make(
DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd)));
}
if (Tok.is(tok::kw_template)) {
assert(!TemplateInfo.TemplateParams &&
"Nested template improperly parsed?");
ObjCDeclContextSwitch ObjCDC(*this);
SourceLocation DeclEnd;
return DeclGroupPtrTy::make(
DeclGroupRef(ParseTemplateDeclarationOrSpecialization(
DeclaratorContext::Member, DeclEnd, AccessAttrs, AS)));
}
if (Tok.is(tok::kw___extension__)) {
ExtensionRAIIObject O(Diags); ConsumeToken();
return ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
TemplateDiags);
}
ParsedAttributes DeclAttrs(AttrFactory);
MaybeParseCXX11Attributes(DeclAttrs);
if (Tok.is(tok::annot_attr_openmp))
return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, DeclAttrs);
if (Tok.is(tok::kw_using)) {
SourceLocation UsingLoc = ConsumeToken();
while (Tok.is(tok::kw_template)) {
SourceLocation TemplateLoc = ConsumeToken();
Diag(TemplateLoc, diag::err_unexpected_template_after_using)
<< FixItHint::CreateRemoval(TemplateLoc);
}
if (Tok.is(tok::kw_namespace)) {
Diag(UsingLoc, diag::err_using_namespace_in_class);
SkipUntil(tok::semi, StopBeforeMatch);
return nullptr;
}
SourceLocation DeclEnd;
return ParseUsingDeclaration(DeclaratorContext::Member, TemplateInfo,
UsingLoc, DeclEnd, DeclAttrs, AS);
}
ParsedAttributes DeclSpecAttrs(AttrFactory);
MaybeParseMicrosoftAttributes(DeclSpecAttrs);
LateParsedAttrList CommonLateParsedAttrs;
ParsingDeclSpec DS(*this, TemplateDiags);
DS.takeAttributesFrom(DeclSpecAttrs);
if (MalformedTypeSpec)
DS.SetTypeSpecError();
bool IsTemplateSpecOrInst =
(TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
SuppressAccessChecks diagsFromTag(*this, IsTemplateSpecOrInst);
ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DeclSpecContext::DSC_class,
&CommonLateParsedAttrs);
if (IsTemplateSpecOrInst)
diagsFromTag.done();
X.restore();
if (DS.hasTagDefinition() &&
TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
DiagnoseMissingSemiAfterTagDefinition(DS, AS, DeclSpecContext::DSC_class,
&CommonLateParsedAttrs))
return nullptr;
MultiTemplateParamsArg TemplateParams(
TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
: nullptr,
TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
if (TryConsumeToken(tok::semi)) {
if (DS.isFriendSpecified())
ProhibitAttributes(DeclAttrs);
RecordDecl *AnonRecord = nullptr;
Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
getCurScope(), AS, DS, DeclAttrs, TemplateParams, false, AnonRecord);
DS.complete(TheDecl);
if (AnonRecord) {
Decl *decls[] = {AnonRecord, TheDecl};
return Actions.BuildDeclaratorGroup(decls);
}
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
ParsingDeclarator DeclaratorInfo(*this, DS, DeclAttrs,
DeclaratorContext::Member);
if (TemplateInfo.TemplateParams)
DeclaratorInfo.setTemplateParameterLists(TemplateParams);
VirtSpecifiers VS;
LateParsedAttrList LateParsedAttrs;
SourceLocation EqualLoc;
SourceLocation PureSpecLoc;
auto TryConsumePureSpecifier = [&](bool AllowDefinition) {
if (Tok.isNot(tok::equal))
return false;
auto &Zero = NextToken();
SmallString<8> Buffer;
if (Zero.isNot(tok::numeric_constant) ||
PP.getSpelling(Zero, Buffer) != "0")
return false;
auto &After = GetLookAheadToken(2);
if (!After.isOneOf(tok::semi, tok::comma) &&
!(AllowDefinition &&
After.isOneOf(tok::l_brace, tok::colon, tok::kw_try)))
return false;
EqualLoc = ConsumeToken();
PureSpecLoc = ConsumeToken();
return true;
};
SmallVector<Decl *, 8> DeclsInGroup;
ExprResult BitfieldSize;
ExprResult TrailingRequiresClause;
bool ExpectSemi = true;
SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
if (ParseCXXMemberDeclaratorBeforeInitializer(
DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
TryConsumeToken(tok::semi);
return nullptr;
}
if (IsTemplateSpecOrInst)
SAC.done();
if (BitfieldSize.isUnset()) {
if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction())
TryConsumePureSpecifier( true);
FunctionDefinitionKind DefinitionKind = FunctionDefinitionKind::Declaration;
if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
DefinitionKind = FunctionDefinitionKind::Definition;
} else if (DeclaratorInfo.isFunctionDeclarator()) {
if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) {
DefinitionKind = FunctionDefinitionKind::Definition;
} else if (Tok.is(tok::equal)) {
const Token &KW = NextToken();
if (KW.is(tok::kw_default))
DefinitionKind = FunctionDefinitionKind::Defaulted;
else if (KW.is(tok::kw_delete))
DefinitionKind = FunctionDefinitionKind::Deleted;
else if (KW.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteAfterFunctionEquals(DeclaratorInfo);
return nullptr;
}
}
}
DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
if (DeclaratorInfo.isFunctionDeclarator() &&
DefinitionKind == FunctionDefinitionKind::Declaration &&
DS.isFriendSpecified()) {
ProhibitAttributes(DeclAttrs);
}
if (DefinitionKind != FunctionDefinitionKind::Declaration) {
if (!DeclaratorInfo.isFunctionDeclarator()) {
Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
ConsumeBrace();
SkipUntil(tok::r_brace);
TryConsumeToken(tok::semi);
return nullptr;
}
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Diag(DeclaratorInfo.getIdentifierLoc(),
diag::err_function_declared_typedef);
DS.ClearStorageClassSpecs();
}
Decl *FunDecl = ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo,
TemplateInfo, VS, PureSpecLoc);
if (FunDecl) {
for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
CommonLateParsedAttrs[i]->addDecl(FunDecl);
}
for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
LateParsedAttrs[i]->addDecl(FunDecl);
}
}
LateParsedAttrs.clear();
if (Tok.is(tok::semi))
ConsumeExtraSemi(AfterMemberFunctionDefinition);
return DeclGroupPtrTy::make(DeclGroupRef(FunDecl));
}
}
while (true) {
InClassInitStyle HasInClassInit = ICIS_NoInit;
bool HasStaticInitializer = false;
if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) {
if (BitfieldSize.isUsable() && !DeclaratorInfo.hasName()) {
Diag(Tok, diag::err_anon_bitfield_member_init);
SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
} else if (DeclaratorInfo.isDeclarationOfFunction()) {
if (!TryConsumePureSpecifier( false))
HasStaticInitializer = true;
} else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
DeclSpec::SCS_static &&
DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
DeclSpec::SCS_typedef &&
!DS.isFriendSpecified()) {
if (BitfieldSize.get())
Diag(Tok, getLangOpts().CPlusPlus20
? diag::warn_cxx17_compat_bitfield_member_init
: diag::ext_bitfield_member_init);
HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
} else {
HasStaticInitializer = true;
}
}
NamedDecl *ThisDecl = nullptr;
if (DS.isFriendSpecified()) {
SmallVector<SourceRange, 4> Ranges;
DeclaratorInfo.getCXX11AttributeRanges(Ranges);
for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
E = Ranges.end();
I != E; ++I)
Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
TemplateParams);
} else {
ThisDecl = Actions.ActOnCXXMemberDeclarator(
getCurScope(), AS, DeclaratorInfo, TemplateParams, BitfieldSize.get(),
VS, HasInClassInit);
if (VarTemplateDecl *VT =
ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
ThisDecl = VT->getTemplatedDecl();
if (ThisDecl)
Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
}
if (HasInClassInit != ICIS_NoInit &&
DeclaratorInfo.getDeclSpec().getStorageClassSpec() ==
DeclSpec::SCS_static) {
HasInClassInit = ICIS_NoInit;
HasStaticInitializer = true;
}
if (PureSpecLoc.isValid() && VS.getAbstractLoc().isValid()) {
Diag(PureSpecLoc, diag::err_duplicate_virt_specifier) << "abstract";
}
if (ThisDecl && PureSpecLoc.isValid())
Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc);
else if (ThisDecl && VS.getAbstractLoc().isValid())
Actions.ActOnPureSpecifier(ThisDecl, VS.getAbstractLoc());
if (HasInClassInit != ICIS_NoInit) {
Diag(Tok, getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_nonstatic_member_init
: diag::ext_nonstatic_member_init);
if (DeclaratorInfo.isArrayOfUnknownBound()) {
Diag(Tok, diag::err_incomplete_array_member_init);
SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
if (ThisDecl)
ThisDecl->setInvalidDecl();
} else
ParseCXXNonStaticMemberInitializer(ThisDecl);
} else if (HasStaticInitializer) {
ExprResult Init = ParseCXXMemberInitializer(
ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
if (Init.isInvalid()) {
if (ThisDecl)
Actions.ActOnUninitializedDecl(ThisDecl);
SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
} else if (ThisDecl)
Actions.AddInitializerToDecl(ThisDecl, Init.get(),
EqualLoc.isInvalid());
} else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Actions.ActOnUninitializedDecl(ThisDecl);
if (ThisDecl) {
if (!ThisDecl->isInvalidDecl()) {
for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
CommonLateParsedAttrs[i]->addDecl(ThisDecl);
for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
LateParsedAttrs[i]->addDecl(ThisDecl);
}
Actions.FinalizeDeclaration(ThisDecl);
DeclsInGroup.push_back(ThisDecl);
if (DeclaratorInfo.isFunctionDeclarator() &&
DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
DeclSpec::SCS_typedef)
HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
}
LateParsedAttrs.clear();
DeclaratorInfo.complete(ThisDecl);
SourceLocation CommaLoc;
if (!TryConsumeToken(tok::comma, CommaLoc))
break;
if (Tok.isAtStartOfLine() &&
!MightBeDeclarator(DeclaratorContext::Member)) {
Diag(CommaLoc, diag::err_expected_semi_declaration)
<< FixItHint::CreateReplacement(CommaLoc, ";");
ExpectSemi = false;
break;
}
DeclaratorInfo.clear();
VS.clear();
BitfieldSize = ExprResult(false);
EqualLoc = PureSpecLoc = SourceLocation();
DeclaratorInfo.setCommaLoc(CommaLoc);
DiagnoseAndSkipCXX11Attributes();
MaybeParseGNUAttributes(DeclaratorInfo);
DiagnoseAndSkipCXX11Attributes();
if (ParseCXXMemberDeclaratorBeforeInitializer(
DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
break;
}
if (ExpectSemi &&
ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
TryConsumeToken(tok::semi);
return nullptr;
}
return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
}
ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
SourceLocation &EqualLoc) {
assert(Tok.isOneOf(tok::equal, tok::l_brace) &&
"Data member initializer not starting with '=' or '{'");
EnterExpressionEvaluationContext Context(
Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, D);
if (TryConsumeToken(tok::equal, EqualLoc)) {
if (Tok.is(tok::kw_delete)) {
const Token &Next = NextToken();
if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) {
if (IsFunction)
Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
<< 1 ;
else
Diag(ConsumeToken(), diag::err_deleted_non_function);
return ExprError();
}
} else if (Tok.is(tok::kw_default)) {
if (IsFunction)
Diag(Tok, diag::err_default_delete_in_multiple_declaration)
<< 0 ;
else
Diag(ConsumeToken(), diag::err_default_special_members)
<< getLangOpts().CPlusPlus20;
return ExprError();
}
}
if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
Diag(Tok, diag::err_ms_property_initializer) << PD;
return ExprError();
}
return ParseInitializer();
}
void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
SourceLocation AttrFixitLoc,
unsigned TagType, Decl *TagDecl) {
if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
assert(isCXX11FinalKeyword() && "not a class definition");
ConsumeToken();
ParsedAttributes Attrs(AttrFactory);
CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace))
return;
}
if (Tok.is(tok::colon)) {
ParseScope ClassScope(this, Scope::ClassScope | Scope::DeclScope);
ParsingClassDefinition ParsingDef(*this, TagDecl, true,
TagType == DeclSpec::TST_interface);
auto OldContext =
Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl);
ParseBaseClause(nullptr);
Actions.ActOnTagFinishSkippedDefinition(OldContext);
if (!Tok.is(tok::l_brace)) {
Diag(PP.getLocForEndOfToken(PrevTokLocation),
diag::err_expected_lbrace_after_base_specifiers);
return;
}
}
assert(Tok.is(tok::l_brace));
BalancedDelimiterTracker T(*this, tok::l_brace);
T.consumeOpen();
T.skipToEnd();
if (Tok.is(tok::kw___attribute)) {
ParsedAttributes Attrs(AttrFactory);
MaybeParseGNUAttributes(Attrs);
}
}
Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas(
AccessSpecifier &AS, ParsedAttributes &AccessAttrs, DeclSpec::TST TagType,
Decl *TagDecl) {
ParenBraceBracketBalancer BalancerRAIIObj(*this);
switch (Tok.getKind()) {
case tok::kw___if_exists:
case tok::kw___if_not_exists:
ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, AS);
return nullptr;
case tok::semi:
ConsumeExtraSemi(InsideStruct, TagType);
return nullptr;
case tok::annot_pragma_vis:
HandlePragmaVisibility();
return nullptr;
case tok::annot_pragma_pack:
HandlePragmaPack();
return nullptr;
case tok::annot_pragma_align:
HandlePragmaAlign();
return nullptr;
case tok::annot_pragma_ms_pointers_to_members:
HandlePragmaMSPointersToMembers();
return nullptr;
case tok::annot_pragma_ms_pragma:
HandlePragmaMSPragma();
return nullptr;
case tok::annot_pragma_ms_vtordisp:
HandlePragmaMSVtorDisp();
return nullptr;
case tok::annot_pragma_dump:
HandlePragmaDump();
return nullptr;
case tok::kw_namespace:
DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
return nullptr;
case tok::kw_private:
if (getLangOpts().OpenCL && !NextToken().is(tok::colon))
return ParseCXXClassMemberDeclaration(AS, AccessAttrs);
LLVM_FALLTHROUGH;
case tok::kw_public:
case tok::kw_protected: {
if (getLangOpts().HLSL)
Diag(Tok.getLocation(), diag::ext_hlsl_access_specifiers);
AccessSpecifier NewAS = getAccessSpecifierIfPresent();
assert(NewAS != AS_none);
AS = NewAS;
SourceLocation ASLoc = Tok.getLocation();
unsigned TokLength = Tok.getLength();
ConsumeToken();
AccessAttrs.clear();
MaybeParseGNUAttributes(AccessAttrs);
SourceLocation EndLoc;
if (TryConsumeToken(tok::colon, EndLoc)) {
} else if (TryConsumeToken(tok::semi, EndLoc)) {
Diag(EndLoc, diag::err_expected)
<< tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
} else {
EndLoc = ASLoc.getLocWithOffset(TokLength);
Diag(EndLoc, diag::err_expected)
<< tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
}
if (TagType == DeclSpec::TST_interface && AS != AS_public) {
Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected);
}
if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc, AccessAttrs)) {
AccessAttrs.clear();
}
return nullptr;
}
case tok::annot_attr_openmp:
case tok::annot_pragma_openmp:
return ParseOpenMPDeclarativeDirectiveWithExtDecl(
AS, AccessAttrs, true, TagType, TagDecl);
default:
if (tok::isPragmaAnnotation(Tok.getKind())) {
Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
<< DeclSpec::getSpecifierName(
TagType, Actions.getASTContext().getPrintingPolicy());
ConsumeAnnotationToken();
return nullptr;
}
return ParseCXXClassMemberDeclaration(AS, AccessAttrs);
}
}
void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
SourceLocation AttrFixitLoc,
ParsedAttributes &Attrs,
unsigned TagType, Decl *TagDecl) {
assert((TagType == DeclSpec::TST_struct ||
TagType == DeclSpec::TST_interface ||
TagType == DeclSpec::TST_union || TagType == DeclSpec::TST_class) &&
"Invalid TagType!");
llvm::TimeTraceScope TimeScope("ParseClass", [&]() {
if (auto *TD = dyn_cast_or_null<NamedDecl>(TagDecl))
return TD->getQualifiedNameAsString();
return std::string("<anonymous>");
});
PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
"parsing struct/union/class body");
bool NonNestedClass = true;
if (!ClassStack.empty()) {
for (const Scope *S = getCurScope(); S; S = S->getParent()) {
if (S->isClassScope()) {
NonNestedClass = false;
if (getCurrentClass().IsInterface) {
Diag(RecordLoc, diag::err_invalid_member_in_interface)
<< 6
<< (isa<NamedDecl>(TagDecl)
? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
: "(anonymous)");
}
break;
}
if (S->isFunctionScope())
break;
}
}
ParseScope ClassScope(this, Scope::ClassScope | Scope::DeclScope);
ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
TagType == DeclSpec::TST_interface);
if (TagDecl)
Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
SourceLocation FinalLoc;
SourceLocation AbstractLoc;
bool IsFinalSpelledSealed = false;
bool IsAbstract = false;
if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
while (true) {
VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
if (Specifier == VirtSpecifiers::VS_None)
break;
if (isCXX11FinalKeyword()) {
if (FinalLoc.isValid()) {
auto Skipped = ConsumeToken();
Diag(Skipped, diag::err_duplicate_class_virt_specifier)
<< VirtSpecifiers::getSpecifierName(Specifier);
} else {
FinalLoc = ConsumeToken();
if (Specifier == VirtSpecifiers::VS_Sealed)
IsFinalSpelledSealed = true;
}
} else {
if (AbstractLoc.isValid()) {
auto Skipped = ConsumeToken();
Diag(Skipped, diag::err_duplicate_class_virt_specifier)
<< VirtSpecifiers::getSpecifierName(Specifier);
} else {
AbstractLoc = ConsumeToken();
IsAbstract = true;
}
}
if (TagType == DeclSpec::TST_interface)
Diag(FinalLoc, diag::err_override_control_interface)
<< VirtSpecifiers::getSpecifierName(Specifier);
else if (Specifier == VirtSpecifiers::VS_Final)
Diag(FinalLoc, getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_override_control_keyword
: diag::ext_override_control_keyword)
<< VirtSpecifiers::getSpecifierName(Specifier);
else if (Specifier == VirtSpecifiers::VS_Sealed)
Diag(FinalLoc, diag::ext_ms_sealed_keyword);
else if (Specifier == VirtSpecifiers::VS_Abstract)
Diag(AbstractLoc, diag::ext_ms_abstract_keyword);
else if (Specifier == VirtSpecifiers::VS_GNU_Final)
Diag(FinalLoc, diag::ext_warn_gnu_final);
}
assert((FinalLoc.isValid() || AbstractLoc.isValid()) &&
"not a class definition");
CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
if (TagDecl)
Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
return;
}
}
if (Tok.is(tok::colon)) {
ParseScope InheritanceScope(this, getCurScope()->getFlags() |
Scope::ClassInheritanceScope);
ParseBaseClause(TagDecl);
if (!Tok.is(tok::l_brace)) {
bool SuggestFixIt = false;
SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
if (Tok.isAtStartOfLine()) {
switch (Tok.getKind()) {
case tok::kw_private:
case tok::kw_protected:
case tok::kw_public:
SuggestFixIt = NextToken().getKind() == tok::colon;
break;
case tok::kw_static_assert:
case tok::r_brace:
case tok::kw_using:
case tok::kw_template:
SuggestFixIt = true;
break;
case tok::identifier:
SuggestFixIt = isConstructorDeclarator(true);
break;
default:
SuggestFixIt = isCXXSimpleDeclaration(false);
break;
}
}
DiagnosticBuilder LBraceDiag =
Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
if (SuggestFixIt) {
LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
PP.EnterToken(Tok, true);
Tok.setKind(tok::l_brace);
} else {
if (TagDecl)
Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
return;
}
}
}
assert(Tok.is(tok::l_brace));
BalancedDelimiterTracker T(*this, tok::l_brace);
T.consumeOpen();
if (TagDecl)
Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
IsFinalSpelledSealed, IsAbstract,
T.getOpenLocation());
AccessSpecifier CurAS;
if (TagType == DeclSpec::TST_class && !getLangOpts().HLSL)
CurAS = AS_private;
else
CurAS = AS_public;
ParsedAttributes AccessAttrs(AttrFactory);
if (TagDecl) {
while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
Tok.isNot(tok::eof)) {
ParseCXXClassMemberDeclarationWithPragmas(
CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl);
MaybeDestroyTemplateIds();
}
T.consumeClose();
} else {
SkipUntil(tok::r_brace);
}
ParsedAttributes attrs(AttrFactory);
MaybeParseGNUAttributes(attrs);
if (TagDecl)
Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
T.getOpenLocation(),
T.getCloseLocation(), attrs);
if (TagDecl && NonNestedClass) {
SourceLocation SavedPrevTokLocation = PrevTokLocation;
ParseLexedPragmas(getCurrentClass());
ParseLexedAttributes(getCurrentClass());
ParseLexedMethodDeclarations(getCurrentClass());
Actions.ActOnFinishCXXMemberDecls();
ParseLexedMemberInitializers(getCurrentClass());
ParseLexedMethodDefs(getCurrentClass());
PrevTokLocation = SavedPrevTokLocation;
Actions.ActOnFinishCXXNonNestedClass();
}
if (TagDecl)
Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
ParsingDef.Pop();
ClassScope.Exit();
}
void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
assert(Tok.is(tok::kw_namespace));
Diag(D->getLocation(), diag::err_missing_end_of_definition) << D;
Diag(Tok.getLocation(), diag::note_missing_end_of_definition_before) << D;
PP.EnterToken(Tok, true);
Tok.startToken();
Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
Tok.setKind(tok::semi);
PP.EnterToken(Tok, true);
Tok.setKind(tok::r_brace);
}
void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
assert(Tok.is(tok::colon) &&
"Constructor initializer always starts with ':'");
PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
SourceLocation ColonLoc = ConsumeToken();
SmallVector<CXXCtorInitializer *, 4> MemInitializers;
bool AnyErrors = false;
do {
if (Tok.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
MemInitializers);
return;
}
MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
if (!MemInit.isInvalid())
MemInitializers.push_back(MemInit.get());
else
AnyErrors = true;
if (Tok.is(tok::comma))
ConsumeToken();
else if (Tok.is(tok::l_brace))
break;
else if (!MemInit.isInvalid() &&
Tok.isOneOf(tok::identifier, tok::coloncolon)) {
SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Diag(Loc, diag::err_ctor_init_missing_comma)
<< FixItHint::CreateInsertion(Loc, ", ");
} else {
if (!MemInit.isInvalid())
Diag(Tok.getLocation(), diag::err_expected_either)
<< tok::l_brace << tok::comma;
SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
break;
}
} while (true);
Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
AnyErrors);
}
MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
CXXScopeSpec SS;
if (ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false))
return true;
IdentifierInfo *II = nullptr;
SourceLocation IdLoc = Tok.getLocation();
DeclSpec DS(AttrFactory);
TypeResult TemplateTypeTy;
if (Tok.is(tok::identifier)) {
II = Tok.getIdentifierInfo();
ConsumeToken();
} else if (Tok.is(tok::annot_decltype)) {
ParseDecltypeSpecifier(DS);
} else {
TemplateIdAnnotation *TemplateId = Tok.is(tok::annot_template_id)
? takeTemplateIdAnnotation(Tok)
: nullptr;
if (TemplateId && TemplateId->mightBeType()) {
AnnotateTemplateIdTokenAsType(SS, true);
assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
TemplateTypeTy = getTypeAnnotation(Tok);
ConsumeAnnotationToken();
} else {
Diag(Tok, diag::err_expected_member_or_base_name);
return true;
}
}
if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
ExprResult InitList = ParseBraceInitializer();
if (InitList.isInvalid())
return true;
SourceLocation EllipsisLoc;
TryConsumeToken(tok::ellipsis, EllipsisLoc);
if (TemplateTypeTy.isInvalid())
return true;
return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
TemplateTypeTy.get(), DS, IdLoc,
InitList.get(), EllipsisLoc);
} else if (Tok.is(tok::l_paren)) {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ExprVector ArgExprs;
CommaLocsTy CommaLocs;
auto RunSignatureHelp = [&] {
if (TemplateTypeTy.isInvalid())
return QualType();
QualType PreferredType = Actions.ProduceCtorInitMemberSignatureHelp(
ConstructorDecl, SS, TemplateTypeTy.get(), ArgExprs, II,
T.getOpenLocation(), false);
CalledSignatureHelp = true;
return PreferredType;
};
if (Tok.isNot(tok::r_paren) &&
ParseExpressionList(ArgExprs, CommaLocs, [&] {
PreferredType.enterFunctionArgument(Tok.getLocation(),
RunSignatureHelp);
})) {
if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
RunSignatureHelp();
SkipUntil(tok::r_paren, StopAtSemi);
return true;
}
T.consumeClose();
SourceLocation EllipsisLoc;
TryConsumeToken(tok::ellipsis, EllipsisLoc);
if (TemplateTypeTy.isInvalid())
return true;
return Actions.ActOnMemInitializer(
ConstructorDecl, getCurScope(), SS, II, TemplateTypeTy.get(), DS, IdLoc,
T.getOpenLocation(), ArgExprs, T.getCloseLocation(), EllipsisLoc);
}
if (TemplateTypeTy.isInvalid())
return true;
if (getLangOpts().CPlusPlus11)
return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
else
return Diag(Tok, diag::err_expected) << tok::l_paren;
}
ExceptionSpecificationType Parser::tryParseExceptionSpecification(
bool Delayed, SourceRange &SpecificationRange,
SmallVectorImpl<ParsedType> &DynamicExceptions,
SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
ExprResult &NoexceptExpr, CachedTokens *&ExceptionSpecTokens) {
ExceptionSpecificationType Result = EST_None;
ExceptionSpecTokens = nullptr;
if (Delayed) {
if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
return EST_None;
bool IsNoexcept = Tok.is(tok::kw_noexcept);
Token StartTok = Tok;
SpecificationRange = SourceRange(ConsumeToken());
if (!Tok.is(tok::l_paren)) {
if (IsNoexcept) {
Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
NoexceptExpr = nullptr;
return EST_BasicNoexcept;
}
Diag(Tok, diag::err_expected_lparen_after) << "throw";
return EST_DynamicNone;
}
ExceptionSpecTokens = new CachedTokens;
ExceptionSpecTokens->push_back(StartTok); ExceptionSpecTokens->push_back(Tok); SpecificationRange.setEnd(ConsumeParen());
ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
true,
true);
SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation());
return EST_Unparsed;
}
if (Tok.is(tok::kw_throw)) {
Result = ParseDynamicExceptionSpecification(
SpecificationRange, DynamicExceptions, DynamicExceptionRanges);
assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
"Produced different number of exception types and ranges.");
}
if (Tok.isNot(tok::kw_noexcept))
return Result;
Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
SourceRange NoexceptRange;
ExceptionSpecificationType NoexceptType = EST_None;
SourceLocation KeywordLoc = ConsumeToken();
if (Tok.is(tok::l_paren)) {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
NoexceptExpr = ParseConstantExpression();
T.consumeClose();
if (!NoexceptExpr.isInvalid()) {
NoexceptExpr =
Actions.ActOnNoexceptSpec(NoexceptExpr.get(), NoexceptType);
NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
} else {
NoexceptType = EST_BasicNoexcept;
}
} else {
NoexceptType = EST_BasicNoexcept;
NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
}
if (Result == EST_None) {
SpecificationRange = NoexceptRange;
Result = NoexceptType;
if (Tok.is(tok::kw_throw)) {
Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
DynamicExceptionRanges);
}
} else {
Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
}
return Result;
}
static void diagnoseDynamicExceptionSpecification(Parser &P, SourceRange Range,
bool IsNoexcept) {
if (P.getLangOpts().CPlusPlus11) {
const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
P.Diag(Range.getBegin(), P.getLangOpts().CPlusPlus17 && !IsNoexcept
? diag::ext_dynamic_exception_spec
: diag::warn_exception_spec_deprecated)
<< Range;
P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
<< Replacement << FixItHint::CreateReplacement(Range, Replacement);
}
}
ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
SourceRange &SpecificationRange, SmallVectorImpl<ParsedType> &Exceptions,
SmallVectorImpl<SourceRange> &Ranges) {
assert(Tok.is(tok::kw_throw) && "expected throw");
SpecificationRange.setBegin(ConsumeToken());
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.consumeOpen()) {
Diag(Tok, diag::err_expected_lparen_after) << "throw";
SpecificationRange.setEnd(SpecificationRange.getBegin());
return EST_DynamicNone;
}
if (Tok.is(tok::ellipsis)) {
SourceLocation EllipsisLoc = ConsumeToken();
if (!getLangOpts().MicrosoftExt)
Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
T.consumeClose();
SpecificationRange.setEnd(T.getCloseLocation());
diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
return EST_MSAny;
}
SourceRange Range;
while (Tok.isNot(tok::r_paren)) {
TypeResult Res(ParseTypeName(&Range));
if (Tok.is(tok::ellipsis)) {
SourceLocation Ellipsis = ConsumeToken();
Range.setEnd(Ellipsis);
if (!Res.isInvalid())
Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
}
if (!Res.isInvalid()) {
Exceptions.push_back(Res.get());
Ranges.push_back(Range);
}
if (!TryConsumeToken(tok::comma))
break;
}
T.consumeClose();
SpecificationRange.setEnd(T.getCloseLocation());
diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
Exceptions.empty());
return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
}
TypeResult Parser::ParseTrailingReturnType(SourceRange &Range,
bool MayBeFollowedByDirectInit) {
assert(Tok.is(tok::arrow) && "expected arrow");
ConsumeToken();
return ParseTypeName(&Range, MayBeFollowedByDirectInit
? DeclaratorContext::TrailingReturnVar
: DeclaratorContext::TrailingReturn);
}
void Parser::ParseTrailingRequiresClause(Declarator &D) {
assert(Tok.is(tok::kw_requires) && "expected requires");
SourceLocation RequiresKWLoc = ConsumeToken();
ExprResult TrailingRequiresClause;
ParseScope ParamScope(this, Scope::DeclScope |
Scope::FunctionDeclarationScope |
Scope::FunctionPrototypeScope);
Actions.ActOnStartTrailingRequiresClause(getCurScope(), D);
llvm::Optional<Sema::CXXThisScopeRAII> ThisScope;
InitCXXThisScopeForDeclaratorIfRelevant(D, D.getDeclSpec(), ThisScope);
TrailingRequiresClause =
ParseConstraintLogicalOrExpression(true);
TrailingRequiresClause =
Actions.ActOnFinishTrailingRequiresClause(TrailingRequiresClause);
if (!D.isDeclarationOfFunction()) {
Diag(RequiresKWLoc,
diag::err_requires_clause_on_declarator_not_declaring_a_function);
return;
}
if (TrailingRequiresClause.isInvalid())
SkipUntil({tok::l_brace, tok::arrow, tok::kw_try, tok::comma, tok::colon},
StopAtSemi | StopBeforeMatch);
else
D.setTrailingRequiresClause(TrailingRequiresClause.get());
if (D.isFunctionDeclarator() && Tok.is(tok::arrow) &&
D.getDeclSpec().getTypeSpecType() == TST_auto) {
SourceLocation ArrowLoc = Tok.getLocation();
SourceRange Range;
TypeResult TrailingReturnType =
ParseTrailingReturnType(Range, false);
if (!TrailingReturnType.isInvalid()) {
Diag(ArrowLoc,
diag::err_requires_clause_must_appear_after_trailing_return)
<< Range;
auto &FunctionChunk = D.getFunctionTypeInfo();
FunctionChunk.HasTrailingReturnType = TrailingReturnType.isUsable();
FunctionChunk.TrailingReturnType = TrailingReturnType.get();
FunctionChunk.TrailingReturnTypeLoc = Range.getBegin();
} else
SkipUntil({tok::equal, tok::l_brace, tok::arrow, tok::kw_try, tok::comma},
StopAtSemi | StopBeforeMatch);
}
}
Sema::ParsingClassState Parser::PushParsingClass(Decl *ClassDecl,
bool NonNestedClass,
bool IsInterface) {
assert((NonNestedClass || !ClassStack.empty()) &&
"Nested class without outer class");
ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
return Actions.PushParsingClass();
}
void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
delete Class->LateParsedDeclarations[I];
delete Class;
}
void Parser::PopParsingClass(Sema::ParsingClassState state) {
assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Actions.PopParsingClass(state);
ParsingClass *Victim = ClassStack.top();
ClassStack.pop();
if (Victim->TopLevelClass) {
DeallocateParsedClasses(Victim);
return;
}
assert(!ClassStack.empty() && "Missing top-level class?");
if (Victim->LateParsedDeclarations.empty()) {
DeallocateParsedClasses(Victim);
return;
}
assert(getCurScope()->isClassScope() &&
"Nested class outside of class scope?");
ClassStack.top()->LateParsedDeclarations.push_back(
new LateParsedClass(this, Victim));
}
IdentifierInfo *
Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc,
Sema::AttributeCompletion Completion,
const IdentifierInfo *Scope) {
switch (Tok.getKind()) {
default:
if (!Tok.isAnnotation()) {
if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Loc = ConsumeToken();
return II;
}
}
return nullptr;
case tok::code_completion:
cutOffParsing();
Actions.CodeCompleteAttribute(getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11
: ParsedAttr::AS_C2x,
Completion, Scope);
return nullptr;
case tok::numeric_constant: {
if (Tok.getLocation().isMacroID()) {
SmallString<8> ExpansionBuf;
SourceLocation ExpansionLoc =
PP.getSourceManager().getExpansionLoc(Tok.getLocation());
StringRef Spelling = PP.getSpelling(ExpansionLoc, ExpansionBuf);
if (Spelling == "__clang__") {
SourceRange TokRange(
ExpansionLoc,
PP.getSourceManager().getExpansionLoc(Tok.getEndLoc()));
Diag(Tok, diag::warn_wrong_clang_attr_namespace)
<< FixItHint::CreateReplacement(TokRange, "_Clang");
Loc = ConsumeToken();
return &PP.getIdentifierTable().get("_Clang");
}
}
return nullptr;
}
case tok::ampamp: case tok::pipe: case tok::pipepipe: case tok::caret: case tok::tilde: case tok::amp: case tok::ampequal: case tok::pipeequal: case tok::caretequal: case tok::exclaim: case tok::exclaimequal: SmallString<8> SpellingBuf;
SourceLocation SpellingLoc =
PP.getSourceManager().getSpellingLoc(Tok.getLocation());
StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf);
if (isLetter(Spelling[0])) {
Loc = ConsumeToken();
return &PP.getIdentifierTable().get(Spelling);
}
return nullptr;
}
}
void Parser::ParseOpenMPAttributeArgs(IdentifierInfo *AttrName,
CachedTokens &OpenMPTokens) {
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.consumeOpen()) {
Diag(Tok, diag::err_expected) << tok::l_paren;
return;
}
if (AttrName->isStr("directive")) {
Token OMPBeginTok;
OMPBeginTok.startToken();
OMPBeginTok.setKind(tok::annot_attr_openmp);
OMPBeginTok.setLocation(Tok.getLocation());
OpenMPTokens.push_back(OMPBeginTok);
ConsumeAndStoreUntil(tok::r_paren, OpenMPTokens, false,
false);
Token OMPEndTok;
OMPEndTok.startToken();
OMPEndTok.setKind(tok::annot_pragma_openmp_end);
OMPEndTok.setLocation(Tok.getLocation());
OpenMPTokens.push_back(OMPEndTok);
} else {
assert(AttrName->isStr("sequence") &&
"Expected either 'directive' or 'sequence'");
do {
SourceLocation IdentLoc;
IdentifierInfo *Ident = TryParseCXX11AttributeIdentifier(IdentLoc);
if (Ident && Ident->isStr("omp") && !ExpectAndConsume(tok::coloncolon))
Ident = TryParseCXX11AttributeIdentifier(IdentLoc);
if (!Ident || (!Ident->isStr("directive") && !Ident->isStr("sequence"))) {
Diag(Tok.getLocation(), diag::err_expected_sequence_or_directive);
SkipUntil(tok::r_paren, StopBeforeMatch);
continue;
}
ParseOpenMPAttributeArgs(Ident, OpenMPTokens);
} while (TryConsumeToken(tok::comma));
}
T.consumeClose();
}
static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
IdentifierInfo *ScopeName) {
switch (
ParsedAttr::getParsedKind(AttrName, ScopeName, ParsedAttr::AS_CXX11)) {
case ParsedAttr::AT_CarriesDependency:
case ParsedAttr::AT_Deprecated:
case ParsedAttr::AT_FallThrough:
case ParsedAttr::AT_CXX11NoReturn:
case ParsedAttr::AT_NoUniqueAddress:
case ParsedAttr::AT_Likely:
case ParsedAttr::AT_Unlikely:
return true;
case ParsedAttr::AT_WarnUnusedResult:
return !ScopeName && AttrName->getName().equals("nodiscard");
case ParsedAttr::AT_Unused:
return !ScopeName && AttrName->getName().equals("maybe_unused");
default:
return false;
}
}
bool Parser::ParseCXX11AttributeArgs(
IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
SourceLocation ScopeLoc, CachedTokens &OpenMPTokens) {
assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
SourceLocation LParenLoc = Tok.getLocation();
const LangOptions &LO = getLangOpts();
ParsedAttr::Syntax Syntax =
LO.CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x;
if (getLangOpts().MicrosoftExt || getLangOpts().HLSL) {
if (hasAttribute(AttributeCommonInfo::Syntax::AS_Microsoft, ScopeName,
AttrName, getTargetInfo(), getLangOpts()))
Syntax = ParsedAttr::AS_Microsoft;
}
if (Syntax != ParsedAttr::AS_Microsoft &&
!hasAttribute(LO.CPlusPlus ? AttributeCommonInfo::Syntax::AS_CXX11
: AttributeCommonInfo::Syntax::AS_C2x,
ScopeName, AttrName, getTargetInfo(), getLangOpts())) {
if (getLangOpts().MicrosoftExt || getLangOpts().HLSL) {
}
ConsumeParen();
SkipUntil(tok::r_paren);
return false;
}
if (ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"))) {
ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
ScopeLoc, Syntax, nullptr);
return true;
}
if (ScopeName && ScopeName->isStr("omp")) {
Diag(AttrNameLoc, getLangOpts().OpenMP >= 51
? diag::warn_omp51_compat_attributes
: diag::ext_omp_attributes);
ParseOpenMPAttributeArgs(AttrName, OpenMPTokens);
return true;
}
unsigned NumArgs;
if (ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang")))
NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc,
ScopeName, ScopeLoc, Syntax);
else
NumArgs = ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
ScopeName, ScopeLoc, Syntax);
if (!Attrs.empty() &&
IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
ParsedAttr &Attr = Attrs.back();
if (Attr.getMaxArgs() && !NumArgs) {
Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Attr.setInvalid(true);
} else if (!Attr.getMaxArgs()) {
Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
<< AttrName
<< FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
Attr.setInvalid(true);
}
}
return true;
}
void Parser::ParseCXX11AttributeSpecifierInternal(ParsedAttributes &Attrs,
CachedTokens &OpenMPTokens,
SourceLocation *EndLoc) {
if (Tok.is(tok::kw_alignas)) {
Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
ParseAlignmentSpecifier(Attrs, EndLoc);
return;
}
assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) &&
"Not a double square bracket attribute list");
SourceLocation OpenLoc = Tok.getLocation();
Diag(OpenLoc, diag::warn_cxx98_compat_attribute);
ConsumeBracket();
checkCompoundToken(OpenLoc, tok::l_square, CompoundToken::AttrBegin);
ConsumeBracket();
SourceLocation CommonScopeLoc;
IdentifierInfo *CommonScopeName = nullptr;
if (Tok.is(tok::kw_using)) {
Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
? diag::warn_cxx14_compat_using_attribute_ns
: diag::ext_using_attribute_ns);
ConsumeToken();
CommonScopeName = TryParseCXX11AttributeIdentifier(
CommonScopeLoc, Sema::AttributeCompletion::Scope);
if (!CommonScopeName) {
Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
SkipUntil(tok::r_square, tok::colon, StopBeforeMatch);
}
if (!TryConsumeToken(tok::colon) && CommonScopeName)
Diag(Tok.getLocation(), diag::err_expected) << tok::colon;
}
llvm::SmallDenseMap<IdentifierInfo *, SourceLocation, 4> SeenAttrs;
bool AttrParsed = false;
while (!Tok.isOneOf(tok::r_square, tok::semi, tok::eof)) {
if (AttrParsed) {
if (ExpectAndConsume(tok::comma)) {
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
continue;
}
AttrParsed = false;
}
while (TryConsumeToken(tok::comma))
;
SourceLocation ScopeLoc, AttrLoc;
IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
AttrName = TryParseCXX11AttributeIdentifier(
AttrLoc, Sema::AttributeCompletion::Attribute, CommonScopeName);
if (!AttrName)
break;
if (TryConsumeToken(tok::coloncolon)) {
ScopeName = AttrName;
ScopeLoc = AttrLoc;
AttrName = TryParseCXX11AttributeIdentifier(
AttrLoc, Sema::AttributeCompletion::Attribute, ScopeName);
if (!AttrName) {
Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
continue;
}
}
if (CommonScopeName) {
if (ScopeName) {
Diag(ScopeLoc, diag::err_using_attribute_ns_conflict)
<< SourceRange(CommonScopeLoc);
} else {
ScopeName = CommonScopeName;
ScopeLoc = CommonScopeLoc;
}
}
if (Tok.is(tok::l_paren))
AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, Attrs, EndLoc,
ScopeName, ScopeLoc, OpenMPTokens);
if (!AttrParsed) {
Attrs.addNew(
AttrName,
SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, AttrLoc),
ScopeName, ScopeLoc, nullptr, 0,
getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x);
AttrParsed = true;
}
if (TryConsumeToken(tok::ellipsis))
Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) << AttrName;
}
if (Tok.is(tok::semi)) {
ConsumeToken();
return;
}
SourceLocation CloseLoc = Tok.getLocation();
if (ExpectAndConsume(tok::r_square))
SkipUntil(tok::r_square);
else if (Tok.is(tok::r_square))
checkCompoundToken(CloseLoc, tok::r_square, CompoundToken::AttrEnd);
if (EndLoc)
*EndLoc = Tok.getLocation();
if (ExpectAndConsume(tok::r_square))
SkipUntil(tok::r_square);
}
void Parser::ParseCXX11Attributes(ParsedAttributes &Attrs) {
assert(standardAttributesAllowed());
SourceLocation StartLoc = Tok.getLocation();
SourceLocation EndLoc = StartLoc;
do {
ParseCXX11AttributeSpecifier(Attrs, &EndLoc);
} while (isCXX11AttributeSpecifier());
Attrs.Range = SourceRange(StartLoc, EndLoc);
}
void Parser::DiagnoseAndSkipCXX11Attributes() {
SourceLocation StartLoc = Tok.getLocation();
SourceLocation EndLoc = SkipCXX11Attributes();
if (EndLoc.isValid()) {
SourceRange Range(StartLoc, EndLoc);
Diag(StartLoc, diag::err_attributes_not_allowed) << Range;
}
}
SourceLocation Parser::SkipCXX11Attributes() {
SourceLocation EndLoc;
if (!isCXX11AttributeSpecifier())
return EndLoc;
do {
if (Tok.is(tok::l_square)) {
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
T.skipToEnd();
EndLoc = T.getCloseLocation();
} else {
assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (!T.consumeOpen())
T.skipToEnd();
EndLoc = T.getCloseLocation();
}
} while (isCXX11AttributeSpecifier());
return EndLoc;
}
void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list");
IdentifierInfo *UuidIdent = Tok.getIdentifierInfo();
assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list");
SourceLocation UuidLoc = Tok.getLocation();
ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.consumeOpen()) {
Diag(Tok, diag::err_expected) << tok::l_paren;
return;
}
ArgsVector ArgExprs;
if (Tok.is(tok::string_literal)) {
ExprResult StringResult = ParseStringLiteralExpression();
if (StringResult.isInvalid())
return;
ArgExprs.push_back(StringResult.get());
} else {
SmallString<42> StrBuffer; StrBuffer += "\"";
SourceLocation StartLoc = Tok.getLocation();
while (Tok.isNot(tok::r_paren)) {
if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
Diag(Tok, diag::err_attribute_uuid_malformed_guid);
SkipUntil(tok::r_paren, StopAtSemi);
return;
}
SmallString<16> SpellingBuffer;
SpellingBuffer.resize(Tok.getLength() + 1);
bool Invalid = false;
StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid);
if (Invalid) {
SkipUntil(tok::r_paren, StopAtSemi);
return;
}
StrBuffer += TokSpelling;
ConsumeAnyToken();
}
StrBuffer += "\"";
if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
Diag(Tok, diag::err_attribute_uuid_malformed_guid);
ConsumeParen();
return;
}
Token Toks[1];
Toks[0].startToken();
Toks[0].setKind(tok::string_literal);
Toks[0].setLocation(StartLoc);
Toks[0].setLiteralData(StrBuffer.data());
Toks[0].setLength(StrBuffer.size());
StringLiteral *UuidString =
cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
ArgExprs.push_back(UuidString);
}
if (!T.consumeClose()) {
Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr,
SourceLocation(), ArgExprs.data(), ArgExprs.size(),
ParsedAttr::AS_Microsoft);
}
}
void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
SourceLocation StartLoc = Tok.getLocation();
SourceLocation EndLoc = StartLoc;
do {
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
while (true) {
SkipUntil(tok::r_square, tok::identifier,
StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
if (Tok.is(tok::code_completion)) {
cutOffParsing();
Actions.CodeCompleteAttribute(AttributeCommonInfo::AS_Microsoft,
Sema::AttributeCompletion::Attribute,
nullptr);
break;
}
if (Tok.isNot(tok::identifier)) break;
if (Tok.getIdentifierInfo()->getName() == "uuid")
ParseMicrosoftUuidAttributeArgs(Attrs);
else {
IdentifierInfo *II = Tok.getIdentifierInfo();
SourceLocation NameLoc = Tok.getLocation();
ConsumeToken();
ParsedAttr::Kind AttrKind =
ParsedAttr::getParsedKind(II, nullptr, ParsedAttr::AS_Microsoft);
if (getLangOpts().HLSL || AttrKind != ParsedAttr::UnknownAttribute) {
bool AttrParsed = false;
if (Tok.is(tok::l_paren)) {
CachedTokens OpenMPTokens;
AttrParsed =
ParseCXX11AttributeArgs(II, NameLoc, Attrs, &EndLoc, nullptr,
SourceLocation(), OpenMPTokens);
ReplayOpenMPAttributeTokens(OpenMPTokens);
}
if (!AttrParsed) {
Attrs.addNew(II, NameLoc, nullptr, SourceLocation(), nullptr, 0,
ParsedAttr::AS_Microsoft);
}
}
}
}
T.consumeClose();
EndLoc = T.getCloseLocation();
} while (Tok.is(tok::l_square));
Attrs.Range = SourceRange(StartLoc, EndLoc);
}
void Parser::ParseMicrosoftIfExistsClassDeclaration(
DeclSpec::TST TagType, ParsedAttributes &AccessAttrs,
AccessSpecifier &CurAS) {
IfExistsCondition Result;
if (ParseMicrosoftIfExistsCondition(Result))
return;
BalancedDelimiterTracker Braces(*this, tok::l_brace);
if (Braces.consumeOpen()) {
Diag(Tok, diag::err_expected) << tok::l_brace;
return;
}
switch (Result.Behavior) {
case IEB_Parse:
break;
case IEB_Dependent:
Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
<< Result.IsIfExists;
LLVM_FALLTHROUGH;
case IEB_Skip:
Braces.skipToEnd();
return;
}
while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, CurAS);
continue;
}
if (Tok.is(tok::semi)) {
ConsumeExtraSemi(InsideStruct, TagType);
continue;
}
AccessSpecifier AS = getAccessSpecifierIfPresent();
if (AS != AS_none) {
CurAS = AS;
SourceLocation ASLoc = Tok.getLocation();
ConsumeToken();
if (Tok.is(tok::colon))
Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation(),
ParsedAttributesView{});
else
Diag(Tok, diag::err_expected) << tok::colon;
ConsumeToken();
continue;
}
ParseCXXClassMemberDeclaration(CurAS, AccessAttrs);
}
Braces.consumeClose();
}