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
62llvm::Expected<lldb::TypeSystemSP>
63GetTypeSystemFromCU(std::shared_ptr<StackFrame> ctx) {
64 SymbolContext symbol_context =
65 ctx->GetSymbolContext(lldb::eSymbolContextCompUnit);
66 lldb::LanguageType language = symbol_context.comp_unit->GetLanguage();
67
68 symbol_context = ctx->GetSymbolContext(lldb::eSymbolContextModule);
69 return symbol_context.module_sp->GetTypeSystemForLanguage(language);
70}
71
72static CompilerType ResolveTypeByName(const std::string &name,
73 ExecutionContextScope &ctx_scope) {
74 // Internally types don't have global scope qualifier in their names and
75 // LLDB doesn't support queries with it too.
76 llvm::StringRef name_ref(name);
77
78 if (name_ref.starts_with("::"))
79 name_ref = name_ref.drop_front(2);
80
81 std::vector<CompilerType> result_type_list;
82 lldb::TargetSP target_sp = ctx_scope.CalculateTarget();
83 if (!name_ref.empty() && target_sp) {
84 ModuleList &images = target_sp->GetImages();
85 TypeQuery query{ConstString(name_ref), TypeQueryOptions::e_exact_match |
86 TypeQueryOptions::e_find_one};
87 TypeResults results;
88 images.FindTypes(nullptr, query, results);
89 const lldb::TypeSP &type_sp = results.GetFirstType();
90 if (type_sp)
91 result_type_list.push_back(type_sp->GetFullCompilerType());
92 }
93
94 if (!result_type_list.empty()) {
95 CompilerType type = result_type_list[0];
96 if (type.IsValid() && type.GetTypeName().GetStringRef() == name_ref)
97 return type;
98 }
99
100 return {};
101}
102
103llvm::Expected<ASTNodeUP> DILParser::Parse(llvm::StringRef dil_input_expr,
104 DILLexer lexer,
105 std::shared_ptr<StackFrame> frame_sp,
106 lldb::DynamicValueType use_dynamic,
107 lldb::DILMode mode) {
108 llvm::Error error = llvm::Error::success();
109 DILParser parser(dil_input_expr, lexer, frame_sp, use_dynamic, error, mode);
110
111 ASTNodeUP node_up = parser.Run();
112 assert(node_up && "ASTNodeUP must not contain a nullptr");
113
114 if (error)
115 return error;
116
117 return node_up;
118}
119
120DILParser::DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,
121 std::shared_ptr<StackFrame> frame_sp,
122 lldb::DynamicValueType use_dynamic, llvm::Error &error,
123 lldb::DILMode mode)
124 : m_ctx_scope(frame_sp), m_input_expr(dil_input_expr),
125 m_dil_lexer(std::move(lexer)), m_error(error), m_use_dynamic(use_dynamic),
126 m_mode(mode) {}
127
129 ASTNodeUP expr = ParseExpression();
130
132
133 return expr;
134}
135
136// Parse an expression.
137//
138// expression:
139// cast_expression
140//
142
143// Parse an additive_expression.
144//
145// additive_expression:
146// multiplicative_expression {"+" multiplicative_expression}
147//
150 assert(lhs && "ASTNodeUP must not contain a nullptr");
151
152 while (CurToken().IsOneOf({Token::plus, Token::minus})) {
153 Token token = CurToken();
154 m_dil_lexer.Advance();
156 assert(rhs && "ASTNodeUP must not contain a nullptr");
157 lhs = std::make_unique<BinaryOpNode>(
159 std::move(lhs), std::move(rhs));
160 }
161
162 return lhs;
163}
164
165// Parse a multiplicative_expression.
166//
167// multiplicative_expression:
168// cast_expression {"*" cast_expression}
169// cast_expression {"/" cast_expression}
170// cast_expression {"%" cast_expression}
171//
173 auto lhs = ParseCastExpression();
174
175 while (CurToken().IsOneOf({Token::star, Token::slash, Token::percent})) {
176 Token token = CurToken();
177 if (token.Is(Token::star) && m_mode != lldb::eDILModeFull) {
178 BailOut("binary multiplication (*) is allowed only in DIL full mode",
179 token.GetLocation(), token.GetSpelling().length());
180 return std::make_unique<ErrorNode>();
181 }
182 m_dil_lexer.Advance();
183 auto rhs = ParseCastExpression();
184 assert(rhs && "ASTNodeUP must not contain a nullptr");
185 lhs = std::make_unique<BinaryOpNode>(
187 std::move(lhs), std::move(rhs));
188 }
189
190 return lhs;
191}
192
193// Parse a cast_expression.
194//
195// cast_expression:
196// unary_expression
197// "(" type_id ")" cast_expression
198
200 if (!CurToken().Is(Token::l_paren))
201 return ParseUnaryExpression();
202
203 // This could be a type cast, try parsing the contents as a type declaration.
204 Token token = CurToken();
205 uint32_t loc = token.GetLocation();
206
207 // Enable lexer backtracking, so that we can rollback in case it's not
208 // actually a type declaration.
209
210 // Start tentative parsing (save token location/idx, for possible rollback).
211 uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx();
212
213 // Consume the token only after enabling the backtracking.
214 m_dil_lexer.Advance();
215
216 // Try parsing the type declaration. If the returned value is not valid,
217 // then we should rollback and try parsing the expression.
218 auto type_id = ParseTypeId();
219 if (type_id) {
220 // Successfully parsed the type declaration. Commit the backtracked
221 // tokens and parse the cast_expression.
222
223 if (!type_id.value().IsValid())
224 return std::make_unique<ErrorNode>();
225
227 m_dil_lexer.Advance();
228 auto rhs = ParseCastExpression();
229 assert(rhs && "ASTNodeUP must not contain a nullptr");
230 return std::make_unique<CastNode>(loc, type_id.value(), std::move(rhs),
232 }
233
234 // Failed to parse the contents of the parentheses as a type declaration.
235 // Rollback the lexer and try parsing it as unary_expression.
236 TentativeParsingRollback(save_token_idx);
237
238 return ParseUnaryExpression();
239}
240
241// Parse an unary_expression.
242//
243// unary_expression:
244// postfix_expression
245// unary_operator cast_expression
246//
247// unary_operator:
248// "&"
249// "*"
250// "+"
251// "-"
252//
254 if (CurToken().IsOneOf(
256 Token token = CurToken();
257 uint32_t loc = token.GetLocation();
258 m_dil_lexer.Advance();
259 auto rhs = ParseCastExpression();
260 assert(rhs && "ASTNodeUP must not contain a nullptr");
261 switch (token.GetKind()) {
262 case Token::star:
263 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Deref,
264 std::move(rhs));
265 case Token::amp:
266 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::AddrOf,
267 std::move(rhs));
268 case Token::minus:
269 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Minus,
270 std::move(rhs));
271 case Token::plus:
272 return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Plus,
273 std::move(rhs));
274 default:
275 llvm_unreachable("invalid token kind");
276 }
277 }
278 return ParsePostfixExpression();
279}
280
281// Parse a postfix_expression.
282//
283// postfix_expression:
284// primary_expression
285// postfix_expression "[" expression "]"
286// postfix_expression "[" expression ":" expression "]"
287// postfix_expression "." id_expression
288// postfix_expression "->" id_expression
289//
292 assert(lhs && "ASTNodeUP must not contain a nullptr");
293 while (CurToken().IsOneOf({Token::l_square, Token::period, Token::arrow})) {
294 uint32_t loc = CurToken().GetLocation();
295 Token token = CurToken();
296 switch (token.GetKind()) {
297 case Token::l_square: {
298 m_dil_lexer.Advance();
299 ASTNodeUP index = ParseExpression();
300 assert(index && "ASTNodeUP must not contain a nullptr");
301 if (CurToken().GetKind() == Token::colon) {
302 m_dil_lexer.Advance();
303 ASTNodeUP last_index = ParseExpression();
304 assert(last_index && "ASTNodeUP must not contain a nullptr");
305 lhs = std::make_unique<BitFieldExtractionNode>(
306 loc, std::move(lhs), std::move(index), std::move(last_index));
307 } else if (CurToken().GetKind() == Token::minus) {
308 BailOut("use of '-' for bitfield range is deprecated; use ':' instead",
309 CurToken().GetLocation(), CurToken().GetSpelling().length());
310 return std::make_unique<ErrorNode>();
311 } else {
312 lhs = std::make_unique<ArraySubscriptNode>(loc, std::move(lhs),
313 std::move(index));
314 }
316 m_dil_lexer.Advance();
317 break;
318 }
319 case Token::period:
320 case Token::arrow: {
321 m_dil_lexer.Advance();
322 Token member_token = CurToken();
323 std::string member_id = ParseIdExpression();
324 lhs = std::make_unique<MemberOfNode>(
325 member_token.GetLocation(), std::move(lhs),
326 token.GetKind() == Token::arrow, member_id);
327 break;
328 }
329 default:
330 llvm_unreachable("invalid token");
331 }
332 }
333
334 return lhs;
335}
336
337// Parse a primary_expression.
338//
339// primary_expression:
340// numeric_literal
341// boolean_literal
342// id_expression
343// "(" expression ")"
344//
347 return ParseNumericLiteral();
348 if (CurToken().IsOneOf({Token::kw_true, Token::kw_false}))
349 return ParseBooleanLiteral();
350 if (CurToken().IsOneOf(
352 // Save the source location for the diagnostics message.
353 uint32_t loc = CurToken().GetLocation();
354 std::string identifier = ParseIdExpression();
355
356 if (!identifier.empty())
357 return std::make_unique<IdentifierNode>(loc, identifier);
358 }
359
360 if (CurToken().Is(Token::l_paren)) {
361 m_dil_lexer.Advance();
362 auto expr = ParseExpression();
364 m_dil_lexer.Advance();
365 return expr;
366 }
367
368 BailOut(llvm::formatv("Unexpected token: {0}", CurToken()),
369 CurToken().GetLocation(), CurToken().GetSpelling().length());
370 return std::make_unique<ErrorNode>();
371}
372
373// Parse nested_name_specifier.
374//
375// nested_name_specifier:
376// type_name "::"
377// namespace_name "::"
378// nested_name_specifier identifier "::"
379//
381 // The first token in nested_name_specifier is always an identifier, or
382 // '(anonymous namespace)'.
383 switch (CurToken().GetKind()) {
384 case Token::l_paren: {
385 // Anonymous namespaces need to be treated specially: They are
386 // represented the the string '(anonymous namespace)', which has a
387 // space in it (throwing off normal parsing) and is not actually
388 // proper C++> Check to see if we're looking at
389 // '(anonymous namespace)::...'
390
391 // Look for all the pieces, in order:
392 // l_paren 'anonymous' 'namespace' r_paren coloncolon
393 if (m_dil_lexer.LookAhead(1).Is(Token::identifier) &&
394 (m_dil_lexer.LookAhead(1).GetSpelling() == "anonymous") &&
395 m_dil_lexer.LookAhead(2).Is(Token::identifier) &&
396 (m_dil_lexer.LookAhead(2).GetSpelling() == "namespace") &&
397 m_dil_lexer.LookAhead(3).Is(Token::r_paren) &&
398 m_dil_lexer.LookAhead(4).Is(Token::coloncolon)) {
399 m_dil_lexer.Advance(4);
400
402 m_dil_lexer.Advance();
403 if (!CurToken().Is(Token::identifier) && !CurToken().Is(Token::l_paren)) {
404 BailOut("Expected an identifier or anonymous namespace, but not found.",
405 CurToken().GetLocation(), CurToken().GetSpelling().length());
406 }
407 // Continue parsing the nested_namespace_specifier.
408 std::string identifier2 = ParseNestedNameSpecifier();
409
410 return "(anonymous namespace)::" + identifier2;
411 }
412
413 return "";
414 } // end of special handling for '(anonymous namespace)'
415 case Token::identifier: {
416 // If the next token is scope ("::"), then this is indeed a
417 // nested_name_specifier
418 if (m_dil_lexer.LookAhead(1).Is(Token::coloncolon)) {
419 // This nested_name_specifier is a single identifier.
420 std::string identifier = CurToken().GetSpelling();
421 m_dil_lexer.Advance(1);
423 m_dil_lexer.Advance();
424 // Continue parsing the nested_name_specifier.
425 return identifier + "::" + ParseNestedNameSpecifier();
426 }
427
428 return "";
429 }
430 default:
431 return "";
432 }
433}
434
435// Parse a type_id.
436//
437// type_id:
438// type_specifier_seq [abstract_declarator]
439//
440// type_specifier_seq:
441// type_specifier [type_specifier]
442//
443// type_specifier:
444// ["::"] [nested_name_specifier] type_name // not handled for now!
445// builtin_typename
446//
447std::optional<CompilerType> DILParser::ParseTypeId() {
448 CompilerType type;
449 auto maybe_builtin_type = ParseBuiltinType();
450 if (maybe_builtin_type) {
451 type = *maybe_builtin_type;
452 } else {
453 // Check to see if we have a user-defined type here.
454 // First build up the user-defined type name.
455 std::string type_name;
456 ParseTypeSpecifierSeq(type_name);
457
458 if (type_name.empty())
459 return {};
460 type = ResolveTypeByName(type_name, *m_ctx_scope);
461 if (!type.IsValid())
462 return {};
463
464 // Same-name identifiers should be preferred over typenames.
466 // TODO: Make type accessible with 'class', 'struct' and 'union' keywords.
467 return {};
468
469 // Same-name identifiers should be preferred over typenames.
470 if (LookupGlobalIdentifier(type_name, m_ctx_scope,
471 m_ctx_scope->CalculateTarget(), m_use_dynamic))
472 // TODO: Make type accessible with 'class', 'struct' and 'union' keywords
473 return {};
474 }
475
476 //
477 // abstract_declarator:
478 // ptr_operator [abstract_declarator]
479 //
480 std::vector<Token> ptr_operators;
481 while (CurToken().IsOneOf({Token::star, Token::amp})) {
482 Token tok = CurToken();
483 ptr_operators.push_back(std::move(tok));
484 m_dil_lexer.Advance();
485 }
486 type = ResolveTypeDeclarators(type, ptr_operators);
487
488 return type;
489}
490
491// Parse a built-in type
492//
493// builtin_typename:
494// identifer_seq
495//
496// identifier_seq
497// identifer [identifier_seq]
498//
499// A built-in type can be a single identifier or a space-separated
500// list of identifiers (e.g. "short" or "long long").
501std::optional<CompilerType> DILParser::ParseBuiltinType() {
502 std::string type_name = "";
503 uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx();
504 bool first_word = true;
505 while (CurToken().GetKind() == Token::identifier) {
506 if (CurToken().GetSpelling() == "const" ||
507 CurToken().GetSpelling() == "volatile")
508 continue;
509 if (!first_word)
510 type_name.push_back(' ');
511 else
512 first_word = false;
513 type_name.append(CurToken().GetSpelling());
514 m_dil_lexer.Advance();
515 }
516
517 if (type_name.size() > 0) {
518 lldb::TargetSP target_sp = m_ctx_scope->CalculateTarget();
519 ConstString const_type_name(type_name.c_str());
520 for (auto type_system_sp : target_sp->GetScratchTypeSystems())
521 if (auto compiler_type =
522 type_system_sp->GetBuiltinTypeByName(const_type_name))
523 return compiler_type;
524 }
525
526 TentativeParsingRollback(save_token_idx);
527 return {};
528}
529
530// Parse a type_specifier_seq.
531//
532// type_specifier_seq:
533// type_specifier [type_specifier_seq]
534//
535void DILParser::ParseTypeSpecifierSeq(std::string &type_name) {
536 while (true) {
537 std::optional<std::string> err_or_string = ParseTypeSpecifier();
538 if (!err_or_string)
539 break;
540 type_name = *err_or_string;
541 }
542}
543
544// Parse a type_specifier.
545//
546// type_specifier:
547// ["::"] [nested_name_specifier] type_name
548//
549// Returns TRUE if a type_specifier was successfully parsed at this location.
550//
551std::optional<std::string> DILParser::ParseTypeSpecifier() {
552 // The type_specifier must be a user-defined type. Try parsing a
553 // simple_type_specifier.
554
555 // Try parsing optional global scope operator.
556 bool global_scope = false;
557 if (CurToken().Is(Token::coloncolon)) {
558 global_scope = true;
559 m_dil_lexer.Advance();
560 }
561
562 // Try parsing optional nested_name_specifier.
563 auto nested_name_specifier = ParseNestedNameSpecifier();
564
565 // Try parsing required type_name.
566 auto type_name_or_err = ParseTypeName();
567 if (!type_name_or_err)
568 return type_name_or_err;
569 std::string type_name = *type_name_or_err;
570
571 // If there is a type_name, then this is indeed a simple_type_specifier.
572 // Global and qualified (namespace/class) scopes can be empty, since they're
573 // optional. In this case type_name is type we're looking for.
574 if (!type_name.empty())
575 // User-defined typenames can't be combined with builtin keywords.
576 return llvm::formatv("{0}{1}{2}", global_scope ? "::" : "",
577 nested_name_specifier, type_name);
578
579 // No type_specifier was found here.
580 return {};
581}
582
583// Parse a type_name.
584//
585// type_name:
586// class_name
587// enum_name
588// typedef_name
589//
590// class_name
591// identifier
592//
593// enum_name
594// identifier
595//
596// typedef_name
597// identifier
598//
599std::optional<std::string> DILParser::ParseTypeName() {
600 // Typename always starts with an identifier.
601 if (CurToken().IsNot(Token::identifier)) {
602 return std::nullopt;
603 }
604
605 // Otherwise look for a class_name, enum_name or a typedef_name.
606 std::string identifier = CurToken().GetSpelling();
607 m_dil_lexer.Advance();
608
609 return identifier;
610}
611
612// Parse an id_expression.
613//
614// id_expression:
615// unqualified_id
616// qualified_id
617//
618// qualified_id:
619// ["::"] [nested_name_specifier] unqualified_id
620// ["::"] identifier
621//
622// identifier:
623// ? Token::identifier ?
624//
626 // Try parsing optional global scope operator.
627 bool global_scope = false;
628 if (CurToken().Is(Token::coloncolon)) {
629 global_scope = true;
630 m_dil_lexer.Advance();
631 }
632
633 // Try parsing optional nested_name_specifier.
634 std::string nested_name_specifier = ParseNestedNameSpecifier();
635
636 // If nested_name_specifier is present, then it's qualified_id production.
637 // Follow the first production rule.
638 if (!nested_name_specifier.empty()) {
639 // Parse unqualified_id and construct a fully qualified id expression.
640 auto unqualified_id = ParseUnqualifiedId();
641
642 return llvm::formatv("{0}{1}{2}", global_scope ? "::" : "",
643 nested_name_specifier, unqualified_id);
644 }
645
646 if (!CurToken().Is(Token::identifier))
647 return "";
648
649 // No nested_name_specifier, but with global scope -- this is also a
650 // qualified_id production. Follow the second production rule.
651 if (global_scope) {
653 std::string identifier = CurToken().GetSpelling();
654 m_dil_lexer.Advance();
655 return llvm::formatv("{0}{1}", global_scope ? "::" : "", identifier);
656 }
657
658 // This is unqualified_id production.
659 return ParseUnqualifiedId();
660}
661
662// Parse an unqualified_id.
663//
664// unqualified_id:
665// identifier
666//
667// identifier:
668// ? Token::identifier ?
669//
672 std::string identifier = CurToken().GetSpelling();
673 m_dil_lexer.Advance();
674 return identifier;
675}
676
679 const std::vector<Token> &ptr_operators) {
680 // Resolve pointers/references.
681 for (Token tk : ptr_operators) {
682 uint32_t loc = tk.GetLocation();
683 if (tk.GetKind() == Token::star) {
684 // Pointers to reference types are forbidden.
685 if (type.IsReferenceType()) {
686 BailOut(llvm::formatv("'type name' declared as a pointer to a "
687 "reference of type {0}",
688 type.TypeDescription()),
689 loc, CurToken().GetSpelling().length());
690 return {};
691 }
692 // Get pointer type for the base type: e.g. int* -> int**.
693 type = type.GetPointerType();
694
695 } else if (tk.GetKind() == Token::amp) {
696 // References to references are forbidden.
697 // FIXME: In future we may want to allow rvalue references (i.e. &&).
698 if (type.IsReferenceType()) {
699 BailOut("type name declared as a reference to a reference", loc,
700 CurToken().GetSpelling().length());
701 return {};
702 }
703 // Get reference type for the base type: e.g. int -> int&.
704 type = type.GetLValueReferenceType();
705 }
706 }
707
708 return type;
709}
710
711// Parse an boolean_literal.
712//
713// boolean_literal:
714// "true"
715// "false"
716//
718 ExpectOneOf(std::vector<Token::Kind>{Token::kw_true, Token::kw_false});
719 uint32_t loc = CurToken().GetLocation();
720 bool literal_value = CurToken().Is(Token::kw_true);
721 m_dil_lexer.Advance();
722 return std::make_unique<BooleanLiteralNode>(loc, literal_value);
723}
724
725void DILParser::BailOut(const std::string &error, uint32_t loc,
726 uint16_t err_len) {
727 if (m_error)
728 // If error is already set, then the parser is in the "bail-out" mode. Don't
729 // do anything and keep the original error.
730 return;
731
732 m_error =
733 llvm::make_error<DILDiagnosticError>(m_input_expr, error, loc, err_len);
734 // Advance the lexer token index to the end of the lexed tokens vector.
735 m_dil_lexer.ResetTokenIdx(m_dil_lexer.NumLexedTokens() - 1);
736}
737
738// Parse a numeric_literal.
739//
740// numeric_literal:
741// ? Token::integer_constant ?
742// ? Token::floating_constant ?
743//
745 ASTNodeUP numeric_constant;
747 numeric_constant = ParseIntegerLiteral();
748 else
749 numeric_constant = ParseFloatingPointLiteral();
750 if (numeric_constant->GetKind() == NodeKind::eErrorNode) {
751 BailOut(llvm::formatv("Failed to parse token as numeric-constant: {0}",
752 CurToken()),
753 CurToken().GetLocation(), CurToken().GetSpelling().length());
754 return numeric_constant;
755 }
756 m_dil_lexer.Advance();
757 return numeric_constant;
758}
759
761 Token token = CurToken();
762 auto spelling = token.GetSpelling();
763 llvm::StringRef spelling_ref = spelling;
764
765 auto radix = llvm::getAutoSenseRadix(spelling_ref);
767 bool is_unsigned = false;
768 if (spelling_ref.consume_back_insensitive("u"))
769 is_unsigned = true;
770 if (spelling_ref.consume_back_insensitive("ll"))
772 else if (spelling_ref.consume_back_insensitive("l"))
774 // Suffix 'u' can be only specified only once, before or after 'l'
775 if (!is_unsigned && spelling_ref.consume_back_insensitive("u"))
776 is_unsigned = true;
777
778 llvm::APInt raw_value;
779 if (!spelling_ref.getAsInteger(radix, raw_value))
780 return std::make_unique<IntegerLiteralNode>(token.GetLocation(), raw_value,
781 radix, is_unsigned, type);
782 return std::make_unique<ErrorNode>();
783}
784
786 Token token = CurToken();
787 auto spelling = token.GetSpelling();
788 llvm::StringRef spelling_ref = spelling;
789
790 llvm::APFloat raw_float(llvm::APFloat::IEEEdouble());
791 if (spelling_ref.consume_back_insensitive("f"))
792 raw_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
793
794 auto StatusOrErr = raw_float.convertFromString(
795 spelling_ref, llvm::APFloat::rmNearestTiesToEven);
796 if (!errorToBool(StatusOrErr.takeError()))
797 return std::make_unique<FloatLiteralNode>(token.GetLocation(), raw_float);
798 return std::make_unique<ErrorNode>();
799}
800
802 if (CurToken().IsNot(kind)) {
803 BailOut(llvm::formatv("expected {0}, got: {1}", kind, CurToken()),
804 CurToken().GetLocation(), CurToken().GetSpelling().length());
805 }
806}
807
808void DILParser::ExpectOneOf(std::vector<Token::Kind> kinds_vec) {
809 if (!CurToken().IsOneOf(kinds_vec)) {
810 BailOut(llvm::formatv("expected any of ({0}), got: {1}",
811 llvm::iterator_range(kinds_vec), CurToken()),
812 CurToken().GetLocation(), CurToken().GetSpelling().length());
813 }
814}
815
816} // 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.
lldb::LanguageType GetLanguage()
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.
Defines a symbol context baton that can be handed other debug core functions.
lldb::ModuleSP module_sp
The Module for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
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:76
void ParseTypeSpecifierSeq(std::string &type_name)
void Expect(Token::Kind kind)
std::optional< CompilerType > ParseTypeId()
void TentativeParsingRollback(uint32_t saved_idx)
Definition DILParser.h:117
ASTNodeUP ParseFloatingPointLiteral()
void ExpectOneOf(std::vector< Token::Kind > kinds_vec)
std::optional< std::string > ParseTypeSpecifier()
std::optional< CompilerType > ParseBuiltinType()
std::shared_ptr< StackFrame > m_ctx_scope
Definition DILParser.h:128
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:137
DILParser(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, llvm::Error &error, lldb::DILMode mode)
static llvm::Expected< ASTNodeUP > Parse(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, lldb::DILMode mode)
std::optional< std::string > ParseTypeName()
llvm::StringRef m_input_expr
Definition DILParser.h:130
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:57
uint32_t GetLocation() const
Definition DILLexer.h:65
Kind GetKind() const
Definition DILLexer.h:53
std::string GetSpelling() const
Definition DILLexer.h:55
@ eNone
Invalid promotion type (results in error).
Definition DILAST.h:60
static CompilerType ResolveTypeByName(const std::string &name, ExecutionContextScope &ctx_scope)
Definition DILParser.cpp:72
llvm::Expected< lldb::TypeSystemSP > GetTypeSystemFromCU(std::shared_ptr< StackFrame > ctx)
Definition DILParser.cpp:63
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:93
BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind)
Translates DIL tokens to BinaryOpKind.
Definition DILAST.cpp:14
lldb::ValueObjectSP LookupGlobalIdentifier(llvm::StringRef name_ref, std::shared_ptr< StackFrame > frame_sp, 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:272
lldb::ValueObjectSP LookupIdentifier(llvm::StringRef name_ref, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic)
Given the name of an identifier (variable name, member name, type name, etc.), find the ValueObject f...
Definition DILEval.cpp:313
LanguageType
Programming language type.
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.