From 40aae6224b1bab4825b11a1d16ea73ba52a04d6b Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Tue, 14 Dec 2021 11:56:12 +0000 Subject: Refactor TyTy::ResolveCompile pass to be in its own file This name likely needs refactored, this class is used to take TyTy types and compile them down to GCC tree's but take into account that we may have already compiled this type before so to preserve the canonical types we "resolve" the type if possible and then compile. --- gcc/rust/backend/rust-compile-context.h | 344 -------------------------------- 1 file changed, 344 deletions(-) (limited to 'gcc/rust/backend/rust-compile-context.h') diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 668e8ba..6347e76 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -334,350 +334,6 @@ private: std::vector func_decls; }; -class TyTyResolveCompile : public TyTy::TyConstVisitor -{ -public: - static tree compile (Context *ctx, const TyTy::BaseType *ty, - bool trait_object_mode = false) - { - TyTyResolveCompile compiler (ctx, trait_object_mode); - ty->accept_vis (compiler); - return compiler.translated; - } - - void visit (const TyTy::ErrorType &) override { gcc_unreachable (); } - void visit (const TyTy::InferType &) override { gcc_unreachable (); } - - void visit (const TyTy::ProjectionType &type) override - { - type.get ()->accept_vis (*this); - } - - void visit (const TyTy::PlaceholderType &type) override - { - type.resolve ()->accept_vis (*this); - } - - void visit (const TyTy::ParamType ¶m) override - { - recursion_count++; - rust_assert (recursion_count < kDefaultRecusionLimit); - - param.resolve ()->accept_vis (*this); - } - - void visit (const TyTy::FnType &type) override - { - Backend::typed_identifier receiver; - std::vector parameters; - std::vector results; - - if (!type.get_return_type ()->is_unit ()) - { - auto hir_type = type.get_return_type (); - auto ret - = TyTyResolveCompile::compile (ctx, hir_type, trait_object_mode); - results.push_back (Backend::typed_identifier ( - "_", ret, - ctx->get_mappings ()->lookup_location (hir_type->get_ref ()))); - } - - for (auto ¶m_pair : type.get_params ()) - { - auto param_tyty = param_pair.second; - auto compiled_param_type - = TyTyResolveCompile::compile (ctx, param_tyty, trait_object_mode); - - auto compiled_param = Backend::typed_identifier ( - param_pair.first->as_string (), compiled_param_type, - ctx->get_mappings ()->lookup_location (param_tyty->get_ref ())); - - parameters.push_back (compiled_param); - } - - if (!type.is_varadic ()) - translated = ctx->get_backend ()->function_type ( - receiver, parameters, results, NULL, - ctx->get_mappings ()->lookup_location (type.get_ref ())); - else - translated = ctx->get_backend ()->function_type_varadic ( - receiver, parameters, results, NULL, - ctx->get_mappings ()->lookup_location (type.get_ref ())); - } - - void visit (const TyTy::FnPtr &type) override - { - tree result_type - = TyTyResolveCompile::compile (ctx, type.get_return_type ()); - - std::vector parameters; - type.iterate_params ([&] (TyTy::BaseType *p) mutable -> bool { - tree pty = TyTyResolveCompile::compile (ctx, p); - parameters.push_back (pty); - return true; - }); - - translated = ctx->get_backend ()->function_ptr_type ( - result_type, parameters, - ctx->get_mappings ()->lookup_location (type.get_ref ())); - } - - void visit (const TyTy::ADTType &type) override - { - if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type)) - return; - - // we dont support enums yet - rust_assert (!type.is_enum ()); - rust_assert (type.number_of_variants () == 1); - - TyTy::VariantDef &variant = *type.get_variants ().at (0); - std::vector fields; - for (size_t i = 0; i < variant.num_fields (); i++) - { - const TyTy::StructFieldType *field = variant.get_field_at_index (i); - tree compiled_field_ty - = TyTyResolveCompile::compile (ctx, field->get_field_type ()); - - Backend::typed_identifier f (field->get_name (), compiled_field_ty, - ctx->get_mappings ()->lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (f)); - } - - tree type_record; - if (type.is_union ()) - type_record = ctx->get_backend ()->union_type (fields); - else - type_record = ctx->get_backend ()->struct_type (fields); - tree named_struct - = ctx->get_backend ()->named_type (type.get_name (), type_record, - ctx->get_mappings ()->lookup_location ( - type.get_ty_ref ())); - - ctx->push_type (named_struct); - translated = named_struct; - - ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type); - } - - void visit (const TyTy::TupleType &type) override - { - if (type.num_fields () == 0) - { - translated = ctx->get_backend ()->unit_type (); - return; - } - - bool ok - = ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type); - if (ok) - return; - - // create implicit struct - std::vector fields; - for (size_t i = 0; i < type.num_fields (); i++) - { - TyTy::BaseType *field = type.get_field (i); - tree compiled_field_ty = TyTyResolveCompile::compile (ctx, field); - - // rustc uses the convention __N, where N is an integer, to - // name the fields of a tuple. We follow this as well, - // because this is used by GDB. One further reason to prefer - // this, rather than simply emitting the integer, is that this - // approach makes it simpler to use a C-only debugger, or - // GDB's C mode, when debugging Rust. - Backend::typed_identifier f ("__" + std::to_string (i), - compiled_field_ty, - ctx->get_mappings ()->lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (f)); - } - - tree struct_type_record = ctx->get_backend ()->struct_type (fields); - tree named_struct - = ctx->get_backend ()->named_type (type.as_string (), struct_type_record, - ctx->get_mappings ()->lookup_location ( - type.get_ty_ref ())); - - ctx->push_type (named_struct); - ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type); - translated = named_struct; - } - - void visit (const TyTy::ArrayType &type) override - { - tree element_type - = TyTyResolveCompile::compile (ctx, type.get_element_type ()); - translated - = ctx->get_backend ()->array_type (element_type, type.get_capacity ()); - } - - void visit (const TyTy::BoolType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::IntType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::UintType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::FloatType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::USizeType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::ISizeType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::CharType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::ReferenceType &type) override - { - tree base_compiled_type - = TyTyResolveCompile::compile (ctx, type.get_base (), trait_object_mode); - if (type.is_mutable ()) - { - translated = ctx->get_backend ()->reference_type (base_compiled_type); - } - else - { - auto base = ctx->get_backend ()->immutable_type (base_compiled_type); - translated = ctx->get_backend ()->reference_type (base); - } - } - - void visit (const TyTy::PointerType &type) override - { - tree base_compiled_type - = TyTyResolveCompile::compile (ctx, type.get_base (), trait_object_mode); - if (type.is_mutable ()) - { - translated = ctx->get_backend ()->pointer_type (base_compiled_type); - } - else - { - auto base = ctx->get_backend ()->immutable_type (base_compiled_type); - translated = ctx->get_backend ()->pointer_type (base); - } - } - - void visit (const TyTy::StrType &type) override - { - tree compiled_type = nullptr; - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type); - rust_assert (ok); - translated = compiled_type; - } - - void visit (const TyTy::NeverType &) override - { - translated = ctx->get_backend ()->unit_type (); - } - - void visit (const TyTy::DynamicObjectType &type) override - { - if (trait_object_mode) - { - translated = ctx->get_backend ()->integer_type ( - true, ctx->get_backend ()->get_pointer_size ()); - return; - } - - if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type)) - return; - - // create implicit struct - auto items = type.get_object_items (); - std::vector fields; - - tree uint = ctx->get_backend ()->integer_type ( - true, ctx->get_backend ()->get_pointer_size ()); - tree uintptr_ty = ctx->get_backend ()->pointer_type (uint); - - Backend::typed_identifier f ("__receiver_trait_obj_ptr", uintptr_ty, - ctx->get_mappings ()->lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (f)); - - for (size_t i = 0; i < items.size (); i++) - { - // mrustc seems to make a vtable consisting of uintptr's - tree uint = ctx->get_backend ()->integer_type ( - true, ctx->get_backend ()->get_pointer_size ()); - tree uintptr_ty = ctx->get_backend ()->pointer_type (uint); - - Backend::typed_identifier f ("__" + std::to_string (i), uintptr_ty, - ctx->get_mappings ()->lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (f)); - } - - tree type_record = ctx->get_backend ()->struct_type (fields); - tree named_struct - = ctx->get_backend ()->named_type (type.get_name (), type_record, - ctx->get_mappings ()->lookup_location ( - type.get_ty_ref ())); - - ctx->push_type (named_struct); - translated = named_struct; - - ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type); - } - - void visit (const TyTy::ClosureType &) override { gcc_unreachable (); } - -private: - TyTyResolveCompile (Context *ctx, bool trait_object_mode) - : ctx (ctx), trait_object_mode (trait_object_mode), translated (nullptr), - recursion_count (0) - {} - - Context *ctx; - bool trait_object_mode; - tree translated; - size_t recursion_count; - - static const size_t kDefaultRecusionLimit = 5; -}; - } // namespace Compile } // namespace Rust -- cgit v1.1