From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id DEC6F3857408 for ; Sun, 25 Jul 2021 12:57:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DEC6F3857408 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x02.wildebeest.org [172.31.17.132]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 36DF5300047B; Sun, 25 Jul 2021 14:57:09 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 23DFD2E80C66; Sun, 25 Jul 2021 14:57:09 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Cc: Mark Wielaard Subject: [PATCH] Support byte and byte string literals Date: Sun, 25 Jul 2021 14:57:05 +0200 Message-Id: <20210725125705.62587-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Jul 2021 12:57:14 -0000 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::null_denotation (const_TokenPtr tok, return std::unique_ptr ( new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING, tok->get_type_hint (), {}, tok->get_locus ())); + case BYTE_STRING_LITERAL: + return std::unique_ptr ( + new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE_STRING, + tok->get_type_hint (), {}, tok->get_locus ())); case CHAR_LITERAL: return std::unique_ptr ( new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR, tok->get_type_hint (), {}, tok->get_locus ())); + case BYTE_CHAR_LITERAL: + return std::unique_ptr ( + new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE, + tok->get_type_hint (), {}, tok->get_locus ())); case TRUE_LITERAL: return std::unique_ptr ( 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