Skip to content

Commit 44cf7ad

Browse files
committed
[LLDB] Add type casting to DIL, part 1 of 3.
This is an alternative to PR 159500, breaking that PR down into three separate PRs, to make it easier to review. This first PR of the three adds the basic framework for doing type casing to the DIL code, but it does not actually do any casting: In this PR the DIL parser only recognizes builtin type names, and the DIL interpreter does not do anything except return the original operand (no casting). The second and third PRs will add most of the type parsing, and do the actual type casting, respectively.
1 parent f767f23 commit 44cf7ad

File tree

7 files changed

+245
-7
lines changed

7 files changed

+245
-7
lines changed

lldb/docs/dil-expr-lang.ebnf

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
(* This is currently a subset of the final DIL Language, matching the current
44
DIL implementation. *)
55
6-
expression = unary_expression ;
6+
expression = cast_expression;
7+
8+
cast_expression = unary_expression
9+
| "(" type_id ")" cast_expression;
710
811
unary_expression = postfix_expression
9-
| unary_operator expression ;
12+
| unary_operator cast_expression ;
1013
1114
unary_operator = "*" | "&" ;
1215
@@ -44,10 +47,28 @@ nested_name_specifier = type_name "::"
4447
| namespace_name '::'
4548
| nested_name_specifier identifier "::" ;
4649
50+
type_id = type_specifier_seq [abstract_declarator] ;
51+
52+
type_specifier_seq = type_specifier [type_specifier];
53+
54+
type_specifier = ["::"] [nested_name_specifier] type_name
55+
| builtin_typename ;
56+
57+
nested_name_specifier = type_name "::"
58+
| namespace_name "::"
59+
| nested_name_specifier identifier "::" ;
60+
61+
abstract_declarator = ptr_operator [abstract_declarator] ;
62+
63+
ptr_operator = "*"
64+
| "&";
65+
4766
type_name = class_name
4867
| enum_name
4968
| typedef_name;
5069
70+
builtin_typename = identifier_seq;
71+
5172
class_name = identifier ;
5273
5374
enum_name = identifier ;
@@ -56,6 +77,7 @@ typedef_name = identifier ;
5677
5778
namespace_name = identifier ;
5879
59-
80+
identifier_seq = identifier
81+
| identifier identifier_seq;
6082
6183

lldb/include/lldb/ValueObject/DILAST.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum class NodeKind {
2121
eArraySubscriptNode,
2222
eBitExtractionNode,
2323
eBooleanLiteralNode,
24+
eCStyleCastNode,
2425
eErrorNode,
2526
eFloatLiteralNode,
2627
eIdentifierNode,
@@ -35,6 +36,14 @@ enum class UnaryOpKind {
3536
Deref, // "*"
3637
};
3738

39+
/// The C-Style casts allowed by DIL.
40+
enum class CStyleCastKind {
41+
eEnumeration,
42+
eNullptr,
43+
eReference,
44+
eNone,
45+
};
46+
3847
/// Forward declaration, for use in DIL AST nodes. Definition is at the very
3948
/// end of this file.
4049
class Visitor;
@@ -244,6 +253,29 @@ class BooleanLiteralNode : public ASTNode {
244253
bool m_value;
245254
};
246255

256+
class CStyleCastNode : public ASTNode {
257+
public:
258+
CStyleCastNode(uint32_t location, CompilerType type, ASTNodeUP operand,
259+
CStyleCastKind kind)
260+
: ASTNode(location, NodeKind::eCStyleCastNode), m_type(type),
261+
m_operand(std::move(operand)), m_cast_kind(kind) {}
262+
263+
llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
264+
265+
CompilerType GetType() const { return m_type; }
266+
ASTNode *GetOperand() const { return m_operand.get(); }
267+
CStyleCastKind GetCastKind() const { return m_cast_kind; }
268+
269+
static bool classof(const ASTNode *node) {
270+
return node->GetKind() == NodeKind::eCStyleCastNode;
271+
}
272+
273+
private:
274+
CompilerType m_type;
275+
ASTNodeUP m_operand;
276+
CStyleCastKind m_cast_kind;
277+
};
278+
247279
/// This class contains one Visit method for each specialized type of
248280
/// DIL AST node. The Visit methods are used to dispatch a DIL AST node to
249281
/// the correct function in the DIL expression evaluator for evaluating that
@@ -267,6 +299,8 @@ class Visitor {
267299
Visit(const FloatLiteralNode *node) = 0;
268300
virtual llvm::Expected<lldb::ValueObjectSP>
269301
Visit(const BooleanLiteralNode *node) = 0;
302+
virtual llvm::Expected<lldb::ValueObjectSP>
303+
Visit(const CStyleCastNode *node) = 0;
270304
};
271305

272306
} // namespace lldb_private::dil

lldb/include/lldb/ValueObject/DILEval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class Interpreter : Visitor {
6060
Visit(const FloatLiteralNode *node) override;
6161
llvm::Expected<lldb::ValueObjectSP>
6262
Visit(const BooleanLiteralNode *node) override;
63+
llvm::Expected<lldb::ValueObjectSP>
64+
Visit(const CStyleCastNode *node) override;
6365

6466
llvm::Expected<CompilerType>
6567
PickIntegerType(lldb::TypeSystemSP type_system,

lldb/include/lldb/ValueObject/DILParser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ class DILParser {
101101
ASTNodeUP ParseFloatingPointLiteral();
102102
ASTNodeUP ParseBooleanLiteral();
103103

104+
ASTNodeUP ParseCastExpression();
105+
std::optional<CompilerType> ParseBuiltinType();
106+
std::optional<CompilerType> ParseTypeId();
107+
CompilerType ResolveTypeDeclarators(CompilerType type,
108+
const std::vector<Token> &ptr_operators);
109+
104110
void BailOut(const std::string &error, uint32_t loc, uint16_t err_len);
105111

106112
void Expect(Token::Kind kind);

lldb/source/ValueObject/DILAST.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ BooleanLiteralNode::Accept(Visitor *v) const {
5151
return v->Visit(this);
5252
}
5353

54+
llvm::Expected<lldb::ValueObjectSP> CStyleCastNode::Accept(Visitor *v) const {
55+
return v->Visit(this);
56+
}
57+
5458
} // namespace lldb_private::dil

lldb/source/ValueObject/DILEval.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,16 @@ Interpreter::Visit(const BooleanLiteralNode *node) {
608608
return ValueObject::CreateValueObjectFromBool(m_target, value, "result");
609609
}
610610

611+
llvm::Expected<lldb::ValueObjectSP>
612+
Interpreter::Visit(const CStyleCastNode *node) {
613+
auto operand_or_err = Evaluate(node->GetOperand());
614+
if (!operand_or_err)
615+
return operand_or_err;
616+
617+
lldb::ValueObjectSP operand = *operand_or_err;
618+
// Don't actually do the cast for now -- that code will be added later.
619+
// For now just return the original operand, unchanged.
620+
return operand;
621+
}
622+
611623
} // namespace lldb_private::dil

lldb/source/ValueObject/DILParser.cpp

Lines changed: 162 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "lldb/ValueObject/DILParser.h"
15+
#include "lldb/Symbol/CompileUnit.h"
1516
#include "lldb/Target/ExecutionContextScope.h"
17+
#include "lldb/Target/LanguageRuntime.h"
1618
#include "lldb/Utility/DiagnosticsRendering.h"
1719
#include "lldb/ValueObject/DILAST.h"
1820
#include "lldb/ValueObject/DILEval.h"
@@ -80,15 +82,62 @@ ASTNodeUP DILParser::Run() {
8082
// Parse an expression.
8183
//
8284
// expression:
83-
// unary_expression
85+
// cast_expression
8486
//
85-
ASTNodeUP DILParser::ParseExpression() { return ParseUnaryExpression(); }
87+
ASTNodeUP DILParser::ParseExpression() { return ParseCastExpression(); }
88+
89+
// Parse a cast_expression.
90+
//
91+
// cast_expression:
92+
// unary_expression
93+
// "(" type_id ")" cast_expression
94+
95+
ASTNodeUP DILParser::ParseCastExpression() {
96+
// This can be a C-style cast, try parsing the contents as a type declaration.
97+
if (CurToken().Is(Token::l_paren)) {
98+
Token token = CurToken();
99+
uint32_t loc = token.GetLocation();
100+
101+
// Enable lexer backtracking, so that we can rollback in case it's not
102+
// actually a type declaration.
103+
104+
// Start tentative parsing (save token location/idx, for possible rollback).
105+
uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx();
106+
107+
// Consume the token only after enabling the backtracking.
108+
m_dil_lexer.Advance();
109+
110+
// Try parsing the type declaration. If the returned value is not valid,
111+
// then we should rollback and try parsing the expression.
112+
auto type_id = ParseTypeId();
113+
if (type_id) {
114+
// Successfully parsed the type declaration. Commit the backtracked
115+
// tokens and parse the cast_expression.
116+
117+
if (!type_id.value().IsValid())
118+
return std::make_unique<ErrorNode>();
119+
120+
Expect(Token::r_paren);
121+
m_dil_lexer.Advance();
122+
auto rhs = ParseCastExpression();
123+
124+
return std::make_unique<CStyleCastNode>(
125+
loc, type_id.value(), std::move(rhs), CStyleCastKind::eNone);
126+
}
127+
128+
// Failed to parse the contents of the parentheses as a type declaration.
129+
// Rollback the lexer and try parsing it as unary_expression.
130+
TentativeParsingRollback(save_token_idx);
131+
}
132+
133+
return ParseUnaryExpression();
134+
}
86135

87136
// Parse an unary_expression.
88137
//
89138
// unary_expression:
90139
// postfix_expression
91-
// unary_operator expression
140+
// unary_operator cast_expression
92141
//
93142
// unary_operator:
94143
// "&"
@@ -99,7 +148,7 @@ ASTNodeUP DILParser::ParseUnaryExpression() {
99148
Token token = CurToken();
100149
uint32_t loc = token.GetLocation();
101150
m_dil_lexer.Advance();
102-
auto rhs = ParseExpression();
151+
auto rhs = ParseCastExpression();
103152
switch (token.GetKind()) {
104153
case Token::star:
105154
return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Deref,
@@ -274,6 +323,81 @@ std::string DILParser::ParseNestedNameSpecifier() {
274323
}
275324
}
276325

326+
// Parse a type_id.
327+
//
328+
// type_id:
329+
// type_specifier_seq [abstract_declarator]
330+
//
331+
// type_specifier_seq:
332+
// type_specifier [type_specifier]
333+
//
334+
// type_specifier:
335+
// ["::"] [nested_name_specifier] type_name // not handled for now!
336+
// builtin_typename
337+
//
338+
std::optional<CompilerType> DILParser::ParseTypeId() {
339+
CompilerType type;
340+
// For now only allow builtin types -- will expand add to this later.
341+
auto maybe_builtin_type = ParseBuiltinType();
342+
if (maybe_builtin_type) {
343+
type = *maybe_builtin_type;
344+
} else
345+
return {};
346+
347+
//
348+
// abstract_declarator:
349+
// ptr_operator [abstract_declarator]
350+
//
351+
std::vector<Token> ptr_operators;
352+
while (CurToken().IsOneOf({Token::star, Token::amp})) {
353+
Token tok = CurToken();
354+
ptr_operators.push_back(std::move(tok));
355+
m_dil_lexer.Advance();
356+
}
357+
type = ResolveTypeDeclarators(type, ptr_operators);
358+
359+
return type;
360+
}
361+
362+
// Parse a built-in type
363+
//
364+
// builtin_typename:
365+
// identifer_seq
366+
//
367+
// identifier_seq
368+
// identifer [identifier_seq]
369+
//
370+
// A built-in type can be a single identifier or a space-separated
371+
// list of identifiers (e.g. "short" or "long long").
372+
std::optional<CompilerType> DILParser::ParseBuiltinType() {
373+
std::string type_name = "";
374+
uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx();
375+
bool first_word = true;
376+
while (CurToken().GetKind() == Token::identifier) {
377+
if (CurToken().GetSpelling() == "const" ||
378+
CurToken().GetSpelling() == "volatile")
379+
continue;
380+
if (!first_word)
381+
type_name.push_back(' ');
382+
else
383+
first_word = false;
384+
type_name.append(CurToken().GetSpelling());
385+
m_dil_lexer.Advance();
386+
}
387+
388+
if (type_name.size() > 0) {
389+
lldb::TargetSP target_sp = m_ctx_scope->CalculateTarget();
390+
ConstString const_type_name(type_name.c_str());
391+
for (auto type_system_sp : target_sp->GetScratchTypeSystems())
392+
if (auto compiler_type =
393+
type_system_sp->GetBuiltinTypeByName(const_type_name))
394+
return compiler_type;
395+
}
396+
397+
TentativeParsingRollback(save_token_idx);
398+
return {};
399+
}
400+
277401
// Parse an id_expression.
278402
//
279403
// id_expression:
@@ -339,6 +463,40 @@ std::string DILParser::ParseUnqualifiedId() {
339463
return identifier;
340464
}
341465

466+
CompilerType
467+
DILParser::ResolveTypeDeclarators(CompilerType type,
468+
const std::vector<Token> &ptr_operators) {
469+
CompilerType bad_type;
470+
// Resolve pointers/references.
471+
for (Token tk : ptr_operators) {
472+
uint32_t loc = tk.GetLocation();
473+
if (tk.GetKind() == Token::star) {
474+
// Pointers to reference types are forbidden.
475+
if (type.IsReferenceType()) {
476+
BailOut(llvm::formatv("'type name' declared as a pointer to a "
477+
"reference of type {0}",
478+
type.TypeDescription()),
479+
loc, CurToken().GetSpelling().length());
480+
return bad_type;
481+
}
482+
// Get pointer type for the base type: e.g. int* -> int**.
483+
type = type.GetPointerType();
484+
485+
} else if (tk.GetKind() == Token::amp) {
486+
// References to references are forbidden.
487+
if (type.IsReferenceType()) {
488+
BailOut("type name declared as a reference to a reference", loc,
489+
CurToken().GetSpelling().length());
490+
return bad_type;
491+
}
492+
// Get reference type for the base type: e.g. int -> int&.
493+
type = type.GetLValueReferenceType();
494+
}
495+
}
496+
497+
return type;
498+
}
499+
342500
// Parse an boolean_literal.
343501
//
344502
// boolean_literal:

0 commit comments

Comments
 (0)