481 lines · cpp
1//===-- DILParser.cpp -----------------------------------------------------===//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-exception6//7// This implements the recursive descent parser for the Data Inspection8// Language (DIL), and its helper functions, which will eventually underlie the9// 'frame variable' command. The language that this parser recognizes is10// described in lldb/docs/dil-expr-lang.ebnf11//12//===----------------------------------------------------------------------===//13 14#include "lldb/ValueObject/DILParser.h"15#include "lldb/Host/common/DiagnosticsRendering.h"16#include "lldb/Target/ExecutionContextScope.h"17#include "lldb/ValueObject/DILAST.h"18#include "lldb/ValueObject/DILEval.h"19#include "llvm/ADT/StringRef.h"20#include "llvm/Support/FormatAdapters.h"21#include <cstdlib>22#include <limits.h>23#include <memory>24#include <sstream>25#include <string>26 27namespace lldb_private::dil {28 29DILDiagnosticError::DILDiagnosticError(llvm::StringRef expr,30 const std::string &message, uint32_t loc,31 uint16_t err_len)32 : ErrorInfo(make_error_code(std::errc::invalid_argument)) {33 DiagnosticDetail::SourceLocation sloc = {34 FileSpec{}, /*line=*/1, static_cast<uint16_t>(loc + 1),35 err_len, false, /*in_user_input=*/true};36 std::string rendered_msg =37 llvm::formatv("<user expression 0>:1:{0}: {1}\n 1 | {2}\n | ^",38 loc + 1, message, expr);39 m_detail.source_location = sloc;40 m_detail.severity = lldb::eSeverityError;41 m_detail.message = message;42 m_detail.rendered = std::move(rendered_msg);43}44 45llvm::Expected<ASTNodeUP>46DILParser::Parse(llvm::StringRef dil_input_expr, DILLexer lexer,47 std::shared_ptr<StackFrame> frame_sp,48 lldb::DynamicValueType use_dynamic, bool use_synthetic,49 bool fragile_ivar, bool check_ptr_vs_member) {50 llvm::Error error = llvm::Error::success();51 DILParser parser(dil_input_expr, lexer, frame_sp, use_dynamic, use_synthetic,52 fragile_ivar, check_ptr_vs_member, error);53 54 ASTNodeUP node_up = parser.Run();55 56 if (error)57 return error;58 59 return node_up;60}61 62DILParser::DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,63 std::shared_ptr<StackFrame> frame_sp,64 lldb::DynamicValueType use_dynamic, bool use_synthetic,65 bool fragile_ivar, bool check_ptr_vs_member,66 llvm::Error &error)67 : m_ctx_scope(frame_sp), m_input_expr(dil_input_expr),68 m_dil_lexer(std::move(lexer)), m_error(error), m_use_dynamic(use_dynamic),69 m_use_synthetic(use_synthetic), m_fragile_ivar(fragile_ivar),70 m_check_ptr_vs_member(check_ptr_vs_member) {}71 72ASTNodeUP DILParser::Run() {73 ASTNodeUP expr = ParseExpression();74 75 Expect(Token::Kind::eof);76 77 return expr;78}79 80// Parse an expression.81//82// expression:83// unary_expression84//85ASTNodeUP DILParser::ParseExpression() { return ParseUnaryExpression(); }86 87// Parse an unary_expression.88//89// unary_expression:90// postfix_expression91// unary_operator expression92//93// unary_operator:94// "&"95// "*"96// "+"97// "-"98//99ASTNodeUP DILParser::ParseUnaryExpression() {100 if (CurToken().IsOneOf(101 {Token::amp, Token::star, Token::minus, Token::plus})) {102 Token token = CurToken();103 uint32_t loc = token.GetLocation();104 m_dil_lexer.Advance();105 auto rhs = ParseExpression();106 switch (token.GetKind()) {107 case Token::star:108 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Deref,109 std::move(rhs));110 case Token::amp:111 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::AddrOf,112 std::move(rhs));113 case Token::minus:114 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Minus,115 std::move(rhs));116 case Token::plus:117 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Plus,118 std::move(rhs));119 default:120 llvm_unreachable("invalid token kind");121 }122 }123 return ParsePostfixExpression();124}125 126// Parse a postfix_expression.127//128// postfix_expression:129// primary_expression130// postfix_expression "[" integer_literal "]"131// postfix_expression "[" integer_literal "-" integer_literal "]"132// postfix_expression "." id_expression133// postfix_expression "->" id_expression134//135ASTNodeUP DILParser::ParsePostfixExpression() {136 ASTNodeUP lhs = ParsePrimaryExpression();137 while (CurToken().IsOneOf({Token::l_square, Token::period, Token::arrow})) {138 uint32_t loc = CurToken().GetLocation();139 Token token = CurToken();140 switch (token.GetKind()) {141 case Token::l_square: {142 m_dil_lexer.Advance();143 std::optional<int64_t> index = ParseIntegerConstant();144 if (!index) {145 BailOut(146 llvm::formatv("failed to parse integer constant: {0}", CurToken()),147 CurToken().GetLocation(), CurToken().GetSpelling().length());148 return std::make_unique<ErrorNode>();149 }150 if (CurToken().GetKind() == Token::minus) {151 m_dil_lexer.Advance();152 std::optional<int64_t> last_index = ParseIntegerConstant();153 if (!last_index) {154 BailOut(llvm::formatv("failed to parse integer constant: {0}",155 CurToken()),156 CurToken().GetLocation(), CurToken().GetSpelling().length());157 return std::make_unique<ErrorNode>();158 }159 lhs = std::make_unique<BitFieldExtractionNode>(160 loc, std::move(lhs), std::move(*index), std::move(*last_index));161 } else {162 lhs = std::make_unique<ArraySubscriptNode>(loc, std::move(lhs),163 std::move(*index));164 }165 Expect(Token::r_square);166 m_dil_lexer.Advance();167 break;168 }169 case Token::period:170 case Token::arrow: {171 m_dil_lexer.Advance();172 Token member_token = CurToken();173 std::string member_id = ParseIdExpression();174 lhs = std::make_unique<MemberOfNode>(175 member_token.GetLocation(), std::move(lhs),176 token.GetKind() == Token::arrow, member_id);177 break;178 }179 default:180 llvm_unreachable("invalid token");181 }182 }183 184 return lhs;185}186 187// Parse a primary_expression.188//189// primary_expression:190// numeric_literal191// boolean_literal192// id_expression193// "(" expression ")"194//195ASTNodeUP DILParser::ParsePrimaryExpression() {196 if (CurToken().IsOneOf({Token::integer_constant, Token::float_constant}))197 return ParseNumericLiteral();198 if (CurToken().IsOneOf({Token::kw_true, Token::kw_false}))199 return ParseBooleanLiteral();200 if (CurToken().IsOneOf(201 {Token::coloncolon, Token::identifier, Token::l_paren})) {202 // Save the source location for the diagnostics message.203 uint32_t loc = CurToken().GetLocation();204 std::string identifier = ParseIdExpression();205 206 if (!identifier.empty())207 return std::make_unique<IdentifierNode>(loc, identifier);208 }209 210 if (CurToken().Is(Token::l_paren)) {211 m_dil_lexer.Advance();212 auto expr = ParseExpression();213 Expect(Token::r_paren);214 m_dil_lexer.Advance();215 return expr;216 }217 218 BailOut(llvm::formatv("Unexpected token: {0}", CurToken()),219 CurToken().GetLocation(), CurToken().GetSpelling().length());220 return std::make_unique<ErrorNode>();221}222 223// Parse nested_name_specifier.224//225// nested_name_specifier:226// type_name "::"227// namespace_name "::"228// nested_name_specifier identifier "::"229//230std::string DILParser::ParseNestedNameSpecifier() {231 // The first token in nested_name_specifier is always an identifier, or232 // '(anonymous namespace)'.233 switch (CurToken().GetKind()) {234 case Token::l_paren: {235 // Anonymous namespaces need to be treated specially: They are236 // represented the the string '(anonymous namespace)', which has a237 // space in it (throwing off normal parsing) and is not actually238 // proper C++> Check to see if we're looking at239 // '(anonymous namespace)::...'240 241 // Look for all the pieces, in order:242 // l_paren 'anonymous' 'namespace' r_paren coloncolon243 if (m_dil_lexer.LookAhead(1).Is(Token::identifier) &&244 (m_dil_lexer.LookAhead(1).GetSpelling() == "anonymous") &&245 m_dil_lexer.LookAhead(2).Is(Token::identifier) &&246 (m_dil_lexer.LookAhead(2).GetSpelling() == "namespace") &&247 m_dil_lexer.LookAhead(3).Is(Token::r_paren) &&248 m_dil_lexer.LookAhead(4).Is(Token::coloncolon)) {249 m_dil_lexer.Advance(4);250 251 Expect(Token::coloncolon);252 m_dil_lexer.Advance();253 if (!CurToken().Is(Token::identifier) && !CurToken().Is(Token::l_paren)) {254 BailOut("Expected an identifier or anonymous namespace, but not found.",255 CurToken().GetLocation(), CurToken().GetSpelling().length());256 }257 // Continue parsing the nested_namespace_specifier.258 std::string identifier2 = ParseNestedNameSpecifier();259 260 return "(anonymous namespace)::" + identifier2;261 }262 263 return "";264 } // end of special handling for '(anonymous namespace)'265 case Token::identifier: {266 // If the next token is scope ("::"), then this is indeed a267 // nested_name_specifier268 if (m_dil_lexer.LookAhead(1).Is(Token::coloncolon)) {269 // This nested_name_specifier is a single identifier.270 std::string identifier = CurToken().GetSpelling();271 m_dil_lexer.Advance(1);272 Expect(Token::coloncolon);273 m_dil_lexer.Advance();274 // Continue parsing the nested_name_specifier.275 return identifier + "::" + ParseNestedNameSpecifier();276 }277 278 return "";279 }280 default:281 return "";282 }283}284 285// Parse an id_expression.286//287// id_expression:288// unqualified_id289// qualified_id290//291// qualified_id:292// ["::"] [nested_name_specifier] unqualified_id293// ["::"] identifier294//295// identifier:296// ? Token::identifier ?297//298std::string DILParser::ParseIdExpression() {299 // Try parsing optional global scope operator.300 bool global_scope = false;301 if (CurToken().Is(Token::coloncolon)) {302 global_scope = true;303 m_dil_lexer.Advance();304 }305 306 // Try parsing optional nested_name_specifier.307 std::string nested_name_specifier = ParseNestedNameSpecifier();308 309 // If nested_name_specifier is present, then it's qualified_id production.310 // Follow the first production rule.311 if (!nested_name_specifier.empty()) {312 // Parse unqualified_id and construct a fully qualified id expression.313 auto unqualified_id = ParseUnqualifiedId();314 315 return llvm::formatv("{0}{1}{2}", global_scope ? "::" : "",316 nested_name_specifier, unqualified_id);317 }318 319 if (!CurToken().Is(Token::identifier))320 return "";321 322 // No nested_name_specifier, but with global scope -- this is also a323 // qualified_id production. Follow the second production rule.324 if (global_scope) {325 Expect(Token::identifier);326 std::string identifier = CurToken().GetSpelling();327 m_dil_lexer.Advance();328 return llvm::formatv("{0}{1}", global_scope ? "::" : "", identifier);329 }330 331 // This is unqualified_id production.332 return ParseUnqualifiedId();333}334 335// Parse an unqualified_id.336//337// unqualified_id:338// identifier339//340// identifier:341// ? Token::identifier ?342//343std::string DILParser::ParseUnqualifiedId() {344 Expect(Token::identifier);345 std::string identifier = CurToken().GetSpelling();346 m_dil_lexer.Advance();347 return identifier;348}349 350// Parse an boolean_literal.351//352// boolean_literal:353// "true"354// "false"355//356ASTNodeUP DILParser::ParseBooleanLiteral() {357 ExpectOneOf(std::vector<Token::Kind>{Token::kw_true, Token::kw_false});358 uint32_t loc = CurToken().GetLocation();359 bool literal_value = CurToken().Is(Token::kw_true);360 m_dil_lexer.Advance();361 return std::make_unique<BooleanLiteralNode>(loc, literal_value);362}363 364void DILParser::BailOut(const std::string &error, uint32_t loc,365 uint16_t err_len) {366 if (m_error)367 // If error is already set, then the parser is in the "bail-out" mode. Don't368 // do anything and keep the original error.369 return;370 371 m_error =372 llvm::make_error<DILDiagnosticError>(m_input_expr, error, loc, err_len);373 // Advance the lexer token index to the end of the lexed tokens vector.374 m_dil_lexer.ResetTokenIdx(m_dil_lexer.NumLexedTokens() - 1);375}376 377// FIXME: Remove this once subscript operator uses ScalarLiteralNode.378// Parse a integer_literal.379//380// integer_literal:381// ? Integer constant ?382//383std::optional<int64_t> DILParser::ParseIntegerConstant() {384 std::string number_spelling;385 if (CurToken().GetKind() == Token::minus) {386 // StringRef::getAsInteger<>() can parse negative numbers.387 // FIXME: Remove this once unary minus operator is added.388 number_spelling = "-";389 m_dil_lexer.Advance();390 }391 number_spelling.append(CurToken().GetSpelling());392 llvm::StringRef spelling_ref = number_spelling;393 int64_t raw_value;394 if (!spelling_ref.getAsInteger<int64_t>(0, raw_value)) {395 m_dil_lexer.Advance();396 return raw_value;397 }398 399 return std::nullopt;400}401 402// Parse a numeric_literal.403//404// numeric_literal:405// ? Token::integer_constant ?406// ? Token::floating_constant ?407//408ASTNodeUP DILParser::ParseNumericLiteral() {409 ASTNodeUP numeric_constant;410 if (CurToken().Is(Token::integer_constant))411 numeric_constant = ParseIntegerLiteral();412 else413 numeric_constant = ParseFloatingPointLiteral();414 if (!numeric_constant) {415 BailOut(llvm::formatv("Failed to parse token as numeric-constant: {0}",416 CurToken()),417 CurToken().GetLocation(), CurToken().GetSpelling().length());418 return std::make_unique<ErrorNode>();419 }420 m_dil_lexer.Advance();421 return numeric_constant;422}423 424ASTNodeUP DILParser::ParseIntegerLiteral() {425 Token token = CurToken();426 auto spelling = token.GetSpelling();427 llvm::StringRef spelling_ref = spelling;428 429 auto radix = llvm::getAutoSenseRadix(spelling_ref);430 IntegerTypeSuffix type = IntegerTypeSuffix::None;431 bool is_unsigned = false;432 if (spelling_ref.consume_back_insensitive("u"))433 is_unsigned = true;434 if (spelling_ref.consume_back_insensitive("ll"))435 type = IntegerTypeSuffix::LongLong;436 else if (spelling_ref.consume_back_insensitive("l"))437 type = IntegerTypeSuffix::Long;438 // Suffix 'u' can be only specified only once, before or after 'l'439 if (!is_unsigned && spelling_ref.consume_back_insensitive("u"))440 is_unsigned = true;441 442 llvm::APInt raw_value;443 if (!spelling_ref.getAsInteger(radix, raw_value))444 return std::make_unique<IntegerLiteralNode>(token.GetLocation(), raw_value,445 radix, is_unsigned, type);446 return nullptr;447}448 449ASTNodeUP DILParser::ParseFloatingPointLiteral() {450 Token token = CurToken();451 auto spelling = token.GetSpelling();452 llvm::StringRef spelling_ref = spelling;453 454 llvm::APFloat raw_float(llvm::APFloat::IEEEdouble());455 if (spelling_ref.consume_back_insensitive("f"))456 raw_float = llvm::APFloat(llvm::APFloat::IEEEsingle());457 458 auto StatusOrErr = raw_float.convertFromString(459 spelling_ref, llvm::APFloat::rmNearestTiesToEven);460 if (!errorToBool(StatusOrErr.takeError()))461 return std::make_unique<FloatLiteralNode>(token.GetLocation(), raw_float);462 return nullptr;463}464 465void DILParser::Expect(Token::Kind kind) {466 if (CurToken().IsNot(kind)) {467 BailOut(llvm::formatv("expected {0}, got: {1}", kind, CurToken()),468 CurToken().GetLocation(), CurToken().GetSpelling().length());469 }470}471 472void DILParser::ExpectOneOf(std::vector<Token::Kind> kinds_vec) {473 if (!CurToken().IsOneOf(kinds_vec)) {474 BailOut(llvm::formatv("expected any of ({0}), got: {1}",475 llvm::iterator_range(kinds_vec), CurToken()),476 CurToken().GetLocation(), CurToken().GetSpelling().length());477 }478}479 480} // namespace lldb_private::dil481