diff options
-rw-r--r-- | gcc/rust/ast/rust-ast.h | 2 | ||||
-rw-r--r-- | gcc/rust/ast/rust-item.h | 2 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 43 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-expr.h | 6 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.h | 2 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 3 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.h | 44 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/torture/byte_str.rs | 4 |
8 files changed, 85 insertions, 21 deletions
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 96b8da4..b0738a2 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -259,10 +259,8 @@ public: { CHAR, STRING, - RAW_STRING, BYTE, BYTE_STRING, - RAW_BYTE_STRING, INT, FLOAT, BOOL diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 7a34144..c7c8b8f 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -32,7 +32,7 @@ class MacroInvocationSemi; // TODO: inline? /*struct AbiName { std::string abi_name; - // Technically is meant to be STRING_LITERAL or RAW_STRING_LITERAL + // Technically is meant to be STRING_LITERAL public: // Returns whether abi name is empty, i.e. doesn't exist. diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index d0c0b74..eb245dc 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -304,8 +304,7 @@ public: } return; - case HIR::Literal::STRING: - case HIR::Literal::BYTE_STRING: { + case HIR::Literal::STRING: { auto base = ctx->get_backend ()->string_constant_expression ( literal_value->as_string ()); translated @@ -313,6 +312,46 @@ public: } return; + case HIR::Literal::BYTE_STRING: { + TyTy::BaseType *tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + expr.get_mappings ().get_hirid (), &tyty)) + { + rust_fatal_error (expr.get_locus (), + "did not resolve type for this array expr"); + return; + } + + // the type here is &[ty; capacity] + rust_assert (tyty->get_kind () == TyTy::TypeKind::REF); + auto ref_tyty = static_cast<TyTy::ReferenceType *> (tyty); + auto base_tyty = ref_tyty->get_base (); + rust_assert (base_tyty->get_kind () == TyTy::TypeKind::ARRAY); + auto array_tyty = static_cast<TyTy::ArrayType *> (base_tyty); + + std::string value_str = expr.get_literal ()->as_string (); + std::vector<Bexpression *> vals; + std::vector<unsigned long> indexes; + for (size_t i = 0; i < value_str.size (); i++) + { + char b = value_str.at (i); + Bexpression *bb + = ctx->get_backend ()->char_constant_expression (b); + vals.push_back (bb); + indexes.push_back (i); + } + + Btype *array_type = TyTyResolveCompile::compile (ctx, array_tyty); + Bexpression *constructed + = ctx->get_backend ()->array_constructor_expression ( + array_type, indexes, vals, expr.get_locus ()); + + translated + = ctx->get_backend ()->address_expression (constructed, + expr.get_locus ()); + } + return; + default: rust_fatal_error (expr.get_locus (), "unknown literal"); return; diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h index ff4c181..1d0b6cc 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.h +++ b/gcc/rust/hir/rust-ast-lower-expr.h @@ -332,18 +332,12 @@ public: case AST::Literal::LitType::STRING: type = HIR::Literal::LitType::STRING; break; - case AST::Literal::LitType::RAW_STRING: - type = HIR::Literal::LitType::RAW_STRING; - break; case AST::Literal::LitType::BYTE: type = HIR::Literal::LitType::BYTE; break; case AST::Literal::LitType::BYTE_STRING: type = HIR::Literal::LitType::BYTE_STRING; break; - case AST::Literal::LitType::RAW_BYTE_STRING: - type = HIR::Literal::LitType::RAW_BYTE_STRING; - break; case AST::Literal::LitType::INT: type = HIR::Literal::LitType::INT; break; diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 8ba6308..5a64662 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -45,10 +45,8 @@ public: { CHAR, STRING, - RAW_STRING, BYTE, BYTE_STRING, - RAW_BYTE_STRING, INT, FLOAT, BOOL diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 8cce933..c2d3720 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -7556,8 +7556,6 @@ Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs) literal_value = t->get_str (); lexer.skip_token (); break; - // case RAW_STRING_LITERAL: - // put here if lexer changes to have these case BYTE_CHAR_LITERAL: type = AST::Literal::BYTE; literal_value = t->get_str (); @@ -7568,7 +7566,6 @@ Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs) literal_value = t->get_str (); lexer.skip_token (); break; - // case RAW_BYTE_STRING_LITERAL: case INT_LITERAL: type = AST::Literal::INT; literal_value = t->get_str (); diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index fe8973a..28b9851 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -609,15 +609,49 @@ 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); + /* This is an arraytype of u8 reference (&[u8;size]). It isn't in + UTF-8, but really just a byte array. Code to construct the array + reference copied from ArrayElemsValues and ArrayType. */ + TyTy::BaseType *u8; + auto ok = context->lookup_builtin ("u8", &u8); rust_assert (ok); + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping capacity_mapping (crate_num, UNKNOWN_NODEID, + mappings->get_next_hir_id ( + crate_num), + UNKNOWN_LOCAL_DEFID); + + /* Capacity is the size of the string (number of chars). + It is a constant, but for fold it to get a Bexpression. */ + std::string capacity_str + = std::to_string (expr.get_literal ()->as_string ().size ()); + HIR::LiteralExpr literal_capacity (capacity_mapping, capacity_str, + HIR::Literal::LitType::INT, + PrimitiveCoreType::CORETYPE_USIZE, + expr.get_locus ()); + + // mark the type for this implicit node + context->insert_type (capacity_mapping, + new TyTy::USizeType ( + capacity_mapping.get_hirid ())); + + Bexpression *capacity + = ConstFold::ConstFoldExpr::fold (&literal_capacity); + + Analysis::NodeMapping array_mapping (crate_num, UNKNOWN_NODEID, + mappings->get_next_hir_id ( + crate_num), + UNKNOWN_LOCAL_DEFID); + + TyTy::ArrayType *array + = new TyTy::ArrayType (array_mapping.get_hirid (), capacity, + TyTy::TyVar (u8->get_ref ())); + context->insert_type (array_mapping, array); + infered = new TyTy::ReferenceType (expr.get_mappings ().get_hirid (), - TyTy::TyVar (base->get_ref ()), false); + TyTy::TyVar (array->get_ref ()), false); } break; diff --git a/gcc/testsuite/rust/compile/torture/byte_str.rs b/gcc/testsuite/rust/compile/torture/byte_str.rs new file mode 100644 index 0000000..28934d2 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/byte_str.rs @@ -0,0 +1,4 @@ +pub fn main() { + let a: &[u8; 4]; + a = b"test"; +} |