Skip to content

Commit 6ebf73e

Browse files
committed
fix: Add SizeTypeSpecifierAST and SignTypeSpecifierAST
1 parent 7fe5c70 commit 6ebf73e

26 files changed

+317
-26
lines changed

packages/cxx-frontend/src/AST.ts

+32
Original file line numberDiff line numberDiff line change
@@ -4711,6 +4711,36 @@ export class VoidTypeSpecifierAST extends SpecifierAST {
47114711
}
47124712
}
47134713

4714+
export class SizeTypeSpecifierAST extends SpecifierAST {
4715+
accept<Context, Result>(
4716+
visitor: ASTVisitor<Context, Result>,
4717+
context: Context,
4718+
): Result {
4719+
return visitor.visitSizeTypeSpecifier(this, context);
4720+
}
4721+
getSpecifierToken(): Token | undefined {
4722+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
4723+
}
4724+
getSpecifier(): TokenKind {
4725+
return cxx.getASTSlot(this.getHandle(), 1);
4726+
}
4727+
}
4728+
4729+
export class SignTypeSpecifierAST extends SpecifierAST {
4730+
accept<Context, Result>(
4731+
visitor: ASTVisitor<Context, Result>,
4732+
context: Context,
4733+
): Result {
4734+
return visitor.visitSignTypeSpecifier(this, context);
4735+
}
4736+
getSpecifierToken(): Token | undefined {
4737+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
4738+
}
4739+
getSpecifier(): TokenKind {
4740+
return cxx.getASTSlot(this.getHandle(), 1);
4741+
}
4742+
}
4743+
47144744
export class VaListTypeSpecifierAST extends SpecifierAST {
47154745
accept<Context, Result>(
47164746
visitor: ASTVisitor<Context, Result>,
@@ -5704,6 +5734,8 @@ const AST_CONSTRUCTORS: Array<
57045734
ExplicitSpecifierAST,
57055735
AutoTypeSpecifierAST,
57065736
VoidTypeSpecifierAST,
5737+
SizeTypeSpecifierAST,
5738+
SignTypeSpecifierAST,
57075739
VaListTypeSpecifierAST,
57085740
IntegralTypeSpecifierAST,
57095741
FloatingPointTypeSpecifierAST,

packages/cxx-frontend/src/ASTKind.ts

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ export enum ASTKind {
231231
ExplicitSpecifier,
232232
AutoTypeSpecifier,
233233
VoidTypeSpecifier,
234+
SizeTypeSpecifier,
235+
SignTypeSpecifier,
234236
VaListTypeSpecifier,
235237
IntegralTypeSpecifier,
236238
FloatingPointTypeSpecifier,

packages/cxx-frontend/src/ASTVisitor.ts

+8
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@ export abstract class ASTVisitor<Context, Result> {
730730
node: ast.VoidTypeSpecifierAST,
731731
context: Context,
732732
): Result;
733+
abstract visitSizeTypeSpecifier(
734+
node: ast.SizeTypeSpecifierAST,
735+
context: Context,
736+
): Result;
737+
abstract visitSignTypeSpecifier(
738+
node: ast.SignTypeSpecifierAST,
739+
context: Context,
740+
): Result;
733741
abstract visitVaListTypeSpecifier(
734742
node: ast.VaListTypeSpecifierAST,
735743
context: Context,

packages/cxx-frontend/src/RecursiveASTVisitor.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,16 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
12381238
context: Context,
12391239
): void {}
12401240

1241+
visitSizeTypeSpecifier(
1242+
node: ast.SizeTypeSpecifierAST,
1243+
context: Context,
1244+
): void {}
1245+
1246+
visitSignTypeSpecifier(
1247+
node: ast.SignTypeSpecifierAST,
1248+
context: Context,
1249+
): void {}
1250+
12411251
visitVaListTypeSpecifier(
12421252
node: ast.VaListTypeSpecifierAST,
12431253
context: Context,

packages/cxx-storybook/src/SyntaxTree.tsx

+21-8
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,31 @@ import { CSSProperties, useEffect, useRef, useState } from "react";
2424
import AutoSizer from "react-virtualized-auto-sizer";
2525

2626
function hasOp(node: any): node is AST & { getOp(): TokenKind } {
27-
return typeof node.getOp === "function";
27+
return typeof node.getOp === "function" && node.getOp();
2828
}
2929

30-
function hasIdentifier(node: any): node is AST & { getIdentifier(): string } {
31-
return typeof node.getIdentifier === "function";
30+
function hasAccessOp(node: any): node is AST & { getAccessOp(): TokenKind } {
31+
return typeof node.getAccessOp === "function" && node.getAccessOp();
3232
}
3333

34-
function hasNamespaceName(
34+
function hasAccessSpecifier(
3535
node: any
36-
): node is AST & { getNamespaceName(): string } {
37-
return typeof node.getNamespaceName === "function";
36+
): node is AST & { getAccessSpecifier(): TokenKind } {
37+
return (
38+
typeof node.getAccessSpecifier === "function" && node.getAccessSpecifier()
39+
);
40+
}
41+
42+
function hasSpecifier(node: any): node is AST & { getSpecifier(): TokenKind } {
43+
return typeof node.getSpecifier === "function" && node.getSpecifier();
44+
}
45+
46+
function hasIdentifier(node: any): node is AST & { getIdentifier(): string } {
47+
return typeof node.getIdentifier === "function" && node.getIdentifier();
3848
}
3949

4050
function hasLiteral(node: any): node is AST & { getLiteral(): string } {
41-
return typeof node.getLiteral === "function";
51+
return typeof node.getLiteral === "function" && node.getLiteral();
4252
}
4353

4454
interface SyntaxTreeProps {
@@ -68,10 +78,13 @@ export function SyntaxTree({ parser, cursorPosition }: SyntaxTreeProps) {
6878
const kind = ASTKind[node.getKind()];
6979

7080
let extra = "";
71-
if (hasNamespaceName(node)) extra += ` (${node.getNamespaceName()})`;
7281
if (hasIdentifier(node)) extra += ` (${node.getIdentifier()})`;
7382
if (hasLiteral(node)) extra += ` (${node.getLiteral()})`;
7483
if (hasOp(node)) extra += ` (${TokenKind[node.getOp()]})`;
84+
if (hasAccessOp(node)) extra += ` (${TokenKind[node.getAccessOp()]})`;
85+
if (hasSpecifier(node)) extra += ` (${TokenKind[node.getSpecifier()]})`;
86+
if (hasAccessSpecifier(node))
87+
extra += ` (${TokenKind[node.getAccessSpecifier()]})`;
7588

7689
const description = `${kind}${extra}`;
7790
const handle = node.getHandle();

src/frontend/cxx/ast_printer.cc

+20
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,26 @@ void ASTPrinter::visit(VoidTypeSpecifierAST* ast) {
17921792
fmt::print(out_, "{}\n", "void-type-specifier");
17931793
}
17941794

1795+
void ASTPrinter::visit(SizeTypeSpecifierAST* ast) {
1796+
fmt::print(out_, "{}\n", "size-type-specifier");
1797+
if (ast->specifier != TokenKind::T_EOF_SYMBOL) {
1798+
++indent_;
1799+
fmt::print(out_, "{:{}}", "", indent_ * 2);
1800+
fmt::print(out_, "specifier: {}\n", Token::spell(ast->specifier));
1801+
--indent_;
1802+
}
1803+
}
1804+
1805+
void ASTPrinter::visit(SignTypeSpecifierAST* ast) {
1806+
fmt::print(out_, "{}\n", "sign-type-specifier");
1807+
if (ast->specifier != TokenKind::T_EOF_SYMBOL) {
1808+
++indent_;
1809+
fmt::print(out_, "{:{}}", "", indent_ * 2);
1810+
fmt::print(out_, "specifier: {}\n", Token::spell(ast->specifier));
1811+
--indent_;
1812+
}
1813+
}
1814+
17951815
void ASTPrinter::visit(VaListTypeSpecifierAST* ast) {
17961816
fmt::print(out_, "{}\n", "va-list-type-specifier");
17971817
if (ast->specifier != TokenKind::T_EOF_SYMBOL) {

src/frontend/cxx/ast_printer.h

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ class ASTPrinter : ASTVisitor {
238238
void visit(ExplicitSpecifierAST* ast) override;
239239
void visit(AutoTypeSpecifierAST* ast) override;
240240
void visit(VoidTypeSpecifierAST* ast) override;
241+
void visit(SizeTypeSpecifierAST* ast) override;
242+
void visit(SignTypeSpecifierAST* ast) override;
241243
void visit(VaListTypeSpecifierAST* ast) override;
242244
void visit(IntegralTypeSpecifierAST* ast) override;
243245
void visit(FloatingPointTypeSpecifierAST* ast) override;

src/parser/cxx/ast.cc

+34-14
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,20 @@ auto NewPlacementAST::lastSourceLocation() -> SourceLocation {
494494
return {};
495495
}
496496

497+
auto NestedNamespaceSpecifierAST::firstSourceLocation() -> SourceLocation {
498+
if (auto loc = cxx::firstSourceLocation(inlineLoc)) return loc;
499+
if (auto loc = cxx::firstSourceLocation(identifierLoc)) return loc;
500+
if (auto loc = cxx::firstSourceLocation(scopeLoc)) return loc;
501+
return {};
502+
}
503+
504+
auto NestedNamespaceSpecifierAST::lastSourceLocation() -> SourceLocation {
505+
if (auto loc = cxx::lastSourceLocation(scopeLoc)) return loc;
506+
if (auto loc = cxx::lastSourceLocation(identifierLoc)) return loc;
507+
if (auto loc = cxx::lastSourceLocation(inlineLoc)) return loc;
508+
return {};
509+
}
510+
497511
auto GlobalNestedNameSpecifierAST::firstSourceLocation() -> SourceLocation {
498512
if (auto loc = cxx::firstSourceLocation(scopeLoc)) return loc;
499513
return {};
@@ -2080,20 +2094,6 @@ auto OpaqueEnumDeclarationAST::lastSourceLocation() -> SourceLocation {
20802094
return {};
20812095
}
20822096

2083-
auto NestedNamespaceSpecifierAST::firstSourceLocation() -> SourceLocation {
2084-
if (auto loc = cxx::firstSourceLocation(inlineLoc)) return loc;
2085-
if (auto loc = cxx::firstSourceLocation(identifierLoc)) return loc;
2086-
if (auto loc = cxx::firstSourceLocation(scopeLoc)) return loc;
2087-
return {};
2088-
}
2089-
2090-
auto NestedNamespaceSpecifierAST::lastSourceLocation() -> SourceLocation {
2091-
if (auto loc = cxx::lastSourceLocation(scopeLoc)) return loc;
2092-
if (auto loc = cxx::lastSourceLocation(identifierLoc)) return loc;
2093-
if (auto loc = cxx::lastSourceLocation(inlineLoc)) return loc;
2094-
return {};
2095-
}
2096-
20972097
auto NamespaceDefinitionAST::firstSourceLocation() -> SourceLocation {
20982098
if (auto loc = cxx::firstSourceLocation(inlineLoc)) return loc;
20992099
if (auto loc = cxx::firstSourceLocation(namespaceLoc)) return loc;
@@ -2694,6 +2694,26 @@ auto VoidTypeSpecifierAST::lastSourceLocation() -> SourceLocation {
26942694
return {};
26952695
}
26962696

2697+
auto SizeTypeSpecifierAST::firstSourceLocation() -> SourceLocation {
2698+
if (auto loc = cxx::firstSourceLocation(specifierLoc)) return loc;
2699+
return {};
2700+
}
2701+
2702+
auto SizeTypeSpecifierAST::lastSourceLocation() -> SourceLocation {
2703+
if (auto loc = cxx::lastSourceLocation(specifierLoc)) return loc;
2704+
return {};
2705+
}
2706+
2707+
auto SignTypeSpecifierAST::firstSourceLocation() -> SourceLocation {
2708+
if (auto loc = cxx::firstSourceLocation(specifierLoc)) return loc;
2709+
return {};
2710+
}
2711+
2712+
auto SignTypeSpecifierAST::lastSourceLocation() -> SourceLocation {
2713+
if (auto loc = cxx::lastSourceLocation(specifierLoc)) return loc;
2714+
return {};
2715+
}
2716+
26972717
auto VaListTypeSpecifierAST::firstSourceLocation() -> SourceLocation {
26982718
if (auto loc = cxx::firstSourceLocation(specifierLoc)) return loc;
26992719
return {};

src/parser/cxx/ast.fbs

+12
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ union Specifier {
244244
ExplicitSpecifier,
245245
AutoTypeSpecifier,
246246
VoidTypeSpecifier,
247+
SizeTypeSpecifier,
248+
SignTypeSpecifier,
247249
VaListTypeSpecifier,
248250
IntegralTypeSpecifier,
249251
FloatingPointTypeSpecifier,
@@ -1391,6 +1393,16 @@ table VoidTypeSpecifier /* SpecifierAST */ {
13911393
void_loc: SourceLocation;
13921394
}
13931395

1396+
table SizeTypeSpecifier /* SpecifierAST */ {
1397+
specifier: uint32;
1398+
specifier_loc: SourceLocation;
1399+
}
1400+
1401+
table SignTypeSpecifier /* SpecifierAST */ {
1402+
specifier: uint32;
1403+
specifier_loc: SourceLocation;
1404+
}
1405+
13941406
table VaListTypeSpecifier /* SpecifierAST */ {
13951407
specifier: uint32;
13961408
specifier_loc: SourceLocation;

src/parser/cxx/ast.h

+26
Original file line numberDiff line numberDiff line change
@@ -2886,6 +2886,32 @@ class VoidTypeSpecifierAST final : public SpecifierAST {
28862886
auto lastSourceLocation() -> SourceLocation override;
28872887
};
28882888

2889+
class SizeTypeSpecifierAST final : public SpecifierAST {
2890+
public:
2891+
SizeTypeSpecifierAST() : SpecifierAST(ASTKind::SizeTypeSpecifier) {}
2892+
2893+
SourceLocation specifierLoc;
2894+
TokenKind specifier = TokenKind::T_EOF_SYMBOL;
2895+
2896+
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
2897+
2898+
auto firstSourceLocation() -> SourceLocation override;
2899+
auto lastSourceLocation() -> SourceLocation override;
2900+
};
2901+
2902+
class SignTypeSpecifierAST final : public SpecifierAST {
2903+
public:
2904+
SignTypeSpecifierAST() : SpecifierAST(ASTKind::SignTypeSpecifier) {}
2905+
2906+
SourceLocation specifierLoc;
2907+
TokenKind specifier = TokenKind::T_EOF_SYMBOL;
2908+
2909+
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
2910+
2911+
auto firstSourceLocation() -> SourceLocation override;
2912+
auto lastSourceLocation() -> SourceLocation override;
2913+
};
2914+
28892915
class VaListTypeSpecifierAST final : public SpecifierAST {
28902916
public:
28912917
VaListTypeSpecifierAST() : SpecifierAST(ASTKind::VaListTypeSpecifier) {}

src/parser/cxx/ast_cloner.cc

+22
Original file line numberDiff line numberDiff line change
@@ -3296,6 +3296,28 @@ void ASTCloner::visit(VoidTypeSpecifierAST* ast) {
32963296
copy->voidLoc = ast->voidLoc;
32973297
}
32983298

3299+
void ASTCloner::visit(SizeTypeSpecifierAST* ast) {
3300+
auto copy = new (arena_) SizeTypeSpecifierAST();
3301+
copy_ = copy;
3302+
3303+
copy->setChecked(ast->checked());
3304+
3305+
copy->specifierLoc = ast->specifierLoc;
3306+
3307+
copy->specifier = ast->specifier;
3308+
}
3309+
3310+
void ASTCloner::visit(SignTypeSpecifierAST* ast) {
3311+
auto copy = new (arena_) SignTypeSpecifierAST();
3312+
copy_ = copy;
3313+
3314+
copy->setChecked(ast->checked());
3315+
3316+
copy->specifierLoc = ast->specifierLoc;
3317+
3318+
copy->specifier = ast->specifier;
3319+
}
3320+
32993321
void ASTCloner::visit(VaListTypeSpecifierAST* ast) {
33003322
auto copy = new (arena_) VaListTypeSpecifierAST();
33013323
copy_ = copy;

src/parser/cxx/ast_cloner.h

+2
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ class ASTCloner : public ASTVisitor {
226226
void visit(ExplicitSpecifierAST* ast) override;
227227
void visit(AutoTypeSpecifierAST* ast) override;
228228
void visit(VoidTypeSpecifierAST* ast) override;
229+
void visit(SizeTypeSpecifierAST* ast) override;
230+
void visit(SignTypeSpecifierAST* ast) override;
229231
void visit(VaListTypeSpecifierAST* ast) override;
230232
void visit(IntegralTypeSpecifierAST* ast) override;
231233
void visit(FloatingPointTypeSpecifierAST* ast) override;

src/parser/cxx/ast_decoder.cc

+24
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ auto ASTDecoder::decodeSpecifier(const void* ptr, io::Specifier type)
601601
case io::Specifier_VoidTypeSpecifier:
602602
return decodeVoidTypeSpecifier(
603603
reinterpret_cast<const io::VoidTypeSpecifier*>(ptr));
604+
case io::Specifier_SizeTypeSpecifier:
605+
return decodeSizeTypeSpecifier(
606+
reinterpret_cast<const io::SizeTypeSpecifier*>(ptr));
607+
case io::Specifier_SignTypeSpecifier:
608+
return decodeSignTypeSpecifier(
609+
reinterpret_cast<const io::SignTypeSpecifier*>(ptr));
604610
case io::Specifier_VaListTypeSpecifier:
605611
return decodeVaListTypeSpecifier(
606612
reinterpret_cast<const io::VaListTypeSpecifier*>(ptr));
@@ -3223,6 +3229,24 @@ auto ASTDecoder::decodeVoidTypeSpecifier(const io::VoidTypeSpecifier* node)
32233229
return ast;
32243230
}
32253231

3232+
auto ASTDecoder::decodeSizeTypeSpecifier(const io::SizeTypeSpecifier* node)
3233+
-> SizeTypeSpecifierAST* {
3234+
if (!node) return nullptr;
3235+
3236+
auto ast = new (pool_) SizeTypeSpecifierAST();
3237+
ast->specifier = static_cast<TokenKind>(node->specifier());
3238+
return ast;
3239+
}
3240+
3241+
auto ASTDecoder::decodeSignTypeSpecifier(const io::SignTypeSpecifier* node)
3242+
-> SignTypeSpecifierAST* {
3243+
if (!node) return nullptr;
3244+
3245+
auto ast = new (pool_) SignTypeSpecifierAST();
3246+
ast->specifier = static_cast<TokenKind>(node->specifier());
3247+
return ast;
3248+
}
3249+
32263250
auto ASTDecoder::decodeVaListTypeSpecifier(const io::VaListTypeSpecifier* node)
32273251
-> VaListTypeSpecifierAST* {
32283252
if (!node) return nullptr;

0 commit comments

Comments
 (0)