Path: blob/main/contrib/llvm-project/lldb/source/ValueObject/DILParser.cpp
213764 views
//===-- DILParser.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6// This implements the recursive descent parser for the Data Inspection7// Language (DIL), and its helper functions, which will eventually underlie the8// 'frame variable' command. The language that this parser recognizes is9// described in lldb/docs/dil-expr-lang.ebnf10//11//===----------------------------------------------------------------------===//1213#include "lldb/ValueObject/DILParser.h"14#include "lldb/Target/ExecutionContextScope.h"15#include "lldb/Utility/DiagnosticsRendering.h"16#include "lldb/ValueObject/DILAST.h"17#include "lldb/ValueObject/DILEval.h"18#include "llvm/ADT/StringRef.h"19#include "llvm/Support/FormatAdapters.h"20#include <cstdlib>21#include <limits.h>22#include <memory>23#include <sstream>24#include <string>2526namespace lldb_private::dil {2728DILDiagnosticError::DILDiagnosticError(llvm::StringRef expr,29const std::string &message, uint32_t loc,30uint16_t err_len)31: ErrorInfo(make_error_code(std::errc::invalid_argument)) {32DiagnosticDetail::SourceLocation sloc = {33FileSpec{}, /*line=*/1, static_cast<uint16_t>(loc + 1),34err_len, false, /*in_user_input=*/true};35std::string rendered_msg =36llvm::formatv("<user expression 0>:1:{0}: {1}\n 1 | {2}\n | ^",37loc + 1, message, expr);38m_detail.source_location = sloc;39m_detail.severity = lldb::eSeverityError;40m_detail.message = message;41m_detail.rendered = std::move(rendered_msg);42}4344llvm::Expected<ASTNodeUP>45DILParser::Parse(llvm::StringRef dil_input_expr, DILLexer lexer,46std::shared_ptr<StackFrame> frame_sp,47lldb::DynamicValueType use_dynamic, bool use_synthetic,48bool fragile_ivar, bool check_ptr_vs_member) {49llvm::Error error = llvm::Error::success();50DILParser parser(dil_input_expr, lexer, frame_sp, use_dynamic, use_synthetic,51fragile_ivar, check_ptr_vs_member, error);5253ASTNodeUP node_up = parser.Run();5455if (error)56return error;5758return node_up;59}6061DILParser::DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,62std::shared_ptr<StackFrame> frame_sp,63lldb::DynamicValueType use_dynamic, bool use_synthetic,64bool fragile_ivar, bool check_ptr_vs_member,65llvm::Error &error)66: m_ctx_scope(frame_sp), m_input_expr(dil_input_expr),67m_dil_lexer(std::move(lexer)), m_error(error), m_use_dynamic(use_dynamic),68m_use_synthetic(use_synthetic), m_fragile_ivar(fragile_ivar),69m_check_ptr_vs_member(check_ptr_vs_member) {}7071ASTNodeUP DILParser::Run() {72ASTNodeUP expr = ParseExpression();7374Expect(Token::Kind::eof);7576return expr;77}7879// Parse an expression.80//81// expression:82// unary_expression83//84ASTNodeUP DILParser::ParseExpression() { return ParseUnaryExpression(); }8586// Parse an unary_expression.87//88// unary_expression:89// postfix_expression90// unary_operator expression91//92// unary_operator:93// "&"94// "*"95//96ASTNodeUP DILParser::ParseUnaryExpression() {97if (CurToken().IsOneOf({Token::amp, Token::star})) {98Token token = CurToken();99uint32_t loc = token.GetLocation();100m_dil_lexer.Advance();101auto rhs = ParseExpression();102switch (token.GetKind()) {103case Token::star:104return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Deref,105std::move(rhs));106case Token::amp:107return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::AddrOf,108std::move(rhs));109110default:111llvm_unreachable("invalid token kind");112}113}114return ParsePostfixExpression();115}116117// Parse a postfix_expression.118//119// postfix_expression:120// primary_expression121// postfix_expression "[" integer_literal "]"122// postfix_expression "[" integer_literal "-" integer_literal "]"123// postfix_expression "." id_expression124// postfix_expression "->" id_expression125//126ASTNodeUP DILParser::ParsePostfixExpression() {127ASTNodeUP lhs = ParsePrimaryExpression();128while (CurToken().IsOneOf({Token::l_square, Token::period, Token::arrow})) {129uint32_t loc = CurToken().GetLocation();130Token token = CurToken();131switch (token.GetKind()) {132case Token::l_square: {133m_dil_lexer.Advance();134std::optional<int64_t> index = ParseIntegerConstant();135if (!index) {136BailOut(137llvm::formatv("failed to parse integer constant: {0}", CurToken()),138CurToken().GetLocation(), CurToken().GetSpelling().length());139return std::make_unique<ErrorNode>();140}141if (CurToken().GetKind() == Token::minus) {142m_dil_lexer.Advance();143std::optional<int64_t> last_index = ParseIntegerConstant();144if (!last_index) {145BailOut(llvm::formatv("failed to parse integer constant: {0}",146CurToken()),147CurToken().GetLocation(), CurToken().GetSpelling().length());148return std::make_unique<ErrorNode>();149}150lhs = std::make_unique<BitFieldExtractionNode>(151loc, std::move(lhs), std::move(*index), std::move(*last_index));152} else {153lhs = std::make_unique<ArraySubscriptNode>(loc, std::move(lhs),154std::move(*index));155}156Expect(Token::r_square);157m_dil_lexer.Advance();158break;159}160case Token::period:161case Token::arrow: {162m_dil_lexer.Advance();163Token member_token = CurToken();164std::string member_id = ParseIdExpression();165lhs = std::make_unique<MemberOfNode>(166member_token.GetLocation(), std::move(lhs),167token.GetKind() == Token::arrow, member_id);168break;169}170default:171llvm_unreachable("invalid token");172}173}174175return lhs;176}177178// Parse a primary_expression.179//180// primary_expression:181// id_expression182// "(" expression ")"183//184ASTNodeUP DILParser::ParsePrimaryExpression() {185if (CurToken().IsOneOf(186{Token::coloncolon, Token::identifier, Token::l_paren})) {187// Save the source location for the diagnostics message.188uint32_t loc = CurToken().GetLocation();189std::string identifier = ParseIdExpression();190191if (!identifier.empty())192return std::make_unique<IdentifierNode>(loc, identifier);193}194195if (CurToken().Is(Token::l_paren)) {196m_dil_lexer.Advance();197auto expr = ParseExpression();198Expect(Token::r_paren);199m_dil_lexer.Advance();200return expr;201}202203BailOut(llvm::formatv("Unexpected token: {0}", CurToken()),204CurToken().GetLocation(), CurToken().GetSpelling().length());205return std::make_unique<ErrorNode>();206}207208// Parse nested_name_specifier.209//210// nested_name_specifier:211// type_name "::"212// namespace_name "::"213// nested_name_specifier identifier "::"214//215std::string DILParser::ParseNestedNameSpecifier() {216// The first token in nested_name_specifier is always an identifier, or217// '(anonymous namespace)'.218switch (CurToken().GetKind()) {219case Token::l_paren: {220// Anonymous namespaces need to be treated specially: They are221// represented the the string '(anonymous namespace)', which has a222// space in it (throwing off normal parsing) and is not actually223// proper C++> Check to see if we're looking at224// '(anonymous namespace)::...'225226// Look for all the pieces, in order:227// l_paren 'anonymous' 'namespace' r_paren coloncolon228if (m_dil_lexer.LookAhead(1).Is(Token::identifier) &&229(m_dil_lexer.LookAhead(1).GetSpelling() == "anonymous") &&230m_dil_lexer.LookAhead(2).Is(Token::identifier) &&231(m_dil_lexer.LookAhead(2).GetSpelling() == "namespace") &&232m_dil_lexer.LookAhead(3).Is(Token::r_paren) &&233m_dil_lexer.LookAhead(4).Is(Token::coloncolon)) {234m_dil_lexer.Advance(4);235236Expect(Token::coloncolon);237m_dil_lexer.Advance();238if (!CurToken().Is(Token::identifier) && !CurToken().Is(Token::l_paren)) {239BailOut("Expected an identifier or anonymous namespace, but not found.",240CurToken().GetLocation(), CurToken().GetSpelling().length());241}242// Continue parsing the nested_namespace_specifier.243std::string identifier2 = ParseNestedNameSpecifier();244245return "(anonymous namespace)::" + identifier2;246}247248return "";249} // end of special handling for '(anonymous namespace)'250case Token::identifier: {251// If the next token is scope ("::"), then this is indeed a252// nested_name_specifier253if (m_dil_lexer.LookAhead(1).Is(Token::coloncolon)) {254// This nested_name_specifier is a single identifier.255std::string identifier = CurToken().GetSpelling();256m_dil_lexer.Advance(1);257Expect(Token::coloncolon);258m_dil_lexer.Advance();259// Continue parsing the nested_name_specifier.260return identifier + "::" + ParseNestedNameSpecifier();261}262263return "";264}265default:266return "";267}268}269270// Parse an id_expression.271//272// id_expression:273// unqualified_id274// qualified_id275//276// qualified_id:277// ["::"] [nested_name_specifier] unqualified_id278// ["::"] identifier279//280// identifier:281// ? Token::identifier ?282//283std::string DILParser::ParseIdExpression() {284// Try parsing optional global scope operator.285bool global_scope = false;286if (CurToken().Is(Token::coloncolon)) {287global_scope = true;288m_dil_lexer.Advance();289}290291// Try parsing optional nested_name_specifier.292std::string nested_name_specifier = ParseNestedNameSpecifier();293294// If nested_name_specifier is present, then it's qualified_id production.295// Follow the first production rule.296if (!nested_name_specifier.empty()) {297// Parse unqualified_id and construct a fully qualified id expression.298auto unqualified_id = ParseUnqualifiedId();299300return llvm::formatv("{0}{1}{2}", global_scope ? "::" : "",301nested_name_specifier, unqualified_id);302}303304if (!CurToken().Is(Token::identifier))305return "";306307// No nested_name_specifier, but with global scope -- this is also a308// qualified_id production. Follow the second production rule.309if (global_scope) {310Expect(Token::identifier);311std::string identifier = CurToken().GetSpelling();312m_dil_lexer.Advance();313return llvm::formatv("{0}{1}", global_scope ? "::" : "", identifier);314}315316// This is unqualified_id production.317return ParseUnqualifiedId();318}319320// Parse an unqualified_id.321//322// unqualified_id:323// identifier324//325// identifier:326// ? Token::identifier ?327//328std::string DILParser::ParseUnqualifiedId() {329Expect(Token::identifier);330std::string identifier = CurToken().GetSpelling();331m_dil_lexer.Advance();332return identifier;333}334335void DILParser::BailOut(const std::string &error, uint32_t loc,336uint16_t err_len) {337if (m_error)338// If error is already set, then the parser is in the "bail-out" mode. Don't339// do anything and keep the original error.340return;341342m_error =343llvm::make_error<DILDiagnosticError>(m_input_expr, error, loc, err_len);344// Advance the lexer token index to the end of the lexed tokens vector.345m_dil_lexer.ResetTokenIdx(m_dil_lexer.NumLexedTokens() - 1);346}347348// Parse a integer_literal.349//350// integer_literal:351// ? Integer constant ?352//353std::optional<int64_t> DILParser::ParseIntegerConstant() {354std::string number_spelling;355if (CurToken().GetKind() == Token::minus) {356// StringRef::getAsInteger<>() can parse negative numbers.357// FIXME: Remove this once unary minus operator is added.358number_spelling = "-";359m_dil_lexer.Advance();360}361number_spelling.append(CurToken().GetSpelling());362llvm::StringRef spelling_ref = number_spelling;363int64_t raw_value;364if (!spelling_ref.getAsInteger<int64_t>(0, raw_value)) {365m_dil_lexer.Advance();366return raw_value;367}368369return std::nullopt;370}371372void DILParser::Expect(Token::Kind kind) {373if (CurToken().IsNot(kind)) {374BailOut(llvm::formatv("expected {0}, got: {1}", kind, CurToken()),375CurToken().GetLocation(), CurToken().GetSpelling().length());376}377}378379} // namespace lldb_private::dil380381382