Skip to content

Commit 914b6c7

Browse files
cmticekuilpd
authored andcommitted
[LLDB] Add boolean literals to DIL. (llvm#157992)
This adds the ability to recognize (and create ValueObjects for) boolean literals ("true", "false") to DIL. This is a preliminary step to adding type casting (and also for the ternary op).
1 parent e839f0d commit 914b6c7

File tree

10 files changed

+73
-2
lines changed

10 files changed

+73
-2
lines changed

lldb/docs/dil-expr-lang.ebnf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ postfix_expression = primary_expression
3636
| postfix_expression "->" id_expression ;
3737
3838
primary_expression = numeric_literal
39+
| boolean_literal
3940
| id_expression
4041
| "(" expression ")" ;
4142
@@ -53,6 +54,8 @@ identifier = ? C99 Identifier ? ;
5354
numeric_literal = ? Integer constant: hexademical, decimal, octal, binary ?
5455
| ? Floating constant ? ;
5556
57+
boolean_literal = "true" | "false" ;
58+
5659
register = "$" ? Register name ? ;
5760
5861
nested_name_specifier = type_name "::"

lldb/include/lldb/ValueObject/DILAST.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum class NodeKind {
2222
eArraySubscriptNode,
2323
eBinaryOpNode,
2424
eBitExtractionNode,
25+
eBooleanLiteralNode,
2526
eCStyleCastNode,
2627
eErrorNode,
2728
eFloatLiteralNode,
@@ -290,6 +291,24 @@ class FloatLiteralNode : public ASTNode {
290291
llvm::APFloat m_value;
291292
};
292293

294+
class BooleanLiteralNode : public ASTNode {
295+
public:
296+
BooleanLiteralNode(uint32_t location, bool value)
297+
: ASTNode(location, NodeKind::eBooleanLiteralNode), m_value(value) {}
298+
299+
llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
300+
301+
bool IsConstLiteral() const override { return true; }
302+
bool GetValue() const & { return m_value; }
303+
304+
static bool classof(const ASTNode *node) {
305+
return node->GetKind() == NodeKind::eBooleanLiteralNode;
306+
}
307+
308+
private:
309+
bool m_value;
310+
};
311+
293312
class CStyleCastNode : public ASTNode {
294313
public:
295314
CStyleCastNode(uint32_t location, CompilerType type, ASTNodeUP operand,
@@ -337,6 +356,8 @@ class Visitor {
337356
virtual llvm::Expected<lldb::ValueObjectSP>
338357
Visit(const FloatLiteralNode *node) = 0;
339358
virtual llvm::Expected<lldb::ValueObjectSP>
359+
Visit(const BooleanLiteralNode *node) = 0;
360+
virtual llvm::Expected<lldb::ValueObjectSP>
340361
Visit(const CStyleCastNode *node) = 0;
341362
};
342363

lldb/include/lldb/ValueObject/DILEval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class Interpreter : Visitor {
6262
llvm::Expected<lldb::ValueObjectSP>
6363
Visit(const FloatLiteralNode *node) override;
6464
llvm::Expected<lldb::ValueObjectSP>
65+
Visit(const BooleanLiteralNode *node) override;
66+
llvm::Expected<lldb::ValueObjectSP>
6567
Visit(const CStyleCastNode *node) override;
6668

6769
lldb::ValueObjectSP

lldb/include/lldb/ValueObject/DILLexer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Token {
3434
greatergreater,
3535
identifier,
3636
integer_constant,
37+
kw_false,
38+
kw_true,
3739
l_paren,
3840
l_square,
3941
lessless,

lldb/include/lldb/ValueObject/DILParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class DILParser {
163163
ASTNodeUP ParseNumericLiteral();
164164
ASTNodeUP ParseIntegerLiteral();
165165
ASTNodeUP ParseFloatingPointLiteral();
166+
ASTNodeUP ParseBooleanLiteral();
166167

167168
ASTNodeUP ParseCastExpression();
168169
std::optional<CompilerType> ParseTypeId(bool must_be_type_id = false);

lldb/source/ValueObject/DILAST.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ llvm::Expected<lldb::ValueObjectSP> FloatLiteralNode::Accept(Visitor *v) const {
7878
return v->Visit(this);
7979
}
8080

81+
llvm::Expected<lldb::ValueObjectSP>
82+
BooleanLiteralNode::Accept(Visitor *v) const {
83+
return v->Visit(this);
84+
}
85+
8186
llvm::Expected<lldb::ValueObjectSP> CStyleCastNode::Accept(Visitor *v) const {
8287
return v->Visit(this);
8388
}

lldb/source/ValueObject/DILEval.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,12 @@ Interpreter::Visit(const FloatLiteralNode *node) {
12961296
"result");
12971297
}
12981298

1299+
llvm::Expected<lldb::ValueObjectSP>
1300+
Interpreter::Visit(const BooleanLiteralNode *node) {
1301+
bool value = node->GetValue();
1302+
return ValueObject::CreateValueObjectFromBool(m_target, value, "result");
1303+
}
1304+
12991305
llvm::Expected<CompilerType> Interpreter::VerifyCStyleCastType(
13001306
lldb::ValueObjectSP &operand, CompilerType &op_type,
13011307
CompilerType target_type, CastPromoKind &promo_kind,

lldb/source/ValueObject/DILLexer.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ llvm::StringRef Token::GetTokenName(Kind kind) {
4040
return "identifier";
4141
case Kind::integer_constant:
4242
return "integer_constant";
43+
case Kind::kw_false:
44+
return "false";
45+
case Kind::kw_true:
46+
return "true";
4347
case Kind::l_paren:
4448
return "l_paren";
4549
case Kind::l_square:
@@ -150,8 +154,14 @@ llvm::Expected<Token> DILLexer::Lex(llvm::StringRef expr,
150154
return Token(kind, maybe_number->str(), position);
151155
}
152156
std::optional<llvm::StringRef> maybe_word = IsWord(expr, remainder);
153-
if (maybe_word)
154-
return Token(Token::identifier, maybe_word->str(), position);
157+
if (maybe_word) {
158+
llvm::StringRef word = *maybe_word;
159+
Token::Kind kind = llvm::StringSwitch<Token::Kind>(word)
160+
.Case("false", Token::kw_false)
161+
.Case("true", Token::kw_true)
162+
.Default(Token::identifier);
163+
return Token(kind, word.str(), position);
164+
}
155165

156166
constexpr std::pair<Token::Kind, const char *> operators[] = {
157167
{Token::arrow, "->"},

lldb/source/ValueObject/DILParser.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,15 @@ ASTNodeUP DILParser::ParsePostfixExpression() {
425425
//
426426
// primary_expression:
427427
// numeric_literal
428+
// boolean_literal
428429
// id_expression
429430
// "(" expression ")"
430431
//
431432
ASTNodeUP DILParser::ParsePrimaryExpression() {
432433
if (CurToken().IsOneOf({Token::integer_constant, Token::float_constant}))
433434
return ParseNumericLiteral();
435+
if (CurToken().IsOneOf({Token::kw_true, Token::kw_false}))
436+
return ParseBooleanLiteral();
434437
if (CurToken().IsOneOf(
435438
{Token::coloncolon, Token::identifier, Token::l_paren})) {
436439
// Save the source location for the diagnostics message.
@@ -811,6 +814,20 @@ std::string DILParser::ParseUnqualifiedId() {
811814
return identifier;
812815
}
813816

817+
// Parse an boolean_literal.
818+
//
819+
// boolean_literal:
820+
// "true"
821+
// "false"
822+
//
823+
ASTNodeUP DILParser::ParseBooleanLiteral() {
824+
ExpectOneOf(std::vector<Token::Kind>{Token::kw_true, Token::kw_false});
825+
uint32_t loc = CurToken().GetLocation();
826+
bool literal_value = CurToken().Is(Token::kw_true);
827+
m_dil_lexer.Advance();
828+
return std::make_unique<BooleanLiteralNode>(loc, literal_value);
829+
}
830+
814831
CompilerType
815832
DILParser::ResolveTypeDeclarators(CompilerType type,
816833
const std::vector<Token> &ptr_operators) {

lldb/test/API/commands/frame/var-dil/expr/Literals/TestFrameVarDILLiterals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def test_literals(self):
1919

2020
self.runCmd("settings set target.experimental.use-DIL true")
2121

22+
# Check boolean literals parsing
23+
self.expect_var_path("true", value="true", type="bool")
24+
self.expect_var_path("false", value="false", type="bool")
25+
2226
# Check number literals parsing
2327
self.expect_var_path("1.0", value="1", type="double")
2428
self.expect_var_path("1.0f", value="1", type="float")

0 commit comments

Comments
 (0)