Skip to content

Commit 5a396e3

Browse files
committed
ast: builder: Remove ASTTypeBuilder
gcc/rust/ChangeLog: * Make-lang.in: Remove object file for ASTTypeBuilder. * ast/rust-ast-builder.h: Remove function. * ast/rust-ast-builder.cc (Builder::new_type): Likewise. (Builder::new_const_param): Use reconstruct_type() instead. (Builder::new_generic_args): Likewise. * expand/rust-derive-default.cc (DeriveDefault::visit_struct): Likewise. (DeriveDefault::visit_tuple): Likewise. * expand/rust-derive-eq.cc (DeriveEq::visit_tuple): Likewise. (DeriveEq::visit_struct): Likewise. (DeriveEq::visit_enum): Likewise. (DeriveEq::visit_union): Likewise. * ast/rust-ast-builder-type.cc: Removed. * ast/rust-ast-builder-type.h: Removed.
1 parent d91e005 commit 5a396e3

7 files changed

+39
-259
lines changed

gcc/rust/Make-lang.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ GRS_OBJS = \
9292
rust/rust-cfg-strip.o \
9393
rust/rust-expand-visitor.o \
9494
rust/rust-ast-builder.o \
95-
rust/rust-ast-builder-type.o \
9695
rust/rust-derive.o \
9796
rust/rust-derive-clone.o \
9897
rust/rust-derive-copy.o \

gcc/rust/ast/rust-ast-builder-type.cc

Lines changed: 0 additions & 163 deletions
This file was deleted.

gcc/rust/ast/rust-ast-builder-type.h

Lines changed: 0 additions & 57 deletions
This file was deleted.

gcc/rust/ast/rust-ast-builder.cc

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "rust-ast-builder.h"
2020
#include "optional.h"
21-
#include "rust-ast-builder-type.h"
2221
#include "rust-ast.h"
2322
#include "rust-common.h"
2423
#include "rust-expr.h"
@@ -531,11 +530,14 @@ Builder::generic_type_param (
531530
std::vector<Attribute> ());
532531
}
533532

534-
std::unique_ptr<Type>
535-
Builder::new_type (Type &type)
533+
std::unique_ptr<Stmt>
534+
Builder::discriminant_value (std::string binding_name, std::string instance)
536535
{
537-
Type *t = ASTTypeBuilder::build (type);
538-
return std::unique_ptr<Type> (t);
536+
auto intrinsic = ptrify (
537+
path_in_expression ({"core", "intrinsics", "discriminant_value"}, true));
538+
539+
return let (identifier_pattern (binding_name), nullptr,
540+
call (std::move (intrinsic), identifier (instance)));
539541
}
540542

541543
std::unique_ptr<GenericParam>
@@ -565,7 +567,7 @@ Builder::new_type_param (
565567
std::unique_ptr<Type> type = nullptr;
566568

567569
if (param.has_type ())
568-
type = new_type (param.get_type ());
570+
type = param.get_type ().reconstruct_type ();
569571

570572
for (auto &&extra_bound : extra_bounds)
571573
type_param_bounds.emplace_back (std::move (extra_bound));
@@ -690,27 +692,34 @@ Builder::new_generic_args (GenericArgs &args)
690692
for (auto &binding : args.get_binding_args ())
691693
{
692694
Type &t = *binding.get_type_ptr ().get ();
693-
std::unique_ptr<Type> ty = new_type (t);
695+
std::unique_ptr<Type> ty = t.reconstruct_type ();
694696
GenericArgsBinding b (binding.get_identifier (), std::move (ty),
695697
binding.get_locus ());
696698
binding_args.push_back (std::move (b));
697699
}
698700

699701
for (auto &arg : args.get_generic_args ())
700702
{
703+
tl::optional<GenericArg> new_arg = tl::nullopt;
704+
701705
switch (arg.get_kind ())
702706
{
703-
case GenericArg::Kind::Type: {
704-
std::unique_ptr<Type> ty = new_type (arg.get_type ());
705-
GenericArg arg = GenericArg::create_type (std::move (ty));
706-
}
707+
case GenericArg::Kind::Type:
708+
new_arg
709+
= GenericArg::create_type (arg.get_type ().reconstruct_type ());
707710
break;
708-
709-
default:
710-
// FIXME
711-
rust_unreachable ();
711+
case GenericArg::Kind::Either:
712+
new_arg
713+
= GenericArg::create_ambiguous (arg.get_path (), arg.get_locus ());
714+
break;
715+
case GenericArg::Kind::Const:
716+
new_arg
717+
= GenericArg::create_const (arg.get_expression ().clone_expr ());
718+
// FIXME: Use `reconstruct()` here, not `clone_expr()`
712719
break;
713720
}
721+
722+
generic_args.emplace_back (*new_arg);
714723
}
715724

716725
return GenericArgs (std::move (lifetime_args), std::move (generic_args),

gcc/rust/ast/rust-ast-builder.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,13 @@ class Builder
289289
std::vector<std::unique_ptr<TypeParamBound>> &&bounds,
290290
std::unique_ptr<Type> &&type = nullptr);
291291

292-
static std::unique_ptr<Type> new_type (Type &type);
292+
/**
293+
* Create a let statement with the discriminant value of a given enum
294+
* instance. This helper exists since it is a common operation in a lot of the
295+
* derive implementations, and it sucks to repeat all the steps every time.
296+
*/
297+
std::unique_ptr<Stmt> discriminant_value (std::string binding_name,
298+
std::string instance = "self");
293299

294300
static std::unique_ptr<GenericParam>
295301
new_lifetime_param (LifetimeParam &param);

gcc/rust/expand/rust-derive-default.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ DeriveDefault::visit_struct (StructStruct &item)
9898
for (auto &field : item.get_fields ())
9999
{
100100
auto name = field.get_field_name ().as_string ();
101-
auto type = Builder::new_type (field.get_field_type ());
101+
auto type = field.get_field_type ().reconstruct_type ();
102102
auto expr = default_call (std::move (type));
103103

104104
cloned_fields.emplace_back (
@@ -120,7 +120,7 @@ DeriveDefault::visit_tuple (TupleStruct &tuple_item)
120120

121121
for (auto &field : tuple_item.get_fields ())
122122
{
123-
auto type = Builder::new_type (field.get_field_type ());
123+
auto type = field.get_field_type ().reconstruct_type ();
124124

125125
defaulted_fields.emplace_back (default_call (std::move (type)));
126126
}

0 commit comments

Comments
 (0)