gccrs: converter: Move literal conversion out

The literal conversion code could be used for the literal_from_string
callback, this means we should move it out of the function in it's own
function. This involves a new switch, which is quite sad but we're not
yet at a performance profiling phase, there may be lower hanging fruits.

gcc/rust/ChangeLog:

	* util/rust-token-converter.cc (handle_suffix): Rework function
	to make it work with the new literal conversion function.
	(convert_literal): Add a new function to convert to a proc macro
	literal from a literal tokenptr. The function will abort if the
	pointer does not point to a literal.
	(convert): Add call to convert literal for every literal case.
	* util/rust-token-converter.h (convert_literal): Add public
	prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
This commit is contained in:
Pierre-Emmanuel Patry 2023-07-27 15:57:07 +02:00 committed by Arthur Cohen
parent 6d1adb5c39
commit ff773b9030
2 changed files with 36 additions and 27 deletions

View File

@ -63,16 +63,41 @@ convert (ProcMacro::Span span)
return span.start;
}
static void
handle_suffix (ProcMacro::TokenStream &ts, const const_TokenPtr &token,
ProcMacro::LitKind kind)
static ProcMacro::Literal
handle_suffix (const const_TokenPtr &token, ProcMacro::LitKind kind)
{
auto str = token->as_string ();
auto lookup = suffixes.lookup (token->get_type_hint ());
auto suffix = suffixes.is_iter_ok (lookup) ? lookup->second : "";
ts.push (ProcMacro::TokenTree::make_tokentree (
ProcMacro::Literal::make_literal (kind, convert (token->get_locus ()), str,
suffix)));
return ProcMacro::Literal::make_literal (kind, convert (token->get_locus ()),
str, suffix);
}
ProcMacro::Literal
convert_literal (const_TokenPtr lit)
{
auto loc = convert (lit->get_locus ());
switch (lit->get_id ())
{
case FLOAT_LITERAL:
return handle_suffix (lit, ProcMacro::LitKind::make_float ());
case INT_LITERAL:
return handle_suffix (lit, ProcMacro::LitKind::make_integer ());
case CHAR_LITERAL:
return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_char (),
loc, lit->as_string ());
case STRING_LITERAL:
return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_str (),
loc, lit->as_string ());
case BYTE_CHAR_LITERAL:
return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_byte (),
loc, lit->as_string ());
case BYTE_STRING_LITERAL:
return ProcMacro::Literal::make_literal (
ProcMacro::LitKind::make_byte_str (), loc, lit->as_string ());
default:
rust_unreachable ();
}
}
ProcMacro::TokenStream
@ -87,32 +112,13 @@ convert (const std::vector<const_TokenPtr> &tokens)
{
// Literals
case FLOAT_LITERAL:
handle_suffix (trees.back (), token,
ProcMacro::LitKind::make_float ());
break;
case INT_LITERAL:
handle_suffix (trees.back (), token,
ProcMacro::LitKind::make_integer ());
break;
case CHAR_LITERAL:
trees.back ().push (ProcMacro::TokenTree::make_tokentree (
ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_char (),
loc, token->as_string ())));
break;
case STRING_LITERAL:
trees.back ().push (ProcMacro::TokenTree::make_tokentree (
ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_str (),
loc, token->as_string ())));
break;
case BYTE_CHAR_LITERAL:
trees.back ().push (ProcMacro::TokenTree::make_tokentree (
ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_byte (),
loc, token->as_string ())));
break;
case BYTE_STRING_LITERAL:
trees.back ().push (ProcMacro::TokenTree::make_tokentree (
ProcMacro::Literal::make_literal (
ProcMacro::LitKind::make_byte_str (), loc, token->as_string ())));
trees.back ().push (
ProcMacro::TokenTree::make_tokentree (convert_literal (token)));
break;
// Ident
case IDENTIFIER:

View File

@ -29,6 +29,9 @@ convert (const std::vector<const_TokenPtr> &tokens);
std::vector<const_TokenPtr>
convert (const ProcMacro::TokenStream &ts);
ProcMacro::Literal
convert_literal (const_TokenPtr lit);
} // namespace Rust
#endif /* ! RUST_TOKEN_CONVERTER_H */