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
38// The following is modeled on class OptionParseError.
40 : public llvm::ErrorInfo<DILDiagnosticError, DiagnosticError> {
42
43public:
44 using llvm::ErrorInfo<DILDiagnosticError, DiagnosticError>::ErrorInfo;
46 : ErrorInfo(make_error_code(std::errc::invalid_argument)),
47 m_detail(std::move(detail)) {}
48
49 DILDiagnosticError(llvm::StringRef expr, const std::string &message,
50 uint32_t loc, uint16_t err_len = 1);
51
52 std::unique_ptr<CloneableError> Clone() const override {
53 return std::make_unique<DILDiagnosticError>(m_detail);
54 }
55
56 llvm::ArrayRef<DiagnosticDetail> GetDetails() const override {
57 return m_detail;
58 }
59
60 std::string message() const override { return m_detail.rendered; }
61};
62
63/// Pure recursive descent parser for C++ like expressions.
64/// EBNF grammar for the parser is described in lldb/docs/dil-expr-lang.ebnf
65class DILParser {
66public:
67 static llvm::Expected<ASTNodeUP> Parse(llvm::StringRef dil_input_expr,
68 DILLexer lexer,
69 std::shared_ptr<StackFrame> frame_sp,
70 lldb::DynamicValueType use_dynamic,
71 lldb::DILMode mode);
72
73 ~DILParser() = default;
74
75private:
76 explicit DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,
77 std::shared_ptr<StackFrame> frame_sp,
78 lldb::DynamicValueType use_dynamic, llvm::Error &error,
79 lldb::DILMode mode);
80
81 ASTNodeUP Run();
82
89
90 std::string ParseNestedNameSpecifier();
91
92 std::string ParseIdExpression();
93 std::string ParseUnqualifiedId();
98
100 std::optional<CompilerType> ParseBuiltinType();
101 std::optional<CompilerType> ParseTypeId();
102 void ParseTypeSpecifierSeq(std::string &type_name);
103 std::optional<std::string> ParseTypeSpecifier();
104 std::optional<std::string> ParseTypeName();
106 const std::vector<Token> &ptr_operators);
107
108 void BailOut(const std::string &error, uint32_t loc, uint16_t err_len);
109
110 void Expect(Token::Kind kind);
111
112 void ExpectOneOf(std::vector<Token::Kind> kinds_vec);
113
114 void TentativeParsingRollback(uint32_t saved_idx) {
115 if (m_error)
116 llvm::consumeError(std::move(m_error));
117 m_dil_lexer.ResetTokenIdx(saved_idx);
118 }
119
120 Token CurToken() { return m_dil_lexer.GetCurrentToken(); }
121
122 // Parser doesn't own the evaluation context. The produced AST may depend on
123 // it (for example, for source locations), so it's expected that expression
124 // context will outlive the parser.
125 std::shared_ptr<StackFrame> m_ctx_scope;
126
127 llvm::StringRef m_input_expr;
128
130
131 // Holds an error if it occures during parsing.
132 llvm::Error &m_error;
133
135
136 // DIL Mode requested by the caller.
138}; // class DILParser
139
140} // namespace lldb_private::dil
141
142#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:45
std::unique_ptr< CloneableError > Clone() const override
Definition DILParser.h:52
llvm::ArrayRef< DiagnosticDetail > GetDetails() const override
Definition DILParser.h:56
std::string message() const override
Definition DILParser.h:60
Class for doing the simple lexing required by DIL.
Definition DILLexer.h:76
void ParseTypeSpecifierSeq(std::string &type_name)
void Expect(Token::Kind kind)
std::optional< CompilerType > ParseTypeId()
void TentativeParsingRollback(uint32_t saved_idx)
Definition DILParser.h:114
ASTNodeUP ParseFloatingPointLiteral()
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:125
void BailOut(const std::string &error, uint32_t loc, uint16_t err_len)
CompilerType ResolveTypeDeclarators(CompilerType type, const std::vector< Token > &ptr_operators)
ASTNodeUP ParseMultiplicativeExpression()
lldb::DynamicValueType m_use_dynamic
Definition DILParser.h:134
DILParser(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, llvm::Error &error, lldb::DILMode mode)
static llvm::Expected< ASTNodeUP > Parse(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, lldb::DILMode mode)
Definition DILParser.cpp:93
std::optional< std::string > ParseTypeName()
llvm::StringRef m_input_expr
Definition DILParser.h:127
std::string ParseNestedNameSpecifier()
Class defining the tokens generated by the DIL lexer and used by the DIL parser.
Definition DILLexer.h:25
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:93
A class that represents a running process on the host machine.
DILMode
Data Inspection Language (DIL) evaluation modes.
A compiler-independent representation of an lldb_private::Diagnostic.