Skip to content

Commit

Permalink
Remove lambda iterators in various HIR classes
Browse files Browse the repository at this point in the history
This patch removes the lambda iterators used in various HIR objects.
These iterators make interacting with the IR for static analysis more
difficult. Instead, get_X () helpers are added for accessing elements,
and uses of the iterators replaced with for loops.

The following objects are adjusted in this patch:
- HIR::ArrayElemsValues
- HIR::TupleExpr
- HIR::StructExprField
- HIR::StructStruct
- HIR::TupleStruct

Fixes: Rust-GCC#703, Rust-GCC#704, Rust-GCC#705, Rust-GCC#706, Rust-GCC#707
  • Loading branch information
dafaust committed Oct 11, 2021
1 parent 99c2830 commit 85338a7
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 148 deletions.
20 changes: 10 additions & 10 deletions gcc/rust/backend/rust-compile-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,11 @@ class CompileExpr : public HIRCompileBase

void visit (HIR::ArrayElemsValues &elems) override
{
elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
Bexpression *translated_expr = CompileExpr::Compile (e, ctx);
constructor.push_back (translated_expr);
return true;
});
for (auto &elem : elems.get_values ())
{
Bexpression *translated_expr = CompileExpr::Compile (elem.get (), ctx);
constructor.push_back (translated_expr);
}
}

void visit (HIR::ArrayElemsCopied &elems) override
Expand Down Expand Up @@ -646,11 +646,11 @@ class CompileExpr : public HIRCompileBase
// this assumes all fields are in order from type resolution and if a base
// struct was specified those fields are filed via accesors
std::vector<Bexpression *> vals;
struct_expr.iterate ([&] (HIR::StructExprField *field) mutable -> bool {
Bexpression *expr = CompileStructExprField::Compile (field, ctx);
vals.push_back (expr);
return true;
});
for (auto &field : struct_expr.get_fields ())
{
Bexpression *expr = CompileStructExprField::Compile (field.get (), ctx);
vals.push_back (expr);
}

translated
= ctx->get_backend ()->constructor_expression (type, vals,
Expand Down
32 changes: 1 addition & 31 deletions gcc/rust/hir/tree/rust-hir-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -796,14 +796,7 @@ class ArrayElemsValues : public ArrayElems

size_t get_num_elements () const { return values.size (); }

void iterate (std::function<bool (Expr *)> cb)
{
for (auto it = values.begin (); it != values.end (); it++)
{
if (!cb ((*it).get ()))
return;
}
}
std::vector<std::unique_ptr<Expr> > &get_values () { return values; }

protected:
ArrayElemsValues *clone_array_elems_impl () const override
Expand Down Expand Up @@ -1070,15 +1063,6 @@ class TupleExpr : public ExprWithoutBlock

bool is_unit () const { return tuple_elems.size () == 0; }

void iterate (std::function<bool (Expr *)> cb)
{
for (auto &tuple_elem : tuple_elems)
{
if (!cb (tuple_elem.get ()))
return;
}
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -1491,15 +1475,6 @@ class StructExprStructFields : public StructExprStruct

void accept_vis (HIRVisitor &vis) override;

void iterate (std::function<bool (StructExprField *)> cb)
{
for (auto &field : fields)
{
if (!cb (field.get ()))
return;
}
}

std::vector<std::unique_ptr<StructExprField> > &get_fields ()
{
return fields;
Expand All @@ -1510,11 +1485,6 @@ class StructExprStructFields : public StructExprStruct
return fields;
};

std::vector<std::unique_ptr<StructExprField> > get_fields_as_owner ()
{
return std::move (fields);
};

void set_fields_as_owner (
std::vector<std::unique_ptr<StructExprField> > new_fields)
{
Expand Down
18 changes: 1 addition & 17 deletions gcc/rust/hir/tree/rust-hir-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1492,14 +1492,7 @@ class StructStruct : public Struct

void accept_vis (HIRVisitor &vis) override;

void iterate (std::function<bool (StructField &)> cb)
{
for (auto &field : fields)
{
if (!cb (field))
return;
}
}
std::vector<StructField> &get_fields () { return fields; }

protected:
/* Use covariance to implement clone function as returning this object
Expand Down Expand Up @@ -1610,15 +1603,6 @@ class TupleStruct : public Struct
std::vector<TupleField> &get_fields () { return fields; }
const std::vector<TupleField> &get_fields () const { return fields; }

void iterate (std::function<bool (TupleField &)> cb)
{
for (auto &field : fields)
{
if (!cb (field))
return;
}
}

protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
Expand Down
24 changes: 12 additions & 12 deletions gcc/rust/lint/rust-lint-marklive.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ class MarkLive : public MarkLiveBase

void visit (HIR::ArrayElemsValues &expr) override
{
expr.iterate ([&] (HIR::Expr *expr) mutable -> bool {
expr->accept_vis (*this);
return true;
});
for (auto &elem : expr.get_values ())
{
elem->accept_vis (*this);
}
}

void visit (HIR::TupleExpr &expr) override
{
expr.iterate ([&] (HIR::Expr *expr) mutable -> bool {
expr->accept_vis (*this);
return true;
});
for (auto &elem : expr.get_tuple_elems ())
{
elem->accept_vis (*this);
}
}

void visit (HIR::BlockExpr &expr) override
Expand Down Expand Up @@ -236,10 +236,10 @@ class MarkLive : public MarkLiveBase

void visit (HIR::StructExprStructFields &stct) override
{
stct.iterate ([&] (HIR::StructExprField *field) -> bool {
field->accept_vis (*this);
return true;
});
for (auto &field : stct.get_fields ())
{
field->accept_vis (*this);
}

stct.get_struct_name ().accept_vis (*this);
if (stct.has_struct_base ())
Expand Down
20 changes: 10 additions & 10 deletions gcc/rust/lint/rust-lint-scan-deadcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ class ScanDeadcode : public MarkLiveBase
else
{
// only warn the unused fields when in unwarned struct.
stct.iterate ([&] (HIR::StructField &field) -> bool {
HirId field_hir_id = field.get_mappings ().get_hirid ();
if (should_warn (field_hir_id))
{
rust_warning_at (field.get_locus (), 0,
"field is never read: %<%s%>",
field.get_field_name ().c_str ());
}
return true;
});
for (auto &field : stct.get_fields ())
{
HirId field_hir_id = field.get_mappings ().get_hirid ();
if (should_warn (field_hir_id))
{
rust_warning_at (field.get_locus (), 0,
"field is never read: %<%s%>",
field.get_field_name ().c_str ());
}
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions gcc/rust/typecheck/rust-hir-type-check-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,11 @@ class TypeCheckExpr : public TypeCheckBase
void visit (HIR::ArrayElemsValues &elems) override
{
std::vector<TyTy::BaseType *> types;
elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
types.push_back (TypeCheckExpr::Resolve (e, false));
return true;
});

for (auto &elem : elems.get_values ())
{
types.push_back (TypeCheckExpr::Resolve (elem.get (), false));
}

infered_array_elems
= TyTy::TyVar::get_implicit_infer_var (root_array_expr_locus).get_tyty ();
Expand Down
44 changes: 23 additions & 21 deletions gcc/rust/typecheck/rust-hir-type-check-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,18 @@ class TypeCheckStmt : public TypeCheckBase
std::vector<TyTy::StructFieldType *> fields;

size_t idx = 0;
struct_decl.iterate ([&] (HIR::TupleField &field) mutable -> bool {
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
std::to_string (idx), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
idx++;
return true;
});
for (auto &field : struct_decl.get_fields ())
{
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
std::to_string (idx), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (),
ty_field->get_field_type ());
idx++;
}

TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
Expand Down Expand Up @@ -196,16 +197,17 @@ class TypeCheckStmt : public TypeCheckBase
}

std::vector<TyTy::StructFieldType *> fields;
struct_decl.iterate ([&] (HIR::StructField &field) mutable -> bool {
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
field.get_field_name (), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
return true;
});
for (auto &field : struct_decl.get_fields ())
{
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
field.get_field_name (), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (),
ty_field->get_field_type ());
}

TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
Expand Down
45 changes: 24 additions & 21 deletions gcc/rust/typecheck/rust-hir-type-check-toplevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ class TypeCheckTopLevel : public TypeCheckBase
std::vector<TyTy::StructFieldType *> fields;

size_t idx = 0;
struct_decl.iterate ([&] (HIR::TupleField &field) mutable -> bool {
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
std::to_string (idx), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
idx++;
return true;
});
for (auto &field : struct_decl.get_fields ())
{
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
std::to_string (idx), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (),
ty_field->get_field_type ());
idx++;
}

TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
Expand Down Expand Up @@ -136,16 +137,18 @@ class TypeCheckTopLevel : public TypeCheckBase
}

std::vector<TyTy::StructFieldType *> fields;
struct_decl.iterate ([&] (HIR::StructField &field) mutable -> bool {
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
field.get_field_name (), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
return true;
});

for (auto &field : struct_decl.get_fields ())
{
TyTy::BaseType *field_type
= TypeCheckType::Resolve (field.get_field_type ().get ());
TyTy::StructFieldType *ty_field
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
field.get_field_name (), field_type);
fields.push_back (ty_field);
context->insert_type (field.get_mappings (),
ty_field->get_field_type ());
}

TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
Expand Down
33 changes: 16 additions & 17 deletions gcc/rust/typecheck/rust-hir-type-check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,21 @@ TypeCheckStructExpr::visit (HIR::StructExprStructFields &struct_expr)

std::vector<TyTy::StructFieldType *> infered_fields;
bool ok = true;
struct_expr.iterate ([&] (HIR::StructExprField *field) mutable -> bool {
resolved_field_value_expr = nullptr;
field->accept_vis (*this);
if (resolved_field_value_expr == nullptr)
{
rust_fatal_error (field->get_locus (),
"failed to resolve type for field");
ok = false;
return false;
}

context->insert_type (field->get_mappings (), resolved_field_value_expr);
return true;
});
for (auto &field : struct_expr.get_fields ())
{
resolved_field_value_expr = nullptr;
field->accept_vis (*this);
if (resolved_field_value_expr == nullptr)
{
rust_fatal_error (field->get_locus (),
"failed to resolve type for field");
ok = false;
break;
}

context->insert_type (field->get_mappings (), resolved_field_value_expr);
}

// something failed setting up the fields
if (!ok)
Expand Down Expand Up @@ -266,10 +267,8 @@ TypeCheckStructExpr::visit (HIR::StructExprStructFields &struct_expr)
// correctly. The GIMPLE backend uses a simple algorithm that assumes each
// assigned field in the constructor is in the same order as the field in
// the type
std::vector<std::unique_ptr<HIR::StructExprField> > expr_fields
= struct_expr.get_fields_as_owner ();
for (auto &f : expr_fields)
f.release ();
for (auto &field : struct_expr.get_fields ())
field.release ();

std::vector<std::unique_ptr<HIR::StructExprField> > ordered_fields;
for (size_t i = 0; i < adtFieldIndexToField.size (); i++)
Expand Down
10 changes: 5 additions & 5 deletions gcc/rust/typecheck/rust-tycheck-dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ class TypeResolverDump : public TypeCheckBase

void visit (HIR::ArrayElemsValues &elems) override
{
elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
e->accept_vis (*this);
dump += ",";
return true;
});
for (auto &elem : elems.get_values ())
{
elem->accept_vis (*this);
dump += ",";
}
}

void visit (HIR::GroupedExpr &expr) override
Expand Down

0 comments on commit 85338a7

Please sign in to comment.