Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,25 @@ HIRCompileBase::compile_constant_expr (
expr_locus);
}

tree
HIRCompileBase::query_compile_const_expr (Context *ctx, TyTy::BaseType *expr_ty,
HIR::Expr &const_value_expr)
{
HIRCompileBase c (ctx);

ctx->push_const_context ();

HirId expr_id = const_value_expr.get_mappings ().get_hirid ();
location_t locus = const_value_expr.get_locus ();
tree capacity_expr = HIRCompileBase::compile_constant_expr (
ctx, expr_id, expr_ty, expr_ty, Resolver::CanonicalPath::create_empty (),
const_value_expr, locus, locus);

ctx->pop_const_context ();

return fold_expr (capacity_expr);
}

tree
HIRCompileBase::indirect_expression (tree expr, location_t locus)
{
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/backend/rust-compile-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class HIRCompileBase
const Resolver::CanonicalPath &canonical_path, HIR::Expr &const_value_expr,
location_t locus, location_t expr_locus);

static tree query_compile_const_expr (Context *ctx, TyTy::BaseType *expr_ty,
HIR::Expr &const_value_expr);

protected:
HIRCompileBase (Context *ctx) : ctx (ctx) {}

Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/backend/rust-compile-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
namespace Rust {
namespace Compile {

Context *
Context::get ()
{
static Context *instance;
if (instance == nullptr)
instance = new Context ();

return instance;
}

Context::Context ()
: tyctx (Resolver::TypeCheckContext::get ()),
mappings (Analysis::Mappings::get ()), mangler (Mangler ())
Expand Down
4 changes: 3 additions & 1 deletion gcc/rust/backend/rust-compile-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct CustomDeriveInfo
class Context
{
public:
Context ();
static Context *get ();

void setup_builtins ();

Expand Down Expand Up @@ -390,6 +390,8 @@ class Context
}

private:
Context ();

Resolver::TypeCheckContext *tyctx;
Analysis::Mappings &mappings;
Mangler mangler;
Expand Down
24 changes: 8 additions & 16 deletions gcc/rust/backend/rust-compile-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ TyTyResolveCompile::visit (const TyTy::InferType &type)

if (orig == lookup)
{
TyTy::BaseType *def = nullptr;
if (type.default_type (&def))
{
translated = TyTyResolveCompile::compile (ctx, def);
return;
}

translated = error_mark_node;
return;
}
Expand Down Expand Up @@ -463,22 +470,7 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
{
tree element_type
= TyTyResolveCompile::compile (ctx, type.get_element_type ());

ctx->push_const_context ();

HIR::Expr &hir_capacity_expr = type.get_capacity_expr ();
TyTy::BaseType *capacity_expr_ty = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
hir_capacity_expr.get_mappings ().get_hirid (), &capacity_expr_ty);
rust_assert (ok);
tree capacity_expr = HIRCompileBase::compile_constant_expr (
ctx, hir_capacity_expr.get_mappings ().get_hirid (), capacity_expr_ty,
capacity_expr_ty, Resolver::CanonicalPath::create_empty (),
hir_capacity_expr, type.get_locus (), hir_capacity_expr.get_locus ());

ctx->pop_const_context ();

tree folded_capacity_expr = fold_expr (capacity_expr);
tree folded_capacity_expr = type.get_capacity ();

// build_index_type takes the maximum index, which is one less than
// the length.
Expand Down
10 changes: 5 additions & 5 deletions gcc/rust/rust-session-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ Session::compile_crate (const char *filename)
Resolver2_0::ImmutableNameResolutionContext::init (name_resolution_ctx);

// type resolve
Compile::Context *ctx = Compile::Context::get ();
Resolver::TypeResolution::Resolve (hir);

Resolver::TypeCheckContext::get ()->get_variance_analysis_ctx ().solve ();
Expand Down Expand Up @@ -728,16 +729,15 @@ Session::compile_crate (const char *filename)
return;

// do compile to gcc generic
Compile::Context ctx;
Compile::CompileCrate::Compile (hir, &ctx);
Compile::CompileCrate::Compile (hir, ctx);

// we can't do static analysis if there are errors to worry about
if (!saw_errors ())
{
// lints
Analysis::ScanDeadcode::Scan (hir);
Analysis::UnusedVariables::Lint (ctx);
Analysis::ReadonlyCheck::Lint (ctx);
Analysis::UnusedVariables::Lint (*ctx);
Analysis::ReadonlyCheck::Lint (*ctx);

// metadata
bool specified_emit_metadata
Expand All @@ -758,7 +758,7 @@ Session::compile_crate (const char *filename)
}

// pass to GCC middle-end
ctx.write_to_backend ();
ctx->write_to_backend ();
}

void
Expand Down
7 changes: 5 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-hir-type-check-base.h"
#include "rust-compile-base.h"
#include "rust-hir-type-check-expr.h"
#include "rust-hir-type-check-type.h"
#include "rust-hir-trait-resolve.h"
Expand Down Expand Up @@ -287,9 +288,11 @@ TypeCheckBase::resolve_literal (const Analysis::NodeMapping &expr_mappings,
crate_num),
UNKNOWN_LOCAL_DEFID);

auto ctx = Compile::Context::get ();
tree capacity = Compile::HIRCompileBase::query_compile_const_expr (
ctx, expected_ty, *literal_capacity);
TyTy::ArrayType *array
= new TyTy::ArrayType (array_mapping.get_hirid (), locus,
*literal_capacity,
= new TyTy::ArrayType (array_mapping.get_hirid (), locus, capacity,
TyTy::TyVar (u8->get_ref ()));
context->insert_type (array_mapping, array);

Expand Down
28 changes: 19 additions & 9 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "rust-hir-type-check-item.h"
#include "rust-type-util.h"
#include "rust-immutable-name-resolution-context.h"
#include "rust-compile-base.h"

// for flag_name_resolution_2_0
#include "options.h"
Expand Down Expand Up @@ -1031,6 +1032,7 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr)

HIR::Expr *capacity_expr = nullptr;
TyTy::BaseType *element_type = nullptr;
TyTy::BaseType *capacity_type = nullptr;
switch (elements.get_array_expr_type ())
{
case HIR::ArrayElems::ArrayExprType::COPIED:
Expand All @@ -1039,7 +1041,7 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr)
= static_cast<HIR::ArrayElemsCopied &> (elements);
element_type = TypeCheckExpr::Resolve (elems.get_elem_to_copy ());

auto capacity_type
auto capacity_expr_ty
= TypeCheckExpr::Resolve (elems.get_num_copies_expr ());

TyTy::BaseType *expected_ty = nullptr;
Expand All @@ -1048,13 +1050,14 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr)
context->insert_type (elems.get_num_copies_expr ().get_mappings (),
expected_ty);

unify_site (expr.get_mappings ().get_hirid (),
TyTy::TyWithLocation (expected_ty),
TyTy::TyWithLocation (
capacity_type, elems.get_num_copies_expr ().get_locus ()),
expr.get_locus ());
unify_site (
expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (expected_ty),
TyTy::TyWithLocation (capacity_expr_ty,
elems.get_num_copies_expr ().get_locus ()),
expr.get_locus ());

capacity_expr = &elems.get_num_copies_expr ();
capacity_type = expected_ty;
}
break;

Expand Down Expand Up @@ -1096,13 +1099,20 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr)
bool ok = context->lookup_builtin ("usize", &expected_ty);
rust_assert (ok);
context->insert_type (mapping, expected_ty);
capacity_type = expected_ty;
}
break;
}

infered = new TyTy::ArrayType (expr.get_mappings ().get_hirid (),
expr.get_locus (), *capacity_expr,
TyTy::TyVar (element_type->get_ref ()));
rust_assert (capacity_expr);
rust_assert (capacity_type);
auto ctx = Compile::Context::get ();
tree capacity
= Compile::HIRCompileBase::query_compile_const_expr (ctx, capacity_type,
*capacity_expr);
infered
= new TyTy::ArrayType (expr.get_mappings ().get_hirid (), expr.get_locus (),
capacity, TyTy::TyVar (element_type->get_ref ()));
}

// empty struct
Expand Down
12 changes: 9 additions & 3 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "rust-substitution-mapper.h"
#include "rust-type-util.h"
#include "rust-system.h"
#include "rust-compile-base.h"

namespace Rust {
namespace Resolver {
Expand Down Expand Up @@ -710,9 +711,14 @@ TypeCheckType::visit (HIR::ArrayType &type)
type.get_size_expr ().get_locus ());

TyTy::BaseType *base = TypeCheckType::Resolve (type.get_element_type ());
translated = new TyTy::ArrayType (type.get_mappings ().get_hirid (),
type.get_locus (), type.get_size_expr (),
TyTy::TyVar (base->get_ref ()));

auto ctx = Compile::Context::get ();
tree capacity
= Compile::HIRCompileBase::query_compile_const_expr (ctx, capacity_type,
type.get_size_expr ());
translated
= new TyTy::ArrayType (type.get_mappings ().get_hirid (), type.get_locus (),
capacity, TyTy::TyVar (base->get_ref ()));
}

void
Expand Down
17 changes: 14 additions & 3 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
#include "rust-tyty-cmp.h"
#include "rust-type-util.h"
#include "rust-hir-type-bounds.h"
#include "print-tree.h"

#include "options.h"
#include "rust-system.h"
#include "tree.h"

namespace Rust {
namespace TyTy {
Expand Down Expand Up @@ -574,7 +576,7 @@ BaseType::monomorphized_clone () const
{
TyVar elm = arr->get_var_element_type ().monomorphized_clone ();
return new ArrayType (arr->get_ref (), arr->get_ty_ref (), ident.locus,
arr->get_capacity_expr (), elm,
arr->get_capacity (), elm,
arr->get_combined_refs ());
}
else if (auto slice = x->try_as<const SliceType> ())
Expand Down Expand Up @@ -2486,7 +2488,16 @@ ArrayType::accept_vis (TyConstVisitor &vis) const
std::string
ArrayType::as_string () const
{
return "[" + get_element_type ()->as_string () + ":" + "CAPACITY" + "]";
std::string capacity_str = "<error>";
if (!error_operand_p (capacity))
{
unsigned HOST_WIDE_INT length = wi::to_wide (capacity).to_uhwi ();

char buf[64];
snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, length);
capacity_str = std::string (buf);
}
return "[" + get_element_type ()->as_string () + "; " + capacity_str + "]";
}

bool
Expand Down Expand Up @@ -2525,7 +2536,7 @@ ArrayType::get_var_element_type () const
BaseType *
ArrayType::clone () const
{
return new ArrayType (get_ref (), get_ty_ref (), ident.locus, capacity_expr,
return new ArrayType (get_ref (), get_ty_ref (), ident.locus, capacity,
element_type, get_combined_refs ());
}

Expand Down
18 changes: 8 additions & 10 deletions gcc/rust/typecheck/rust-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "rust-tyty-region.h"
#include "rust-system.h"
#include "rust-hir.h"
#include "tree.h"

namespace Rust {

Expand Down Expand Up @@ -1156,19 +1157,18 @@ class ArrayType : public BaseType
public:
static constexpr auto KIND = TypeKind::ARRAY;

ArrayType (HirId ref, location_t locus, HIR::Expr &capacity_expr, TyVar base,
ArrayType (HirId ref, location_t locus, tree capacity, TyVar base,
std::set<HirId> refs = std::set<HirId> ())
: BaseType (ref, ref, TypeKind::ARRAY,
{Resolver::CanonicalPath::create_empty (), locus}, refs),
element_type (base), capacity_expr (capacity_expr)
element_type (base), capacity (capacity)
{}

ArrayType (HirId ref, HirId ty_ref, location_t locus,
HIR::Expr &capacity_expr, TyVar base,
std::set<HirId> refs = std::set<HirId> ())
ArrayType (HirId ref, HirId ty_ref, location_t locus, tree capacity,
TyVar base, std::set<HirId> refs = std::set<HirId> ())
: BaseType (ref, ty_ref, TypeKind::ARRAY,
{Resolver::CanonicalPath::create_empty (), locus}, refs),
element_type (base), capacity_expr (capacity_expr)
element_type (base), capacity (capacity)
{}

void accept_vis (TyVisitor &vis) override;
Expand All @@ -1187,15 +1187,13 @@ class ArrayType : public BaseType

BaseType *clone () const final override;

HIR::Expr &get_capacity_expr () const { return capacity_expr; }
tree get_capacity () const { return capacity; }

ArrayType *handle_substitions (SubstitutionArgumentMappings &mappings);

private:
TyVar element_type;
// FIXME: I dont think this should be in tyty - tyty should already be const
// evaluated
HIR::Expr &capacity_expr;
tree capacity;
};

class SliceType : public BaseType
Expand Down
Loading
Loading