public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Remove hack to handle forward declared items
@ 2022-06-08 12:01 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:01 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:c8a0c6eb4b9ea3779f01418cfc97a6761ca4a957
commit c8a0c6eb4b9ea3779f01418cfc97a6761ca4a957
Author: Philip Herron <philip.herron@embecosm.com>
Date: Mon Jan 24 18:38:58 2022 +0000
Remove hack to handle forward declared items
We used to use a compile_fns flag as a method to handle the case of code
such as:
```rust
fn foo() {
bar()
}
fn bar() { }
```
The compile_fns flag when set to false would allow us to do a toplevel
scan to compile the initial fndecl prototype of the functions as a method
of handling the case of the call to bar() within the body of foo.
The backend is setup now that we can 'query_compile' by compiling the
item as required with a cache if we have already done so.
Diff:
---
gcc/rust/backend/rust-compile-expr.cc | 135 ++++++++++++++++++++++++--
gcc/rust/backend/rust-compile-expr.h | 105 +-------------------
gcc/rust/backend/rust-compile-extern.h | 11 +--
gcc/rust/backend/rust-compile-implitem.h | 14 +--
gcc/rust/backend/rust-compile-item.h | 30 +++---
gcc/rust/backend/rust-compile-resolve-path.cc | 17 ++--
gcc/rust/backend/rust-compile.cc | 9 +-
7 files changed, 163 insertions(+), 158 deletions(-)
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 427cd322fbc..1048027dc9f 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -720,10 +720,9 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
if (resolved_item != nullptr)
{
if (!fntype->has_subsititions_defined ())
- return CompileInherentImplItem::Compile (resolved_item, ctx, true);
+ return CompileInherentImplItem::Compile (resolved_item, ctx);
- return CompileInherentImplItem::Compile (resolved_item, ctx, true,
- fntype);
+ return CompileInherentImplItem::Compile (resolved_item, ctx, fntype);
}
// it might be resolved to a trait item
@@ -778,9 +777,9 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
HIR::ImplItem *impl_item = candidate.item.impl.impl_item;
if (!fntype->has_subsititions_defined ())
- return CompileInherentImplItem::Compile (impl_item, ctx, true);
+ return CompileInherentImplItem::Compile (impl_item, ctx);
- return CompileInherentImplItem::Compile (impl_item, ctx, true, fntype);
+ return CompileInherentImplItem::Compile (impl_item, ctx, fntype);
}
}
@@ -1226,11 +1225,11 @@ HIRCompileBase::resolve_deref_adjustment (Resolver::Adjustment &adjustment,
tree fn_address = error_mark_node;
if (!lookup->has_subsititions_defined ())
- fn_address = CompileInherentImplItem::Compile (resolved_item, ctx, true,
- nullptr, true, locus);
+ fn_address = CompileInherentImplItem::Compile (resolved_item, ctx, nullptr,
+ true, locus);
else
- fn_address = CompileInherentImplItem::Compile (resolved_item, ctx, true,
- lookup, true, locus);
+ fn_address = CompileInherentImplItem::Compile (resolved_item, ctx, lookup,
+ true, locus);
// does it need a reference to call
tree adjusted_argument = expression;
@@ -1255,5 +1254,123 @@ HIRCompileBase::resolve_deref_adjustment (Resolver::Adjustment &adjustment,
locus);
}
+void
+CompileExpr::visit (HIR::IdentifierExpr &expr)
+{
+ NodeId ast_node_id = expr.get_mappings ().get_nodeid ();
+
+ bool is_value = false;
+ 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,
+ &ref_node_id))
+ {
+ rust_error_at (expr.get_locus (),
+ "Failed to lookup type reference for node: %s",
+ expr.as_string ().c_str ());
+ return;
+ }
+
+ if (ref_node_id == UNKNOWN_NODEID)
+ {
+ rust_fatal_error (expr.get_locus (), "unresolved IdentifierExpr: %s",
+ expr.as_string ().c_str ());
+ return;
+ }
+
+ // node back to HIR
+ HirId ref;
+ if (!ctx->get_mappings ()->lookup_node_to_hir (
+ expr.get_mappings ().get_crate_num (), ref_node_id, &ref))
+ {
+ rust_error_at (expr.get_locus (), "reverse lookup failure");
+ return;
+ }
+
+ TyTy::BaseType *lookup = nullptr;
+ if (!ctx->get_tyctx ()->lookup_type (ref, &lookup))
+ {
+ rust_fatal_error (expr.get_locus (),
+ "failed to find type relevant to this context: %s",
+ expr.get_mappings ().as_string ().c_str ());
+ return;
+ }
+
+ bool is_type_ref = !is_value;
+ if (is_type_ref)
+ {
+ // this might be a case for
+ //
+ // struct S;
+ //
+ // fn main() {
+ // let s = S;
+ // }
+
+ if (lookup->is_unit ())
+ {
+ translated = ctx->get_backend ()->unit_expression ();
+ return;
+ }
+
+ // rust actually treats like this an fn call or structs with fields but
+ // unit structs are just the struct name lets catch it with an is-unit
+ // check
+ gcc_unreachable ();
+ }
+
+ tree fn = NULL_TREE;
+ Bvariable *var = nullptr;
+ if (ctx->lookup_const_decl (ref, &translated))
+ {
+ return;
+ }
+ else if (ctx->lookup_function_decl (ref, &fn))
+ {
+ translated
+ = ctx->get_backend ()->function_code_expression (fn, expr.get_locus ());
+ }
+ else if (ctx->lookup_var_decl (ref, &var))
+ {
+ translated = ctx->get_backend ()->var_expression (var, expr.get_locus ());
+ }
+ else if (ctx->lookup_pattern_binding (ref, &translated))
+ {
+ return;
+ }
+ else
+ {
+ // lets try and query compile it to an item/impl item
+ HIR::Item *resolved_item = ctx->get_mappings ()->lookup_hir_item (
+ expr.get_mappings ().get_crate_num (), ref);
+ bool is_hir_item = resolved_item != nullptr;
+ if (!is_hir_item)
+ {
+ translated = error_mark_node;
+ return;
+ }
+
+ if (!lookup->has_subsititions_defined ())
+ translated = CompileItem::compile (resolved_item, ctx, nullptr, true,
+ expr.get_locus ());
+ else
+ translated = CompileItem::compile (resolved_item, ctx, lookup, true,
+ expr.get_locus ());
+ }
+}
+
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index 5464c88845f..4cc4dfc1197 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -127,110 +127,7 @@ public:
void visit (HIR::MethodCallExpr &expr) override;
- void visit (HIR::IdentifierExpr &expr) override
- {
- NodeId ast_node_id = expr.get_mappings ().get_nodeid ();
-
- bool is_value = false;
- 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,
- &ref_node_id))
- {
- rust_error_at (expr.get_locus (),
- "Failed to lookup type reference for node: %s",
- expr.as_string ().c_str ());
- return;
- }
-
- if (ref_node_id == UNKNOWN_NODEID)
- {
- rust_fatal_error (expr.get_locus (), "unresolved IdentifierExpr: %s",
- expr.as_string ().c_str ());
- return;
- }
-
- // node back to HIR
- HirId ref;
- if (!ctx->get_mappings ()->lookup_node_to_hir (
- expr.get_mappings ().get_crate_num (), ref_node_id, &ref))
- {
- rust_error_at (expr.get_locus (), "reverse lookup failure");
- return;
- }
-
- TyTy::BaseType *lookup = nullptr;
- if (!ctx->get_tyctx ()->lookup_type (ref, &lookup))
- {
- rust_fatal_error (expr.get_locus (),
- "failed to find type relevant to this context: %s",
- expr.get_mappings ().as_string ().c_str ());
- return;
- }
-
- bool is_type_ref = !is_value;
- if (is_type_ref)
- {
- // this might be a case for
- //
- // struct S;
- //
- // fn main() {
- // let s = S;
- // }
-
- if (lookup->is_unit ())
- {
- translated = ctx->get_backend ()->unit_expression ();
- return;
- }
-
- // rust actually treats like this an fn call or structs with fields but
- // unit structs are just the struct name lets catch it with an is-unit
- // check
- gcc_unreachable ();
- }
-
- tree fn = NULL_TREE;
- Bvariable *var = nullptr;
- if (ctx->lookup_const_decl (ref, &translated))
- {
- return;
- }
- else if (ctx->lookup_function_decl (ref, &fn))
- {
- translated
- = ctx->get_backend ()->function_code_expression (fn,
- expr.get_locus ());
- }
- else if (ctx->lookup_var_decl (ref, &var))
- {
- translated
- = ctx->get_backend ()->var_expression (var, expr.get_locus ());
- }
- else if (ctx->lookup_pattern_binding (ref, &translated))
- {
- return;
- }
- else
- {
- rust_fatal_error (expr.get_locus (),
- "failed to lookup compiled reference");
- }
- }
+ void visit (HIR::IdentifierExpr &expr) override;
void visit (HIR::LiteralExpr &expr) override
{
diff --git a/gcc/rust/backend/rust-compile-extern.h b/gcc/rust/backend/rust-compile-extern.h
index 100b1f54de9..507865a85bd 100644
--- a/gcc/rust/backend/rust-compile-extern.h
+++ b/gcc/rust/backend/rust-compile-extern.h
@@ -37,10 +37,9 @@ class CompileExternItem : public HIRCompileBase
public:
static void compile (HIR::ExternalItem *item, Context *ctx,
- bool compile_fns = true,
TyTy::BaseType *concrete = nullptr)
{
- CompileExternItem compiler (ctx, compile_fns, concrete);
+ CompileExternItem compiler (ctx, concrete);
item->accept_vis (compiler);
}
@@ -70,9 +69,6 @@ public:
void visit (HIR::ExternalFunctionItem &function) override
{
- if (!compile_fns)
- return;
-
TyTy::BaseType *fntype_tyty;
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
&fntype_tyty))
@@ -146,11 +142,10 @@ public:
}
private:
- CompileExternItem (Context *ctx, bool compile_fns, TyTy::BaseType *concrete)
- : HIRCompileBase (ctx), compile_fns (compile_fns), concrete (concrete)
+ CompileExternItem (Context *ctx, TyTy::BaseType *concrete)
+ : HIRCompileBase (ctx), concrete (concrete)
{}
- bool compile_fns;
TyTy::BaseType *concrete;
};
diff --git a/gcc/rust/backend/rust-compile-implitem.h b/gcc/rust/backend/rust-compile-implitem.h
index fd92b0fe929..d646c8391f6 100644
--- a/gcc/rust/backend/rust-compile-implitem.h
+++ b/gcc/rust/backend/rust-compile-implitem.h
@@ -34,12 +34,12 @@ class CompileInherentImplItem : public HIRCompileBase
using Rust::Compile::HIRCompileBase::visit;
public:
- static tree Compile (HIR::ImplItem *item, Context *ctx, bool compile_fns,
+ static tree Compile (HIR::ImplItem *item, Context *ctx,
TyTy::BaseType *concrete = nullptr,
bool is_query_mode = false,
Location ref_locus = Location ())
{
- CompileInherentImplItem compiler (ctx, compile_fns, concrete, ref_locus);
+ CompileInherentImplItem compiler (ctx, concrete, ref_locus);
item->accept_vis (compiler);
if (is_query_mode
@@ -79,9 +79,6 @@ public:
void visit (HIR::Function &function) override
{
- if (!compile_fns)
- return;
-
TyTy::BaseType *fntype_tyty;
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
&fntype_tyty))
@@ -305,14 +302,13 @@ public:
}
private:
- CompileInherentImplItem (Context *ctx, bool compile_fns,
- TyTy::BaseType *concrete, Location ref_locus)
- : HIRCompileBase (ctx), compile_fns (compile_fns), concrete (concrete),
+ CompileInherentImplItem (Context *ctx, TyTy::BaseType *concrete,
+ Location ref_locus)
+ : HIRCompileBase (ctx), concrete (concrete),
reference (ctx->get_backend ()->error_expression ()),
ref_locus (ref_locus)
{}
- bool compile_fns;
TyTy::BaseType *concrete;
tree reference;
Location ref_locus;
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h
index f400f5214d4..73f6967b272 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -38,12 +38,12 @@ protected:
using Rust::Compile::HIRCompileBase::visit;
public:
- static tree compile (HIR::Item *item, Context *ctx, bool compile_fns = true,
+ static tree compile (HIR::Item *item, Context *ctx,
TyTy::BaseType *concrete = nullptr,
bool is_query_mode = false,
Location ref_locus = Location ())
{
- CompileItem compiler (ctx, compile_fns, concrete, ref_locus);
+ CompileItem compiler (ctx, concrete, ref_locus);
item->accept_vis (compiler);
if (is_query_mode
@@ -56,6 +56,16 @@ public:
void visit (HIR::StaticItem &var) override
{
+ // have we already compiled this?
+ Bvariable *static_decl_ref = nullptr;
+ if (ctx->lookup_var_decl (var.get_mappings ().get_hirid (),
+ &static_decl_ref))
+ {
+ reference
+ = ctx->get_backend ()->var_expression (static_decl_ref, ref_locus);
+ return;
+ }
+
TyTy::BaseType *resolved_type = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (),
&resolved_type);
@@ -191,9 +201,6 @@ public:
void visit (HIR::Function &function) override
{
- if (!compile_fns)
- return;
-
TyTy::BaseType *fntype_tyty;
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
&fntype_tyty))
@@ -395,32 +402,29 @@ public:
}
for (auto &impl_item : impl_block.get_impl_items ())
- CompileInherentImplItem::Compile (impl_item.get (), ctx, compile_fns);
+ CompileInherentImplItem::Compile (impl_item.get (), ctx);
}
void visit (HIR::ExternBlock &extern_block) override
{
for (auto &item : extern_block.get_extern_items ())
{
- CompileExternItem::compile (item.get (), ctx, compile_fns, concrete);
+ CompileExternItem::compile (item.get (), ctx, concrete);
}
}
void visit (HIR::Module &module) override
{
for (auto &item : module.get_items ())
- CompileItem::compile (item.get (), ctx, compile_fns);
+ CompileItem::compile (item.get (), ctx);
}
protected:
- CompileItem (Context *ctx, bool compile_fns, TyTy::BaseType *concrete,
- Location ref_locus)
- : HIRCompileBase (ctx), compile_fns (compile_fns), concrete (concrete),
- reference (ctx->get_backend ()->error_expression ()),
+ CompileItem (Context *ctx, TyTy::BaseType *concrete, Location ref_locus)
+ : HIRCompileBase (ctx), concrete (concrete), reference (error_mark_node),
ref_locus (ref_locus)
{}
- bool compile_fns;
TyTy::BaseType *concrete;
tree reference;
Location ref_locus;
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index e2c0354a46b..ddb6c914d18 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -166,10 +166,10 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
if (is_hir_item)
{
if (!lookup->has_subsititions_defined ())
- return CompileItem::compile (resolved_item, ctx, true, nullptr, true,
+ return CompileItem::compile (resolved_item, ctx, nullptr, true,
expr_locus);
else
- return CompileItem::compile (resolved_item, ctx, true, lookup, true,
+ return CompileItem::compile (resolved_item, ctx, lookup, true,
expr_locus);
}
else
@@ -194,11 +194,11 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
rust_assert (ok);
if (!lookup->has_subsititions_defined ())
- return CompileInherentImplItem::Compile (resolved_item, ctx, true,
+ return CompileInherentImplItem::Compile (resolved_item, ctx,
nullptr, true, expr_locus);
else
- return CompileInherentImplItem::Compile (resolved_item, ctx, true,
- lookup, true, expr_locus);
+ return CompileInherentImplItem::Compile (resolved_item, ctx, lookup,
+ true, expr_locus);
}
else
{
@@ -276,13 +276,12 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
rust_assert (ok);
if (!lookup->has_subsititions_defined ())
- return CompileInherentImplItem::Compile (impl_item, ctx, true,
+ return CompileInherentImplItem::Compile (impl_item, ctx,
nullptr, true,
expr_locus);
else
- return CompileInherentImplItem::Compile (impl_item, ctx, true,
- lookup, true,
- expr_locus);
+ return CompileInherentImplItem::Compile (impl_item, ctx, lookup,
+ true, expr_locus);
lookup->set_ty_ref (impl_item->get_impl_mappings ().get_hirid ());
}
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index dcf698adedc..b0447af7505 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -46,10 +46,7 @@ void
CompileCrate::go ()
{
for (auto &item : crate.items)
- CompileItem::compile (item.get (), ctx, false);
-
- for (auto &item : crate.items)
- CompileItem::compile (item.get (), ctx, true);
+ CompileItem::compile (item.get (), ctx);
}
// rust-compile-block.h
@@ -274,7 +271,7 @@ HIRCompileBase::compile_locals_for_block (Resolver::Rib &rib, tree fndecl,
if (is_item)
{
HIR::Item *item = static_cast<HIR::Item *> (decl);
- CompileItem::compile (item, ctx, true);
+ CompileItem::compile (item, ctx);
return true;
}
@@ -528,7 +525,7 @@ HIRCompileBase::compute_address_for_trait_item (
lookup_fntype = lookup_fntype->handle_substitions (mappings);
}
- return CompileInherentImplItem::Compile (associated_function, ctx, true,
+ return CompileInherentImplItem::Compile (associated_function, ctx,
lookup_fntype, true, locus);
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-08 12:01 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:01 [gcc/devel/rust/master] Remove hack to handle forward declared items Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).