Skip to content

Commit

Permalink
fix: Add support for c++23 lambda specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Sep 20, 2023
1 parent e3669c7 commit eac4fad
Show file tree
Hide file tree
Showing 28 changed files with 831 additions and 560 deletions.
20 changes: 18 additions & 2 deletions packages/cxx-frontend/src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,21 @@ export class LambdaIntroducerAST extends AST {
}
}

export class LambdaSpecifierAST extends AST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
context: Context,
): Result {
return visitor.visitLambdaSpecifier(this, context);
}
getSpecifierToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
}
getSpecifier(): TokenKind {
return cxx.getASTSlot(this.getHandle(), 1);
}
}

export class LambdaDeclaratorAST extends AST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
Expand All @@ -564,13 +579,13 @@ export class LambdaDeclaratorAST extends AST {
getRparenToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
}
*getDeclSpecifierList(): Generator<SpecifierAST | undefined> {
*getLambdaSpecifierList(): Generator<LambdaSpecifierAST | undefined> {
for (
let it = cxx.getASTSlot(this.getHandle(), 3);
it;
it = cxx.getListNext(it)
) {
yield AST.from<SpecifierAST>(cxx.getListValue(it), this.parser);
yield AST.from<LambdaSpecifierAST>(cxx.getListValue(it), this.parser);
}
}
getExceptionSpecifier(): ExceptionSpecifierAST | undefined {
Expand Down Expand Up @@ -5601,6 +5616,7 @@ const AST_CONSTRUCTORS: Array<
ParameterDeclarationClauseAST,
ParametersAndQualifiersAST,
LambdaIntroducerAST,
LambdaSpecifierAST,
LambdaDeclaratorAST,
TrailingReturnTypeAST,
CtorInitializerAST,
Expand Down
1 change: 1 addition & 0 deletions packages/cxx-frontend/src/ASTKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum ASTKind {
ParameterDeclarationClause,
ParametersAndQualifiers,
LambdaIntroducer,
LambdaSpecifier,
LambdaDeclarator,
TrailingReturnType,
CtorInitializer,
Expand Down
201 changes: 101 additions & 100 deletions packages/cxx-frontend/src/ASTSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,104 +136,105 @@ export enum ASTSlot {
isVirtual = 114,
lambdaDeclarator = 115,
lambdaIntroducer = 116,
lbraceLoc = 117,
lbracket2Loc = 118,
lbracketLoc = 119,
leftExpression = 120,
lessLoc = 121,
literal = 122,
literalLoc = 123,
literalOperatorId = 124,
lparen2Loc = 125,
lparenLoc = 126,
memInitializerList = 127,
memberId = 128,
minusGreaterLoc = 129,
moduleDeclaration = 130,
moduleLoc = 131,
moduleName = 132,
modulePartition = 133,
moduleQualifier = 134,
mutableLoc = 135,
namespaceLoc = 136,
nestedNameSpecifier = 137,
nestedNamespaceSpecifierList = 138,
newDeclarator = 139,
newInitalizer = 140,
newLoc = 141,
newPlacement = 142,
noexceptLoc = 143,
op = 144,
opLoc = 145,
openLoc = 146,
operatorFunctionId = 147,
operatorLoc = 148,
parameterDeclarationClause = 149,
parameterDeclarationList = 150,
parametersAndQualifiers = 151,
privateLoc = 152,
privateModuleFragment = 153,
ptrOpList = 154,
questionLoc = 155,
rangeDeclaration = 156,
rangeInitializer = 157,
rbraceLoc = 158,
rbracket2Loc = 159,
rbracketLoc = 160,
refLoc = 161,
refOp = 162,
refQualifierLoc = 163,
requirementBody = 164,
requirementList = 165,
requiresClause = 166,
requiresLoc = 167,
restrictLoc = 168,
returnLoc = 169,
rightExpression = 170,
rparen2Loc = 171,
rparenLoc = 172,
scopeLoc = 173,
semicolonLoc = 174,
sizeExpression = 175,
sizeofLoc = 176,
specifier = 177,
specifierLoc = 178,
starLoc = 179,
statement = 180,
statementList = 181,
staticAssertLoc = 182,
staticLoc = 183,
stringLiteral = 184,
stringliteralLoc = 185,
switchLoc = 186,
templateArgumentList = 187,
templateId = 188,
templateLoc = 189,
templateParameterList = 190,
thisLoc = 191,
threadLoc = 192,
threadLocalLoc = 193,
throwLoc = 194,
tildeLoc = 195,
trailingReturnType = 196,
tryLoc = 197,
typeConstraint = 198,
typeId = 199,
typeIdList = 200,
typeSpecifier = 201,
typeSpecifierList = 202,
typeTraits = 203,
typeTraitsLoc = 204,
typedefLoc = 205,
typeidLoc = 206,
typenameLoc = 207,
underlyingTypeLoc = 208,
unqualifiedId = 209,
usingDeclaratorList = 210,
usingLoc = 211,
virtualLoc = 212,
voidLoc = 213,
volatileLoc = 214,
whileLoc = 215,
yieldLoc = 216,
lambdaSpecifierList = 117,
lbraceLoc = 118,
lbracket2Loc = 119,
lbracketLoc = 120,
leftExpression = 121,
lessLoc = 122,
literal = 123,
literalLoc = 124,
literalOperatorId = 125,
lparen2Loc = 126,
lparenLoc = 127,
memInitializerList = 128,
memberId = 129,
minusGreaterLoc = 130,
moduleDeclaration = 131,
moduleLoc = 132,
moduleName = 133,
modulePartition = 134,
moduleQualifier = 135,
mutableLoc = 136,
namespaceLoc = 137,
nestedNameSpecifier = 138,
nestedNamespaceSpecifierList = 139,
newDeclarator = 140,
newInitalizer = 141,
newLoc = 142,
newPlacement = 143,
noexceptLoc = 144,
op = 145,
opLoc = 146,
openLoc = 147,
operatorFunctionId = 148,
operatorLoc = 149,
parameterDeclarationClause = 150,
parameterDeclarationList = 151,
parametersAndQualifiers = 152,
privateLoc = 153,
privateModuleFragment = 154,
ptrOpList = 155,
questionLoc = 156,
rangeDeclaration = 157,
rangeInitializer = 158,
rbraceLoc = 159,
rbracket2Loc = 160,
rbracketLoc = 161,
refLoc = 162,
refOp = 163,
refQualifierLoc = 164,
requirementBody = 165,
requirementList = 166,
requiresClause = 167,
requiresLoc = 168,
restrictLoc = 169,
returnLoc = 170,
rightExpression = 171,
rparen2Loc = 172,
rparenLoc = 173,
scopeLoc = 174,
semicolonLoc = 175,
sizeExpression = 176,
sizeofLoc = 177,
specifier = 178,
specifierLoc = 179,
starLoc = 180,
statement = 181,
statementList = 182,
staticAssertLoc = 183,
staticLoc = 184,
stringLiteral = 185,
stringliteralLoc = 186,
switchLoc = 187,
templateArgumentList = 188,
templateId = 189,
templateLoc = 190,
templateParameterList = 191,
thisLoc = 192,
threadLoc = 193,
threadLocalLoc = 194,
throwLoc = 195,
tildeLoc = 196,
trailingReturnType = 197,
tryLoc = 198,
typeConstraint = 199,
typeId = 200,
typeIdList = 201,
typeSpecifier = 202,
typeSpecifierList = 203,
typeTraits = 204,
typeTraitsLoc = 205,
typedefLoc = 206,
typeidLoc = 207,
typenameLoc = 208,
underlyingTypeLoc = 209,
unqualifiedId = 210,
usingDeclaratorList = 211,
usingLoc = 212,
virtualLoc = 213,
voidLoc = 214,
volatileLoc = 215,
whileLoc = 216,
yieldLoc = 217,
}
4 changes: 4 additions & 0 deletions packages/cxx-frontend/src/ASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export abstract class ASTVisitor<Context, Result> {
node: ast.LambdaIntroducerAST,
context: Context,
): Result;
abstract visitLambdaSpecifier(
node: ast.LambdaSpecifierAST,
context: Context,
): Result;
abstract visitLambdaDeclarator(
node: ast.LambdaDeclaratorAST,
context: Context,
Expand Down
4 changes: 3 additions & 1 deletion packages/cxx-frontend/src/RecursiveASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
}
}

visitLambdaSpecifier(node: ast.LambdaSpecifierAST, context: Context): void {}

visitLambdaDeclarator(node: ast.LambdaDeclaratorAST, context: Context): void {
this.accept(node.getParameterDeclarationClause(), context);
for (const element of node.getDeclSpecifierList()) {
for (const element of node.getLambdaSpecifierList()) {
this.accept(element, context);
}
this.accept(node.getExceptionSpecifier(), context);
Expand Down
16 changes: 13 additions & 3 deletions src/frontend/cxx/ast_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,24 @@ void ASTPrinter::visit(LambdaIntroducerAST* ast) {
}
}

void ASTPrinter::visit(LambdaSpecifierAST* ast) {
fmt::print(out_, "{}\n", "lambda-specifier");
if (ast->specifier != TokenKind::T_EOF_SYMBOL) {
++indent_;
fmt::print(out_, "{:{}}", "", indent_ * 2);
fmt::print(out_, "specifier: {}\n", Token::spell(ast->specifier));
--indent_;
}
}

void ASTPrinter::visit(LambdaDeclaratorAST* ast) {
fmt::print(out_, "{}\n", "lambda-declarator");
accept(ast->parameterDeclarationClause, "parameter-declaration-clause");
if (ast->declSpecifierList) {
if (ast->lambdaSpecifierList) {
++indent_;
fmt::print(out_, "{:{}}", "", indent_ * 2);
fmt::print(out_, "{}\n", "decl-specifier-list");
for (auto it = ast->declSpecifierList; it; it = it->next) {
fmt::print(out_, "{}\n", "lambda-specifier-list");
for (auto it = ast->lambdaSpecifierList; it; it = it->next) {
accept(it->value);
}
--indent_;
Expand Down
1 change: 1 addition & 0 deletions src/frontend/cxx/ast_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ASTPrinter : ASTVisitor {
void visit(ParameterDeclarationClauseAST* ast) override;
void visit(ParametersAndQualifiersAST* ast) override;
void visit(LambdaIntroducerAST* ast) override;
void visit(LambdaSpecifierAST* ast) override;
void visit(LambdaDeclaratorAST* ast) override;
void visit(TrailingReturnTypeAST* ast) override;
void visit(CtorInitializerAST* ast) override;
Expand Down
14 changes: 12 additions & 2 deletions src/parser/cxx/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,22 @@ auto LambdaIntroducerAST::lastSourceLocation() -> SourceLocation {
return {};
}

auto LambdaSpecifierAST::firstSourceLocation() -> SourceLocation {
if (auto loc = cxx::firstSourceLocation(specifierLoc)) return loc;
return {};
}

auto LambdaSpecifierAST::lastSourceLocation() -> SourceLocation {
if (auto loc = cxx::lastSourceLocation(specifierLoc)) return loc;
return {};
}

auto LambdaDeclaratorAST::firstSourceLocation() -> SourceLocation {
if (auto loc = cxx::firstSourceLocation(lparenLoc)) return loc;
if (auto loc = cxx::firstSourceLocation(parameterDeclarationClause))
return loc;
if (auto loc = cxx::firstSourceLocation(rparenLoc)) return loc;
if (auto loc = cxx::firstSourceLocation(declSpecifierList)) return loc;
if (auto loc = cxx::firstSourceLocation(lambdaSpecifierList)) return loc;
if (auto loc = cxx::firstSourceLocation(exceptionSpecifier)) return loc;
if (auto loc = cxx::firstSourceLocation(attributeList)) return loc;
if (auto loc = cxx::firstSourceLocation(trailingReturnType)) return loc;
Expand All @@ -262,7 +272,7 @@ auto LambdaDeclaratorAST::lastSourceLocation() -> SourceLocation {
if (auto loc = cxx::lastSourceLocation(trailingReturnType)) return loc;
if (auto loc = cxx::lastSourceLocation(attributeList)) return loc;
if (auto loc = cxx::lastSourceLocation(exceptionSpecifier)) return loc;
if (auto loc = cxx::lastSourceLocation(declSpecifierList)) return loc;
if (auto loc = cxx::lastSourceLocation(lambdaSpecifierList)) return loc;
if (auto loc = cxx::lastSourceLocation(rparenLoc)) return loc;
if (auto loc = cxx::lastSourceLocation(parameterDeclarationClause))
return loc;
Expand Down
8 changes: 7 additions & 1 deletion src/parser/cxx/ast.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ union AST {
ParameterDeclarationClause,
ParametersAndQualifiers,
LambdaIntroducer,
LambdaSpecifier,
LambdaDeclarator,
TrailingReturnType,
CtorInitializer,
Expand Down Expand Up @@ -405,9 +406,14 @@ table LambdaIntroducer /* AST */ {
rbracket_loc: SourceLocation;
}

table LambdaSpecifier /* AST */ {
specifier: uint32;
specifier_loc: SourceLocation;
}

table LambdaDeclarator /* AST */ {
parameter_declaration_clause: ParameterDeclarationClause;
decl_specifier_list: [Specifier];
lambda_specifier_list: [LambdaSpecifier];
exception_specifier: ExceptionSpecifier;
attribute_list: [AttributeSpecifier];
trailing_return_type: TrailingReturnType;
Expand Down
Loading

0 comments on commit eac4fad

Please sign in to comment.