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::dil {
26
33
34// The following is modeled on class OptionParseError.
36 : public llvm::ErrorInfo<DILDiagnosticError, DiagnosticError> {
38
39public:
40 using llvm::ErrorInfo<DILDiagnosticError, DiagnosticError>::ErrorInfo;
42 : ErrorInfo(make_error_code(std::errc::invalid_argument)),
43 m_detail(std::move(detail)) {}
44
45 DILDiagnosticError(llvm::StringRef expr, const std::string &message,
46 uint32_t loc, uint16_t err_len = 1);
47
48 std::unique_ptr<CloneableError> Clone() const override {
49 return std::make_unique<DILDiagnosticError>(m_detail);
50 }
51
52 llvm::ArrayRef<DiagnosticDetail> GetDetails() const override {
53 return m_detail;
54 }
55
56 std::string message() const override { return m_detail.rendered; }
57};
58
59/// Pure recursive descent parser for C++ like expressions.
60/// EBNF grammar for the parser is described in lldb/docs/dil-expr-lang.ebnf
61class DILParser {
62public:
63 static llvm::Expected<ASTNodeUP> Parse(llvm::StringRef dil_input_expr,
64 DILLexer lexer,
65 std::shared_ptr<StackFrame> frame_sp,
66 lldb::DynamicValueType use_dynamic,
67 bool use_synthetic, bool fragile_ivar,
68 bool check_ptr_vs_member);
69
70 ~DILParser() = default;
71
72 bool UseSynthetic() { return m_use_synthetic; }
73
74 bool UseFragileIvar() { return m_fragile_ivar; }
75
77
79
80private:
81 explicit DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,
82 std::shared_ptr<StackFrame> frame_sp,
83 lldb::DynamicValueType use_dynamic, bool use_synthetic,
84 bool fragile_ivar, bool check_ptr_vs_member,
85 llvm::Error &error);
86
87 ASTNodeUP Run();
88
93
94 std::string ParseNestedNameSpecifier();
95
96 std::string ParseIdExpression();
97 std::string ParseUnqualifiedId();
102
104 std::optional<CompilerType> ParseBuiltinType();
105 std::optional<CompilerType> ParseTypeId();
107 const std::vector<Token> &ptr_operators);
108
109 void BailOut(const std::string &error, uint32_t loc, uint16_t err_len);
110
111 void Expect(Token::Kind kind);
112
113 void ExpectOneOf(std::vector<Token::Kind> kinds_vec);
114
115 void TentativeParsingRollback(uint32_t saved_idx) {
116 if (m_error)
117 llvm::consumeError(std::move(m_error));
118 m_dil_lexer.ResetTokenIdx(saved_idx);
119 }
120
121 Token CurToken() { return m_dil_lexer.GetCurrentToken(); }
122
123 // Parser doesn't own the evaluation context. The produced AST may depend on
124 // it (for example, for source locations), so it's expected that expression
125 // context will outlive the parser.
126 std::shared_ptr<StackFrame> m_ctx_scope;
127
128 llvm::StringRef m_input_expr;
129
131
132 // Holds an error if it occures during parsing.
133 llvm::Error &m_error;
134
139}; // class DILParser
140
141} // namespace lldb_private::dil
142
143#endif // LLDB_VALUEOBJECT_DILPARSER_H
static llvm::raw_ostream & error(Stream &strm)
Generic representation of a type in a programming language.
DILDiagnosticError(DiagnosticDetail detail)
Definition DILParser.h:41
std::unique_ptr< CloneableError > Clone() const override
Definition DILParser.h:48
llvm::ArrayRef< DiagnosticDetail > GetDetails() const override
Definition DILParser.h:52
std::string message() const override
Definition DILParser.h:56
Class for doing the simple lexing required by DIL.
Definition DILLexer.h:72
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:48
lldb::DynamicValueType UseDynamic()
Definition DILParser.h:78
std::optional< CompilerType > ParseTypeId()
void TentativeParsingRollback(uint32_t saved_idx)
Definition DILParser.h:115
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)
Definition DILParser.cpp:65
void ExpectOneOf(std::vector< Token::Kind > kinds_vec)
std::optional< CompilerType > ParseBuiltinType()
std::shared_ptr< StackFrame > m_ctx_scope
Definition DILParser.h:126
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:135
llvm::StringRef m_input_expr
Definition DILParser.h:128
std::string ParseNestedNameSpecifier()
Class defining the tokens generated by the DIL lexer and used by the DIL parser.
Definition DILLexer.h:24
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:79
A compiler-independent representation of an lldb_private::Diagnostic.