LLDB mainline
DILParser.h
Go to the documentation of this file.
1//===-- DILParser.h ---------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_VALUEOBJECT_DILPARSER_H
10#define LLDB_VALUEOBJECT_DILPARSER_H
11
14#include "lldb/Utility/Status.h"
17#include "llvm/Support/Error.h"
18#include <memory>
19#include <optional>
20#include <string>
21#include <system_error>
22#include <tuple>
23#include <vector>
24
25namespace lldb_private {
26class StackFrame;
27}
28
29namespace lldb_private::dil {
30
37
38llvm::Expected<lldb::TypeSystemSP>
39GetTypeSystemFromCU(std::shared_ptr<StackFrame> ctx);
40
41// The following is modeled on class OptionParseError.
43 : public llvm::ErrorInfo<DILDiagnosticError, DiagnosticError> {
45
46public:
47 using llvm::ErrorInfo<DILDiagnosticError, DiagnosticError>::ErrorInfo;
49 : ErrorInfo(make_error_code(std::errc::invalid_argument)),
50 m_detail(std::move(detail)) {}
51
52 DILDiagnosticError(llvm::StringRef expr, const std::string &message,
53 uint32_t loc, uint16_t err_len = 1);
54
55 std::unique_ptr<CloneableError> Clone() const override {
56 return std::make_unique<DILDiagnosticError>(m_detail);
57 }
58
59 llvm::ArrayRef<DiagnosticDetail> GetDetails() const override {
60 return m_detail;
61 }
62
63 std::string message() const override { return m_detail.rendered; }
64};
65
66/// Pure recursive descent parser for C++ like expressions.
67/// EBNF grammar for the parser is described in lldb/docs/dil-expr-lang.ebnf
68class DILParser {
69public:
70 static llvm::Expected<ASTNodeUP> Parse(llvm::StringRef dil_input_expr,
71 DILLexer lexer,
72 std::shared_ptr<StackFrame> frame_sp,
73 lldb::DynamicValueType use_dynamic,
74 bool use_synthetic, bool fragile_ivar,
75 bool check_ptr_vs_member);
76
77 ~DILParser() = default;
78
79 bool UseSynthetic() { return m_use_synthetic; }
80
81 bool UseFragileIvar() { return m_fragile_ivar; }
82
84
86
87private:
88 explicit DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,
89 std::shared_ptr<StackFrame> frame_sp,
90 lldb::DynamicValueType use_dynamic, bool use_synthetic,
91 bool fragile_ivar, bool check_ptr_vs_member,
92 llvm::Error &error);
93
94 ASTNodeUP Run();
95
100
101 std::string ParseNestedNameSpecifier();
102
103 std::string ParseIdExpression();
104 std::string ParseUnqualifiedId();
109
111 std::optional<CompilerType> ParseBuiltinType();
112 std::optional<CompilerType> ParseTypeId();
113 void ParseTypeSpecifierSeq(std::string &type_name);
114 std::optional<std::string> ParseTypeSpecifier();
115 std::optional<std::string> ParseTypeName();
117 const std::vector<Token> &ptr_operators);
118
119 void BailOut(const std::string &error, uint32_t loc, uint16_t err_len);
120
121 void Expect(Token::Kind kind);
122
123 void ExpectOneOf(std::vector<Token::Kind> kinds_vec);
124
125 void TentativeParsingRollback(uint32_t saved_idx) {
126 if (m_error)
127 llvm::consumeError(std::move(m_error));
128 m_dil_lexer.ResetTokenIdx(saved_idx);
129 }
130
131 Token CurToken() { return m_dil_lexer.GetCurrentToken(); }
132
133 // Parser doesn't own the evaluation context. The produced AST may depend on
134 // it (for example, for source locations), so it's expected that expression
135 // context will outlive the parser.
136 std::shared_ptr<StackFrame> m_ctx_scope;
137
138 llvm::StringRef m_input_expr;
139
141
142 // Holds an error if it occures during parsing.
143 llvm::Error &m_error;
144
149}; // class DILParser
150
151} // namespace lldb_private::dil
152
153#endif // LLDB_VALUEOBJECT_DILPARSER_H
static llvm::raw_ostream & error(Stream &strm)
Generic representation of a type in a programming language.
This base class provides an interface to stack frames.
Definition StackFrame.h:44
DILDiagnosticError(DiagnosticDetail detail)
Definition DILParser.h:48
std::unique_ptr< CloneableError > Clone() const override
Definition DILParser.h:55
llvm::ArrayRef< DiagnosticDetail > GetDetails() const override
Definition DILParser.h:59
std::string message() const override
Definition DILParser.h:63
Class for doing the simple lexing required by DIL.
Definition DILLexer.h:74
void ParseTypeSpecifierSeq(std::string &type_name)
void Expect(Token::Kind kind)
static llvm::Expected< ASTNodeUP > Parse(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, bool use_synthetic, bool fragile_ivar, bool check_ptr_vs_member)
Definition DILParser.cpp:90
lldb::DynamicValueType UseDynamic()
Definition DILParser.h:85
std::optional< CompilerType > ParseTypeId()
void TentativeParsingRollback(uint32_t saved_idx)
Definition DILParser.h:125
ASTNodeUP ParseFloatingPointLiteral()
DILParser(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, bool use_synthetic, bool fragile_ivar, bool check_ptr_vs_member, llvm::Error &error)
void ExpectOneOf(std::vector< Token::Kind > kinds_vec)
std::optional< std::string > ParseTypeSpecifier()
std::optional< CompilerType > ParseBuiltinType()
std::shared_ptr< StackFrame > m_ctx_scope
Definition DILParser.h:136
void BailOut(const std::string &error, uint32_t loc, uint16_t err_len)
CompilerType ResolveTypeDeclarators(CompilerType type, const std::vector< Token > &ptr_operators)
lldb::DynamicValueType m_use_dynamic
Definition DILParser.h:145
std::optional< std::string > ParseTypeName()
llvm::StringRef m_input_expr
Definition DILParser.h:138
std::string ParseNestedNameSpecifier()
Class defining the tokens generated by the DIL lexer and used by the DIL parser.
Definition DILLexer.h:25
llvm::Expected< lldb::TypeSystemSP > GetTypeSystemFromCU(std::shared_ptr< StackFrame > ctx)
Definition DILParser.cpp:49
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:79
A class that represents a running process on the host machine.
A compiler-independent representation of an lldb_private::Diagnostic.