diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 135 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 105 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-extern.h | 11 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-implitem.h | 14 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-item.h | 30 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-resolve-path.cc | 17 | ||||
-rw-r--r-- | 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 427cd32..1048027 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 5464c88..4cc4dfc 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 100b1f5..507865a 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 fd92b0f..d646c83 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 f400f52..73f6967 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 e2c0354..ddb6c91 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 dcf698a..b0447af 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); } |