aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-base.cc56
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc10
-rw-r--r--gcc/rust/backend/rust-compile-expr.h4
-rw-r--r--gcc/rust/backend/rust-compile-resolve-path.cc18
-rw-r--r--gcc/rust/backend/rust-compile-stmt.h8
-rw-r--r--gcc/rust/backend/rust-compile-var-decl.h70
6 files changed, 54 insertions, 112 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 3de80d9..8fa3fa1 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -333,39 +333,33 @@ std::vector<Bvariable *>
HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib,
tree fndecl)
{
+ CrateNum crate = ctx->get_mappings ()->get_current_crate ();
+
std::vector<Bvariable *> locals;
for (auto it : rib.get_declarations ())
{
- auto node_id = it.first;
-
- Resolver::Definition d;
- bool ok = ctx->get_resolver ()->lookup_definition (node_id, &d);
- rust_assert (ok);
-
- HIR::Stmt *decl = nullptr;
- if (!ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl))
- {
- // might be an extern block see fix for
- // https://github.com/Rust-GCC/gccrs/issues/976
- continue;
- }
-
- // if its a function we extract this out side of this fn context
- // and it is not a local to this function
- bool is_item = ctx->get_mappings ()->lookup_hir_item (
- decl->get_mappings ().get_crate_num (),
- decl->get_mappings ().get_hirid ())
- != nullptr;
- if (is_item)
- {
- HIR::Item *item = static_cast<HIR::Item *> (decl);
- CompileItem::compile (item, ctx);
- }
-
- Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
+ NodeId node_id = it.first;
+ HirId ref = UNKNOWN_HIRID;
+ if (!ctx->get_mappings ()->lookup_node_to_hir (crate, node_id, &ref))
+ continue;
+
+ // we only care about local patterns
+ HIR::Pattern *pattern
+ = ctx->get_mappings ()->lookup_hir_pattern (crate, ref);
+ if (pattern == nullptr)
+ continue;
+
+ // lookup the type
+ TyTy::BaseType *tyty = nullptr;
+ if (!ctx->get_tyctx ()->lookup_type (ref, &tyty))
+ continue;
+
+ // compile the local
+ tree type = TyTyResolveCompile::compile (ctx, tyty);
+ Bvariable *compiled
+ = CompileVarDecl::compile (fndecl, type, pattern, ctx);
locals.push_back (compiled);
- };
-
+ }
return locals;
}
@@ -482,7 +476,9 @@ HIRCompileBase::compile_function (
compiled_param_type, param_locus);
param_vars.push_back (compiled_param_var);
- ctx->insert_var_decl (referenced_param.get_mappings ().get_hirid (),
+
+ const HIR::Pattern &param_pattern = *referenced_param.get_param_name ();
+ ctx->insert_var_decl (param_pattern.get_pattern_mappings ().get_hirid (),
compiled_param_var);
}
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index b7dad12..b153451 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -1432,16 +1432,6 @@ CompileExpr::visit (HIR::IdentifierExpr &expr)
NodeId ref_node_id = UNKNOWN_NODEID;
if (ctx->get_resolver ()->lookup_resolved_name (ast_node_id, &ref_node_id))
{
- // these ref_node_ids will resolve to a pattern declaration but we are
- // interested in the definition that this refers to get the parent id
- Resolver::Definition def;
- if (!ctx->get_resolver ()->lookup_definition (ref_node_id, &def))
- {
- rust_error_at (expr.get_locus (),
- "unknown reference for resolved name");
- return;
- }
- ref_node_id = def.parent;
is_value = true;
}
else if (!ctx->get_resolver ()->lookup_resolved_type (ast_node_id,
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index 593e1f9..3b729c7 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -512,8 +512,8 @@ public:
void visit (HIR::FieldAccessExpr &expr) override
{
- tree receiver_ref
- = CompileExpr::Compile (expr.get_receiver_expr ().get (), ctx);
+ HIR::Expr *receiver_expr = expr.get_receiver_expr ().get ();
+ tree receiver_ref = CompileExpr::Compile (receiver_expr, ctx);
// resolve the receiver back to ADT type
TyTy::BaseType *receiver = nullptr;
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index 4423912..2cf81e0 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -53,22 +53,12 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
// need to look up the reference for this identifier
NodeId ref_node_id = UNKNOWN_NODEID;
- if (ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
- &ref_node_id))
+ if (!ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
+ &ref_node_id))
{
- Resolver::Definition def;
- if (!ctx->get_resolver ()->lookup_definition (ref_node_id, &def))
- {
- rust_error_at (expr_locus, "unknown reference for resolved name");
- return error_mark_node;
- }
- ref_node_id = def.parent;
- }
+ // this can fail because it might be a Constructor for something
+ // in that case the caller should attempt ResolvePathType::Compile
- // this can fail because it might be a Constructor for something
- // in that case the caller should attempt ResolvePathType::Compile
- if (ref_node_id == UNKNOWN_NODEID)
- {
// it might be an enum data-less enum variant
if (lookup->get_kind () != TyTy::TypeKind::ADT)
return error_mark_node;
diff --git a/gcc/rust/backend/rust-compile-stmt.h b/gcc/rust/backend/rust-compile-stmt.h
index 5f17777..aa17a4a 100644
--- a/gcc/rust/backend/rust-compile-stmt.h
+++ b/gcc/rust/backend/rust-compile-stmt.h
@@ -51,9 +51,11 @@ public:
if (!stmt.has_init_expr ())
return;
+ const HIR::Pattern &stmt_pattern = *stmt.get_pattern ();
+ HirId stmt_id = stmt_pattern.get_pattern_mappings ().get_hirid ();
+
TyTy::BaseType *ty = nullptr;
- if (!ctx->get_tyctx ()->lookup_type (stmt.get_mappings ().get_hirid (),
- &ty))
+ if (!ctx->get_tyctx ()->lookup_type (stmt_id, &ty))
{
// FIXME this should be an assertion instead
rust_fatal_error (stmt.get_locus (),
@@ -62,7 +64,7 @@ public:
}
Bvariable *var = nullptr;
- if (!ctx->lookup_var_decl (stmt.get_mappings ().get_hirid (), &var))
+ if (!ctx->lookup_var_decl (stmt_id, &var))
{
// FIXME this should be an assertion instead and use error mark node
rust_fatal_error (stmt.get_locus (),
diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h
index 2dab39e..e2ee05b 100644
--- a/gcc/rust/backend/rust-compile-var-decl.h
+++ b/gcc/rust/backend/rust-compile-var-decl.h
@@ -25,36 +25,19 @@
namespace Rust {
namespace Compile {
-class CompileVarDecl : public HIRCompileBase,
- public HIR::HIRPatternVisitor,
- public HIR::HIRStmtVisitor
+class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor
{
using HIR::HIRPatternVisitor::visit;
- using HIR::HIRStmtVisitor::visit;
public:
- static ::Bvariable *compile (tree fndecl, HIR::Stmt *stmt, Context *ctx)
+ static ::Bvariable *compile (tree fndecl, tree translated_type,
+ HIR::Pattern *pattern, Context *ctx)
{
- CompileVarDecl compiler (ctx, fndecl);
- stmt->accept_vis (compiler);
- ctx->insert_var_decl (stmt->get_mappings ().get_hirid (),
- compiler.compiled_variable);
+ CompileVarDecl compiler (ctx, fndecl, translated_type);
+ pattern->accept_vis (compiler);
return compiler.compiled_variable;
}
- void visit (HIR::LetStmt &stmt) override
- {
- locus = stmt.get_locus ();
- TyTy::BaseType *resolved_type = nullptr;
- bool ok = ctx->get_tyctx ()->lookup_type (stmt.get_mappings ().get_hirid (),
- &resolved_type);
- rust_assert (ok);
-
- translated_type = TyTyResolveCompile::compile (ctx, resolved_type);
- stmt.get_pattern ()->accept_vis (
- static_cast<HIR::HIRPatternVisitor &> (*this));
- }
-
void visit (HIR::IdentifierPattern &pattern) override
{
if (!pattern.is_mut ())
@@ -63,7 +46,10 @@ public:
compiled_variable
= ctx->get_backend ()->local_variable (fndecl, pattern.get_identifier (),
translated_type, NULL /*decl_var*/,
- locus);
+ pattern.get_locus ());
+
+ HirId stmt_id = pattern.get_pattern_mappings ().get_hirid ();
+ ctx->insert_var_decl (stmt_id, compiled_variable);
}
void visit (HIR::WildcardPattern &pattern) override
@@ -72,7 +58,11 @@ public:
compiled_variable
= ctx->get_backend ()->local_variable (fndecl, "_", translated_type,
- NULL /*decl_var*/, locus);
+ NULL /*decl_var*/,
+ pattern.get_locus ());
+
+ HirId stmt_id = pattern.get_pattern_mappings ().get_hirid ();
+ ctx->insert_var_decl (stmt_id, compiled_variable);
}
// Empty visit for unused Pattern HIR nodes.
@@ -87,41 +77,15 @@ public:
void visit (HIR::TuplePattern &) override {}
void visit (HIR::TupleStructPattern &) override {}
- // Empty visit for unused Stmt HIR nodes.
- void visit (HIR::EnumItemTuple &) override {}
- void visit (HIR::EnumItemStruct &) override {}
- void visit (HIR::EnumItem &item) override {}
- void visit (HIR::TupleStruct &tuple_struct) override {}
- void visit (HIR::EnumItemDiscriminant &) override {}
- void visit (HIR::TypePathSegmentFunction &segment) override {}
- void visit (HIR::TypePath &path) override {}
- void visit (HIR::QualifiedPathInType &path) override {}
- void visit (HIR::Module &module) override {}
- void visit (HIR::ExternCrate &crate) override {}
- void visit (HIR::UseDeclaration &use_decl) override {}
- void visit (HIR::Function &function) override {}
- void visit (HIR::TypeAlias &type_alias) override {}
- void visit (HIR::StructStruct &struct_item) override {}
- void visit (HIR::Enum &enum_item) override {}
- void visit (HIR::Union &union_item) override {}
- void visit (HIR::ConstantItem &const_item) override {}
- void visit (HIR::StaticItem &static_item) override {}
- void visit (HIR::Trait &trait) override {}
- void visit (HIR::ImplBlock &impl) override {}
- void visit (HIR::ExternBlock &block) override {}
- void visit (HIR::EmptyStmt &stmt) override {}
- void visit (HIR::ExprStmtWithoutBlock &stmt) override {}
- void visit (HIR::ExprStmtWithBlock &stmt) override {}
-
private:
- CompileVarDecl (Context *ctx, tree fndecl)
- : HIRCompileBase (ctx), fndecl (fndecl), translated_type (error_mark_node),
+ CompileVarDecl (Context *ctx, tree fndecl, tree translated_type)
+ : HIRCompileBase (ctx), fndecl (fndecl), translated_type (translated_type),
compiled_variable (ctx->get_backend ()->error_variable ())
{}
tree fndecl;
tree translated_type;
- Location locus;
+
Bvariable *compiled_variable;
};