#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Sema/ParsedTemplate.h"
using namespace clang;
bool Parser::isCXXDeclarationStatement() {
switch (Tok.getKind()) {
case tok::kw_asm:
case tok::kw_namespace:
case tok::kw_using:
case tok::kw_static_assert:
case tok::kw__Static_assert:
return true;
default:
return isCXXSimpleDeclaration(false);
}
}
bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
bool InvalidAsDeclaration = false;
TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
&InvalidAsDeclaration);
if (TPR != TPResult::Ambiguous)
return TPR != TPResult::False;
if (InvalidAsDeclaration)
return false;
{
RevertingTentativeParsingAction PA(*this);
TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
}
if (TPR == TPResult::Error)
return true;
if (TPR == TPResult::Ambiguous)
TPR = TPResult::True;
assert(TPR == TPResult::True || TPR == TPResult::False);
return TPR == TPResult::True;
}
Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
switch (Tok.getKind()) {
case tok::kw__Atomic:
if (NextToken().isNot(tok::l_paren)) {
ConsumeToken();
break;
}
LLVM_FALLTHROUGH;
case tok::kw_typeof:
case tok::kw___attribute:
case tok::kw___underlying_type: {
ConsumeToken();
if (Tok.isNot(tok::l_paren))
return TPResult::Error;
ConsumeParen();
if (!SkipUntil(tok::r_paren))
return TPResult::Error;
break;
}
case tok::kw_class:
case tok::kw_struct:
case tok::kw_union:
case tok::kw___interface:
case tok::kw_enum:
ConsumeToken();
if (!TrySkipAttributes())
return TPResult::Error;
if (TryAnnotateOptionalCXXScopeToken())
return TPResult::Error;
if (Tok.is(tok::annot_cxxscope))
ConsumeAnnotationToken();
if (Tok.is(tok::identifier))
ConsumeToken();
else if (Tok.is(tok::annot_template_id))
ConsumeAnnotationToken();
else
return TPResult::Error;
break;
case tok::annot_cxxscope:
ConsumeAnnotationToken();
LLVM_FALLTHROUGH;
default:
ConsumeAnyToken();
if (getLangOpts().ObjC && Tok.is(tok::less))
return TryParseProtocolQualifiers();
break;
}
return TPResult::Ambiguous;
}
Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
return TPResult::Error;
if (Tok.isNot(tok::l_paren)) {
TPResult TPR = isCXXDeclarationSpecifier();
if (TPR == TPResult::Ambiguous)
return TPResult::True;
if (TPR == TPResult::True || TPR == TPResult::Error)
return TPR;
assert(TPR == TPResult::False);
}
TPResult TPR = TryParseInitDeclaratorList();
if (TPR != TPResult::Ambiguous)
return TPR;
if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
return TPResult::False;
return TPResult::Ambiguous;
}
Parser::TPResult Parser::TryParseInitDeclaratorList() {
while (true) {
TPResult TPR = TryParseDeclarator(false);
if (TPR != TPResult::Ambiguous)
return TPR;
if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
return TPResult::True;
if (Tok.is(tok::l_paren)) {
ConsumeParen();
if (!SkipUntil(tok::r_paren, StopAtSemi))
return TPResult::Error;
} else if (Tok.is(tok::l_brace)) {
return TPResult::True;
} else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
return TPResult::True;
}
if (!TryConsumeToken(tok::comma))
break;
}
return TPResult::Ambiguous;
}
struct Parser::ConditionDeclarationOrInitStatementState {
Parser &P;
bool CanBeExpression = true;
bool CanBeCondition = true;
bool CanBeInitStatement;
bool CanBeForRangeDecl;
ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement,
bool CanBeForRangeDecl)
: P(P), CanBeInitStatement(CanBeInitStatement),
CanBeForRangeDecl(CanBeForRangeDecl) {}
bool resolved() {
return CanBeExpression + CanBeCondition + CanBeInitStatement +
CanBeForRangeDecl < 2;
}
void markNotExpression() {
CanBeExpression = false;
if (!resolved()) {
RevertingTentativeParsingAction PA(P);
if (CanBeForRangeDecl) {
unsigned QuestionColonDepth = 0;
while (true) {
P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
StopBeforeMatch);
if (P.Tok.is(tok::question))
++QuestionColonDepth;
else if (P.Tok.is(tok::colon)) {
if (QuestionColonDepth)
--QuestionColonDepth;
else {
CanBeCondition = CanBeInitStatement = false;
return;
}
} else {
CanBeForRangeDecl = false;
break;
}
P.ConsumeToken();
}
} else {
P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
}
if (P.Tok.isNot(tok::r_paren))
CanBeCondition = CanBeForRangeDecl = false;
if (P.Tok.isNot(tok::semi))
CanBeInitStatement = false;
}
}
bool markNotCondition() {
CanBeCondition = false;
return resolved();
}
bool markNotForRangeDecl() {
CanBeForRangeDecl = false;
return resolved();
}
bool update(TPResult IsDecl) {
switch (IsDecl) {
case TPResult::True:
markNotExpression();
assert(resolved() && "can't continue after tentative parsing bails out");
break;
case TPResult::False:
CanBeCondition = CanBeInitStatement = CanBeForRangeDecl = false;
break;
case TPResult::Ambiguous:
break;
case TPResult::Error:
CanBeExpression = CanBeCondition = CanBeInitStatement =
CanBeForRangeDecl = false;
break;
}
return resolved();
}
ConditionOrInitStatement result() const {
assert(CanBeExpression + CanBeCondition + CanBeInitStatement +
CanBeForRangeDecl < 2 &&
"result called but not yet resolved");
if (CanBeExpression)
return ConditionOrInitStatement::Expression;
if (CanBeCondition)
return ConditionOrInitStatement::ConditionDecl;
if (CanBeInitStatement)
return ConditionOrInitStatement::InitStmtDecl;
if (CanBeForRangeDecl)
return ConditionOrInitStatement::ForRangeDecl;
return ConditionOrInitStatement::Error;
}
};
bool Parser::isEnumBase(bool AllowSemi) {
assert(Tok.is(tok::colon) && "should be looking at the ':'");
RevertingTentativeParsingAction PA(*this);
ConsumeToken();
bool InvalidAsDeclSpec = false;
TPResult R = isCXXDeclarationSpecifier( TPResult::True,
&InvalidAsDeclSpec);
if (R == TPResult::Ambiguous) {
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
return true;
if (Tok.is(tok::l_brace) || (AllowSemi && Tok.is(tok::semi)))
return true;
R = isCXXDeclarationSpecifier(TPResult::True, &InvalidAsDeclSpec);
}
return R != TPResult::False;
}
Parser::ConditionOrInitStatement
Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement,
bool CanBeForRangeDecl) {
ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement,
CanBeForRangeDecl);
if (CanBeInitStatement && Tok.is(tok::kw_using))
return ConditionOrInitStatement::InitStmtDecl;
if (State.update(isCXXDeclarationSpecifier()))
return State.result();
RevertingTentativeParsingAction PA(*this);
if (State.update(TryConsumeDeclarationSpecifier()))
return State.result();
assert(Tok.is(tok::l_paren) && "Expected '('");
while (true) {
if (State.update(TryParseDeclarator(false)))
return State.result();
if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
(getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
State.markNotExpression();
return State.result();
}
if (State.CanBeForRangeDecl && Tok.is(tok::colon))
return ConditionOrInitStatement::ForRangeDecl;
if (State.markNotCondition())
return State.result();
if (State.markNotForRangeDecl())
return State.result();
if (Tok.is(tok::l_paren)) {
ConsumeParen();
SkipUntil(tok::r_paren, StopAtSemi);
}
if (!TryConsumeToken(tok::comma))
break;
}
if (State.CanBeCondition && Tok.is(tok::r_paren))
return ConditionOrInitStatement::ConditionDecl;
else if (State.CanBeInitStatement && Tok.is(tok::semi))
return ConditionOrInitStatement::InitStmtDecl;
else
return ConditionOrInitStatement::Expression;
}
bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
isAmbiguous = false;
TPResult TPR = isCXXDeclarationSpecifier();
if (TPR != TPResult::Ambiguous)
return TPR != TPResult::False;
RevertingTentativeParsingAction PA(*this);
TryConsumeDeclarationSpecifier();
assert(Tok.is(tok::l_paren) && "Expected '('");
TPR = TryParseDeclarator(true, false);
if (TPR == TPResult::Error)
TPR = TPResult::True;
if (TPR == TPResult::Ambiguous) {
if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
TPR = TPResult::True;
isAmbiguous = true;
} else if (Context == TypeIdAsTemplateArgument &&
(Tok.isOneOf(tok::greater, tok::comma) ||
(getLangOpts().CPlusPlus11 &&
(Tok.isOneOf(tok::greatergreater,
tok::greatergreatergreater) ||
(Tok.is(tok::ellipsis) &&
NextToken().isOneOf(tok::greater, tok::greatergreater,
tok::greatergreatergreater,
tok::comma)))))) {
TPR = TPResult::True;
isAmbiguous = true;
} else
TPR = TPResult::False;
}
assert(TPR == TPResult::True || TPR == TPResult::False);
return TPR == TPResult::True;
}
Parser::CXX11AttributeKind
Parser::isCXX11AttributeSpecifier(bool Disambiguate,
bool OuterMightBeMessageSend) {
if (Tok.is(tok::kw_alignas))
return CAK_AttributeSpecifier;
if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
return CAK_NotAttributeSpecifier;
if (!Disambiguate && !getLangOpts().ObjC)
return CAK_AttributeSpecifier;
if (GetLookAheadToken(2).is(tok::kw_using))
return CAK_AttributeSpecifier;
RevertingTentativeParsingAction PA(*this);
ConsumeBracket();
if (!getLangOpts().ObjC) {
ConsumeBracket();
bool IsAttribute = SkipUntil(tok::r_square);
IsAttribute &= Tok.is(tok::r_square);
return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
}
{
RevertingTentativeParsingAction LambdaTPA(*this);
LambdaIntroducer Intro;
LambdaIntroducerTentativeParse Tentative;
if (ParseLambdaIntroducer(Intro, &Tentative)) {
return CAK_NotAttributeSpecifier;
}
switch (Tentative) {
case LambdaIntroducerTentativeParse::MessageSend:
return CAK_NotAttributeSpecifier;
case LambdaIntroducerTentativeParse::Success:
case LambdaIntroducerTentativeParse::Incomplete:
if (Tok.is(tok::r_square))
return CAK_AttributeSpecifier;
if (OuterMightBeMessageSend)
return CAK_NotAttributeSpecifier;
return CAK_InvalidAttributeSpecifier;
case LambdaIntroducerTentativeParse::Invalid:
break;
}
}
ConsumeBracket();
bool IsAttribute = true;
while (Tok.isNot(tok::r_square)) {
if (Tok.is(tok::comma)) {
return CAK_AttributeSpecifier;
}
SourceLocation Loc;
if (!TryParseCXX11AttributeIdentifier(Loc)) {
IsAttribute = false;
break;
}
if (Tok.is(tok::coloncolon)) {
ConsumeToken();
if (!TryParseCXX11AttributeIdentifier(Loc)) {
IsAttribute = false;
break;
}
}
if (Tok.is(tok::l_paren)) {
ConsumeParen();
if (!SkipUntil(tok::r_paren)) {
IsAttribute = false;
break;
}
}
TryConsumeToken(tok::ellipsis);
if (!TryConsumeToken(tok::comma))
break;
}
if (IsAttribute) {
if (Tok.is(tok::r_square)) {
ConsumeBracket();
IsAttribute = Tok.is(tok::r_square);
} else {
IsAttribute = false;
}
}
if (IsAttribute)
return CAK_AttributeSpecifier;
return CAK_NotAttributeSpecifier;
}
bool Parser::TrySkipAttributes() {
while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
tok::kw_alignas)) {
if (Tok.is(tok::l_square)) {
ConsumeBracket();
if (Tok.isNot(tok::l_square))
return false;
ConsumeBracket();
if (!SkipUntil(tok::r_square) || Tok.isNot(tok::r_square))
return false;
ConsumeBracket();
} else {
ConsumeToken();
if (Tok.isNot(tok::l_paren))
return false;
ConsumeParen();
if (!SkipUntil(tok::r_paren))
return false;
}
}
return true;
}
Parser::TPResult Parser::TryParsePtrOperatorSeq() {
while (true) {
if (TryAnnotateOptionalCXXScopeToken(true))
return TPResult::Error;
if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
(Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
ConsumeAnyToken();
if (!TrySkipAttributes())
return TPResult::Error;
while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
tok::kw__Nonnull, tok::kw__Nullable,
tok::kw__Nullable_result, tok::kw__Null_unspecified,
tok::kw__Atomic))
ConsumeToken();
} else {
return TPResult::True;
}
}
}
Parser::TPResult Parser::TryParseOperatorId() {
assert(Tok.is(tok::kw_operator));
ConsumeToken();
switch (Tok.getKind()) {
case tok::kw_new: case tok::kw_delete:
ConsumeToken();
if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
ConsumeBracket();
ConsumeBracket();
}
return TPResult::True;
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
case tok::Token:
#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
#include "clang/Basic/OperatorKinds.def"
ConsumeToken();
return TPResult::True;
case tok::l_square:
if (NextToken().is(tok::r_square)) {
ConsumeBracket();
ConsumeBracket();
return TPResult::True;
}
break;
case tok::l_paren:
if (NextToken().is(tok::r_paren)) {
ConsumeParen();
ConsumeParen();
return TPResult::True;
}
break;
default:
break;
}
if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
bool FoundUDSuffix = false;
do {
FoundUDSuffix |= Tok.hasUDSuffix();
ConsumeStringToken();
} while (isTokenStringLiteral());
if (!FoundUDSuffix) {
if (Tok.is(tok::identifier))
ConsumeToken();
else
return TPResult::Error;
}
return TPResult::True;
}
bool AnyDeclSpecifiers = false;
while (true) {
TPResult TPR = isCXXDeclarationSpecifier();
if (TPR == TPResult::Error)
return TPR;
if (TPR == TPResult::False) {
if (!AnyDeclSpecifiers)
return TPResult::Error;
break;
}
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
return TPResult::Error;
AnyDeclSpecifiers = true;
}
return TryParsePtrOperatorSeq();
}
Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
bool mayHaveIdentifier,
bool mayHaveDirectInit) {
if (TryParsePtrOperatorSeq() == TPResult::Error)
return TPResult::Error;
if (Tok.is(tok::ellipsis))
ConsumeToken();
if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
(Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
NextToken().is(tok::kw_operator)))) &&
mayHaveIdentifier) {
if (Tok.is(tok::annot_cxxscope)) {
CXXScopeSpec SS;
Actions.RestoreNestedNameSpecifierAnnotation(
Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
if (SS.isInvalid())
return TPResult::Error;
ConsumeAnnotationToken();
} else if (Tok.is(tok::identifier)) {
TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
}
if (Tok.is(tok::kw_operator)) {
if (TryParseOperatorId() == TPResult::Error)
return TPResult::Error;
} else
ConsumeToken();
} else if (Tok.is(tok::l_paren)) {
ConsumeParen();
if (mayBeAbstract &&
(Tok.is(tok::r_paren) || (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
isDeclarationSpecifier())) { TPResult TPR = TryParseFunctionDeclarator();
if (TPR != TPResult::Ambiguous)
return TPR;
} else {
if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
tok::kw___regcall, tok::kw___vectorcall))
return TPResult::True; TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
if (TPR != TPResult::Ambiguous)
return TPR;
if (Tok.isNot(tok::r_paren))
return TPResult::False;
ConsumeParen();
}
} else if (!mayBeAbstract) {
return TPResult::False;
}
if (mayHaveDirectInit)
return TPResult::Ambiguous;
while (true) {
TPResult TPR(TPResult::Ambiguous);
if (Tok.is(tok::l_paren)) {
if (!mayBeAbstract && !isCXXFunctionDeclarator())
break;
ConsumeParen();
TPR = TryParseFunctionDeclarator();
} else if (Tok.is(tok::l_square)) {
TPR = TryParseBracketDeclarator();
} else if (Tok.is(tok::kw_requires)) {
TPR = TPResult::True;
} else {
break;
}
if (TPR != TPResult::Ambiguous)
return TPR;
}
return TPResult::Ambiguous;
}
bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
return llvm::is_contained(TentativelyDeclaredIdentifiers, II);
}
namespace {
class TentativeParseCCC final : public CorrectionCandidateCallback {
public:
TentativeParseCCC(const Token &Next) {
WantRemainingKeywords = false;
WantTypeSpecifiers =
Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater, tok::l_brace,
tok::identifier, tok::comma);
}
bool ValidateCandidate(const TypoCorrection &Candidate) override {
if (Candidate.isResolved() && !Candidate.isKeyword() &&
llvm::all_of(Candidate,
[](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
return false;
return CorrectionCandidateCallback::ValidateCandidate(Candidate);
}
std::unique_ptr<CorrectionCandidateCallback> clone() override {
return std::make_unique<TentativeParseCCC>(*this);
}
};
}
Parser::TPResult
Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
bool *InvalidAsDeclSpec) {
auto IsPlaceholderSpecifier = [&] (TemplateIdAnnotation *TemplateId,
int Lookahead) {
return TemplateId->Kind == TNK_Concept_template &&
GetLookAheadToken(Lookahead + 1).isOneOf(tok::kw_auto, tok::kw_decltype,
tok::identifier);
};
switch (Tok.getKind()) {
case tok::identifier: {
if (TryAltiVecVectorToken())
return TPResult::True;
const Token &Next = NextToken();
if (!getLangOpts().ObjC && Next.is(tok::identifier))
return TPResult::True;
if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
TentativeParseCCC CCC(Next);
switch (TryAnnotateName(&CCC)) {
case ANK_Error:
return TPResult::Error;
case ANK_TentativeDecl:
return TPResult::False;
case ANK_TemplateName:
if (getLangOpts().CPlusPlus17) {
if (TryAnnotateTypeOrScopeToken())
return TPResult::Error;
if (Tok.isNot(tok::identifier))
break;
}
return GreaterThanIsOperator ? TPResult::True : TPResult::False;
case ANK_Unresolved:
return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
case ANK_Success:
break;
}
assert(Tok.isNot(tok::identifier) &&
"TryAnnotateName succeeded without producing an annotation");
} else {
if (TryAnnotateTypeOrScopeToken())
return TPResult::Error;
if (Tok.is(tok::identifier))
return TPResult::False;
}
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
}
case tok::kw_typename: if (TryAnnotateTypeOrScopeToken())
return TPResult::Error;
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
case tok::coloncolon: { const Token &Next = NextToken();
if (Next.isOneOf(tok::kw_new, tok::kw_delete)) return TPResult::False;
LLVM_FALLTHROUGH;
}
case tok::kw___super:
case tok::kw_decltype:
if (TryAnnotateTypeOrScopeToken())
return TPResult::Error;
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
case tok::kw_friend:
case tok::kw_typedef:
case tok::kw_constexpr:
case tok::kw_consteval:
case tok::kw_constinit:
case tok::kw_register:
case tok::kw_static:
case tok::kw_extern:
case tok::kw_mutable:
case tok::kw_auto:
case tok::kw___thread:
case tok::kw_thread_local:
case tok::kw__Thread_local:
case tok::kw_inline:
case tok::kw_virtual:
case tok::kw_explicit:
case tok::kw___module_private__:
case tok::kw___unknown_anytype:
case tok::kw_class:
case tok::kw_struct:
case tok::kw_union:
case tok::kw___interface:
case tok::kw_enum:
case tok::kw_const:
case tok::kw_volatile:
return TPResult::True;
case tok::kw_private:
if (!getLangOpts().OpenCL)
return TPResult::False;
LLVM_FALLTHROUGH;
case tok::kw___private:
case tok::kw___local:
case tok::kw___global:
case tok::kw___constant:
case tok::kw___generic:
case tok::kw___read_only:
case tok::kw___write_only:
case tok::kw___read_write:
case tok::kw_pipe:
case tok::kw_restrict:
case tok::kw__Complex:
case tok::kw___attribute:
case tok::kw___auto_type:
return TPResult::True;
case tok::kw___declspec:
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
case tok::kw___thiscall:
case tok::kw___regcall:
case tok::kw___vectorcall:
case tok::kw___w64:
case tok::kw___sptr:
case tok::kw___uptr:
case tok::kw___ptr64:
case tok::kw___ptr32:
case tok::kw___forceinline:
case tok::kw___unaligned:
case tok::kw__Nonnull:
case tok::kw__Nullable:
case tok::kw__Nullable_result:
case tok::kw__Null_unspecified:
case tok::kw___kindof:
return TPResult::True;
case tok::kw___pascal:
return TPResult::True;
case tok::kw___vector:
return TPResult::True;
case tok::annot_template_id: {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
if ((TemplateId->hasInvalidName() ||
TemplateId->Kind == TNK_Undeclared_template) &&
InvalidAsDeclSpec) {
*InvalidAsDeclSpec = NextToken().is(tok::l_paren);
return TPResult::Ambiguous;
}
if (TemplateId->hasInvalidName())
return TPResult::Error;
if (IsPlaceholderSpecifier(TemplateId, 0))
return TPResult::True;
if (TemplateId->Kind != TNK_Type_template)
return TPResult::False;
CXXScopeSpec SS;
AnnotateTemplateIdTokenAsType(SS);
assert(Tok.is(tok::annot_typename));
goto case_typename;
}
case tok::annot_cxxscope: if (TryAnnotateTypeOrScopeToken())
return TPResult::Error;
if (!Tok.is(tok::annot_typename)) {
if (Tok.is(tok::annot_cxxscope) &&
NextToken().is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId =
takeTemplateIdAnnotation(NextToken());
if (TemplateId->hasInvalidName()) {
if (InvalidAsDeclSpec) {
*InvalidAsDeclSpec = NextToken().is(tok::l_paren);
return TPResult::Ambiguous;
}
return TPResult::Error;
}
if (IsPlaceholderSpecifier(TemplateId, 1))
return TPResult::True;
}
if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
CXXScopeSpec SS;
Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
Tok.getAnnotationRange(),
SS);
if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
RevertingTentativeParsingAction PA(*this);
ConsumeAnnotationToken();
ConsumeToken();
bool isIdentifier = Tok.is(tok::identifier);
TPResult TPR = TPResult::False;
if (!isIdentifier)
TPR = isCXXDeclarationSpecifier(BracedCastResult,
InvalidAsDeclSpec);
if (isIdentifier ||
TPR == TPResult::True || TPR == TPResult::Error)
return TPResult::Error;
if (InvalidAsDeclSpec) {
*InvalidAsDeclSpec = true;
return TPResult::Ambiguous;
} else {
if (getLangOpts().MSVCCompat) {
if (((Tok.is(tok::amp) || Tok.is(tok::star)) &&
(NextToken().is(tok::r_paren) ||
NextToken().is(tok::greater))) ||
(Tok.is(tok::ampamp) && NextToken().is(tok::greater)))
return TPResult::True;
}
}
} else {
switch (TryAnnotateName()) {
case ANK_Error:
return TPResult::Error;
case ANK_TentativeDecl:
return TPResult::False;
case ANK_TemplateName:
if (getLangOpts().CPlusPlus17) {
if (TryAnnotateTypeOrScopeToken())
return TPResult::Error;
if (Tok.isNot(tok::identifier))
break;
}
return (getLangOpts().CPlusPlus17 || GreaterThanIsOperator)
? TPResult::True
: TPResult::False;
case ANK_Unresolved:
return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
case ANK_Success:
break;
}
assert(Tok.isNot(tok::annot_cxxscope) ||
NextToken().isNot(tok::identifier));
return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
}
}
return TPResult::False;
}
LLVM_FALLTHROUGH;
case tok::annot_typename:
case_typename:
if (getLangOpts().ObjC && NextToken().is(tok::less)) {
RevertingTentativeParsingAction PA(*this);
ConsumeAnyToken();
TPResult TPR = TryParseProtocolQualifiers();
bool isFollowedByParen = Tok.is(tok::l_paren);
bool isFollowedByBrace = Tok.is(tok::l_brace);
if (TPR == TPResult::Error)
return TPResult::Error;
if (isFollowedByParen)
return TPResult::Ambiguous;
if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
return BracedCastResult;
return TPResult::True;
}
LLVM_FALLTHROUGH;
case tok::kw_char:
case tok::kw_wchar_t:
case tok::kw_char8_t:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_bool:
case tok::kw_short:
case tok::kw_int:
case tok::kw_long:
case tok::kw___int64:
case tok::kw___int128:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw_half:
case tok::kw_float:
case tok::kw_double:
case tok::kw___bf16:
case tok::kw__Float16:
case tok::kw___float128:
case tok::kw___ibm128:
case tok::kw_void:
case tok::annot_decltype:
#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
#include "clang/Basic/OpenCLImageTypes.def"
if (NextToken().is(tok::l_paren))
return TPResult::Ambiguous;
if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
return BracedCastResult;
if (isStartOfObjCClassMessageMissingOpenBracket())
return TPResult::False;
return TPResult::True;
case tok::kw_typeof: {
if (NextToken().isNot(tok::l_paren))
return TPResult::True;
RevertingTentativeParsingAction PA(*this);
TPResult TPR = TryParseTypeofSpecifier();
bool isFollowedByParen = Tok.is(tok::l_paren);
bool isFollowedByBrace = Tok.is(tok::l_brace);
if (TPR == TPResult::Error)
return TPResult::Error;
if (isFollowedByParen)
return TPResult::Ambiguous;
if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
return BracedCastResult;
return TPResult::True;
}
case tok::kw___underlying_type:
return TPResult::True;
case tok::kw__Atomic:
return TPResult::True;
case tok::kw__BitInt:
case tok::kw__ExtInt: {
if (NextToken().isNot(tok::l_paren))
return TPResult::Error;
RevertingTentativeParsingAction PA(*this);
ConsumeToken();
ConsumeParen();
if (!SkipUntil(tok::r_paren, StopAtSemi))
return TPResult::Error;
if (Tok.is(tok::l_paren))
return TPResult::Ambiguous;
if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
return BracedCastResult;
return TPResult::True;
}
default:
return TPResult::False;
}
}
bool Parser::isCXXDeclarationSpecifierAType() {
switch (Tok.getKind()) {
case tok::annot_decltype:
case tok::annot_template_id:
case tok::annot_typename:
case tok::kw_typeof:
case tok::kw___underlying_type:
return true;
case tok::kw_class:
case tok::kw_struct:
case tok::kw_union:
case tok::kw___interface:
case tok::kw_enum:
return true;
case tok::kw_char:
case tok::kw_wchar_t:
case tok::kw_char8_t:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_bool:
case tok::kw_short:
case tok::kw_int:
case tok::kw__ExtInt:
case tok::kw__BitInt:
case tok::kw_long:
case tok::kw___int64:
case tok::kw___int128:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw_half:
case tok::kw_float:
case tok::kw_double:
case tok::kw___bf16:
case tok::kw__Float16:
case tok::kw___float128:
case tok::kw___ibm128:
case tok::kw_void:
case tok::kw___unknown_anytype:
case tok::kw___auto_type:
#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
#include "clang/Basic/OpenCLImageTypes.def"
return true;
case tok::kw_auto:
return getLangOpts().CPlusPlus11;
case tok::kw__Atomic:
return NextToken().is(tok::l_paren);
default:
return false;
}
}
Parser::TPResult Parser::TryParseTypeofSpecifier() {
assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
ConsumeToken();
assert(Tok.is(tok::l_paren) && "Expected '('");
ConsumeParen();
if (!SkipUntil(tok::r_paren, StopAtSemi))
return TPResult::Error;
return TPResult::Ambiguous;
}
Parser::TPResult Parser::TryParseProtocolQualifiers() {
assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
ConsumeToken();
do {
if (Tok.isNot(tok::identifier))
return TPResult::Error;
ConsumeToken();
if (Tok.is(tok::comma)) {
ConsumeToken();
continue;
}
if (Tok.is(tok::greater)) {
ConsumeToken();
return TPResult::Ambiguous;
}
} while (false);
return TPResult::Error;
}
bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
RevertingTentativeParsingAction PA(*this);
ConsumeParen();
bool InvalidAsDeclaration = false;
TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
if (TPR == TPResult::Ambiguous) {
if (Tok.isNot(tok::r_paren))
TPR = TPResult::False;
else {
const Token &Next = NextToken();
if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
tok::kw_throw, tok::kw_noexcept, tok::l_square,
tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
isCXX11VirtSpecifier(Next))
TPR = TPResult::True;
else if (InvalidAsDeclaration)
TPR = TPResult::False;
}
}
if (IsAmbiguous && TPR == TPResult::Ambiguous)
*IsAmbiguous = true;
return TPR != TPResult::False;
}
Parser::TPResult
Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
bool VersusTemplateArgument) {
if (Tok.is(tok::r_paren))
return TPResult::Ambiguous;
while (true) {
if (Tok.is(tok::ellipsis)) {
ConsumeToken();
if (Tok.is(tok::r_paren))
return TPResult::True; else
return TPResult::False;
}
if (isCXX11AttributeSpecifier(false,
true))
return TPResult::True;
ParsedAttributes attrs(AttrFactory);
MaybeParseMicrosoftAttributes(attrs);
TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
InvalidAsDeclaration);
if (TPR != TPResult::Ambiguous &&
!(VersusTemplateArgument && TPR == TPResult::True))
return TPR;
bool SeenType = false;
do {
SeenType |= isCXXDeclarationSpecifierAType();
if (TryConsumeDeclarationSpecifier() == TPResult::Error)
return TPResult::Error;
if (SeenType && Tok.is(tok::identifier))
return TPResult::True;
TPR = isCXXDeclarationSpecifier(TPResult::False,
InvalidAsDeclaration);
if (TPR == TPResult::Error)
return TPR;
if (TPR == TPResult::True && !VersusTemplateArgument)
return TPR;
} while (TPR != TPResult::False);
TPR = TryParseDeclarator(true);
if (TPR != TPResult::Ambiguous)
return TPR;
if (Tok.is(tok::kw___attribute))
return TPResult::True;
if (VersusTemplateArgument)
return Tok.is(tok::equal) ? TPResult::True : TPResult::False;
if (Tok.is(tok::equal)) {
if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
return TPResult::Error;
}
if (Tok.is(tok::ellipsis)) {
ConsumeToken();
if (Tok.is(tok::r_paren))
return TPResult::True; else
return TPResult::False;
}
if (!TryConsumeToken(tok::comma))
break;
}
return TPResult::Ambiguous;
}
Parser::TPResult Parser::TryParseFunctionDeclarator() {
TPResult TPR = TryParseParameterDeclarationClause();
if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
TPR = TPResult::False;
if (TPR == TPResult::False || TPR == TPResult::Error)
return TPR;
if (!SkipUntil(tok::r_paren, StopAtSemi))
return TPResult::Error;
while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw___unaligned,
tok::kw_restrict))
ConsumeToken();
if (Tok.isOneOf(tok::amp, tok::ampamp))
ConsumeToken();
if (Tok.is(tok::kw_throw)) {
ConsumeToken();
if (Tok.isNot(tok::l_paren))
return TPResult::Error;
ConsumeParen();
if (!SkipUntil(tok::r_paren, StopAtSemi))
return TPResult::Error;
}
if (Tok.is(tok::kw_noexcept)) {
ConsumeToken();
if (Tok.is(tok::l_paren)) {
ConsumeParen();
if (!SkipUntil(tok::r_paren, StopAtSemi))
return TPResult::Error;
}
}
return TPResult::Ambiguous;
}
Parser::TPResult Parser::TryParseBracketDeclarator() {
ConsumeBracket();
if (Tok.is(tok::l_brace))
return TPResult::False;
if (!SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch))
return TPResult::Error;
if (Tok.isNot(tok::r_square))
return TPResult::False;
ConsumeBracket();
return TPResult::Ambiguous;
}
Parser::TPResult Parser::isTemplateArgumentList(unsigned TokensToSkip) {
if (!TokensToSkip) {
if (Tok.isNot(tok::less))
return TPResult::False;
if (NextToken().is(tok::greater))
return TPResult::True;
}
RevertingTentativeParsingAction PA(*this);
while (TokensToSkip) {
ConsumeAnyToken();
--TokensToSkip;
}
if (!TryConsumeToken(tok::less))
return TPResult::False;
bool InvalidAsTemplateArgumentList = false;
if (isCXXDeclarationSpecifier(TPResult::False,
&InvalidAsTemplateArgumentList) ==
TPResult::True)
return TPResult::True;
if (InvalidAsTemplateArgumentList)
return TPResult::False;
if (SkipUntil({tok::greater, tok::greatergreater, tok::greatergreatergreater},
StopAtSemi | StopBeforeMatch))
return TPResult::Ambiguous;
return TPResult::False;
}
Parser::TPResult Parser::isExplicitBool() {
assert(Tok.is(tok::l_paren) && "expected to be looking at a '(' token");
RevertingTentativeParsingAction PA(*this);
ConsumeParen();
while (Tok.is(tok::l_paren))
ConsumeParen();
if (TryAnnotateOptionalCXXScopeToken())
return TPResult::Error;
CXXScopeSpec SS;
if (Tok.is(tok::annot_cxxscope)) {
Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
Tok.getAnnotationRange(),
SS);
ConsumeAnnotationToken();
}
if (Tok.is(tok::kw_operator))
return TPResult::Ambiguous;
if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
return TPResult::True;
if (!Actions.isCurrentClassName(Tok.is(tok::identifier)
? *Tok.getIdentifierInfo()
: *takeTemplateIdAnnotation(Tok)->Name,
getCurScope(), &SS))
return TPResult::True;
if (!NextToken().is(tok::r_paren) &&
!isConstructorDeclarator(SS.isEmpty(),
false))
return TPResult::True;
return TPResult::Ambiguous;
}