aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-04-28 13:01:05 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-16 18:37:16 +0100
commita21a4bc8947b628512db2fa00cdbcea883f31deb (patch)
treefa1bf0b639e8b1aa366f0a60296a2ba06898737b /gcc
parente9ced4a3a581fd06e9bd6c8862a53607b4395f34 (diff)
downloadgcc-a21a4bc8947b628512db2fa00cdbcea883f31deb.zip
gcc-a21a4bc8947b628512db2fa00cdbcea883f31deb.tar.gz
gcc-a21a4bc8947b628512db2fa00cdbcea883f31deb.tar.bz2
gccrs: tokenstream: Add literal int draft conversion
Add a first draft for the literal integer conversion to tokenstream Literal types. gcc/rust/ChangeLog: * ast/rust-ast-tokenstream.cc (dispatch_integer_literals): Add a static function to dispatch depending on the core type. (TokenStream::collect): Add call to dispatch function. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast-tokenstream.cc87
1 files changed, 87 insertions, 0 deletions
diff --git a/gcc/rust/ast/rust-ast-tokenstream.cc b/gcc/rust/ast/rust-ast-tokenstream.cc
index 5442e1c..d77c1cf 100644
--- a/gcc/rust/ast/rust-ast-tokenstream.cc
+++ b/gcc/rust/ast/rust-ast-tokenstream.cc
@@ -41,6 +41,89 @@ pop_group (std::vector<ProcMacro::TokenStream> &streams,
streams.back ().push (tt);
}
+static void
+dispatch_integer_literals (ProcMacro::TokenStream &ts, TokenPtr &token)
+{
+ std::string::size_type sz;
+ auto str = token->as_string ();
+ unsigned long long uvalue;
+ long long svalue;
+ bool suffixed = false;
+
+ switch (token->get_type_hint ())
+ {
+ case CORETYPE_U8:
+ uvalue = std::stoull (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_u8 (uvalue, suffixed)));
+ break;
+ case CORETYPE_U16:
+ uvalue = std::stoull (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_u16 (uvalue, suffixed)));
+ break;
+ case CORETYPE_U32:
+ uvalue = std::stoull (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_u32 (uvalue, suffixed)));
+ break;
+ case CORETYPE_U64:
+ uvalue = std::stoull (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_u32 (uvalue, suffixed)));
+ break;
+ case CORETYPE_I8:
+ svalue = std::stoll (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_i8 (svalue, suffixed)));
+ break;
+ case CORETYPE_I16:
+ svalue = std::stoll (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_i16 (svalue, suffixed)));
+ break;
+ case CORETYPE_I32:
+ svalue = std::stoll (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_i32 (svalue, suffixed)));
+ break;
+ case CORETYPE_I64:
+ svalue = std::stoll (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_i32 (svalue, suffixed)));
+ break;
+ case CORETYPE_INT:
+ svalue = std::stoll (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_isize (svalue, suffixed)));
+ break;
+ case CORETYPE_UINT:
+ uvalue = std::stoull (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_usize (uvalue, suffixed)));
+ break;
+ case CORETYPE_UNKNOWN:
+ svalue = std::stoll (str, &sz);
+ suffixed = sz == str.length ();
+ ts.push (ProcMacro::TokenTree::make_tokentree (
+ ProcMacro::Literal::make_i32 (svalue, false)));
+ break;
+ default:
+ gcc_unreachable ();
+ break;
+ }
+}
+
ProcMacro::TokenStream
TokenStream::collect () const
{
@@ -51,6 +134,10 @@ TokenStream::collect () const
switch (token->get_id ())
{
// Literals
+ case INT_LITERAL:
+ dispatch_integer_literals (trees.back (), token);
+ break;
+ // FIXME: Why does BYTE_CHAR_LITERAL is not handled by rustc ?
case CHAR_LITERAL: // TODO: UTF-8 handling
trees.back ().push (ProcMacro::TokenTree::make_tokentree (
ProcMacro::Literal::make_char (token->as_string ()[0])));