LLDB mainline
DILParser.cpp
Go to the documentation of this file.
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-exception
6//
7// This implements the recursive descent parser for the Data Inspection
8// Language (DIL), and its helper functions, which will eventually underlie the
9// 'frame variable' command. The language that this parser recognizes is
10// described in lldb/docs/dil-expr-lang.ebnf
11//
12//===----------------------------------------------------------------------===//
13
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/FormatAdapters.h"
24#include <cstdlib>
25#include <limits.h>
26#include <memory>
27#include <sstream>
28#include <string>
29
30namespace lldb_private::dil {
31
33 const std::string &message, uint32_t loc,
34 uint16_t err_len)
35 : ErrorInfo(make_error_code(std::errc::invalid_argument)) {
37 FileSpec{}, /*line=*/1, static_cast<uint16_t>(loc + 1),
38 err_len, false, /*in_user_input=*/true};
39 // If the error is not handled by `RenderDiagnosticDetails`, this creates an
40 // error message that can be displayed instead.
41 // Example:
42 // (lldb) script lldb.frame.GetValueForVariablePath("1 + foo")
43 // error: <user expression>:1:5: use of undeclared identifier 'foo'
44 // 1 | 1 + foo
45 // | ^~~
46 auto msg = llvm::formatv("<user expression>:1:{0}: {1}\n 1 | {2}\n |",
47 loc + 1, message, expr);
48 std::string rendered_str;
49 llvm::raw_string_ostream rendered_os(rendered_str);
50 rendered_os << msg.str();
51 rendered_os << llvm::indent(loc + 1) << "^";
52 if (err_len > 1) {
53 // Underline the rest of the erroneous token after the cursor '^'.
54 rendered_os << std::string(err_len - 1, '~');
55 }
56 m_detail.source_location = sloc;
58 m_detail.message = message;
59 m_detail.rendered = std::move(rendered_str);
60}
61
62CompilerType ResolveTypeByName(const std::string &name,
63 ExecutionContextScope &ctx_scope) {
64 // Internally types don't have global scope qualifier in their names and
65 // LLDB doesn't support queries with it too.
66 llvm::StringRef name_ref(name);
67
68 if (name_ref.starts_with("::"))
69 name_ref = name_ref.drop_front(2);
70
71 std::vector<CompilerType> result_type_list;
72 lldb::TargetSP target_sp = ctx_scope.CalculateTarget();
73 if (!name_ref.empty() && target_sp) {
74 ModuleList &images = target_sp->GetImages();
75 TypeQuery query{ConstString(name_ref), TypeQueryOptions::e_exact_match |
76 TypeQueryOptions::e_find_one};
77 TypeResults results;
78 images.FindTypes(nullptr, query, results);
79 const lldb::TypeSP &type_sp = results.GetFirstType();
80 if (type_sp)
81 result_type_list.push_back(type_sp->GetFullCompilerType());
82 }
83
84 if (!result_type_list.empty()) {
85 CompilerType type = result_type_list[0];
86 if (type.IsValid() && type.GetTypeName().GetStringRef() == name_ref)
87 return type;
88 }
89
90 return {};
91}
92
93llvm::Expected<ASTNodeUP> DILParser::Parse(llvm::StringRef dil_input_expr,
94 DILLexer lexer,
95 StackFrame &stack_frame,
96 lldb::DynamicValueType use_dynamic,
97 lldb::DILMode mode) {
98 llvm::Error error = llvm::Error::success();
99 DILParser parser(dil_input_expr, lexer, stack_frame, use_dynamic, error,
100 mode);
101
102 ASTNodeUP node_up = parser.Run();
103 assert(node_up && "ASTNodeUP must not contain a nullptr");
104
105 if (error)
106 return error;
107
108 return node_up;
109}
110
111DILParser::DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,
112 StackFrame &stack_frame,
113 lldb::DynamicValueType use_dynamic, llvm::Error &error,
114 lldb::DILMode mode)
115 : m_stack_frame(stack_frame), m_input_expr(dil_input_expr),
116 m_dil_lexer(std::move(lexer)), m_error(error), m_use_dynamic(use_dynamic),
117 m_mode(mode) {}
118
120 ASTNodeUP expr = ParseExpression();
121
123
124 return expr;
125}
126
127// Parse an expression.
128//
129// expression:
130// assignment_expression
131//
133
134// Parse an assignment_expression
135//
136// assignment_expression
137// shift_expression
138// shift_expression assignment_operator assignment_expression
139//
140// assignment_operator:
141// "="
142// "+="
143// "-="
144//
146 auto lhs = ParseShiftExpression();
147 assert(lhs && "ASTNodeUP must not contain a nullptr");
148
149 // Check if it's an assignment expression.
151 // That's an assignment!
152 Token token = CurToken();
153 m_dil_lexer.Advance();
154 auto rhs = ParseAssignmentExpression();
155 assert(rhs && "ASTNodeUP must not contain a nullptr");
156 lhs = std::make_unique<BinaryOpNode>(
158 std::move(lhs), std::move(rhs));
159 }
160 return lhs;
161}
162
163// Parse a shift_expression.
164//
165// shift_expression:
166// additive_expression {"<<" additive_expression}
167// additive_expression {">>" additive_expression}
168//
170 auto lhs = ParseAdditiveExpression();
171 assert(lhs && "ASTNodeUP must not contain a nullptr");
172
173 while (CurToken().IsOneOf({Token::lessless, Token::greatergreater})) {
174 Token token = CurToken();
175 m_dil_lexer.Advance();
176 auto rhs = ParseAdditiveExpression();
177 assert(rhs && "ASTNodeUP must not contain a nullptr");
178 lhs = std::make_unique<BinaryOpNode>(
180 std::move(lhs), std::move(rhs));
181 }
182
183 return lhs;
184}
185
186// Parse an additive_expression.
187//
188// additive_expression:
189// multiplicative_expression {"+" multiplicative_expression}
190// multiplicative_expression {"-" multiplicative_expression}
191//
194 assert(lhs && "ASTNodeUP must not contain a nullptr");
195
196 while (CurToken().IsOneOf({Token::plus, Token::minus})) {
197 Token token = CurToken();
198 m_dil_lexer.Advance();
200 assert(rhs && "ASTNodeUP must not contain a nullptr");
201 lhs = std::make_unique<BinaryOpNode>(
203 std::move(lhs), std::move(rhs));
204 }
205
206 return lhs;
207}
208
209// Parse a multiplicative_expression.
210//
211// multiplicative_expression:
212// cast_expression {"*" cast_expression}
213// cast_expression {"/" cast_expression}
214// cast_expression {"%" cast_expression}
215//
217 auto lhs = ParseCastExpression();
218
219 while (CurToken().IsOneOf({Token::star, Token::slash, Token::percent})) {
220 Token token = CurToken();
221 if (token.Is(Token::star) && m_mode != lldb::eDILModeFull) {
222 BailOut("binary multiplication (*) is allowed only in DIL full mode",
223 token.GetLocation(), token.GetSpelling().length());
224 return std::make_unique<ErrorNode>();
225 }
226 m_dil_lexer.Advance();
227 auto rhs = ParseCastExpression();
228 assert(rhs && "ASTNodeUP must not contain a nullptr");
229 lhs = std::make_unique<BinaryOpNode>(
231 std::move(lhs), std::move(rhs));
232 }
233
234 return lhs;
235}
236
237// Parse a cast_expression.
238//
239// cast_expression:
240// unary_expression
241// "(" type_id ")" cast_expression
242
244 if (!CurToken().Is(Token::l_paren))
245 return ParseUnaryExpression();
246
247 // This could be a type cast, try parsing the contents as a type declaration.
248 Token token = CurToken();
249 uint32_t loc = token.GetLocation();
250
251 // Enable lexer backtracking, so that we can rollback in case it's not
252 // actually a type declaration.
253
254 // Start tentative parsing (save token location/idx, for possible rollback).
255 uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx();
256
257 // Consume the token only after enabling the backtracking.
258 m_dil_lexer.Advance();
259
260 // Try parsing the type declaration. If the returned value is not valid,
261 // then we should rollback and try parsing the expression.
262 auto type_id = ParseTypeId();
263 if (type_id) {
264 // Successfully parsed the type declaration. Commit the backtracked
265 // tokens and parse the cast_expression.
266
267 if (!type_id.value().IsValid())
268 return std::make_unique<ErrorNode>();
269
271 m_dil_lexer.Advance();
272 auto rhs = ParseCastExpression();
273 assert(rhs && "ASTNodeUP must not contain a nullptr");
274 return std::make_unique<CastNode>(loc, type_id.value(), std::move(rhs),
276 }
277
278 // Failed to parse the contents of the parentheses as a type declaration.
279 // Rollback the lexer and try parsing it as unary_expression.
280 TentativeParsingRollback(save_token_idx);
281
282 return ParseUnaryExpression();
283}
284
285// Parse an unary_expression.
286//
287// unary_expression:
288// postfix_expression
289// unary_operator cast_expression
290//
291// unary_operator:
292// "&"
293// "*"
294// "+"
295// "-"
296//
298 if (CurToken().IsOneOf(
300 Token token = CurToken();
301 uint32_t loc = token.GetLocation();
302 m_dil_lexer.Advance();
303 auto rhs = ParseCastExpression();
304 assert(rhs && "ASTNodeUP must not contain a nullptr");
305 switch (token.GetKind()) {
306 case Token::star:
307 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Deref,
308 std::move(rhs));
309 case Token::amp:
310 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::AddrOf,
311 std::move(rhs));
312 case Token::minus:
313 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Minus,
314 std::move(rhs));
315 case Token::plus:
316 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Plus,
317 std::move(rhs));
318 default:
319 llvm_unreachable("invalid token kind");
320 }
321 }
322 return ParsePostfixExpression();
323}
324
325// Parse a postfix_expression.
326//
327// postfix_expression:
328// primary_expression
329// postfix_expression "[" expression "]"
330// postfix_expression "[" expression ":" expression "]"
331// postfix_expression "." id_expression
332// postfix_expression "->" id_expression
333//
336 assert(lhs && "ASTNodeUP must not contain a nullptr");
337 while (CurToken().IsOneOf({Token::l_square, Token::period, Token::arrow})) {
338 uint32_t loc = CurToken().GetLocation();
339 Token token = CurToken();
340 switch (token.GetKind()) {
341 case Token::l_square: {
342 m_dil_lexer.Advance();
343 ASTNodeUP index = ParseExpression();
344 assert(index && "ASTNodeUP must not contain a nullptr");
345 if (CurToken().GetKind() == Token::colon) {
346 m_dil_lexer.Advance();
347 ASTNodeUP last_index = ParseExpression();
348 assert(last_index && "ASTNodeUP must not contain a nullptr");
349 lhs = std::make_unique<BitFieldExtractionNode>(
350 loc, std::move(lhs), std::move(index), std::move(last_index));
351 } else if (CurToken().GetKind() == Token::minus) {
352 BailOut("use of '-' for bitfield range is deprecated; use ':' instead",
353 CurToken().GetLocation(), CurToken().GetSpelling().length());
354 return std::make_unique<ErrorNode>();
355 } else {
356 lhs = std::make_unique<ArraySubscriptNode>(loc, std::move(lhs),
357 std::move(index));
358 }
360 m_dil_lexer.Advance();
361 break;
362 }
363 case Token::period:
364 case Token::arrow: {
365 m_dil_lexer.Advance();
366 Token member_token = CurToken();
367 std::string member_id = ParseIdExpression();
368 lhs = std::make_unique<MemberOfNode>(
369 member_token.GetLocation(), std::move(lhs),
370 token.GetKind() == Token::arrow, member_id);
371 break;
372 }
373 default:
374 llvm_unreachable("invalid token");
375 }
376 }
377
378 return lhs;
379}
380
381// Parse a primary_expression.
382//
383// primary_expression:
384// numeric_literal
385// boolean_literal
386// id_expression
387// "(" expression ")"
388//
391 return ParseNumericLiteral();
392 if (CurToken().IsOneOf({Token::kw_true, Token::kw_false}))
393 return ParseBooleanLiteral();
394 if (CurToken().IsOneOf(
396 // Save the source location for the diagnostics message.
397 uint32_t loc = CurToken().GetLocation();
398 std::string identifier = ParseIdExpression();
399
400 if (!identifier.empty())
401 return std::make_unique<IdentifierNode>(loc, identifier);
402 }
403
404 if (CurToken().Is(Token::l_paren)) {
405 m_dil_lexer.Advance();
406 auto expr = ParseExpression();
408 m_dil_lexer.Advance();
409 return expr;
410 }
411
412 BailOut(llvm::formatv("Unexpected token: {0}", CurToken()),
413 CurToken().GetLocation(), CurToken().GetSpelling().length());
414 return std::make_unique<ErrorNode>();
415}
416
417// Parse nested_name_specifier.
418//
419// nested_name_specifier:
420// type_name "::"
421// namespace_name "::"
422// nested_name_specifier identifier "::"
423//
425 // The first token in nested_name_specifier is always an identifier, or
426 // '(anonymous namespace)'.
427 switch (CurToken().GetKind()) {
428 case Token::l_paren: {
429 // Anonymous namespaces need to be treated specially: They are
430 // represented the the string '(anonymous namespace)', which has a
431 // space in it (throwing off normal parsing) and is not actually
432 // proper C++> Check to see if we're looking at
433 // '(anonymous namespace)::...'
434
435 // Look for all the pieces, in order:
436 // l_paren 'anonymous' 'namespace' r_paren coloncolon
437 if (m_dil_lexer.LookAhead(1).Is(Token::identifier) &&
438 (m_dil_lexer.LookAhead(1).GetSpelling() == "anonymous") &&
439 m_dil_lexer.LookAhead(2).Is(Token::identifier) &&
440 (m_dil_lexer.LookAhead(2).GetSpelling() == "namespace") &&
441 m_dil_lexer.LookAhead(3).Is(Token::r_paren) &&
442 m_dil_lexer.LookAhead(4).Is(Token::coloncolon)) {
443 m_dil_lexer.Advance(4);
444
446 m_dil_lexer.Advance();
447 if (!CurToken().Is(Token::identifier) && !CurToken().Is(Token::l_paren)) {
448 BailOut("Expected an identifier or anonymous namespace, but not found.",
449 CurToken().GetLocation(), CurToken().GetSpelling().length());
450 }
451 // Continue parsing the nested_namespace_specifier.
452 std::string identifier2 = ParseNestedNameSpecifier();
453
454 return "(anonymous namespace)::" + identifier2;
455 }
456
457 return "";
458 } // end of special handling for '(anonymous namespace)'
459 case Token::identifier: {
460 // If the next token is scope ("::"), then this is indeed a
461 // nested_name_specifier
462 if (m_dil_lexer.LookAhead(1).Is(Token::coloncolon)) {
463 // This nested_name_specifier is a single identifier.
464 std::string identifier = CurToken().GetSpelling();
465 m_dil_lexer.Advance(1);
467 m_dil_lexer.Advance();
468 // Continue parsing the nested_name_specifier.
469 return identifier + "::" + ParseNestedNameSpecifier();
470 }
471
472 return "";
473 }
474 default:
475 return "";
476 }
477}
478
479// Parse a type_id.
480//
481// type_id:
482// type_specifier_seq [abstract_declarator]
483//
484// type_specifier_seq:
485// type_specifier [type_specifier]
486//
487// type_specifier:
488// ["::"] [nested_name_specifier] type_name // not handled for now!
489// builtin_typename
490//
491std::optional<CompilerType> DILParser::ParseTypeId() {
492 CompilerType type;
493 auto maybe_builtin_type = ParseBuiltinType();
494 if (maybe_builtin_type) {
495 type = *maybe_builtin_type;
496 } else {
497 // Check to see if we have a user-defined type here.
498 // First build up the user-defined type name.
499 std::string type_name;
500 ParseTypeSpecifierSeq(type_name);
501
502 if (type_name.empty())
503 return {};
504 type = ResolveTypeByName(type_name, m_stack_frame);
505 if (!type.IsValid())
506 return {};
507
508 // Same-name identifiers should be preferred over typenames.
510 // TODO: Make type accessible with 'class', 'struct' and 'union' keywords.
511 return {};
512
513 // Same-name identifiers should be preferred over typenames.
515 m_stack_frame.CalculateTarget(), m_use_dynamic))
516 // TODO: Make type accessible with 'class', 'struct' and 'union' keywords
517 return {};
518 }
519
520 //
521 // abstract_declarator:
522 // ptr_operator [abstract_declarator]
523 //
524 std::vector<Token> ptr_operators;
525 while (CurToken().IsOneOf({Token::star, Token::amp})) {
526 Token tok = CurToken();
527 ptr_operators.push_back(std::move(tok));
528 m_dil_lexer.Advance();
529 }
530 type = ResolveTypeDeclarators(type, ptr_operators);
531
532 return type;
533}
534
535// Parse a built-in type
536//
537// builtin_typename:
538// identifer_seq
539//
540// identifier_seq
541// identifer [identifier_seq]
542//
543// A built-in type can be a single identifier or a space-separated
544// list of identifiers (e.g. "short" or "long long").
545std::optional<CompilerType> DILParser::ParseBuiltinType() {
546 std::string type_name = "";
547 uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx();
548 bool first_word = true;
549 while (CurToken().GetKind() == Token::identifier) {
550 if (CurToken().GetSpelling() == "const" ||
551 CurToken().GetSpelling() == "volatile")
552 continue;
553 if (!first_word)
554 type_name.push_back(' ');
555 else
556 first_word = false;
557 type_name.append(CurToken().GetSpelling());
558 m_dil_lexer.Advance();
559 }
560
561 if (type_name.size() > 0) {
562 lldb::TargetSP target_sp = m_stack_frame.CalculateTarget();
563 ConstString const_type_name(type_name);
564 for (auto type_system_sp : target_sp->GetScratchTypeSystems())
565 if (auto compiler_type =
566 type_system_sp->GetBuiltinTypeByName(const_type_name))
567 return compiler_type;
568 }
569
570 TentativeParsingRollback(save_token_idx);
571 return {};
572}
573
574// Parse a type_specifier_seq.
575//
576// type_specifier_seq:
577// type_specifier [type_specifier_seq]
578//
579void DILParser::ParseTypeSpecifierSeq(std::string &type_name) {
580 while (true) {
581 std::optional<std::string> err_or_string = ParseTypeSpecifier();
582 if (!err_or_string)
583 break;
584 type_name = *err_or_string;
585 }
586}
587
588// Parse a type_specifier.
589//
590// type_specifier:
591// ["::"] [nested_name_specifier] type_name
592//
593// Returns TRUE if a type_specifier was successfully parsed at this location.
594//
595std::optional<std::string> DILParser::ParseTypeSpecifier() {
596 // The type_specifier must be a user-defined type. Try parsing a
597 // simple_type_specifier.
598
599 // Try parsing optional global scope operator.
600 bool global_scope = false;
601 if (CurToken().Is(Token::coloncolon)) {
602 global_scope = true;
603 m_dil_lexer.Advance();
604 }
605
606 // Try parsing optional nested_name_specifier.
607 auto nested_name_specifier = ParseNestedNameSpecifier();
608
609 // Try parsing required type_name.
610 auto type_name_or_err = ParseTypeName();
611 if (!type_name_or_err)
612 return type_name_or_err;
613 std::string type_name = *type_name_or_err;
614
615 // If there is a type_name, then this is indeed a simple_type_specifier.
616 // Global and qualified (namespace/class) scopes can be empty, since they're
617 // optional. In this case type_name is type we're looking for.
618 if (!type_name.empty())
619 // User-defined typenames can't be combined with builtin keywords.
620 return llvm::formatv("{0}{1}{2}", global_scope ? "::" : "",
621 nested_name_specifier, type_name);
622
623 // No type_specifier was found here.
624 return {};
625}
626
627// Parse a type_name.
628//
629// type_name:
630// class_name
631// enum_name
632// typedef_name
633//
634// class_name
635// identifier
636//
637// enum_name
638// identifier
639//
640// typedef_name
641// identifier
642//
643std::optional<std::string> DILParser::ParseTypeName() {
644 // Typename always starts with an identifier.
645 if (CurToken().IsNot(Token::identifier)) {
646 return std::nullopt;
647 }
648
649 // Otherwise look for a class_name, enum_name or a typedef_name.
650 std::string identifier = CurToken().GetSpelling();
651 m_dil_lexer.Advance();
652
653 return identifier;
654}
655
656// Parse an id_expression.
657//
658// id_expression:
659// unqualified_id
660// qualified_id
661//
662// qualified_id:
663// ["::"] [nested_name_specifier] unqualified_id
664// ["::"] identifier
665//
666// identifier:
667// ? Token::identifier ?
668//
670 // Try parsing optional global scope operator.
671 bool global_scope = false;
672 if (CurToken().Is(Token::coloncolon)) {
673 global_scope = true;
674 m_dil_lexer.Advance();
675 }
676
677 // Try parsing optional nested_name_specifier.
678 std::string nested_name_specifier = ParseNestedNameSpecifier();
679
680 // If nested_name_specifier is present, then it's qualified_id production.
681 // Follow the first production rule.
682 if (!nested_name_specifier.empty()) {
683 // Parse unqualified_id and construct a fully qualified id expression.
684 auto unqualified_id = ParseUnqualifiedId();
685
686 return llvm::formatv("{0}{1}{2}", global_scope ? "::" : "",
687 nested_name_specifier, unqualified_id);
688 }
689
690 if (!CurToken().Is(Token::identifier))
691 return "";
692
693 // No nested_name_specifier, but with global scope -- this is also a
694 // qualified_id production. Follow the second production rule.
695 if (global_scope) {
697 std::string identifier = CurToken().GetSpelling();
698 m_dil_lexer.Advance();
699 return llvm::formatv("{0}{1}", global_scope ? "::" : "", identifier);
700 }
701
702 // This is unqualified_id production.
703 return ParseUnqualifiedId();
704}
705
706// Parse an unqualified_id.
707//
708// unqualified_id:
709// identifier
710//
711// identifier:
712// ? Token::identifier ?
713//
716 std::string identifier = CurToken().GetSpelling();
717 m_dil_lexer.Advance();
718 return identifier;
719}
720
723 const std::vector<Token> &ptr_operators) {
724 // Resolve pointers/references.
725 for (Token tk : ptr_operators) {
726 uint32_t loc = tk.GetLocation();
727 if (tk.GetKind() == Token::star) {
728 // Pointers to reference types are forbidden.
729 if (type.IsReferenceType()) {
730 BailOut(llvm::formatv("'type name' declared as a pointer to a "
731 "reference of type {0}",
732 type.TypeDescription()),
733 loc, CurToken().GetSpelling().length());
734 return {};
735 }
736 // Get pointer type for the base type: e.g. int* -> int**.
737 type = type.GetPointerType();
738
739 } else if (tk.GetKind() == Token::amp) {
740 // References to references are forbidden.
741 // FIXME: In future we may want to allow rvalue references (i.e. &&).
742 if (type.IsReferenceType()) {
743 BailOut("type name declared as a reference to a reference", loc,
744 CurToken().GetSpelling().length());
745 return {};
746 }
747 // Get reference type for the base type: e.g. int -> int&.
748 type = type.GetLValueReferenceType();
749 }
750 }
751
752 return type;
753}
754
755// Parse an boolean_literal.
756//
757// boolean_literal:
758// "true"
759// "false"
760//
762 ExpectOneOf(std::vector<Token::Kind>{Token::kw_true, Token::kw_false});
763 uint32_t loc = CurToken().GetLocation();
764 bool literal_value = CurToken().Is(Token::kw_true);
765 m_dil_lexer.Advance();
766 return std::make_unique<BooleanLiteralNode>(loc, literal_value);
767}
768
769void DILParser::BailOut(const std::string &error, uint32_t loc,
770 uint16_t err_len) {
771 if (m_error)
772 // If error is already set, then the parser is in the "bail-out" mode. Don't
773 // do anything and keep the original error.
774 return;
775
776 m_error =
777 llvm::make_error<DILDiagnosticError>(m_input_expr, error, loc, err_len);
778 // Advance the lexer token index to the end of the lexed tokens vector.
779 m_dil_lexer.ResetTokenIdx(m_dil_lexer.NumLexedTokens() - 1);
780}
781
782// Parse a numeric_literal.
783//
784// numeric_literal:
785// ? Token::integer_constant ?
786// ? Token::floating_constant ?
787//
789 ASTNodeUP numeric_constant;
791 numeric_constant = ParseIntegerLiteral();
792 else
793 numeric_constant = ParseFloatingPointLiteral();
794 if (numeric_constant->GetKind() == NodeKind::eErrorNode) {
795 BailOut(llvm::formatv("Failed to parse token as numeric-constant: {0}",
796 CurToken()),
797 CurToken().GetLocation(), CurToken().GetSpelling().length());
798 return numeric_constant;
799 }
800 m_dil_lexer.Advance();
801 return numeric_constant;
802}
803
805 Token token = CurToken();
806 auto spelling = token.GetSpelling();
807 llvm::StringRef spelling_ref = spelling;
808
809 auto radix = llvm::getAutoSenseRadix(spelling_ref);
811 bool is_unsigned = false;
812 if (spelling_ref.consume_back_insensitive("u"))
813 is_unsigned = true;
814 if (spelling_ref.consume_back_insensitive("ll"))
816 else if (spelling_ref.consume_back_insensitive("l"))
818 // Suffix 'u' can be only specified only once, before or after 'l'
819 if (!is_unsigned && spelling_ref.consume_back_insensitive("u"))
820 is_unsigned = true;
821
822 llvm::APInt raw_value;
823 if (!spelling_ref.getAsInteger(radix, raw_value))
824 return std::make_unique<IntegerLiteralNode>(token.GetLocation(), raw_value,
825 radix, is_unsigned, type);
826 return std::make_unique<ErrorNode>();
827}
828
830 Token token = CurToken();
831 auto spelling = token.GetSpelling();
832 llvm::StringRef spelling_ref = spelling;
833
834 llvm::APFloat raw_float(llvm::APFloat::IEEEdouble());
835 if (spelling_ref.consume_back_insensitive("f"))
836 raw_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
837
838 auto StatusOrErr = raw_float.convertFromString(
839 spelling_ref, llvm::APFloat::rmNearestTiesToEven);
840 if (!errorToBool(StatusOrErr.takeError()))
841 return std::make_unique<FloatLiteralNode>(token.GetLocation(), raw_float);
842 return std::make_unique<ErrorNode>();
843}
844
846 if (CurToken().IsNot(kind)) {
847 BailOut(llvm::formatv("expected {0}, got: {1}", kind, CurToken()),
848 CurToken().GetLocation(), CurToken().GetSpelling().length());
849 }
850}
851
852void DILParser::ExpectOneOf(std::vector<Token::Kind> kinds_vec) {
853 if (!CurToken().IsOneOf(kinds_vec)) {
854 BailOut(llvm::formatv("expected any of ({0}), got: {1}",
855 llvm::iterator_range(kinds_vec), CurToken()),
856 CurToken().GetLocation(), CurToken().GetSpelling().length());
857 }
858}
859
860} // namespace lldb_private::dil
static llvm::raw_ostream & error(Stream &strm)
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
A uniqued constant string class.
Definition ConstString.h:40
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 file utility class.
Definition FileSpec.h:57
A collection class for Module objects.
Definition ModuleList.h:125
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.
Definition StackFrame.h:44
A class that contains all state required for type lookups.
Definition Type.h:104
This class tracks the state and results of a TypeQuery.
Definition Type.h:344
lldb::TypeSP GetFirstType() const
Definition Type.h:385
DILDiagnosticError(DiagnosticDetail detail)
Definition DILParser.h:48
std::string message() const override
Definition DILParser.h:63
Class for doing the simple lexing required by DIL.
Definition DILLexer.h:81
void ParseTypeSpecifierSeq(std::string &type_name)
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)
Definition DILParser.cpp:93
void TentativeParsingRollback(uint32_t saved_idx)
Definition DILParser.h:118
ASTNodeUP ParseFloatingPointLiteral()
void ExpectOneOf(std::vector< Token::Kind > kinds_vec)
std::optional< std::string > ParseTypeSpecifier()
ASTNodeUP ParseAssignmentExpression()
std::optional< CompilerType > ParseBuiltinType()
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 ParseMultiplicativeExpression()
lldb::DynamicValueType m_use_dynamic
Definition DILParser.h:138
std::optional< std::string > ParseTypeName()
llvm::StringRef m_input_expr
Definition DILParser.h:131
std::string ParseNestedNameSpecifier()
Class defining the tokens generated by the DIL lexer and used by the DIL parser.
Definition DILLexer.h:25
bool Is(Kind kind) const
Definition DILLexer.h:62
uint32_t GetLocation() const
Definition DILLexer.h:70
Kind GetKind() const
Definition DILLexer.h:58
std::string GetSpelling() const
Definition DILLexer.h:60
@ eNone
Invalid promotion type (results in error).
Definition DILAST.h:65
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...
Definition DILEval.cpp:326
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:98
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.
Definition DILEval.cpp:284
BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind)
Translates DIL tokens to BinaryOpKind.
Definition DILAST.cpp:14
CompilerType ResolveTypeByName(const std::string &name, ExecutionContextScope &ctx_scope)
Definition DILParser.cpp:62
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.