Skip to content

Commit eac4fad

Browse files
committed
fix: Add support for c++23 lambda specifiers
1 parent e3669c7 commit eac4fad

28 files changed

+831
-560
lines changed

packages/cxx-frontend/src/AST.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,21 @@ export class LambdaIntroducerAST extends AST {
545545
}
546546
}
547547

548+
export class LambdaSpecifierAST extends AST {
549+
accept<Context, Result>(
550+
visitor: ASTVisitor<Context, Result>,
551+
context: Context,
552+
): Result {
553+
return visitor.visitLambdaSpecifier(this, context);
554+
}
555+
getSpecifierToken(): Token | undefined {
556+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
557+
}
558+
getSpecifier(): TokenKind {
559+
return cxx.getASTSlot(this.getHandle(), 1);
560+
}
561+
}
562+
548563
export class LambdaDeclaratorAST extends AST {
549564
accept<Context, Result>(
550565
visitor: ASTVisitor<Context, Result>,
@@ -564,13 +579,13 @@ export class LambdaDeclaratorAST extends AST {
564579
getRparenToken(): Token | undefined {
565580
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
566581
}
567-
*getDeclSpecifierList(): Generator<SpecifierAST | undefined> {
582+
*getLambdaSpecifierList(): Generator<LambdaSpecifierAST | undefined> {
568583
for (
569584
let it = cxx.getASTSlot(this.getHandle(), 3);
570585
it;
571586
it = cxx.getListNext(it)
572587
) {
573-
yield AST.from<SpecifierAST>(cxx.getListValue(it), this.parser);
588+
yield AST.from<LambdaSpecifierAST>(cxx.getListValue(it), this.parser);
574589
}
575590
}
576591
getExceptionSpecifier(): ExceptionSpecifierAST | undefined {
@@ -5601,6 +5616,7 @@ const AST_CONSTRUCTORS: Array<
56015616
ParameterDeclarationClauseAST,
56025617
ParametersAndQualifiersAST,
56035618
LambdaIntroducerAST,
5619+
LambdaSpecifierAST,
56045620
LambdaDeclaratorAST,
56055621
TrailingReturnTypeAST,
56065622
CtorInitializerAST,

packages/cxx-frontend/src/ASTKind.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export enum ASTKind {
3535
ParameterDeclarationClause,
3636
ParametersAndQualifiers,
3737
LambdaIntroducer,
38+
LambdaSpecifier,
3839
LambdaDeclarator,
3940
TrailingReturnType,
4041
CtorInitializer,

packages/cxx-frontend/src/ASTSlot.ts

Lines changed: 101 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -136,104 +136,105 @@ export enum ASTSlot {
136136
isVirtual = 114,
137137
lambdaDeclarator = 115,
138138
lambdaIntroducer = 116,
139-
lbraceLoc = 117,
140-
lbracket2Loc = 118,
141-
lbracketLoc = 119,
142-
leftExpression = 120,
143-
lessLoc = 121,
144-
literal = 122,
145-
literalLoc = 123,
146-
literalOperatorId = 124,
147-
lparen2Loc = 125,
148-
lparenLoc = 126,
149-
memInitializerList = 127,
150-
memberId = 128,
151-
minusGreaterLoc = 129,
152-
moduleDeclaration = 130,
153-
moduleLoc = 131,
154-
moduleName = 132,
155-
modulePartition = 133,
156-
moduleQualifier = 134,
157-
mutableLoc = 135,
158-
namespaceLoc = 136,
159-
nestedNameSpecifier = 137,
160-
nestedNamespaceSpecifierList = 138,
161-
newDeclarator = 139,
162-
newInitalizer = 140,
163-
newLoc = 141,
164-
newPlacement = 142,
165-
noexceptLoc = 143,
166-
op = 144,
167-
opLoc = 145,
168-
openLoc = 146,
169-
operatorFunctionId = 147,
170-
operatorLoc = 148,
171-
parameterDeclarationClause = 149,
172-
parameterDeclarationList = 150,
173-
parametersAndQualifiers = 151,
174-
privateLoc = 152,
175-
privateModuleFragment = 153,
176-
ptrOpList = 154,
177-
questionLoc = 155,
178-
rangeDeclaration = 156,
179-
rangeInitializer = 157,
180-
rbraceLoc = 158,
181-
rbracket2Loc = 159,
182-
rbracketLoc = 160,
183-
refLoc = 161,
184-
refOp = 162,
185-
refQualifierLoc = 163,
186-
requirementBody = 164,
187-
requirementList = 165,
188-
requiresClause = 166,
189-
requiresLoc = 167,
190-
restrictLoc = 168,
191-
returnLoc = 169,
192-
rightExpression = 170,
193-
rparen2Loc = 171,
194-
rparenLoc = 172,
195-
scopeLoc = 173,
196-
semicolonLoc = 174,
197-
sizeExpression = 175,
198-
sizeofLoc = 176,
199-
specifier = 177,
200-
specifierLoc = 178,
201-
starLoc = 179,
202-
statement = 180,
203-
statementList = 181,
204-
staticAssertLoc = 182,
205-
staticLoc = 183,
206-
stringLiteral = 184,
207-
stringliteralLoc = 185,
208-
switchLoc = 186,
209-
templateArgumentList = 187,
210-
templateId = 188,
211-
templateLoc = 189,
212-
templateParameterList = 190,
213-
thisLoc = 191,
214-
threadLoc = 192,
215-
threadLocalLoc = 193,
216-
throwLoc = 194,
217-
tildeLoc = 195,
218-
trailingReturnType = 196,
219-
tryLoc = 197,
220-
typeConstraint = 198,
221-
typeId = 199,
222-
typeIdList = 200,
223-
typeSpecifier = 201,
224-
typeSpecifierList = 202,
225-
typeTraits = 203,
226-
typeTraitsLoc = 204,
227-
typedefLoc = 205,
228-
typeidLoc = 206,
229-
typenameLoc = 207,
230-
underlyingTypeLoc = 208,
231-
unqualifiedId = 209,
232-
usingDeclaratorList = 210,
233-
usingLoc = 211,
234-
virtualLoc = 212,
235-
voidLoc = 213,
236-
volatileLoc = 214,
237-
whileLoc = 215,
238-
yieldLoc = 216,
139+
lambdaSpecifierList = 117,
140+
lbraceLoc = 118,
141+
lbracket2Loc = 119,
142+
lbracketLoc = 120,
143+
leftExpression = 121,
144+
lessLoc = 122,
145+
literal = 123,
146+
literalLoc = 124,
147+
literalOperatorId = 125,
148+
lparen2Loc = 126,
149+
lparenLoc = 127,
150+
memInitializerList = 128,
151+
memberId = 129,
152+
minusGreaterLoc = 130,
153+
moduleDeclaration = 131,
154+
moduleLoc = 132,
155+
moduleName = 133,
156+
modulePartition = 134,
157+
moduleQualifier = 135,
158+
mutableLoc = 136,
159+
namespaceLoc = 137,
160+
nestedNameSpecifier = 138,
161+
nestedNamespaceSpecifierList = 139,
162+
newDeclarator = 140,
163+
newInitalizer = 141,
164+
newLoc = 142,
165+
newPlacement = 143,
166+
noexceptLoc = 144,
167+
op = 145,
168+
opLoc = 146,
169+
openLoc = 147,
170+
operatorFunctionId = 148,
171+
operatorLoc = 149,
172+
parameterDeclarationClause = 150,
173+
parameterDeclarationList = 151,
174+
parametersAndQualifiers = 152,
175+
privateLoc = 153,
176+
privateModuleFragment = 154,
177+
ptrOpList = 155,
178+
questionLoc = 156,
179+
rangeDeclaration = 157,
180+
rangeInitializer = 158,
181+
rbraceLoc = 159,
182+
rbracket2Loc = 160,
183+
rbracketLoc = 161,
184+
refLoc = 162,
185+
refOp = 163,
186+
refQualifierLoc = 164,
187+
requirementBody = 165,
188+
requirementList = 166,
189+
requiresClause = 167,
190+
requiresLoc = 168,
191+
restrictLoc = 169,
192+
returnLoc = 170,
193+
rightExpression = 171,
194+
rparen2Loc = 172,
195+
rparenLoc = 173,
196+
scopeLoc = 174,
197+
semicolonLoc = 175,
198+
sizeExpression = 176,
199+
sizeofLoc = 177,
200+
specifier = 178,
201+
specifierLoc = 179,
202+
starLoc = 180,
203+
statement = 181,
204+
statementList = 182,
205+
staticAssertLoc = 183,
206+
staticLoc = 184,
207+
stringLiteral = 185,
208+
stringliteralLoc = 186,
209+
switchLoc = 187,
210+
templateArgumentList = 188,
211+
templateId = 189,
212+
templateLoc = 190,
213+
templateParameterList = 191,
214+
thisLoc = 192,
215+
threadLoc = 193,
216+
threadLocalLoc = 194,
217+
throwLoc = 195,
218+
tildeLoc = 196,
219+
trailingReturnType = 197,
220+
tryLoc = 198,
221+
typeConstraint = 199,
222+
typeId = 200,
223+
typeIdList = 201,
224+
typeSpecifier = 202,
225+
typeSpecifierList = 203,
226+
typeTraits = 204,
227+
typeTraitsLoc = 205,
228+
typedefLoc = 206,
229+
typeidLoc = 207,
230+
typenameLoc = 208,
231+
underlyingTypeLoc = 209,
232+
unqualifiedId = 210,
233+
usingDeclaratorList = 211,
234+
usingLoc = 212,
235+
virtualLoc = 213,
236+
voidLoc = 214,
237+
volatileLoc = 215,
238+
whileLoc = 216,
239+
yieldLoc = 217,
239240
}

packages/cxx-frontend/src/ASTVisitor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export abstract class ASTVisitor<Context, Result> {
6363
node: ast.LambdaIntroducerAST,
6464
context: Context,
6565
): Result;
66+
abstract visitLambdaSpecifier(
67+
node: ast.LambdaSpecifierAST,
68+
context: Context,
69+
): Result;
6670
abstract visitLambdaDeclarator(
6771
node: ast.LambdaDeclaratorAST,
6872
context: Context,

packages/cxx-frontend/src/RecursiveASTVisitor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
139139
}
140140
}
141141

142+
visitLambdaSpecifier(node: ast.LambdaSpecifierAST, context: Context): void {}
143+
142144
visitLambdaDeclarator(node: ast.LambdaDeclaratorAST, context: Context): void {
143145
this.accept(node.getParameterDeclarationClause(), context);
144-
for (const element of node.getDeclSpecifierList()) {
146+
for (const element of node.getLambdaSpecifierList()) {
145147
this.accept(element, context);
146148
}
147149
this.accept(node.getExceptionSpecifier(), context);

src/frontend/cxx/ast_printer.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,24 @@ void ASTPrinter::visit(LambdaIntroducerAST* ast) {
289289
}
290290
}
291291

292+
void ASTPrinter::visit(LambdaSpecifierAST* ast) {
293+
fmt::print(out_, "{}\n", "lambda-specifier");
294+
if (ast->specifier != TokenKind::T_EOF_SYMBOL) {
295+
++indent_;
296+
fmt::print(out_, "{:{}}", "", indent_ * 2);
297+
fmt::print(out_, "specifier: {}\n", Token::spell(ast->specifier));
298+
--indent_;
299+
}
300+
}
301+
292302
void ASTPrinter::visit(LambdaDeclaratorAST* ast) {
293303
fmt::print(out_, "{}\n", "lambda-declarator");
294304
accept(ast->parameterDeclarationClause, "parameter-declaration-clause");
295-
if (ast->declSpecifierList) {
305+
if (ast->lambdaSpecifierList) {
296306
++indent_;
297307
fmt::print(out_, "{:{}}", "", indent_ * 2);
298-
fmt::print(out_, "{}\n", "decl-specifier-list");
299-
for (auto it = ast->declSpecifierList; it; it = it->next) {
308+
fmt::print(out_, "{}\n", "lambda-specifier-list");
309+
for (auto it = ast->lambdaSpecifierList; it; it = it->next) {
300310
accept(it->value);
301311
}
302312
--indent_;

src/frontend/cxx/ast_printer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ASTPrinter : ASTVisitor {
5757
void visit(ParameterDeclarationClauseAST* ast) override;
5858
void visit(ParametersAndQualifiersAST* ast) override;
5959
void visit(LambdaIntroducerAST* ast) override;
60+
void visit(LambdaSpecifierAST* ast) override;
6061
void visit(LambdaDeclaratorAST* ast) override;
6162
void visit(TrailingReturnTypeAST* ast) override;
6263
void visit(CtorInitializerAST* ast) override;

src/parser/cxx/ast.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,22 @@ auto LambdaIntroducerAST::lastSourceLocation() -> SourceLocation {
244244
return {};
245245
}
246246

247+
auto LambdaSpecifierAST::firstSourceLocation() -> SourceLocation {
248+
if (auto loc = cxx::firstSourceLocation(specifierLoc)) return loc;
249+
return {};
250+
}
251+
252+
auto LambdaSpecifierAST::lastSourceLocation() -> SourceLocation {
253+
if (auto loc = cxx::lastSourceLocation(specifierLoc)) return loc;
254+
return {};
255+
}
256+
247257
auto LambdaDeclaratorAST::firstSourceLocation() -> SourceLocation {
248258
if (auto loc = cxx::firstSourceLocation(lparenLoc)) return loc;
249259
if (auto loc = cxx::firstSourceLocation(parameterDeclarationClause))
250260
return loc;
251261
if (auto loc = cxx::firstSourceLocation(rparenLoc)) return loc;
252-
if (auto loc = cxx::firstSourceLocation(declSpecifierList)) return loc;
262+
if (auto loc = cxx::firstSourceLocation(lambdaSpecifierList)) return loc;
253263
if (auto loc = cxx::firstSourceLocation(exceptionSpecifier)) return loc;
254264
if (auto loc = cxx::firstSourceLocation(attributeList)) return loc;
255265
if (auto loc = cxx::firstSourceLocation(trailingReturnType)) return loc;
@@ -262,7 +272,7 @@ auto LambdaDeclaratorAST::lastSourceLocation() -> SourceLocation {
262272
if (auto loc = cxx::lastSourceLocation(trailingReturnType)) return loc;
263273
if (auto loc = cxx::lastSourceLocation(attributeList)) return loc;
264274
if (auto loc = cxx::lastSourceLocation(exceptionSpecifier)) return loc;
265-
if (auto loc = cxx::lastSourceLocation(declSpecifierList)) return loc;
275+
if (auto loc = cxx::lastSourceLocation(lambdaSpecifierList)) return loc;
266276
if (auto loc = cxx::lastSourceLocation(rparenLoc)) return loc;
267277
if (auto loc = cxx::lastSourceLocation(parameterDeclarationClause))
268278
return loc;

src/parser/cxx/ast.fbs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ union AST {
4646
ParameterDeclarationClause,
4747
ParametersAndQualifiers,
4848
LambdaIntroducer,
49+
LambdaSpecifier,
4950
LambdaDeclarator,
5051
TrailingReturnType,
5152
CtorInitializer,
@@ -405,9 +406,14 @@ table LambdaIntroducer /* AST */ {
405406
rbracket_loc: SourceLocation;
406407
}
407408

409+
table LambdaSpecifier /* AST */ {
410+
specifier: uint32;
411+
specifier_loc: SourceLocation;
412+
}
413+
408414
table LambdaDeclarator /* AST */ {
409415
parameter_declaration_clause: ParameterDeclarationClause;
410-
decl_specifier_list: [Specifier];
416+
lambda_specifier_list: [LambdaSpecifier];
411417
exception_specifier: ExceptionSpecifier;
412418
attribute_list: [AttributeSpecifier];
413419
trailing_return_type: TrailingReturnType;

0 commit comments

Comments
 (0)