A byte literal is an u8 created as a ascii char or hex escape e.g. b'X'. A byte string literal is a string created from ascii or hex chars. bytes are represented as u8 and byte strings as str (with just ascii < 256 chars), but it should really be &'static [u8; n]. --- gcc/rust/backend/rust-compile-expr.h | 9 ++++++++- gcc/rust/parse/rust-parse-impl.h | 8 ++++++++ gcc/rust/rust-backend.h | 3 +++ gcc/rust/rust-gcc.cc | 9 +++++++++ gcc/rust/typecheck/rust-hir-type-check-expr.h | 19 +++++++++++++++++++ .../rust/compile/torture/byte_char_str.rs | 8 ++++++++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/compile/torture/byte_char_str.rs
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index dff4712e18e..fa6a53991ac 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -278,7 +278,14 @@ public: } return; - case HIR::Literal::STRING: { + case HIR::Literal::BYTE: { + char c = literal_value->as_string ().c_str ()[0]; + translated = ctx->get_backend ()->char_constant_expression (c); + } + return; + + case HIR::Literal::STRING: + case HIR::Literal::BYTE_STRING: { auto base = ctx->get_backend ()->string_constant_expression ( literal_value->as_string ()); translated diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index be261715c6c..73600d22d60 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -12545,10 +12545,18 @@ Parser<ManagedTokenSource>::null_denotation (const_TokenPtr tok, return std::unique_ptr<AST::LiteralExpr> ( new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING, tok->get_type_hint (), {}, tok->get_locus ())); + case BYTE_STRING_LITERAL: + return std::unique_ptr<AST::LiteralExpr> ( + new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE_STRING, + tok->get_type_hint (), {}, tok->get_locus ())); case CHAR_LITERAL: return std::unique_ptr<AST::LiteralExpr> ( new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR, tok->get_type_hint (), {}, tok->get_locus ())); + case BYTE_CHAR_LITERAL: + return std::unique_ptr<AST::LiteralExpr> ( + new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE, + tok->get_type_hint (), {}, tok->get_locus ())); case TRUE_LITERAL: return std::unique_ptr<AST::LiteralExpr> ( new AST::LiteralExpr ("true", AST::Literal::BOOL, tok->get_type_hint (), diff --git a/gcc/rust/rust-backend.h b/gcc/rust/rust-backend.h index 35271b60f43..1dd4aba12ca 100644 --- a/gcc/rust/rust-backend.h +++ b/gcc/rust/rust-backend.h @@ -331,6 +331,9 @@ public: // Return an expression for the string value VAL. virtual Bexpression *string_constant_expression (const std::string &val) = 0; + // Get a char literal + virtual Bexpression *char_constant_expression (char c) = 0; + // Get a char literal virtual Bexpression *wchar_constant_expression (wchar_t c) = 0; diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc index 74a8b5221f1..23a91ad9bcb 100644 --- a/gcc/rust/rust-gcc.cc +++ b/gcc/rust/rust-gcc.cc @@ -333,6 +333,8 @@ public: Bexpression *wchar_constant_expression (wchar_t c); + Bexpression *char_constant_expression (char c); + Bexpression *boolean_constant_expression (bool val); Bexpression *real_part_expression (Bexpression *bcomplex, Location); @@ -1557,6 +1559,13 @@ Gcc_backend::wchar_constant_expression (wchar_t c) return this->make_expression (ret); } +Bexpression * +Gcc_backend::char_constant_expression (char c) +{ + tree ret = build_int_cst (this->char_type ()->get_tree (), c); + return this->make_expression (ret); +} + // Make a constant boolean expression. Bexpression * diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index 166535acba0..6e5b2312f50 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -542,6 +542,12 @@ public: } break; + case HIR::Literal::LitType::BYTE: { + auto ok = context->lookup_builtin ("u8", &infered); + rust_assert (ok); + } + break; + case HIR::Literal::LitType::STRING: { TyTy::BaseType *base = nullptr; auto ok = context->lookup_builtin ("str", &base); @@ -553,6 +559,19 @@ public: } break; + case HIR::Literal::LitType::BYTE_STRING: { + /* We just treat this as a string, but it really is an arraytype of + u8. It isn't in UTF-8, but really just a byte array. */ + TyTy::BaseType *base = nullptr; + auto ok = context->lookup_builtin ("str", &base); + rust_assert (ok); + + infered + = new TyTy::ReferenceType (expr.get_mappings ().get_hirid (), + TyTy::TyVar (base->get_ref ()), false); + } + break; + default: gcc_unreachable (); break; diff --git a/gcc/testsuite/rust/compile/torture/byte_char_str.rs b/gcc/testsuite/rust/compile/torture/byte_char_str.rs new file mode 100644 index 00000000000..bc3ec5014e8 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/byte_char_str.rs @@ -0,0 +1,8 @@ +pub fn main () +{ + let _c = 'x'; + let _bc = b'x'; + + let _s = "abc"; + let _bs = b"abc"; +} -- 2.32.0 -- Gcc-rust mailing list Gcc-rust@gcc.gnu.org https://gcc.gnu.org/mailman/listinfo/gcc-rust