diff options
Diffstat (limited to 'gcc/rust/backend/rust-compile-base.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 97 |
1 files changed, 66 insertions, 31 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 8166c3a..12b9561 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -25,7 +25,8 @@ #include "rust-compile-type.h" #include "rust-constexpr.h" #include "rust-diagnostics.h" -#include "rust-expr.h" // for AST::AttrInputLiteral +#include "rust-expr.h" // for AST::AttrInputLiteral +#include "rust-hir-map.h" #include "rust-macro.h" // for AST::MetaNameValueStr #include "rust-hir-path-probe.h" #include "rust-type-util.h" @@ -39,6 +40,9 @@ #include "tree.h" #include "print-tree.h" +// rust-name-resolution-2.0 +#include "options.h" + namespace Rust { namespace Compile { @@ -545,7 +549,7 @@ HIRCompileBase::mark_addressable (tree exp, location_t locus) } tree -HIRCompileBase::address_expression (tree expr, location_t location) +HIRCompileBase::address_expression (tree expr, location_t location, tree ptrty) { if (expr == error_mark_node) return error_mark_node; @@ -553,7 +557,22 @@ HIRCompileBase::address_expression (tree expr, location_t location) if (!mark_addressable (expr, location)) return error_mark_node; - return build_fold_addr_expr_loc (location, expr); + if (ptrty == NULL || ptrty == error_mark_node) + ptrty = build_pointer_type (TREE_TYPE (expr)); + + return build_fold_addr_expr_with_type_loc (location, expr, ptrty); +} + +tree +HIRCompileBase::compile_constant_expr ( + Context *ctx, HirId coercion_id, TyTy::BaseType *resolved_type, + TyTy::BaseType *expected_type, const Resolver::CanonicalPath &canonical_path, + HIR::Expr &const_value_expr, location_t locus, location_t expr_locus) +{ + HIRCompileBase c (ctx); + return c.compile_constant_item (coercion_id, resolved_type, expected_type, + canonical_path, const_value_expr, locus, + expr_locus); } tree @@ -582,8 +601,9 @@ HIRCompileBase::compile_function_body (tree fndecl, if (function_body.has_expr ()) { - location_t locus = function_body.get_final_expr ()->get_locus (); - tree return_value = CompileExpr::Compile (function_body.expr.get (), ctx); + location_t locus = function_body.get_final_expr ().get_locus (); + tree return_value + = CompileExpr::Compile (function_body.get_final_expr (), ctx); // we can only return this if non unit value return type if (!fn_return_ty->is_unit ()) @@ -646,7 +666,8 @@ get_abi (const AST::AttrVec &outer_attrs, tree HIRCompileBase::compile_function ( - const std::string &fn_name, HIR::SelfParam &self_param, + bool is_root_item, const std::string &fn_name, + tl::optional<HIR::SelfParam> &self_param, std::vector<HIR::FunctionParam> &function_params, const HIR::FunctionQualifiers &qualifiers, HIR::Visibility &visibility, AST::AttrVec &outer_attrs, location_t locus, HIR::BlockExpr *function_body, @@ -657,7 +678,7 @@ HIRCompileBase::compile_function ( = canonical_path.get () + fntype->subst_as_string (); // we don't mangle the main fn since we haven't implemented the main shim - bool is_main_fn = fn_name.compare ("main") == 0; + bool is_main_fn = fn_name.compare ("main") == 0 && is_root_item; if (is_main_fn) { rust_assert (!main_identifier_node); @@ -666,6 +687,11 @@ HIRCompileBase::compile_function ( } std::string asm_name = fn_name; + auto &mappings = Analysis::Mappings::get (); + + if (flag_name_resolution_2_0) + ir_symbol_name = mappings.get_current_crate_name () + "::" + ir_symbol_name; + unsigned int flags = 0; tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, "" /* asm_name */, flags, locus); @@ -688,39 +714,39 @@ HIRCompileBase::compile_function ( // setup the params TyTy::BaseType *tyret = fntype->get_return_type (); std::vector<Bvariable *> param_vars; - if (!self_param.is_error ()) + if (self_param) { rust_assert (fntype->is_method ()); TyTy::BaseType *self_tyty_lookup = fntype->get_self_type (); tree self_type = TyTyResolveCompile::compile (ctx, self_tyty_lookup); Bvariable *compiled_self_param - = CompileSelfParam::compile (ctx, fndecl, self_param, self_type, - self_param.get_locus ()); + = CompileSelfParam::compile (ctx, fndecl, self_param.value (), + self_type, self_param->get_locus ()); param_vars.push_back (compiled_self_param); - ctx->insert_var_decl (self_param.get_mappings ().get_hirid (), + ctx->insert_var_decl (self_param->get_mappings ().get_hirid (), compiled_self_param); } // offset from + 1 for the TyTy::FnType being used when this is a method to // skip over Self on the FnType - bool is_method = !self_param.is_error (); + bool is_method = self_param.has_value (); size_t i = is_method ? 1 : 0; for (auto &referenced_param : function_params) { - auto tyty_param = fntype->param_at (i++); - auto param_tyty = tyty_param.second; + auto &tyty_param = fntype->param_at (i++); + auto param_tyty = tyty_param.get_type (); auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); location_t param_locus = referenced_param.get_locus (); Bvariable *compiled_param_var - = CompileFnParam::compile (ctx, fndecl, &referenced_param, + = CompileFnParam::compile (ctx, fndecl, referenced_param, compiled_param_type, param_locus); param_vars.push_back (compiled_param_var); - const HIR::Pattern ¶m_pattern = *referenced_param.get_param_name (); + const HIR::Pattern ¶m_pattern = referenced_param.get_param_name (); ctx->insert_var_decl (param_pattern.get_mappings ().get_hirid (), compiled_param_var); } @@ -767,15 +793,20 @@ HIRCompileBase::compile_function ( tree HIRCompileBase::compile_constant_item ( - TyTy::BaseType *resolved_type, const Resolver::CanonicalPath &canonical_path, - HIR::Expr *const_value_expr, location_t locus) + HirId coercion_id, TyTy::BaseType *resolved_type, + TyTy::BaseType *expected_type, const Resolver::CanonicalPath &canonical_path, + HIR::Expr &const_value_expr, location_t locus, location_t expr_locus) { const std::string &ident = canonical_path.get (); tree type = TyTyResolveCompile::compile (ctx, resolved_type); tree const_type = build_qualified_type (type, TYPE_QUAL_CONST); + + tree actual_type = TyTyResolveCompile::compile (ctx, expected_type); + tree actual_const_type = build_qualified_type (actual_type, TYPE_QUAL_CONST); + bool is_block_expr - = const_value_expr->get_expression_type () == HIR::Expr::ExprType::Block; + = const_value_expr.get_expression_type () == HIR::Expr::ExprType::Block; // in order to compile a block expr we want to reuse as much existing // machineary that we already have. This means the best approach is to @@ -789,14 +820,14 @@ HIRCompileBase::compile_constant_item ( TREE_READONLY (fndecl) = 1; tree enclosing_scope = NULL_TREE; - location_t start_location = const_value_expr->get_locus (); - location_t end_location = const_value_expr->get_locus (); + location_t start_location = const_value_expr.get_locus (); + location_t end_location = const_value_expr.get_locus (); if (is_block_expr) { - HIR::BlockExpr *function_body - = static_cast<HIR::BlockExpr *> (const_value_expr); - start_location = function_body->get_locus (); - end_location = function_body->get_end_locus (); + HIR::BlockExpr &function_body + = static_cast<HIR::BlockExpr &> (const_value_expr); + start_location = function_body.get_locus (); + end_location = function_body.get_end_locus (); } tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/, @@ -814,9 +845,9 @@ HIRCompileBase::compile_constant_item ( if (is_block_expr) { - HIR::BlockExpr *function_body - = static_cast<HIR::BlockExpr *> (const_value_expr); - compile_function_body (fndecl, *function_body, resolved_type); + HIR::BlockExpr &function_body + = static_cast<HIR::BlockExpr &> (const_value_expr); + compile_function_body (fndecl, function_body, resolved_type); } else { @@ -824,7 +855,7 @@ HIRCompileBase::compile_constant_item ( tree return_expr = Backend::return_statement (fndecl, value, - const_value_expr->get_locus ()); + const_value_expr.get_locus ()); ctx->add_statement (return_expr); } @@ -841,7 +872,11 @@ HIRCompileBase::compile_constant_item ( tree call = build_call_array_loc (locus, const_type, fndecl, 0, NULL); tree folded_expr = fold_expr (call); - return named_constant_expression (const_type, ident, folded_expr, locus); + // coercion site + tree coerced = coercion_site (coercion_id, folded_expr, resolved_type, + expected_type, locus, expr_locus); + + return named_constant_expression (actual_const_type, ident, coerced, locus); } tree @@ -992,7 +1027,7 @@ HIRCompileBase::resolve_method_address (TyTy::FnType *fntype, tree HIRCompileBase::unit_expression (location_t locus) { - tree unit_type = TyTyResolveCompile::get_unit_type (); + tree unit_type = TyTyResolveCompile::get_unit_type (ctx); return Backend::constructor_expression (unit_type, false, {}, -1, locus); } |