aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-01-19 12:03:01 +0000
committerPhilip Herron <herron.philip@googlemail.com>2021-01-20 09:59:22 +0000
commitf6d33adc6656839aebb4dca02df8efc8be6aedd2 (patch)
tree9d4bc5c4da1a45c82bd2bb9e73d1c000071e9b4d /gcc
parentcb44a8feb815ee31946b33e713c62ac2d333d7be (diff)
downloadgcc-f6d33adc6656839aebb4dca02df8efc8be6aedd2.zip
gcc-f6d33adc6656839aebb4dca02df8efc8be6aedd2.tar.gz
gcc-f6d33adc6656839aebb4dca02df8efc8be6aedd2.tar.bz2
Fix bug using ADT types as return types to functions
When we use anything other than builtin types for returns or parameters the type resolver was missing the NodeId mappings meaning it would alawys fail to resolve them. Then in gimple conversion we need to be able to reference the already created RECORD types instead of always creating new instances.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/backend/rust-compile-context.h80
-rw-r--r--gcc/rust/backend/rust-compile-item.h15
-rw-r--r--gcc/rust/backend/rust-compile-tyty.h48
-rw-r--r--gcc/rust/hir/rust-ast-lower-item.h3
-rw-r--r--gcc/rust/hir/tree/rust-hir-item.h2
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.h10
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-stmt.h10
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-toplevel.h22
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-type.h13
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check.cc2
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check.h2
-rw-r--r--gcc/rust/typecheck/rust-tyctx.cc6
-rw-r--r--gcc/rust/typecheck/rust-tyty-resolver.h2
-rw-r--r--gcc/rust/util/rust-hir-map.cc2
-rw-r--r--gcc/testsuite/rust.test/compilable/struct_base_init_1.rs12
15 files changed, 132 insertions, 97 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index 5288e51..6516c71 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -225,12 +225,43 @@ public:
void visit (TyTy::InferType &type) override { gcc_unreachable (); }
- void visit (TyTy::FnType &type) override { gcc_unreachable (); }
-
void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
+ void visit (TyTy::FnType &type) override
+ {
+ Backend::Btyped_identifier receiver;
+ std::vector<Backend::Btyped_identifier> parameters;
+ std::vector<Backend::Btyped_identifier> results;
+
+ if (!type.get_return_type ()->is_unit ())
+ {
+ auto hir_type = type.get_return_type ();
+ auto ret = TyTyResolveCompile::compile (ctx, hir_type);
+ results.push_back (Backend::Btyped_identifier (
+ "_", ret,
+ ctx->get_mappings ()->lookup_location (hir_type->get_ref ())));
+ }
+
+ for (size_t i = 0; i < type.num_params (); i++)
+ {
+ auto param_tyty = type.param_at (i);
+ auto compiled_param_type
+ = TyTyResolveCompile::compile (ctx, param_tyty->get_base_type ());
+
+ auto compiled_param = Backend::Btyped_identifier (
+ param_tyty->get_identifier (), compiled_param_type,
+ ctx->get_mappings ()->lookup_location (param_tyty->get_ref ()));
+
+ parameters.push_back (compiled_param);
+ }
+
+ translated = ctx->get_backend ()->function_type (
+ receiver, parameters, results, NULL,
+ ctx->get_mappings ()->lookup_location (type.get_ref ()));
+ }
+
void visit (TyTy::UnitType &type) override
{
translated = ctx->get_backend ()->void_type ();
@@ -320,6 +351,51 @@ private:
::Btype *translated;
};
+class TyTyCompileParam : public TyTy::TyVisitor
+{
+public:
+ static ::Bvariable *compile (Context *ctx, Bfunction *fndecl,
+ TyTy::TyBase *ty)
+ {
+ TyTyCompileParam compiler (ctx, fndecl);
+ ty->accept_vis (compiler);
+ rust_assert (compiler.translated != nullptr);
+ return compiler.translated;
+ }
+
+ ~TyTyCompileParam () {}
+
+ void visit (TyTy::UnitType &type) override { gcc_unreachable (); }
+ void visit (TyTy::InferType &type) override { gcc_unreachable (); }
+ void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
+ void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
+ void visit (TyTy::FnType &type) override { gcc_unreachable (); }
+ void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
+ void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
+ void visit (TyTy::IntType &type) override { gcc_unreachable (); }
+ void visit (TyTy::UintType &type) override { gcc_unreachable (); }
+ void visit (TyTy::FloatType &type) override { gcc_unreachable (); }
+ void visit (TyTy::ErrorType &type) override { gcc_unreachable (); }
+
+ void visit (TyTy::ParamType &type) override
+ {
+ auto btype = TyTyResolveCompile::compile (ctx, type.get_base_type ());
+ bool tree_addressable = false;
+ translated = ctx->get_backend ()->parameter_variable (
+ fndecl, type.get_identifier (), btype, tree_addressable,
+ ctx->get_mappings ()->lookup_location (type.get_ref ()));
+ }
+
+private:
+ TyTyCompileParam (Context *ctx, ::Bfunction *fndecl)
+ : ctx (ctx), fndecl (fndecl), translated (nullptr)
+ {}
+
+ Context *ctx;
+ ::Bfunction *fndecl;
+ ::Bvariable *translated;
+};
+
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h
index c8cffc7..82dbc9b 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -127,16 +127,16 @@ public:
return;
}
- TyTy::TyBase *fnType;
+ TyTy::TyBase *fntype;
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
- &fnType))
+ &fntype))
{
rust_fatal_error (function.locus, "failed to lookup function type");
return;
}
// convert to the actual function type
- auto compiled_fn_type = TyTyCompile::compile (ctx->get_backend (), fnType);
+ ::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
unsigned int flags = 0;
bool is_main_fn = function.function_name.compare ("main") == 0;
@@ -159,15 +159,14 @@ public:
ctx->insert_function_decl (function.get_mappings ().get_hirid (), fndecl);
// setup the params
- TyTy::TyBase *tyret = TyTyExtractRetFromFnType::compile (fnType);
+ TyTy::TyBase *tyret = TyTyExtractRetFromFnType::compile (fntype);
std::vector<TyTy::ParamType *> typarams
- = TyTyExtractParamsFromFnType::compile (fnType);
+ = TyTyExtractParamsFromFnType::compile (fntype);
std::vector<Bvariable *> param_vars;
for (auto &it : typarams)
{
- auto compiled_param
- = TyTyCompileParam::compile (ctx->get_backend (), fndecl, it);
+ auto compiled_param = TyTyCompileParam::compile (ctx, fndecl, it);
param_vars.push_back (compiled_param);
ctx->insert_var_decl (it->get_ref (), compiled_param);
@@ -226,7 +225,7 @@ public:
Bvariable *return_address = nullptr;
if (function.has_function_return_type ())
{
- Btype *return_type = TyTyCompile::compile (ctx->get_backend (), tyret);
+ Btype *return_type = TyTyResolveCompile::compile (ctx, tyret);
bool address_is_taken = false;
Bstatement *ret_var_stmt = nullptr;
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h
index cd220e0..3eb2ca5 100644
--- a/gcc/rust/backend/rust-compile-tyty.h
+++ b/gcc/rust/backend/rust-compile-tyty.h
@@ -278,54 +278,6 @@ private:
TyTy::TyBase *translated;
};
-class TyTyCompileParam : public TyTy::TyVisitor
-{
-public:
- static ::Bvariable *compile (::Backend *backend, Bfunction *fndecl,
- TyTy::TyBase *ty)
- {
- TyTyCompileParam compiler (backend, fndecl);
- ty->accept_vis (compiler);
- rust_assert (compiler.translated != nullptr);
- return compiler.translated;
- }
-
- ~TyTyCompileParam () {}
-
- void visit (TyTy::UnitType &type) override { gcc_unreachable (); }
- void visit (TyTy::InferType &type) override { gcc_unreachable (); }
- void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
- void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
- void visit (TyTy::FnType &type) override { gcc_unreachable (); }
- void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
- void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
- void visit (TyTy::IntType &type) override { gcc_unreachable (); }
- void visit (TyTy::UintType &type) override { gcc_unreachable (); }
- void visit (TyTy::FloatType &type) override { gcc_unreachable (); }
- void visit (TyTy::ErrorType &type) override { gcc_unreachable (); }
-
- void visit (TyTy::ParamType &type) override
- {
- auto btype = TyTyCompile::compile (backend, type.get_base_type ());
- bool tree_addressable = false;
- translated = backend->parameter_variable (fndecl, type.get_identifier (),
- btype, tree_addressable,
- mappings->lookup_location (
- type.get_ref ()));
- }
-
-private:
- TyTyCompileParam (::Backend *backend, ::Bfunction *fndecl)
- : backend (backend), translated (nullptr), fndecl (fndecl),
- mappings (Analysis::Mappings::get ())
- {}
-
- ::Backend *backend;
- ::Bvariable *translated;
- ::Bfunction *fndecl;
- Analysis::Mappings *mappings;
-};
-
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h
index 6122335..ec4ef6b 100644
--- a/gcc/rust/hir/rust-ast-lower-item.h
+++ b/gcc/rust/hir/rust-ast-lower-item.h
@@ -274,8 +274,7 @@ public:
for (auto &param : fn->function_params)
{
mappings->insert_hir_param (mapping.get_crate_num (),
- param.get_mappings ()->get_hirid (),
- &param);
+ param.get_mappings ().get_hirid (), &param);
mappings->insert_location (crate_num, mapping.get_hirid (),
param.get_locus ());
}
diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h
index 6a58ca0..00608bd 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -448,7 +448,7 @@ public:
Type *get_type () { return type.get (); }
- Analysis::NodeMapping *get_mappings () { return &mappings; }
+ Analysis::NodeMapping &get_mappings () { return mappings; }
};
// Visibility of item - if the item has it, then it is some form of public
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index fddbdeb..dfa319b 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -41,8 +41,7 @@ public:
resolver.infered
= new TyTy::UnitType (expr->get_mappings ().get_hirid ());
- resolver.context->insert_type (expr->get_mappings ().get_hirid (),
- resolver.infered);
+ resolver.context->insert_type (expr->get_mappings (), resolver.infered);
return resolver.infered;
}
@@ -215,7 +214,12 @@ public:
"assignment infer - reverse lookup failure");
return;
}
- context->insert_type (ref, infered);
+
+ context->insert_type (
+ Analysis::NodeMapping (
+ expr.get_lhs ()->get_mappings ().get_crate_num (), ref_node_id, ref,
+ UNKNOWN_LOCAL_DEFID),
+ infered);
}
}
diff --git a/gcc/rust/typecheck/rust-hir-type-check-stmt.h b/gcc/rust/typecheck/rust-hir-type-check-stmt.h
index bf754db..e60c051 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-stmt.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-stmt.h
@@ -69,26 +69,24 @@ public:
return;
}
- context->insert_type (stmt.get_mappings ().get_hirid (), combined);
+ context->insert_type (stmt.get_mappings (), combined);
}
else
{
// let x:i32;
if (specified_ty != nullptr)
{
- context->insert_type (stmt.get_mappings ().get_hirid (),
- specified_ty);
+ context->insert_type (stmt.get_mappings (), specified_ty);
}
// let x = 123;
else if (init_expr_ty != nullptr)
{
- context->insert_type (stmt.get_mappings ().get_hirid (),
- init_expr_ty);
+ context->insert_type (stmt.get_mappings (), init_expr_ty);
}
// let x;
else
{
- context->insert_type (stmt.get_mappings ().get_hirid (),
+ context->insert_type (stmt.get_mappings (),
new TyTy::InferType (
stmt.get_mappings ().get_hirid ()));
}
diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
index 55c0d38..e2bc3ed 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
@@ -49,8 +49,7 @@ public:
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
std::to_string (idx), field_type);
fields.push_back (ty_field);
- context->insert_type (field.get_mappings ().get_hirid (),
- ty_field->get_field_type ());
+ context->insert_type (field.get_mappings (), ty_field->get_field_type ());
idx++;
return true;
});
@@ -60,7 +59,7 @@ public:
struct_decl.get_identifier (), true,
std::move (fields));
- context->insert_type (struct_decl.get_mappings ().get_hirid (), type);
+ context->insert_type (struct_decl.get_mappings (), type);
}
void visit (HIR::StructStruct &struct_decl)
@@ -73,8 +72,7 @@ public:
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
field.get_field_name (), field_type);
fields.push_back (ty_field);
- context->insert_type (field.get_mappings ().get_hirid (),
- ty_field->get_field_type ());
+ context->insert_type (field.get_mappings (), ty_field->get_field_type ());
return true;
});
@@ -83,7 +81,7 @@ public:
struct_decl.get_identifier (), false,
std::move (fields));
- context->insert_type (struct_decl.get_mappings ().get_hirid (), type);
+ context->insert_type (struct_decl.get_mappings (), type);
}
void visit (HIR::StaticItem &var)
@@ -91,8 +89,7 @@ public:
TyTy::TyBase *type = TypeCheckType::Resolve (var.get_type ());
TyTy::TyBase *expr_type = TypeCheckExpr::Resolve (var.get_expr ());
- context->insert_type (var.get_mappings ().get_hirid (),
- type->combine (expr_type));
+ context->insert_type (var.get_mappings (), type->combine (expr_type));
}
void visit (HIR::ConstantItem &constant)
@@ -100,8 +97,7 @@ public:
TyTy::TyBase *type = TypeCheckType::Resolve (constant.get_type ());
TyTy::TyBase *expr_type = TypeCheckExpr::Resolve (constant.get_expr ());
- context->insert_type (constant.get_mappings ().get_hirid (),
- type->combine (expr_type));
+ context->insert_type (constant.get_mappings (), type->combine (expr_type));
}
void visit (HIR::Function &function)
@@ -123,16 +119,16 @@ public:
// get the name as well required for later on
auto param_type = TypeCheckType::Resolve (param.type.get ());
auto param_tyty
- = new TyTy::ParamType (param.get_mappings ()->get_hirid (),
+ = new TyTy::ParamType (param.get_mappings ().get_hirid (),
param.param_name->as_string (), param_type);
params.push_back (param_tyty);
- context->insert_type (param.get_mappings ()->get_hirid (), param_tyty);
+ context->insert_type (param.get_mappings (), param_tyty);
}
auto fnType = new TyTy::FnType (function.get_mappings ().get_hirid (),
params, ret_type);
- context->insert_type (function.get_mappings ().get_hirid (), fnType);
+ context->insert_type (function.get_mappings (), fnType);
}
private:
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.h b/gcc/rust/typecheck/rust-hir-type-check-type.h
index 77dc9c0..579dfb0 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.h
@@ -70,7 +70,7 @@ public:
type->accept_vis (resolver);
if (resolver.translated != nullptr)
- resolver.context->insert_type (type->get_mappings ().get_hirid (),
+ resolver.context->insert_type (type->get_mappings (),
resolver.translated);
return resolver.translated;
@@ -112,10 +112,6 @@ public:
void visit (HIR::TypePath &path)
{
- // check if this is already defined or not
- if (context->lookup_type (path.get_mappings ().get_hirid (), &translated))
- return;
-
// lookup the Node this resolves to
NodeId ref;
if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (),
@@ -132,11 +128,10 @@ public:
{
// we got an HIR node
if (context->lookup_type (hir_lookup, &translated))
- return;
+ {
+ return;
+ }
}
-
- // this might be a struct type (TyTy::ADT) reference
- // TODO
gcc_unreachable ();
}
diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc
index a33dad1..aa3d505 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check.cc
@@ -116,7 +116,7 @@ TypeCheckStructExpr::visit (HIR::StructExprStructFields &struct_expr)
return false;
}
- context->insert_type (field->get_mappings ().get_hirid (), resolved_field);
+ context->insert_type (field->get_mappings (), resolved_field);
return true;
});
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h
index 3572f22..e0458a7 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -37,7 +37,7 @@ public:
bool lookup_builtin (std::string name, TyTy::TyBase **type);
void insert_builtin (HirId id, NodeId ref, TyTy::TyBase *type);
- void insert_type (HirId id, TyTy::TyBase *type);
+ void insert_type (const Analysis::NodeMapping &mappings, TyTy::TyBase *type);
bool lookup_type (HirId id, TyTy::TyBase **type);
void insert_type_by_node_id (NodeId ref, HirId id);
diff --git a/gcc/rust/typecheck/rust-tyctx.cc b/gcc/rust/typecheck/rust-tyctx.cc
index 15b4c26..65c5563 100644
--- a/gcc/rust/typecheck/rust-tyctx.cc
+++ b/gcc/rust/typecheck/rust-tyctx.cc
@@ -73,9 +73,13 @@ TypeCheckContext::insert_builtin (HirId id, NodeId ref, TyTy::TyBase *type)
}
void
-TypeCheckContext::insert_type (HirId id, TyTy::TyBase *type)
+TypeCheckContext::insert_type (const Analysis::NodeMapping &mappings,
+ TyTy::TyBase *type)
{
rust_assert (type != nullptr);
+ NodeId ref = mappings.get_nodeid ();
+ HirId id = mappings.get_hirid ();
+ node_id_refs[ref] = id;
resolved[id] = type;
}
diff --git a/gcc/rust/typecheck/rust-tyty-resolver.h b/gcc/rust/typecheck/rust-tyty-resolver.h
index eb03ff9..a5906d8 100644
--- a/gcc/rust/typecheck/rust-tyty-resolver.h
+++ b/gcc/rust/typecheck/rust-tyty-resolver.h
@@ -159,7 +159,7 @@ public:
}
// insert the new resolved definition
- context->insert_type (decl->get_mappings ().get_hirid (), resolved_tyty);
+ context->insert_type (decl->get_mappings (), resolved_tyty);
return true;
});
}
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index c6e177b..951bdcc 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -332,7 +332,7 @@ Mappings::insert_hir_param (CrateNum crateNum, HirId id,
rust_assert (lookup_hir_stmt (crateNum, id) == nullptr);
hirParamMappings[crateNum][id] = param;
- nodeIdToHirMappings[crateNum][param->get_mappings ()->get_nodeid ()] = id;
+ nodeIdToHirMappings[crateNum][param->get_mappings ().get_nodeid ()] = id;
}
HIR::FunctionParam *
diff --git a/gcc/testsuite/rust.test/compilable/struct_base_init_1.rs b/gcc/testsuite/rust.test/compilable/struct_base_init_1.rs
new file mode 100644
index 0000000..3c0b24a
--- /dev/null
+++ b/gcc/testsuite/rust.test/compilable/struct_base_init_1.rs
@@ -0,0 +1,12 @@
+struct Foo {
+ a: i32,
+ b: i32,
+}
+
+fn foo() -> Foo {
+ Foo { a: 42, b: 32 }
+}
+
+fn main() {
+ let _f = Foo { a: 10, ..foo() };
+}