Skip to content

Commit a2fbad8

Browse files
committed
fix: Store the value of the boolean literals in the AST
1 parent ad55a0d commit a2fbad8

File tree

8 files changed

+42
-6
lines changed

8 files changed

+42
-6
lines changed

src/frontend/cxx/ast_printer.cc

+4
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,10 @@ void ASTPrinter::visit(CharLiteralExpressionAST* ast) {
610610

611611
void ASTPrinter::visit(BoolLiteralExpressionAST* ast) {
612612
fmt::print(out_, "{}\n", "bool-literal-expression");
613+
++indent_;
614+
fmt::print(out_, "{:{}}", "", indent_ * 2);
615+
fmt::print(out_, "value: {}\n", ast->value);
616+
--indent_;
613617
}
614618

615619
void ASTPrinter::visit(IntLiteralExpressionAST* ast) {

src/parser/cxx/ast.fbs

-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,6 @@ table CharLiteralExpression /* ExpressionAST */ {
760760
}
761761

762762
table BoolLiteralExpression /* ExpressionAST */ {
763-
literal: uint32;
764763
literal_loc: SourceLocation;
765764
}
766765

src/parser/cxx/ast.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ class BoolLiteralExpressionAST final : public ExpressionAST {
10211021
BoolLiteralExpressionAST() : ExpressionAST(ASTKind::BoolLiteralExpression) {}
10221022

10231023
SourceLocation literalLoc;
1024-
TokenKind literal = TokenKind::T_EOF_SYMBOL;
1024+
bool value = false;
10251025

10261026
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
10271027

src/parser/cxx/ast_cloner.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ void ASTCloner::visit(BoolLiteralExpressionAST* ast) {
10081008

10091009
copy->literalLoc = ast->literalLoc;
10101010

1011-
copy->literal = ast->literal;
1011+
copy->value = ast->value;
10121012
}
10131013

10141014
void ASTCloner::visit(IntLiteralExpressionAST* ast) {

src/parser/cxx/ast_decoder.cc

-1
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,6 @@ auto ASTDecoder::decodeBoolLiteralExpression(
14991499
if (!node) return nullptr;
15001500

15011501
auto ast = new (pool_) BoolLiteralExpressionAST();
1502-
ast->literal = static_cast<TokenKind>(node->literal());
15031502
return ast;
15041503
}
15051504

src/parser/cxx/ast_encoder.cc

-1
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,6 @@ void ASTEncoder::visit(BoolLiteralExpressionAST* ast) {
17021702

17031703
io::BoolLiteralExpression::Builder builder{fbb_};
17041704
builder.add_literal_loc(literalLoc.o);
1705-
builder.add_literal(static_cast<std::uint32_t>(ast->literal));
17061705

17071706
offset_ = builder.Finish().Union();
17081707
type_ = io::Expression_BoolLiteralExpression;

src/parser/cxx/parser.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,10 @@ auto Parser::parse_literal(ExpressionAST*& yyast) -> bool {
382382
auto ast = new (pool) BoolLiteralExpressionAST();
383383
yyast = ast;
384384

385+
const auto value = LA().is(TokenKind::T_TRUE);
386+
385387
ast->literalLoc = consumeToken();
386-
ast->literal = unit->tokenKind(ast->literalLoc);
388+
ast->value = value;
387389

388390
return true;
389391
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %cxx -verify -ast-dump %s | %filecheck %s --match-full-lines
2+
3+
const bool ok = true;
4+
const bool ko = false;
5+
6+
// CHECK:translation-unit
7+
// CHECK-NEXT: declaration-list
8+
// CHECK-NEXT: simple-declaration
9+
// CHECK-NEXT: decl-specifier-list
10+
// CHECK-NEXT: const-qualifier
11+
// CHECK-NEXT: integral-type-specifier
12+
// CHECK-NEXT: init-declarator-list
13+
// CHECK-NEXT: init-declarator
14+
// CHECK-NEXT: declarator: declarator
15+
// CHECK-NEXT: core-declarator: id-declarator
16+
// CHECK-NEXT: name: simple-name
17+
// CHECK-NEXT: identifier: ok
18+
// CHECK-NEXT: initializer: equal-initializer
19+
// CHECK-NEXT: expression: bool-literal-expression
20+
// CHECK-NEXT: value: true
21+
// CHECK-NEXT: simple-declaration
22+
// CHECK-NEXT: decl-specifier-list
23+
// CHECK-NEXT: const-qualifier
24+
// CHECK-NEXT: integral-type-specifier
25+
// CHECK-NEXT: init-declarator-list
26+
// CHECK-NEXT: init-declarator
27+
// CHECK-NEXT: declarator: declarator
28+
// CHECK-NEXT: core-declarator: id-declarator
29+
// CHECK-NEXT: name: simple-name
30+
// CHECK-NEXT: identifier: ko
31+
// CHECK-NEXT: initializer: equal-initializer
32+
// CHECK-NEXT: expression: bool-literal-expression
33+
// CHECK-NEXT: value: false

0 commit comments

Comments
 (0)