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