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