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
38CompilerType ResolveTypeByName(const std::string &name,
39 ExecutionContext &exe_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 /// Parse the lexed tokens.
71 /// \returns An ASTNode tree or an error.
72 static llvm::Expected<ASTNodeUP>
73 Parse(ExecutionContext &exe_ctx, llvm::StringRef dil_input_expr,
74 DILLexer lexer, lldb::DynamicValueType use_dynamic, lldb::DILMode mode);
75
76 ~DILParser() = default;
77
78private:
79 explicit DILParser(ExecutionContext &exe_ctx, llvm::StringRef dil_input_expr,
80 DILLexer lexer, lldb::DynamicValueType use_dynamic,
81 llvm::Error &error, lldb::DILMode mode);
82
83 ASTNodeUP Run();
84
103
104 std::string ParseNestedNameSpecifier();
105
106 std::string ParseIdExpression();
107 std::string ParseUnqualifiedId();
112
114 std::optional<CompilerType> ParseBuiltinType();
115 std::optional<CompilerType> ParseTypeId();
116 void ParseTypeSpecifierSeq(std::string &type_name);
117 std::optional<std::string> ParseTypeSpecifier();
118 std::optional<std::string> ParseTypeName();
120 const std::vector<Token> &ptr_operators);
121
122 void BailOut(const std::string &error, uint32_t loc, uint16_t err_len);
123
124 void Expect(Token::Kind kind);
125
126 void ExpectOneOf(std::vector<Token::Kind> kinds_vec);
127
128 void TentativeParsingRollback(uint32_t saved_idx) {
129 if (m_error)
130 llvm::consumeError(std::move(m_error));
131 m_dil_lexer.ResetTokenIdx(saved_idx);
132 }
133
134 Token CurToken() { return m_dil_lexer.GetCurrentToken(); }
135
136 // Parser doesn't own the evaluation context. The produced AST may depend on
137 // it (for example, for source locations), so it's expected that expression
138 // context will outlive the parser.
140
141 llvm::StringRef m_input_expr;
142
144
145 // Holds an error if it occures during parsing.
146 llvm::Error &m_error;
147
149
150 // DIL Mode requested by the caller.
152}; // class DILParser
153
154} // namespace lldb_private::dil
155
156#endif // LLDB_VALUEOBJECT_DILPARSER_H
static llvm::raw_ostream & error(Stream &strm)
Generic representation of a type in a programming language.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
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:104
ASTNodeUP ParseInclusiveOrExpression()
ASTNodeUP ParseConditionalBranches(ASTNodeUP condition)
void ParseTypeSpecifierSeq(std::string &type_name)
void Expect(Token::Kind kind)
std::optional< CompilerType > ParseTypeId()
void TentativeParsingRollback(uint32_t saved_idx)
Definition DILParser.h:128
ASTNodeUP ParseLogicalAndExpression()
void ExpectOneOf(std::vector< Token::Kind > kinds_vec)
std::optional< std::string > ParseTypeSpecifier()
ASTNodeUP ParseRelationalExpression()
DILParser(ExecutionContext &exe_ctx, llvm::StringRef dil_input_expr, DILLexer lexer, lldb::DynamicValueType use_dynamic, llvm::Error &error, lldb::DILMode mode)
static llvm::Expected< ASTNodeUP > Parse(ExecutionContext &exe_ctx, llvm::StringRef dil_input_expr, DILLexer lexer, lldb::DynamicValueType use_dynamic, lldb::DILMode mode)
Parse the lexed tokens.
Definition DILParser.cpp:92
ASTNodeUP ParseAssignmentExpression()
ExecutionContext m_exe_ctx
Definition DILParser.h:139
std::optional< CompilerType > ParseBuiltinType()
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:148
std::optional< std::string > ParseTypeName()
llvm::StringRef m_input_expr
Definition DILParser.h:141
ASTNodeUP ParseConditionalExpression()
std::string ParseNestedNameSpecifier()
ASTNodeUP ParseExclusiveOrExpression()
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:123
CompilerType ResolveTypeByName(const std::string &name, ExecutionContext &exe_ctx)
Definition DILParser.cpp:61
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.