23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/FormatAdapters.h"
34 const std::string &
message, uint32_t loc,
36 : ErrorInfo(make_error_code(std::errc::invalid_argument)) {
38 FileSpec{}, 1,
static_cast<uint16_t
>(loc + 1),
39 err_len,
false,
true};
47 auto msg = llvm::formatv(
"<user expression>:1:{0}: {1}\n 1 | {2}\n |",
49 std::string rendered_str;
50 llvm::raw_string_ostream rendered_os(rendered_str);
51 rendered_os << msg.str();
52 rendered_os << llvm::indent(loc + 1) <<
"^";
55 rendered_os << std::string(err_len - 1,
'~');
60 m_detail.rendered = std::move(rendered_str);
67 llvm::StringRef name_ref(name);
69 if (name_ref.starts_with(
"::"))
70 name_ref = name_ref.drop_front(2);
72 std::vector<CompilerType> result_type_list;
74 if (!name_ref.empty() && target_sp) {
77 TypeQueryOptions::e_find_one};
79 images.
FindTypes(
nullptr, query, results);
82 result_type_list.push_back(type_sp->GetFullCompilerType());
85 if (!result_type_list.empty()) {
99 llvm::Error
error = llvm::Error::success();
100 DILParser parser(dil_input_expr, lexer, stack_frame, use_dynamic,
error,
104 assert(node_up &&
"ASTNodeUP must not contain a nullptr");
108 "[DILParser::Parse] DIL parser failed:\n{0}",
109 llvm::toStringWithoutConsuming(
error));
160 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
172 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
173 lhs = std::make_unique<BinaryOpNode>(
175 std::move(lhs), std::move(rhs));
202 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
211 assert(condition &&
"ASTNodeUP must not contain a nullptr");
216 assert(true_op &&
"ASTNodeUP must not contain a nullptr");
220 assert(false_op &&
"ASTNodeUP must not contain a nullptr");
221 return std::make_unique<ConditionalNode>(
222 token.
GetLocation(), std::move(condition), std::move(true_op),
223 std::move(false_op));
233 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
239 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
240 lhs = std::make_unique<BinaryOpNode>(
242 std::move(lhs), std::move(rhs));
255 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
261 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
262 lhs = std::make_unique<BinaryOpNode>(
264 std::move(lhs), std::move(rhs));
277 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
283 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
284 lhs = std::make_unique<BinaryOpNode>(
286 std::move(lhs), std::move(rhs));
299 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
305 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
306 lhs = std::make_unique<BinaryOpNode>(
308 std::move(lhs), std::move(rhs));
321 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
326 BailOut(
"bitwise and (&) is allowed only in DIL full mode",
328 return std::make_unique<ErrorNode>();
332 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
333 lhs = std::make_unique<BinaryOpNode>(
335 std::move(lhs), std::move(rhs));
349 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
355 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
356 lhs = std::make_unique<BinaryOpNode>(
358 std::move(lhs), std::move(rhs));
374 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
381 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
382 lhs = std::make_unique<BinaryOpNode>(
384 std::move(lhs), std::move(rhs));
398 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
404 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
405 lhs = std::make_unique<BinaryOpNode>(
407 std::move(lhs), std::move(rhs));
421 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
427 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
428 lhs = std::make_unique<BinaryOpNode>(
430 std::move(lhs), std::move(rhs));
449 BailOut(
"binary multiplication (*) is allowed only in DIL full mode",
451 return std::make_unique<ErrorNode>();
455 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
456 lhs = std::make_unique<BinaryOpNode>(
458 std::move(lhs), std::move(rhs));
482 uint32_t save_token_idx =
m_dil_lexer.GetCurrentTokenIdx();
494 if (!type_id.value().IsValid())
495 return std::make_unique<ErrorNode>();
500 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
501 return std::make_unique<CastNode>(loc, type_id.value(), std::move(rhs),
534 assert(rhs &&
"ASTNodeUP must not contain a nullptr");
555 BailOut(
"Decrement operator is not supported. Use `-=` instead.",
557 return std::make_unique<ErrorNode>();
559 BailOut(
"Increment operator is not supported. Use `+=` instead.",
561 return std::make_unique<ErrorNode>();
563 llvm_unreachable(
"invalid token kind");
580 assert(lhs &&
"ASTNodeUP must not contain a nullptr");
589 assert(index &&
"ASTNodeUP must not contain a nullptr");
593 assert(last_index &&
"ASTNodeUP must not contain a nullptr");
594 lhs = std::make_unique<BitFieldExtractionNode>(
595 loc, std::move(lhs), std::move(index), std::move(last_index));
597 BailOut(
"use of '-' for bitfield range is deprecated; use ':' instead",
599 return std::make_unique<ErrorNode>();
601 lhs = std::make_unique<ArraySubscriptNode>(loc, std::move(lhs),
613 lhs = std::make_unique<MemberOfNode>(
619 BailOut(
"Decrement operator is not supported. Use `-=` instead.",
621 return std::make_unique<ErrorNode>();
623 BailOut(
"Increment operator is not supported. Use `+=` instead.",
625 return std::make_unique<ErrorNode>();
627 llvm_unreachable(
"invalid token");
653 if (!identifier.empty()) {
656 uint32_t save_token_idx =
m_dil_lexer.GetCurrentTokenIdx();
661 return std::make_unique<SizeOfNode>(loc, *type_id);
667 return std::make_unique<SizeOfNode>(loc, std::move(expr));
669 return std::make_unique<IdentifierNode>(loc, identifier);
683 return std::make_unique<ErrorNode>();
707 (
m_dil_lexer.LookAhead(1).GetSpelling() ==
"anonymous") &&
709 (
m_dil_lexer.LookAhead(2).GetSpelling() ==
"namespace") &&
717 BailOut(
"Expected an identifier or anonymous namespace, but not found.",
723 return "(anonymous namespace)::" + identifier2;
763 if (maybe_builtin_type) {
764 type = *maybe_builtin_type;
768 std::string type_name;
771 if (type_name.empty())
793 std::vector<Token> ptr_operators;
796 ptr_operators.push_back(std::move(tok));
815 std::string type_name =
"";
816 uint32_t save_token_idx =
m_dil_lexer.GetCurrentTokenIdx();
817 bool first_word =
true;
819 if (
CurToken().GetSpelling() ==
"const" ||
820 CurToken().GetSpelling() ==
"volatile") {
825 type_name.push_back(
' ');
828 type_name.append(
CurToken().GetSpelling());
832 if (type_name.size() > 0) {
835 for (
auto type_system_sp : target_sp->GetScratchTypeSystems())
836 if (
auto compiler_type =
837 type_system_sp->GetBuiltinTypeByName(const_type_name))
838 return compiler_type;
855 type_name = *err_or_string;
871 bool global_scope =
false;
882 if (!type_name_or_err)
883 return type_name_or_err;
884 std::string type_name = *type_name_or_err;
889 if (!type_name.empty())
891 return llvm::formatv(
"{0}{1}{2}", global_scope ?
"::" :
"",
892 nested_name_specifier, type_name);
942 bool global_scope =
false;
953 if (!nested_name_specifier.empty()) {
957 return llvm::formatv(
"{0}{1}{2}", global_scope ?
"::" :
"",
958 nested_name_specifier, unqualified_id);
970 return llvm::formatv(
"{0}{1}", global_scope ?
"::" :
"", identifier);
994 const std::vector<Token> &ptr_operators) {
996 for (
Token tk : ptr_operators) {
997 uint32_t loc = tk.GetLocation();
1001 BailOut(llvm::formatv(
"'type name' declared as a pointer to a "
1002 "reference of type {0}",
1004 loc,
CurToken().GetSpelling().length());
1014 BailOut(
"type name declared as a reference to a reference", loc,
1015 CurToken().GetSpelling().length());
1037 return std::make_unique<BooleanLiteralNode>(loc, literal_value);
1066 BailOut(llvm::formatv(
"Failed to parse token as numeric-constant: {0}",
1069 return numeric_constant;
1072 return numeric_constant;
1078 llvm::StringRef spelling_ref = spelling;
1080 auto radix = llvm::getAutoSenseRadix(spelling_ref);
1082 bool is_unsigned =
false;
1083 if (spelling_ref.consume_back_insensitive(
"u"))
1085 if (spelling_ref.consume_back_insensitive(
"ll"))
1087 else if (spelling_ref.consume_back_insensitive(
"l"))
1090 if (!is_unsigned && spelling_ref.consume_back_insensitive(
"u"))
1093 llvm::APInt raw_value;
1094 if (!spelling_ref.getAsInteger(radix, raw_value))
1095 return std::make_unique<IntegerLiteralNode>(token.
GetLocation(), raw_value,
1096 radix, is_unsigned, type);
1097 return std::make_unique<ErrorNode>();
1103 llvm::StringRef spelling_ref = spelling;
1105 llvm::APFloat raw_float(llvm::APFloat::IEEEdouble());
1106 if (spelling_ref.consume_back_insensitive(
"f"))
1107 raw_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
1109 auto StatusOrErr = raw_float.convertFromString(
1110 spelling_ref, llvm::APFloat::rmNearestTiesToEven);
1111 if (!errorToBool(StatusOrErr.takeError()))
1112 return std::make_unique<FloatLiteralNode>(token.
GetLocation(), raw_float);
1113 return std::make_unique<ErrorNode>();
1118 BailOut(
"Assignment is allowed only at top level.",
1128 if (!
CurToken().IsOneOf(kinds_vec)) {
1129 BailOut(llvm::formatv(
"expected any of ({0}), got: {1}",
1130 llvm::iterator_range(kinds_vec),
CurToken()),
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
uint32_t GetKind(uint32_t data)
Return the type kind encoded in the given data.
Generic representation of a type in a programming language.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
CompilerType GetLValueReferenceType() const
Return a new CompilerType that is a L value reference to this type if this type is valid and the type...
ConstString GetTypeName(bool BaseOnly=false) const
bool IsReferenceType(CompilerType *pointee_type=nullptr, bool *is_rvalue=nullptr) const
std::string TypeDescription()
A uniqued constant string class.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
virtual lldb::TargetSP CalculateTarget()=0
A collection class for Module objects.
void FindTypes(Module *search_first, const TypeQuery &query, lldb_private::TypeResults &results) const
Find types using a type-matching object that contains all search parameters.
This base class provides an interface to stack frames.
A class that contains all state required for type lookups.
This class tracks the state and results of a TypeQuery.
lldb::TypeSP GetFirstType() const
DILDiagnosticError(DiagnosticDetail detail)
std::string message() const override
DiagnosticDetail m_detail
Class for doing the simple lexing required by DIL.
ASTNodeUP ParseAdditiveExpression()
ASTNodeUP ParseInclusiveOrExpression()
ASTNodeUP ParseUnaryExpression()
ASTNodeUP ParseLogicalOrExpression()
ASTNodeUP ParseConditionalBranches(ASTNodeUP condition)
void ParseTypeSpecifierSeq(std::string &type_name)
ASTNodeUP ParseIntegerLiteral()
void Expect(Token::Kind kind)
std::optional< CompilerType > ParseTypeId()
static llvm::Expected< ASTNodeUP > Parse(llvm::StringRef dil_input_expr, DILLexer lexer, StackFrame &stack_frame, lldb::DynamicValueType use_dynamic, lldb::DILMode mode)
Parse the lexed tokens.
void TentativeParsingRollback(uint32_t saved_idx)
ASTNodeUP ParseLogicalAndExpression()
ASTNodeUP ParseEqualityExpression()
ASTNodeUP ParseFloatingPointLiteral()
ASTNodeUP ParseExpression()
ASTNodeUP ParseAndExpression()
void ExpectOneOf(std::vector< Token::Kind > kinds_vec)
std::optional< std::string > ParseTypeSpecifier()
ASTNodeUP ParseRelationalExpression()
ASTNodeUP ParsePureExpression()
ASTNodeUP ParseNumericLiteral()
ASTNodeUP ParseAssignmentExpression()
std::optional< CompilerType > ParseBuiltinType()
ASTNodeUP ParsePrimaryExpression()
DILParser(llvm::StringRef dil_input_expr, DILLexer lexer, StackFrame &stack_frame, lldb::DynamicValueType use_dynamic, llvm::Error &error, lldb::DILMode mode)
void BailOut(const std::string &error, uint32_t loc, uint16_t err_len)
CompilerType ResolveTypeDeclarators(CompilerType type, const std::vector< Token > &ptr_operators)
ASTNodeUP ParsePostfixExpression()
std::string ParseIdExpression()
ASTNodeUP ParseMultiplicativeExpression()
lldb::DynamicValueType m_use_dynamic
std::string ParseUnqualifiedId()
ASTNodeUP ParseCastExpression()
ASTNodeUP ParseBooleanLiteral()
std::optional< std::string > ParseTypeName()
llvm::StringRef m_input_expr
ASTNodeUP ParseConditionalExpression()
std::string ParseNestedNameSpecifier()
ASTNodeUP ParseExclusiveOrExpression()
ASTNodeUP ParseShiftExpression()
StackFrame & m_stack_frame
Class defining the tokens generated by the DIL lexer and used by the DIL parser.
uint32_t GetLocation() const
std::string GetSpelling() const
@ eNone
Invalid promotion type (results in error).
lldb::ValueObjectSP LookupIdentifier(llvm::StringRef name_ref, StackFrame &stack_frame, lldb::DynamicValueType use_dynamic)
Given the name of an identifier (variable name, member name, type name, etc.), find the ValueObject f...
std::unique_ptr< ASTNode > ASTNodeUP
lldb::ValueObjectSP LookupGlobalIdentifier(llvm::StringRef name_ref, StackFrame &stack_frame, lldb::TargetSP target_sp, lldb::DynamicValueType use_dynamic)
Given the name of an identifier, check to see if it matches the name of a global variable.
BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind)
Translates DIL tokens to BinaryOpKind.
CompilerType ResolveTypeByName(const std::string &name, ExecutionContextScope &ctx_scope)
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Type > TypeSP
std::shared_ptr< lldb_private::Target > TargetSP
DILMode
Data Inspection Language (DIL) evaluation modes.
@ eDILModeFull
Allowed: everything supported by DIL.
A source location consisting of a file name and position.