|
| 1 | +// Copyright (C) 2020-2026 Free Software Foundation, Inc. |
| 2 | + |
| 3 | +// This file is part of GCC. |
| 4 | + |
| 5 | +// GCC is free software; you can redistribute it and/or modify it under |
| 6 | +// the terms of the GNU General Public License as published by the Free |
| 7 | +// Software Foundation; either version 3, or (at your option) any later |
| 8 | +// version. |
| 9 | + |
| 10 | +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +// for more details. |
| 14 | + |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with GCC; see the file COPYING3. If not see |
| 17 | +// <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +#include "rust-compile-datum.h" |
| 20 | +#include "rust-backend.h" |
| 21 | +#include "rust-compile-context.h" |
| 22 | +#include "rust-compile-drop.h" |
| 23 | +#include "rust-compile-type.h" |
| 24 | +#include "rust-tyty.h" |
| 25 | +namespace Rust { |
| 26 | +namespace Compile { |
| 27 | + |
| 28 | +Datum |
| 29 | +Datum::to_lvalue (Context *ctx, HirId expr_id) |
| 30 | +{ |
| 31 | + if (kind == DatumKind::Lvalue) |
| 32 | + return *this; |
| 33 | + assert (is_valid () && "invalid datum"); |
| 34 | + DropContext *drop_ctx = DropContext::get (ctx); |
| 35 | + tree compiled_type = TyTyResolveCompile::compile (ctx, ty); |
| 36 | + assert_node (compiled_type, "something wrong with compiled_type"); |
| 37 | + tree pstatement = NULL_TREE; |
| 38 | + tree fndecl = ctx->peek_fn ().fndecl; |
| 39 | + tree bind_tree = ctx->peek_enclosing_scope (); |
| 40 | + LocalVariable tmp |
| 41 | + = Backend::temporary_variable (fndecl, bind_tree, compiled_type, val, false, |
| 42 | + locus, &pstatement); |
| 43 | + assert (pstatement != NULL_TREE); |
| 44 | + ctx->add_statement (pstatement); |
| 45 | + val = Backend::var_expression (tmp, locus); |
| 46 | + if (drop_ctx->needs_drop (ty)) |
| 47 | + { |
| 48 | + DropPath tmp_path; |
| 49 | + tmp_path.root = expr_id; |
| 50 | + tmp_path.var_name = "tmp_expr_" + std::to_string (expr_id); |
| 51 | + DropPlace *place = drop_ctx->make_drop_place (val, tmp_path, ty); |
| 52 | + assert (place != nullptr); |
| 53 | + tree drop_expr = drop_ctx->drop (place); |
| 54 | + assert_node (drop_expr, "drop expr is not valid"); |
| 55 | + ctx->add_cleanup (drop_expr); |
| 56 | + ctx->add_statement (drop_ctx->init (place)); |
| 57 | + drop_place = place; |
| 58 | + } |
| 59 | + kind = DatumKind::Lvalue; |
| 60 | + return *this; |
| 61 | +} |
| 62 | + |
| 63 | +Datum |
| 64 | +Datum::to_rvalue (Context *ctx) |
| 65 | +{ |
| 66 | + if (kind == DatumKind::Rvalue) |
| 67 | + return *this; |
| 68 | + assert (is_valid () && "invalid datum"); |
| 69 | + DropContext *drop_ctx = DropContext::get (ctx); |
| 70 | + tree compiled_type = TyTyResolveCompile::compile (ctx, ty); |
| 71 | + assert_node (compiled_type, "something wrong with compiled type"); |
| 72 | + tree pstatement = NULL_TREE; |
| 73 | + tree fndecl = ctx->peek_fn ().fndecl; |
| 74 | + tree bind_tree = ctx->peek_enclosing_scope (); |
| 75 | + LocalVariable scratch |
| 76 | + = Backend::temporary_variable (fndecl, bind_tree, compiled_type, NULL_TREE, |
| 77 | + false, locus, &pstatement); |
| 78 | + assert (pstatement != NULL_TREE); |
| 79 | + ctx->add_statement (pstatement); |
| 80 | + tree scratch_expr = Backend::var_expression (scratch, locus); |
| 81 | + tree copy = Backend::assignment_statement (scratch_expr, val, locus); |
| 82 | + ctx->add_statement (copy); |
| 83 | + if (needs_drop (drop_ctx) && drop_place) |
| 84 | + ctx->add_statement (drop_ctx->uninit (drop_place)); |
| 85 | + return Datum::new_rvalue (scratch_expr, ty, locus); |
| 86 | +} |
| 87 | + |
| 88 | +void |
| 89 | +Datum::store_to (Context *ctx, tree dest_ptr) |
| 90 | +{ |
| 91 | + tree assignment = Backend::assignment_statement (dest_ptr, val, locus); |
| 92 | + ctx->add_statement (assignment); |
| 93 | + post_store (ctx); |
| 94 | +} |
| 95 | + |
| 96 | +void |
| 97 | +Datum::post_store (Context *ctx) |
| 98 | +{ |
| 99 | + if (kind == DatumKind::Lvalue && ty != nullptr && drop_place != nullptr) |
| 100 | + { |
| 101 | + DropContext *drop_ctx = DropContext::get (ctx); |
| 102 | + if (drop_ctx->needs_drop (ty)) |
| 103 | + ctx->add_statement (drop_ctx->uninit (drop_place)); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +Datum |
| 108 | +Datum::get_field (Context *ctx, size_t field_index, location_t field_locus) |
| 109 | +{ |
| 110 | + tree field_expr |
| 111 | + = Backend::struct_field_expression (val, field_index, field_locus); |
| 112 | + TyTy::BaseType *field_ty = nullptr; |
| 113 | + assert (is_valid () && "invalid datum in get_field"); |
| 114 | + TyTy::BaseType *resolved = ty->destructure (); |
| 115 | + switch (resolved->get_kind ()) |
| 116 | + { |
| 117 | + case TyTy::TypeKind::ADT: |
| 118 | + { |
| 119 | + TyTy::ADTType *adt = resolved->as<TyTy::ADTType> (); |
| 120 | + TyTy::VariantDef *variant = adt->get_variants ().at (0); |
| 121 | + field_ty = variant->get_field_at_index (field_index)->get_field_type (); |
| 122 | + break; |
| 123 | + } |
| 124 | + case TyTy::TypeKind::TUPLE: |
| 125 | + { |
| 126 | + TyTy::TupleType *tuple = resolved->as<TyTy::TupleType> (); |
| 127 | + field_ty = tuple->get_field (field_index); |
| 128 | + break; |
| 129 | + } |
| 130 | + default: |
| 131 | + todo_die ("handle all remaining cases explicitly"); |
| 132 | + break; |
| 133 | + } |
| 134 | + DropPlace *field_dp = nullptr; |
| 135 | + if (drop_place != nullptr && !drop_place->is_leaf ()) |
| 136 | + { |
| 137 | + DropPath child_path = drop_place->path.child (field_index); |
| 138 | + for (auto *child : drop_place->children) |
| 139 | + if (child->path == child_path) |
| 140 | + field_dp = child; |
| 141 | + } |
| 142 | + return Datum::new_lvalue (field_expr, field_ty, field_dp, field_locus); |
| 143 | +} |
| 144 | + |
| 145 | +void |
| 146 | +Datum::add_clean_if_rvalue (Context *ctx, HirId expr_id) |
| 147 | +{ |
| 148 | + if (kind == DatumKind::Lvalue) |
| 149 | + return; |
| 150 | + DropContext *drop_ctx = DropContext::get (ctx); |
| 151 | + if (!needs_drop (drop_ctx)) |
| 152 | + return; |
| 153 | + to_lvalue (ctx, expr_id); |
| 154 | +} |
| 155 | +} // namespace Compile |
| 156 | +} // namespace Rust |
0 commit comments