-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[LLDB] Add type casting to DIL, part 1 of 3. #165199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cmtice
wants to merge
7
commits into
llvm:main
Choose a base branch
from
cmtice:dil-typecast-outline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44cf7ad
[LLDB] Add type casting to DIL, part 1 of 3.
cmtice 3e53e25
Address reviewer comments:
cmtice 9b3d261
Rmove unnecessary 'bad_type'; add FIXME comment.
cmtice f6c7946
Fix doxygen comments.
cmtice eb27812
Merge remote-tracking branch 'origin/main' into dil-typecast-outline
cmtice 215160a
Update DIL interpreter to return an error message for type casting.
cmtice e682895
Fix clang format issues.
cmtice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,9 @@ | |
|
|
||
| #include "lldb/ValueObject/DILParser.h" | ||
| #include "lldb/Host/common/DiagnosticsRendering.h" | ||
| #include "lldb/Symbol/CompileUnit.h" | ||
| #include "lldb/Target/ExecutionContextScope.h" | ||
| #include "lldb/Target/LanguageRuntime.h" | ||
| #include "lldb/ValueObject/DILAST.h" | ||
| #include "lldb/ValueObject/DILEval.h" | ||
| #include "llvm/ADT/StringRef.h" | ||
|
|
@@ -80,15 +82,63 @@ ASTNodeUP DILParser::Run() { | |
| // Parse an expression. | ||
| // | ||
| // expression: | ||
| // unary_expression | ||
| // cast_expression | ||
| // | ||
| ASTNodeUP DILParser::ParseExpression() { return ParseUnaryExpression(); } | ||
| ASTNodeUP DILParser::ParseExpression() { return ParseCastExpression(); } | ||
|
|
||
| // Parse a cast_expression. | ||
| // | ||
| // cast_expression: | ||
| // unary_expression | ||
| // "(" type_id ")" cast_expression | ||
|
|
||
| ASTNodeUP DILParser::ParseCastExpression() { | ||
| if (!CurToken().Is(Token::l_paren)) | ||
| return ParseUnaryExpression(); | ||
|
|
||
| // This could be a type cast, try parsing the contents as a type declaration. | ||
| Token token = CurToken(); | ||
| uint32_t loc = token.GetLocation(); | ||
|
|
||
| // Enable lexer backtracking, so that we can rollback in case it's not | ||
| // actually a type declaration. | ||
|
|
||
| // Start tentative parsing (save token location/idx, for possible rollback). | ||
| uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx(); | ||
|
|
||
| // Consume the token only after enabling the backtracking. | ||
| m_dil_lexer.Advance(); | ||
|
|
||
| // Try parsing the type declaration. If the returned value is not valid, | ||
| // then we should rollback and try parsing the expression. | ||
| auto type_id = ParseTypeId(); | ||
| if (type_id) { | ||
| // Successfully parsed the type declaration. Commit the backtracked | ||
| // tokens and parse the cast_expression. | ||
|
|
||
| if (!type_id.value().IsValid()) | ||
| return std::make_unique<ErrorNode>(); | ||
|
|
||
| Expect(Token::r_paren); | ||
| m_dil_lexer.Advance(); | ||
| auto rhs = ParseCastExpression(); | ||
|
|
||
| return std::make_unique<CastNode>(loc, type_id.value(), std::move(rhs), | ||
| CastKind::eNone); | ||
| } | ||
|
|
||
| // Failed to parse the contents of the parentheses as a type declaration. | ||
| // Rollback the lexer and try parsing it as unary_expression. | ||
| TentativeParsingRollback(save_token_idx); | ||
|
|
||
| return ParseUnaryExpression(); | ||
| } | ||
|
|
||
| // Parse an unary_expression. | ||
| // | ||
| // unary_expression: | ||
| // postfix_expression | ||
| // unary_operator expression | ||
| // unary_operator cast_expression | ||
| // | ||
| // unary_operator: | ||
| // "&" | ||
|
|
@@ -102,7 +152,7 @@ ASTNodeUP DILParser::ParseUnaryExpression() { | |
| Token token = CurToken(); | ||
| uint32_t loc = token.GetLocation(); | ||
| m_dil_lexer.Advance(); | ||
| auto rhs = ParseExpression(); | ||
| auto rhs = ParseCastExpression(); | ||
| switch (token.GetKind()) { | ||
| case Token::star: | ||
| return std::make_unique<UnaryOpNode>(loc, UnaryOpKind::Deref, | ||
|
|
@@ -282,6 +332,81 @@ std::string DILParser::ParseNestedNameSpecifier() { | |
| } | ||
| } | ||
|
|
||
| // Parse a type_id. | ||
| // | ||
| // type_id: | ||
| // type_specifier_seq [abstract_declarator] | ||
| // | ||
| // type_specifier_seq: | ||
| // type_specifier [type_specifier] | ||
| // | ||
| // type_specifier: | ||
| // ["::"] [nested_name_specifier] type_name // not handled for now! | ||
| // builtin_typename | ||
| // | ||
| std::optional<CompilerType> DILParser::ParseTypeId() { | ||
| CompilerType type; | ||
| // For now only allow builtin types -- will expand add to this later. | ||
| auto maybe_builtin_type = ParseBuiltinType(); | ||
| if (maybe_builtin_type) { | ||
| type = *maybe_builtin_type; | ||
| } else | ||
| return {}; | ||
|
|
||
| // | ||
| // abstract_declarator: | ||
| // ptr_operator [abstract_declarator] | ||
| // | ||
| std::vector<Token> ptr_operators; | ||
| while (CurToken().IsOneOf({Token::star, Token::amp})) { | ||
| Token tok = CurToken(); | ||
| ptr_operators.push_back(std::move(tok)); | ||
| m_dil_lexer.Advance(); | ||
| } | ||
| type = ResolveTypeDeclarators(type, ptr_operators); | ||
|
|
||
| return type; | ||
| } | ||
|
|
||
| // Parse a built-in type | ||
| // | ||
| // builtin_typename: | ||
| // identifer_seq | ||
| // | ||
| // identifier_seq | ||
| // identifer [identifier_seq] | ||
| // | ||
| // A built-in type can be a single identifier or a space-separated | ||
| // list of identifiers (e.g. "short" or "long long"). | ||
| std::optional<CompilerType> DILParser::ParseBuiltinType() { | ||
| std::string type_name = ""; | ||
| uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx(); | ||
| bool first_word = true; | ||
| while (CurToken().GetKind() == Token::identifier) { | ||
| if (CurToken().GetSpelling() == "const" || | ||
| CurToken().GetSpelling() == "volatile") | ||
Michael137 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue; | ||
| if (!first_word) | ||
| type_name.push_back(' '); | ||
| else | ||
| first_word = false; | ||
| type_name.append(CurToken().GetSpelling()); | ||
| m_dil_lexer.Advance(); | ||
| } | ||
|
|
||
| if (type_name.size() > 0) { | ||
| lldb::TargetSP target_sp = m_ctx_scope->CalculateTarget(); | ||
| ConstString const_type_name(type_name.c_str()); | ||
| for (auto type_system_sp : target_sp->GetScratchTypeSystems()) | ||
| if (auto compiler_type = | ||
| type_system_sp->GetBuiltinTypeByName(const_type_name)) | ||
| return compiler_type; | ||
| } | ||
|
|
||
| TentativeParsingRollback(save_token_idx); | ||
| return {}; | ||
| } | ||
|
|
||
| // Parse an id_expression. | ||
| // | ||
| // id_expression: | ||
|
|
@@ -347,6 +472,40 @@ std::string DILParser::ParseUnqualifiedId() { | |
| return identifier; | ||
| } | ||
|
|
||
| CompilerType | ||
| DILParser::ResolveTypeDeclarators(CompilerType type, | ||
| const std::vector<Token> &ptr_operators) { | ||
| // Resolve pointers/references. | ||
| for (Token tk : ptr_operators) { | ||
| uint32_t loc = tk.GetLocation(); | ||
| if (tk.GetKind() == Token::star) { | ||
| // Pointers to reference types are forbidden. | ||
| if (type.IsReferenceType()) { | ||
| BailOut(llvm::formatv("'type name' declared as a pointer to a " | ||
| "reference of type {0}", | ||
| type.TypeDescription()), | ||
| loc, CurToken().GetSpelling().length()); | ||
| return {}; | ||
| } | ||
| // Get pointer type for the base type: e.g. int* -> int**. | ||
| type = type.GetPointerType(); | ||
|
|
||
| } else if (tk.GetKind() == Token::amp) { | ||
| // References to references are forbidden. | ||
| // FIXME: In future we may want to allow rvalue references (i.e. &&). | ||
| if (type.IsReferenceType()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a FIXME for rvalue references (i.e.,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| BailOut("type name declared as a reference to a reference", loc, | ||
| CurToken().GetSpelling().length()); | ||
| return {}; | ||
| } | ||
| // Get reference type for the base type: e.g. int -> int&. | ||
| type = type.GetLValueReferenceType(); | ||
| } | ||
| } | ||
|
|
||
| return type; | ||
| } | ||
|
|
||
| // Parse an boolean_literal. | ||
| // | ||
| // boolean_literal: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.