diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-01-18 14:51:36 +0000 |
---|---|---|
committer | Philip Herron <herron.philip@googlemail.com> | 2021-01-20 09:59:22 +0000 |
commit | 9a942d6fbd0cc087cbc801ce4681a498e59dce2c (patch) | |
tree | 5198e76f054e741f5ba9793e111bfd63be88e80d /gcc/rust/backend/rust-compile.cc | |
parent | 89631998d2ffda0c0c05066c148c6fc19398da5c (diff) | |
download | gcc-9a942d6fbd0cc087cbc801ce4681a498e59dce2c.zip gcc-9a942d6fbd0cc087cbc801ce4681a498e59dce2c.tar.gz gcc-9a942d6fbd0cc087cbc801ce4681a498e59dce2c.tar.bz2 |
Add in TupleStruct support
This adds in tuple struct support with name and type resolution. The
arguments and number of arguments are validated against. Test cases
added for those errors too.
Diffstat (limited to 'gcc/rust/backend/rust-compile.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 772d975..6519a47 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -48,6 +48,53 @@ CompileCrate::go () CompileItem::compile (item.get (), ctx, true); } +// rust-compile-expr.h + +void +CompileExpr::visit (HIR::CallExpr &expr) +{ + // this can be a function call or it can be a constructor for a tuple struct + Bexpression *fn = ResolvePathRef::Compile (expr.get_fnexpr (), ctx); + if (fn != nullptr) + { + std::vector<Bexpression *> args; + expr.iterate_params ([&] (HIR::Expr *p) mutable -> bool { + Bexpression *compiled_expr = CompileExpr::Compile (p, ctx); + rust_assert (compiled_expr != nullptr); + args.push_back (compiled_expr); + return true; + }); + + auto fncontext = ctx->peek_fn (); + translated + = ctx->get_backend ()->call_expression (fncontext.fndecl, fn, args, + nullptr, expr.get_locus ()); + } + else + { + Btype *type = ResolvePathType::Compile (expr.get_fnexpr (), ctx); + if (type == nullptr) + { + rust_fatal_error (expr.get_locus (), + "failed to lookup type associated with call"); + return; + } + + // 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; + expr.iterate_params ([&] (HIR::Expr *argument) mutable -> bool { + Bexpression *e = CompileExpr::Compile (argument, ctx); + vals.push_back (e); + return true; + }); + + translated + = ctx->get_backend ()->constructor_expression (type, vals, + expr.get_locus ()); + } +} + // rust-compile-block.h void |