aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-02-11 10:29:35 +0000
committerGitHub <noreply@github.com>2022-02-11 10:29:35 +0000
commit9023bb8687347559702340c24cd72301f0656a59 (patch)
treed4059dbe453be902ebd68d4704160db6942bb1b3 /gcc
parente2823b6747b36362387e5b26e8792f06185d7de8 (diff)
parent4242d45d3d6590630ea88909ed996b8cf2640d35 (diff)
downloadgcc-9023bb8687347559702340c24cd72301f0656a59.zip
gcc-9023bb8687347559702340c24cd72301f0656a59.tar.gz
gcc-9023bb8687347559702340c24cd72301f0656a59.tar.bz2
Merge #918
918: Refactor code to reuse a canonical way to compile items r=philberty a=philberty This is a big cleanup so all paths that compile functions and constants end up in the same path so we avoid any duplication in how we actually compile a function or constant. Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/Make-lang.in2
-rw-r--r--gcc/rust/backend/rust-compile-base.cc298
-rw-r--r--gcc/rust/backend/rust-compile-base.h31
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc15
-rw-r--r--gcc/rust/backend/rust-compile-expr.h8
-rw-r--r--gcc/rust/backend/rust-compile-extern.h6
-rw-r--r--gcc/rust/backend/rust-compile-implitem.cc102
-rw-r--r--gcc/rust/backend/rust-compile-implitem.h511
-rw-r--r--gcc/rust/backend/rust-compile-intrinsic.cc2
-rw-r--r--gcc/rust/backend/rust-compile-item.cc202
-rw-r--r--gcc/rust/backend/rust-compile-item.h370
-rw-r--r--gcc/rust/backend/rust-compile-resolve-path.cc24
-rw-r--r--gcc/rust/backend/rust-compile-resolve-path.h2
-rw-r--r--gcc/rust/backend/rust-compile-stmt.h28
-rw-r--r--gcc/rust/backend/rust-compile-var-decl.h3
-rw-r--r--gcc/rust/backend/rust-compile.cc100
-rw-r--r--gcc/rust/hir/tree/rust-hir-item.h16
-rw-r--r--gcc/rust/rust-backend.h54
-rw-r--r--gcc/rust/rust-gcc.cc197
-rw-r--r--gcc/rust/rust-session-manager.cc31
-rw-r--r--gcc/rust/rust-target.h9
-rw-r--r--gcc/rust/typecheck/rust-tycheck-dump.h3
22 files changed, 768 insertions, 1246 deletions
diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 660ed23..4f76216 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -96,6 +96,8 @@ GRS_OBJS = \
rust/rust-compile-intrinsic.o \
rust/rust-compile-pattern.o \
rust/rust-base62.o \
+ rust/rust-compile-item.o \
+ rust/rust-compile-implitem.o \
rust/rust-compile-expr.o \
rust/rust-compile-type.o \
rust/rust-constexpr.o \
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 81598c4..9f936d2 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -17,6 +17,11 @@
// <http://www.gnu.org/licenses/>.
#include "rust-compile-base.h"
+#include "rust-compile-item.h"
+#include "rust-compile-stmt.h"
+#include "rust-compile-fnparam.h"
+#include "rust-compile-var-decl.h"
+
#include "fold-const.h"
#include "stringpool.h"
@@ -169,5 +174,298 @@ HIRCompileBase::address_expression (tree expr, Location location)
return build_fold_addr_expr_loc (location.gcc_location (), expr);
}
+std::vector<Bvariable *>
+HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib,
+ tree fndecl)
+{
+ std::vector<Bvariable *> locals;
+ rib.iterate_decls ([&] (NodeId n, Location) mutable -> bool {
+ Resolver::Definition d;
+ bool ok = ctx->get_resolver ()->lookup_definition (n, &d);
+ rust_assert (ok);
+
+ HIR::Stmt *decl = nullptr;
+ ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
+ rust_assert (ok);
+
+ // if its a function we extract this out side of this fn context
+ // and it is not a local to this function
+ bool is_item = ctx->get_mappings ()->lookup_hir_item (
+ decl->get_mappings ().get_crate_num (),
+ decl->get_mappings ().get_hirid ())
+ != nullptr;
+ if (is_item)
+ {
+ HIR::Item *item = static_cast<HIR::Item *> (decl);
+ CompileItem::compile (item, ctx);
+ return true;
+ }
+
+ Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
+ locals.push_back (compiled);
+
+ return true;
+ });
+
+ return locals;
+}
+
+void
+HIRCompileBase::compile_function_body (Context *ctx, tree fndecl,
+ HIR::BlockExpr &function_body,
+ bool has_return_type)
+{
+ for (auto &s : function_body.get_statements ())
+ {
+ auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
+ if (compiled_expr != nullptr)
+ {
+ tree compiled_stmt
+ = ctx->get_backend ()->expression_statement (fndecl, compiled_expr);
+ ctx->add_statement (compiled_stmt);
+ }
+ }
+
+ if (function_body.has_expr ())
+ {
+ // the previous passes will ensure this is a valid return
+ // or a valid trailing expression
+ tree compiled_expr
+ = CompileExpr::Compile (function_body.expr.get (), ctx);
+
+ if (compiled_expr != nullptr)
+ {
+ if (has_return_type)
+ {
+ std::vector<tree> retstmts;
+ retstmts.push_back (compiled_expr);
+
+ auto ret = ctx->get_backend ()->return_statement (
+ fndecl, retstmts,
+ function_body.get_final_expr ()->get_locus ());
+ ctx->add_statement (ret);
+ }
+ else
+ {
+ tree final_stmt
+ = ctx->get_backend ()->expression_statement (fndecl,
+ compiled_expr);
+ ctx->add_statement (final_stmt);
+ }
+ }
+ }
+}
+
+tree
+HIRCompileBase::compile_function (
+ Context *ctx, const std::string &fn_name, HIR::SelfParam &self_param,
+ std::vector<HIR::FunctionParam> &function_params,
+ const HIR::FunctionQualifiers &qualifiers, HIR::Visibility &visibility,
+ AST::AttrVec &outer_attrs, Location locus, HIR::BlockExpr *function_body,
+ const Resolver::CanonicalPath *canonical_path, TyTy::FnType *fntype,
+ bool function_has_return)
+{
+ tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
+ std::string ir_symbol_name
+ = 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;
+ std::string asm_name = fn_name;
+ if (!is_main_fn)
+ asm_name = ctx->mangle_item (fntype, *canonical_path);
+
+ unsigned int flags = 0;
+ tree fndecl = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name,
+ asm_name, flags, locus);
+ setup_attributes_on_fndecl (fndecl, is_main_fn, !visibility.is_error (),
+ qualifiers, outer_attrs);
+ setup_abi_options (fndecl, fntype->get_abi ());
+
+ // insert into the context
+ ctx->insert_function_decl (fntype, fndecl);
+
+ // setup the params
+ TyTy::BaseType *tyret = fntype->get_return_type ();
+ std::vector<Bvariable *> param_vars;
+ if (!self_param.is_error ())
+ {
+ 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 ());
+
+ param_vars.push_back (compiled_self_param);
+ 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 ();
+ 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 compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty);
+
+ Location param_locus = referenced_param.get_locus ();
+ Bvariable *compiled_param_var
+ = CompileFnParam::compile (ctx, fndecl, &referenced_param,
+ compiled_param_type, param_locus);
+
+ param_vars.push_back (compiled_param_var);
+ ctx->insert_var_decl (referenced_param.get_mappings ().get_hirid (),
+ compiled_param_var);
+ }
+
+ if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
+ return error_mark_node;
+
+ // lookup locals
+ auto body_mappings = function_body->get_mappings ();
+ Resolver::Rib *rib = nullptr;
+ bool ok
+ = ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (), &rib);
+ rust_assert (ok);
+
+ std::vector<Bvariable *> locals
+ = compile_locals_for_block (ctx, *rib, fndecl);
+
+ tree enclosing_scope = NULL_TREE;
+ Location start_location = function_body->get_locus ();
+ Location end_location = function_body->get_end_locus ();
+
+ tree code_block = ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
+ start_location, end_location);
+ ctx->push_block (code_block);
+
+ Bvariable *return_address = nullptr;
+ if (function_has_return)
+ {
+ tree return_type = TyTyResolveCompile::compile (ctx, tyret);
+
+ bool address_is_taken = false;
+ tree ret_var_stmt = NULL_TREE;
+
+ return_address
+ = ctx->get_backend ()->temporary_variable (fndecl, code_block,
+ return_type, NULL,
+ address_is_taken, locus,
+ &ret_var_stmt);
+
+ ctx->add_statement (ret_var_stmt);
+ }
+
+ ctx->push_fn (fndecl, return_address);
+ compile_function_body (ctx, fndecl, *function_body, function_has_return);
+ tree bind_tree = ctx->pop_block ();
+
+ gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
+ DECL_SAVED_TREE (fndecl) = bind_tree;
+
+ ctx->pop_fn ();
+ ctx->push_function (fndecl);
+
+ return fndecl;
+}
+
+tree
+HIRCompileBase::compile_constant_item (
+ Context *ctx, TyTy::BaseType *resolved_type,
+ const Resolver::CanonicalPath *canonical_path, HIR::Expr *const_value_expr,
+ Location 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);
+
+ bool is_block_expr
+ = const_value_expr->get_expression_type () == HIR::Expr::ExprType::Block;
+
+ // compile the expression
+ tree folded_expr = error_mark_node;
+ if (!is_block_expr)
+ {
+ tree value = CompileExpr::Compile (const_value_expr, ctx);
+ folded_expr = ConstCtx::fold (value);
+ }
+ else
+ {
+ // 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
+ // make a _fake_ function with a block so it can hold onto temps then
+ // use our constexpr code to fold it completely or error_mark_node
+ Backend::typed_identifier receiver;
+ tree compiled_fn_type = ctx->get_backend ()->function_type (
+ receiver, {}, {Backend::typed_identifier ("_", const_type, locus)},
+ NULL, locus);
+
+ tree fndecl
+ = ctx->get_backend ()->function (compiled_fn_type, ident, "", 0, locus);
+ TREE_READONLY (fndecl) = 1;
+
+ tree enclosing_scope = NULL_TREE;
+ HIR::BlockExpr *function_body
+ = static_cast<HIR::BlockExpr *> (const_value_expr);
+ Location start_location = function_body->get_locus ();
+ Location end_location = function_body->get_end_locus ();
+
+ tree code_block
+ = ctx->get_backend ()->block (fndecl, enclosing_scope, {},
+ start_location, end_location);
+ ctx->push_block (code_block);
+
+ bool address_is_taken = false;
+ tree ret_var_stmt = NULL_TREE;
+ Bvariable *return_address
+ = ctx->get_backend ()->temporary_variable (fndecl, code_block,
+ const_type, NULL,
+ address_is_taken, locus,
+ &ret_var_stmt);
+
+ ctx->add_statement (ret_var_stmt);
+ ctx->push_fn (fndecl, return_address);
+
+ compile_function_body (ctx, fndecl, *function_body, true);
+ tree bind_tree = ctx->pop_block ();
+
+ gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
+ DECL_SAVED_TREE (fndecl) = bind_tree;
+
+ ctx->pop_fn ();
+
+ // lets fold it into a call expr
+ tree call = build_call_array_loc (locus.gcc_location (), const_type,
+ fndecl, 0, NULL);
+ folded_expr = ConstCtx::fold (call);
+ }
+
+ return named_constant_expression (const_type, ident, folded_expr, locus);
+}
+
+tree
+HIRCompileBase::named_constant_expression (tree type_tree,
+ const std::string &name,
+ tree const_val, Location location)
+{
+ if (type_tree == error_mark_node || const_val == error_mark_node)
+ return error_mark_node;
+
+ tree name_tree = get_identifier_with_length (name.data (), name.length ());
+ tree decl
+ = build_decl (location.gcc_location (), CONST_DECL, name_tree, type_tree);
+ DECL_INITIAL (decl) = const_val;
+ TREE_CONSTANT (decl) = 1;
+ TREE_READONLY (decl) = 1;
+
+ rust_preserve_from_gc (decl);
+ return decl;
+}
+
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h
index ec75356..a52886c 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -35,14 +35,9 @@ protected:
Context *ctx;
+protected:
Context *get_context () { return ctx; }
- void compile_function_body (tree fndecl, HIR::BlockExpr &function_body,
- bool has_return_type);
-
- bool compile_locals_for_block (Resolver::Rib &rib, tree fndecl,
- std::vector<Bvariable *> &locals);
-
tree coercion_site (tree rvalue, TyTy::BaseType *actual,
TyTy::BaseType *expected, Location lvalue_locus,
Location rvalue_locus);
@@ -81,6 +76,30 @@ protected:
static tree address_expression (tree, Location);
static bool mark_addressable (tree, Location);
+
+ static std::vector<Bvariable *>
+ compile_locals_for_block (Context *ctx, Resolver::Rib &rib, tree fndecl);
+
+ static void compile_function_body (Context *ctx, tree fndecl,
+ HIR::BlockExpr &function_body,
+ bool has_return_type);
+
+ static tree compile_function (
+ Context *ctx, const std::string &fn_name, HIR::SelfParam &self_param,
+ std::vector<HIR::FunctionParam> &function_params,
+ const HIR::FunctionQualifiers &qualifiers, HIR::Visibility &visibility,
+ AST::AttrVec &outer_attrs, Location locus, HIR::BlockExpr *function_body,
+ const Resolver::CanonicalPath *canonical_path, TyTy::FnType *fntype,
+ bool function_has_return);
+
+ static tree
+ compile_constant_item (Context *ctx, TyTy::BaseType *resolved_type,
+ const Resolver::CanonicalPath *canonical_path,
+ HIR::Expr *const_value_expr, Location locus);
+
+ static tree named_constant_expression (tree type_tree,
+ const std::string &name,
+ tree const_val, Location location);
};
} // namespace Compile
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index f65e1fd..a592e35 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -18,6 +18,7 @@
#include "rust-compile.h"
#include "rust-compile-item.h"
+#include "rust-compile-implitem.h"
#include "rust-compile-expr.h"
#include "rust-compile-struct-field-expr.h"
#include "rust-hir-trait-resolve.h"
@@ -204,7 +205,7 @@ CompileExpr::visit (HIR::MatchExpr &expr)
expr.get_scrutinee_expr ()->get_mappings ().get_hirid (),
&scrutinee_expr_tyty))
{
- translated = ctx->get_backend ()->error_expression ();
+ translated = error_mark_node;
return;
}
@@ -220,7 +221,7 @@ CompileExpr::visit (HIR::MatchExpr &expr)
if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
&expr_tyty))
{
- translated = ctx->get_backend ()->error_expression ();
+ translated = error_mark_node;
return;
}
@@ -643,7 +644,7 @@ CompileExpr::compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn,
}
if (ref == nullptr)
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
// get any indirection sorted out
if (receiver->get_kind () == TyTy::TypeKind::REF)
@@ -713,7 +714,7 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
tree fn = NULL_TREE;
if (ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
{
- return ctx->get_backend ()->function_code_expression (fn, expr_locus);
+ return address_expression (fn, expr_locus);
}
// Now we can try and resolve the address since this might be a forward
@@ -765,8 +766,7 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
// contain an implementation we should actually return
// error_mark_node
- return CompileTraitItem::Compile (receiver,
- trait_item_ref->get_hir_trait_item (),
+ return CompileTraitItem::Compile (trait_item_ref->get_hir_trait_item (),
ctx, fntype, true, expr_locus);
}
else
@@ -1343,8 +1343,7 @@ CompileExpr::visit (HIR::IdentifierExpr &expr)
}
else if (ctx->lookup_function_decl (ref, &fn))
{
- translated
- = ctx->get_backend ()->function_code_expression (fn, expr.get_locus ());
+ translated = address_expression (fn, expr.get_locus ());
}
else if (ctx->lookup_var_decl (ref, &var))
{
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index 592d280..2fee3be 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -383,8 +383,8 @@ public:
ctx->add_statement (ret_var_stmt);
}
- auto code_block = CompileBlock::compile (&expr, ctx, tmp);
- auto block_stmt = ctx->get_backend ()->block_statement (code_block);
+ auto block_stmt = CompileBlock::compile (&expr, ctx, tmp);
+ rust_assert (TREE_CODE (block_stmt) == BIND_EXPR);
ctx->add_statement (block_stmt);
if (tmp != NULL)
@@ -680,9 +680,9 @@ public:
= ctx->get_backend ()->expression_statement (fnctx.fndecl, exit_expr);
ctx->add_statement (break_stmt);
- tree code_block
+ tree code_block_stmt
= CompileBlock::compile (expr.get_loop_block ().get (), ctx, nullptr);
- tree code_block_stmt = ctx->get_backend ()->block_statement (code_block);
+ rust_assert (TREE_CODE (code_block_stmt) == BIND_EXPR);
ctx->add_statement (code_block_stmt);
ctx->pop_loop_begin_label ();
diff --git a/gcc/rust/backend/rust-compile-extern.h b/gcc/rust/backend/rust-compile-extern.h
index 143b240..1412e7a 100644
--- a/gcc/rust/backend/rust-compile-extern.h
+++ b/gcc/rust/backend/rust-compile-extern.h
@@ -21,12 +21,6 @@
#include "rust-compile-base.h"
#include "rust-compile-intrinsic.h"
-#include "rust-compile-tyty.h"
-#include "rust-compile-implitem.h"
-#include "rust-compile-var-decl.h"
-#include "rust-compile-stmt.h"
-#include "rust-compile-expr.h"
-#include "rust-compile-fnparam.h"
namespace Rust {
namespace Compile {
diff --git a/gcc/rust/backend/rust-compile-implitem.cc b/gcc/rust/backend/rust-compile-implitem.cc
new file mode 100644
index 0000000..8dc18d3
--- /dev/null
+++ b/gcc/rust/backend/rust-compile-implitem.cc
@@ -0,0 +1,102 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-compile-implitem.h"
+#include "rust-compile-expr.h"
+#include "rust-compile-fnparam.h"
+
+namespace Rust {
+namespace Compile {
+
+void
+CompileTraitItem::visit (HIR::TraitItemConst &constant)
+{
+ rust_assert (concrete != nullptr);
+ TyTy::BaseType *resolved_type = concrete;
+
+ const Resolver::CanonicalPath *canonical_path = nullptr;
+ bool ok = ctx->get_mappings ()->lookup_canonical_path (
+ constant.get_mappings ().get_crate_num (),
+ constant.get_mappings ().get_nodeid (), &canonical_path);
+ rust_assert (ok);
+
+ HIR::Expr *const_value_expr = constant.get_expr ().get ();
+ tree const_expr
+ = compile_constant_item (ctx, resolved_type, canonical_path,
+ const_value_expr, constant.get_locus ());
+ ctx->push_const (const_expr);
+ ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
+
+ reference = const_expr;
+}
+
+void
+CompileTraitItem::visit (HIR::TraitItemFunc &func)
+{
+ rust_assert (func.has_block_defined ());
+
+ rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
+ TyTy::FnType *fntype = static_cast<TyTy::FnType *> (concrete);
+
+ // items can be forward compiled which means we may not need to invoke this
+ // code. We might also have already compiled this generic function as well.
+ tree lookup = NULL_TREE;
+ if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
+ fntype->get_id (), fntype))
+ {
+ // has this been added to the list then it must be finished
+ if (ctx->function_completed (lookup))
+ {
+ tree dummy = NULL_TREE;
+ if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
+ {
+ ctx->insert_function_decl (fntype, lookup);
+ }
+ reference = address_expression (lookup, ref_locus);
+ return;
+ }
+ }
+
+ if (fntype->has_subsititions_defined ())
+ {
+ // override the Hir Lookups for the substituions in this context
+ fntype->override_context ();
+ }
+
+ const Resolver::CanonicalPath *canonical_path = nullptr;
+ bool ok = ctx->get_mappings ()->lookup_canonical_path (
+ func.get_mappings ().get_crate_num (), func.get_mappings ().get_nodeid (),
+ &canonical_path);
+ rust_assert (ok);
+
+ // FIXME
+ HIR::Visibility vis (HIR::Visibility::PublicVisType::NONE,
+ AST::SimplePath::create_empty ());
+ HIR::TraitFunctionDecl &function = func.get_decl ();
+ tree fndecl
+ = compile_function (ctx, function.get_function_name (),
+ function.get_self (), function.get_function_params (),
+ function.get_qualifiers (), vis,
+ func.get_outer_attrs (), func.get_locus (),
+ func.get_block_expr ().get (), canonical_path, fntype,
+ function.has_return_type ());
+ reference = address_expression (fndecl, ref_locus);
+}
+
+} // namespace Compile
+} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-implitem.h b/gcc/rust/backend/rust-compile-implitem.h
index 9320276..4d49c0b 100644
--- a/gcc/rust/backend/rust-compile-implitem.h
+++ b/gcc/rust/backend/rust-compile-implitem.h
@@ -19,19 +19,17 @@
#ifndef RUST_COMPILE_IMPLITEM_H
#define RUST_COMPILE_IMPLITEM_H
-#include "rust-compile-base.h"
-#include "rust-compile-tyty.h"
-#include "rust-compile-var-decl.h"
-#include "rust-compile-stmt.h"
+#include "rust-compile-item.h"
#include "rust-compile-expr.h"
#include "rust-compile-fnparam.h"
namespace Rust {
namespace Compile {
-class CompileInherentImplItem : public HIRCompileBase
+// this is a proxy for HIR::ImplItem's back to use the normel HIR::Item path
+class CompileInherentImplItem : public CompileItem
{
- using Rust::Compile::HIRCompileBase::visit;
+ using Rust::Compile::CompileItem::visit;
public:
static tree Compile (HIR::ImplItem *item, Context *ctx,
@@ -42,273 +40,18 @@ public:
CompileInherentImplItem compiler (ctx, concrete, ref_locus);
item->accept_vis (compiler);
- if (is_query_mode
- && ctx->get_backend ()->is_error_expression (compiler.reference))
+ if (is_query_mode && compiler.reference == error_mark_node)
rust_internal_error_at (ref_locus, "failed to compile impl item: %s",
item->as_string ().c_str ());
return compiler.reference;
}
- void visit (HIR::ConstantItem &constant) override
- {
- TyTy::BaseType *resolved_type = nullptr;
- bool ok
- = ctx->get_tyctx ()->lookup_type (constant.get_mappings ().get_hirid (),
- &resolved_type);
- rust_assert (ok);
-
- tree type = TyTyResolveCompile::compile (ctx, resolved_type);
- tree value = CompileExpr::Compile (constant.get_expr (), ctx);
-
- const Resolver::CanonicalPath *canonical_path = nullptr;
- ok = ctx->get_mappings ()->lookup_canonical_path (
- constant.get_mappings ().get_crate_num (),
- constant.get_mappings ().get_nodeid (), &canonical_path);
- rust_assert (ok);
-
- std::string ident = canonical_path->get ();
- tree const_expr = ctx->get_backend ()->named_constant_expression (
- type, constant.get_identifier (), value, constant.get_locus ());
-
- ctx->push_const (const_expr);
- ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
-
- reference = const_expr;
- }
-
- void visit (HIR::Function &function) override
- {
- TyTy::BaseType *fntype_tyty;
- if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
- &fntype_tyty))
- {
- rust_fatal_error (function.get_locus (),
- "failed to lookup function type");
- return;
- }
-
- rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
- TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
- if (fntype->has_subsititions_defined ())
- {
- // we cant do anything for this only when it is used and a concrete type
- // is given
- if (concrete == nullptr)
- return;
- else
- {
- rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
- fntype = static_cast<TyTy::FnType *> (concrete);
- }
- }
-
- // items can be forward compiled which means we may not need to invoke this
- // code. We might also have already compiled this generic function as well.
- tree lookup = NULL_TREE;
- if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
- fntype->get_id (), fntype))
- {
- // has this been added to the list then it must be finished
- if (ctx->function_completed (lookup))
- {
- tree dummy = NULL_TREE;
- if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
- {
- ctx->insert_function_decl (fntype, lookup);
- }
- reference
- = ctx->get_backend ()->function_code_expression (lookup,
- ref_locus);
- return;
- }
- }
-
- if (fntype->has_subsititions_defined ())
- {
- // override the Hir Lookups for the substituions in this context
- fntype->override_context ();
- }
-
- // convert to the actual function type
- tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
-
- const Resolver::CanonicalPath *canonical_path = nullptr;
- bool ok = ctx->get_mappings ()->lookup_canonical_path (
- function.get_mappings ().get_crate_num (),
- function.get_mappings ().get_nodeid (), &canonical_path);
- rust_assert (ok);
-
- std::string ir_symbol_name
- = canonical_path->get () + fntype->subst_as_string ();
- std::string asm_name = ctx->mangle_item (fntype, *canonical_path);
-
- unsigned int flags = 0;
- tree fndecl
- = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name,
- asm_name, flags, function.get_locus ());
- setup_attributes_on_fndecl (fndecl, false, function.has_visibility (),
- function.get_qualifiers (),
- function.get_outer_attrs ());
- ctx->insert_function_decl (fntype, fndecl);
-
- // setup the params
- TyTy::BaseType *tyret = fntype->get_return_type ();
- std::vector<Bvariable *> param_vars;
-
- if (function.is_method ())
- {
- // insert self
- TyTy::BaseType *self_tyty_lookup = nullptr;
- if (!ctx->get_tyctx ()->lookup_type (
- function.get_self_param ().get_mappings ().get_hirid (),
- &self_tyty_lookup))
- {
- rust_error_at (function.get_self_param ().get_locus (),
- "failed to lookup self param type");
- return;
- }
-
- tree self_type = TyTyResolveCompile::compile (ctx, self_tyty_lookup);
- if (self_type == nullptr)
- {
- rust_error_at (function.get_self_param ().get_locus (),
- "failed to compile self param type");
- return;
- }
-
- Bvariable *compiled_self_param
- = CompileSelfParam::compile (ctx, fndecl, function.get_self_param (),
- self_type,
- function.get_self_param ().get_locus ());
- if (compiled_self_param == nullptr)
- {
- rust_error_at (function.get_self_param ().get_locus (),
- "failed to compile self param variable");
- return;
- }
-
- param_vars.push_back (compiled_self_param);
- ctx->insert_var_decl (
- function.get_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
- size_t i = function.is_method () ? 1 : 0;
- for (auto referenced_param : function.get_function_params ())
- {
- auto tyty_param = fntype->param_at (i);
- auto param_tyty = tyty_param.second;
-
- auto compiled_param_type
- = TyTyResolveCompile::compile (ctx, param_tyty);
- if (compiled_param_type == nullptr)
- {
- rust_error_at (referenced_param.get_locus (),
- "failed to compile parameter type");
- return;
- }
-
- Location param_locus
- = ctx->get_mappings ()->lookup_location (param_tyty->get_ref ());
- Bvariable *compiled_param_var
- = CompileFnParam::compile (ctx, fndecl, &referenced_param,
- compiled_param_type, param_locus);
- if (compiled_param_var == nullptr)
- {
- rust_error_at (param_locus, "Failed to compile parameter variable");
- return;
- }
-
- param_vars.push_back (compiled_param_var);
-
- ctx->insert_var_decl (referenced_param.get_mappings ().get_hirid (),
- compiled_param_var);
- i++;
- }
-
- if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
- {
- rust_fatal_error (function.get_locus (),
- "failed to setup parameter variables");
- return;
- }
-
- // lookup locals
- auto block_expr = function.get_definition ().get ();
- auto body_mappings = block_expr->get_mappings ();
-
- Resolver::Rib *rib = nullptr;
- if (!ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
- &rib))
- {
- rust_fatal_error (function.get_locus (),
- "failed to setup locals per block");
- return;
- }
-
- std::vector<Bvariable *> locals;
- ok = compile_locals_for_block (*rib, fndecl, locals);
- rust_assert (ok);
-
- tree enclosing_scope = NULL_TREE;
- HIR::BlockExpr *function_body = function.get_definition ().get ();
- Location start_location = function_body->get_locus ();
- Location end_location = function_body->get_end_locus ();
-
- tree code_block
- = ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
- start_location, end_location);
- ctx->push_block (code_block);
-
- Bvariable *return_address = nullptr;
- if (function.has_function_return_type ())
- {
- tree return_type = TyTyResolveCompile::compile (ctx, tyret);
-
- bool address_is_taken = false;
- tree ret_var_stmt = NULL_TREE;
-
- return_address = ctx->get_backend ()->temporary_variable (
- fndecl, code_block, return_type, NULL, address_is_taken,
- function.get_locus (), &ret_var_stmt);
-
- ctx->add_statement (ret_var_stmt);
- }
-
- ctx->push_fn (fndecl, return_address);
-
- compile_function_body (fndecl, *function.get_definition ().get (),
- function.has_function_return_type ());
-
- ctx->pop_block ();
- auto body = ctx->get_backend ()->block_statement (code_block);
- if (!ctx->get_backend ()->function_set_body (fndecl, body))
- {
- rust_error_at (function.get_locus (), "failed to set body to function");
- return;
- }
-
- ctx->pop_fn ();
- ctx->push_function (fndecl);
-
- reference
- = ctx->get_backend ()->function_code_expression (fndecl, ref_locus);
- }
-
private:
CompileInherentImplItem (Context *ctx, TyTy::BaseType *concrete,
Location ref_locus)
- : HIRCompileBase (ctx), concrete (concrete),
- reference (ctx->get_backend ()->error_expression ()),
- ref_locus (ref_locus)
+ : CompileItem (ctx, concrete, ref_locus)
{}
-
- TyTy::BaseType *concrete;
- tree reference;
- Location ref_locus;
};
class CompileTraitItem : public HIRCompileBase
@@ -316,256 +59,30 @@ class CompileTraitItem : public HIRCompileBase
using Rust::Compile::HIRCompileBase::visit;
public:
- static tree Compile (const TyTy::BaseType *self, HIR::TraitItem *item,
- Context *ctx, TyTy::BaseType *concrete,
- bool is_query_mode = false,
+ static tree Compile (HIR::TraitItem *item, Context *ctx,
+ TyTy::BaseType *concrete, bool is_query_mode = false,
Location ref_locus = Location ())
{
- CompileTraitItem compiler (self, ctx, concrete, ref_locus);
+ CompileTraitItem compiler (ctx, concrete, ref_locus);
item->accept_vis (compiler);
- if (is_query_mode
- && ctx->get_backend ()->is_error_expression (compiler.reference))
+ if (is_query_mode && compiler.reference == error_mark_node)
rust_internal_error_at (ref_locus, "failed to compile trait item: %s",
item->as_string ().c_str ());
return compiler.reference;
}
- void visit (HIR::TraitItemConst &constant) override
- {
- rust_assert (concrete != nullptr);
- TyTy::BaseType *resolved_type = concrete;
-
- tree type = TyTyResolveCompile::compile (ctx, resolved_type);
- tree value = CompileExpr::Compile (constant.get_expr ().get (), ctx);
-
- const Resolver::CanonicalPath *canonical_path = nullptr;
- bool ok = ctx->get_mappings ()->lookup_canonical_path (
- constant.get_mappings ().get_crate_num (),
- constant.get_mappings ().get_nodeid (), &canonical_path);
- rust_assert (ok);
-
- std::string ident = canonical_path->get ();
- tree const_expr = ctx->get_backend ()->named_constant_expression (
- type, constant.get_name (), value, constant.get_locus ());
-
- ctx->push_const (const_expr);
- ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
-
- reference = const_expr;
- }
-
- void visit (HIR::TraitItemFunc &func) override
- {
- rust_assert (func.has_block_defined ());
-
- rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
- TyTy::FnType *fntype = static_cast<TyTy::FnType *> (concrete);
-
- // items can be forward compiled which means we may not need to invoke this
- // code. We might also have already compiled this generic function as well.
- tree lookup = NULL_TREE;
- if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
- fntype->get_id (), fntype))
- {
- // has this been added to the list then it must be finished
- if (ctx->function_completed (lookup))
- {
- tree dummy = NULL_TREE;
- if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
- {
- ctx->insert_function_decl (fntype, lookup);
- }
- reference
- = ctx->get_backend ()->function_code_expression (lookup,
- ref_locus);
- return;
- }
- }
-
- if (fntype->has_subsititions_defined ())
- {
- // override the Hir Lookups for the substituions in this context
- fntype->override_context ();
- }
-
- // convert to the actual function type
- tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
- HIR::TraitFunctionDecl &function = func.get_decl ();
-
- const Resolver::CanonicalPath *canonical_path = nullptr;
- bool ok = ctx->get_mappings ()->lookup_canonical_path (
- func.get_mappings ().get_crate_num (), func.get_mappings ().get_nodeid (),
- &canonical_path);
- rust_assert (ok);
-
- std::string fn_identifier = canonical_path->get ();
- std::string asm_name = ctx->mangle_item (fntype, *canonical_path);
-
- unsigned int flags = 0;
- tree fndecl
- = ctx->get_backend ()->function (compiled_fn_type, fn_identifier,
- asm_name, flags, func.get_locus ());
- setup_attributes_on_fndecl (fndecl, false, false,
- func.get_decl ().get_qualifiers (),
- func.get_outer_attrs ());
- ctx->insert_function_decl (fntype, fndecl);
-
- // setup the params
- TyTy::BaseType *tyret = fntype->get_return_type ();
- std::vector<Bvariable *> param_vars;
-
- if (function.is_method ())
- {
- // insert self
- TyTy::BaseType *self_tyty_lookup = nullptr;
- if (!ctx->get_tyctx ()->lookup_type (
- function.get_self ().get_mappings ().get_hirid (),
- &self_tyty_lookup))
- {
- rust_error_at (function.get_self ().get_locus (),
- "failed to lookup self param type");
- return;
- }
-
- tree self_type = TyTyResolveCompile::compile (ctx, self_tyty_lookup);
- if (self_type == nullptr)
- {
- rust_error_at (function.get_self ().get_locus (),
- "failed to compile self param type");
- return;
- }
+ void visit (HIR::TraitItemConst &constant) override;
- Bvariable *compiled_self_param
- = CompileSelfParam::compile (ctx, fndecl, function.get_self (),
- self_type,
- function.get_self ().get_locus ());
- if (compiled_self_param == nullptr)
- {
- rust_error_at (function.get_self ().get_locus (),
- "failed to compile self param variable");
- return;
- }
-
- param_vars.push_back (compiled_self_param);
- ctx->insert_var_decl (function.get_self ().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
- size_t i = function.is_method () ? 1 : 0;
- for (auto referenced_param : function.get_function_params ())
- {
- auto tyty_param = fntype->param_at (i);
- auto param_tyty = tyty_param.second;
-
- auto compiled_param_type
- = TyTyResolveCompile::compile (ctx, param_tyty);
- if (compiled_param_type == nullptr)
- {
- rust_error_at (referenced_param.get_locus (),
- "failed to compile parameter type");
- return;
- }
-
- Location param_locus
- = ctx->get_mappings ()->lookup_location (param_tyty->get_ref ());
- Bvariable *compiled_param_var
- = CompileFnParam::compile (ctx, fndecl, &referenced_param,
- compiled_param_type, param_locus);
- if (compiled_param_var == nullptr)
- {
- rust_error_at (param_locus, "Failed to compile parameter variable");
- return;
- }
-
- param_vars.push_back (compiled_param_var);
-
- ctx->insert_var_decl (referenced_param.get_mappings ().get_hirid (),
- compiled_param_var);
- i++;
- }
-
- if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
- {
- rust_fatal_error (func.get_locus (),
- "failed to setup parameter variables");
- return;
- }
-
- // lookup locals
- auto block_expr = func.get_block_expr ().get ();
- auto body_mappings = block_expr->get_mappings ();
-
- Resolver::Rib *rib = nullptr;
- if (!ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
- &rib))
- {
- rust_fatal_error (func.get_locus (),
- "failed to setup locals per block");
- return;
- }
-
- std::vector<Bvariable *> locals;
- ok = compile_locals_for_block (*rib, fndecl, locals);
- rust_assert (ok);
-
- tree enclosing_scope = NULL_TREE;
- HIR::BlockExpr *function_body = func.get_block_expr ().get ();
- Location start_location = function_body->get_locus ();
- Location end_location = function_body->get_end_locus ();
-
- tree code_block
- = ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
- start_location, end_location);
- ctx->push_block (code_block);
-
- Bvariable *return_address = nullptr;
- if (function.has_return_type ())
- {
- tree return_type = TyTyResolveCompile::compile (ctx, tyret);
-
- bool address_is_taken = false;
- tree ret_var_stmt = NULL_TREE;
-
- return_address = ctx->get_backend ()->temporary_variable (
- fndecl, code_block, return_type, NULL, address_is_taken,
- func.get_locus (), &ret_var_stmt);
-
- ctx->add_statement (ret_var_stmt);
- }
-
- ctx->push_fn (fndecl, return_address);
-
- compile_function_body (fndecl, *func.get_block_expr ().get (),
- function.has_return_type ());
-
- ctx->pop_block ();
- auto body = ctx->get_backend ()->block_statement (code_block);
- if (!ctx->get_backend ()->function_set_body (fndecl, body))
- {
- rust_error_at (func.get_locus (), "failed to set body to function");
- return;
- }
-
- ctx->pop_fn ();
- ctx->push_function (fndecl);
-
- reference
- = ctx->get_backend ()->function_code_expression (fndecl, ref_locus);
- }
+ void visit (HIR::TraitItemFunc &func) override;
private:
- CompileTraitItem (const TyTy::BaseType *self, Context *ctx,
- TyTy::BaseType *concrete, Location ref_locus)
- : HIRCompileBase (ctx), self (self), concrete (concrete),
- reference (ctx->get_backend ()->error_expression ()),
+ CompileTraitItem (Context *ctx, TyTy::BaseType *concrete, Location ref_locus)
+ : HIRCompileBase (ctx), concrete (concrete), reference (error_mark_node),
ref_locus (ref_locus)
{}
- const TyTy::BaseType *self;
TyTy::BaseType *concrete;
tree reference;
Location ref_locus;
diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc
index 69626a9..8c5b073 100644
--- a/gcc/rust/backend/rust-compile-intrinsic.cc
+++ b/gcc/rust/backend/rust-compile-intrinsic.cc
@@ -85,7 +85,7 @@ Intrinsics::compile (TyTy::FnType *fntype)
Location locus = ctx->get_mappings ()->lookup_location (fntype->get_ref ());
rust_error_at (locus, "unknown builtin");
- return ctx->get_backend ()->error_function ();
+ return error_mark_node;
}
} // namespace Compile
diff --git a/gcc/rust/backend/rust-compile-item.cc b/gcc/rust/backend/rust-compile-item.cc
new file mode 100644
index 0000000..d42cc1e
--- /dev/null
+++ b/gcc/rust/backend/rust-compile-item.cc
@@ -0,0 +1,202 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-compile-item.h"
+#include "rust-compile-implitem.h"
+#include "rust-compile-expr.h"
+#include "rust-compile-extern.h"
+#include "rust-constexpr.h"
+
+namespace Rust {
+namespace Compile {
+
+void
+CompileItem::visit (HIR::StaticItem &var)
+{
+ // have we already compiled this?
+ Bvariable *static_decl_ref = nullptr;
+ if (ctx->lookup_var_decl (var.get_mappings ().get_hirid (), &static_decl_ref))
+ {
+ reference
+ = ctx->get_backend ()->var_expression (static_decl_ref, ref_locus);
+ return;
+ }
+
+ TyTy::BaseType *resolved_type = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (),
+ &resolved_type);
+ rust_assert (ok);
+
+ tree type = TyTyResolveCompile::compile (ctx, resolved_type);
+ tree value = CompileExpr::Compile (var.get_expr (), ctx);
+
+ const Resolver::CanonicalPath *canonical_path = nullptr;
+ ok = ctx->get_mappings ()->lookup_canonical_path (
+ var.get_mappings ().get_crate_num (), var.get_mappings ().get_nodeid (),
+ &canonical_path);
+ rust_assert (ok);
+
+ std::string name = canonical_path->get ();
+ std::string asm_name = ctx->mangle_item (resolved_type, *canonical_path);
+
+ bool is_external = false;
+ bool is_hidden = false;
+ bool in_unique_section = true;
+
+ Bvariable *static_global
+ = ctx->get_backend ()->global_variable (name, asm_name, type, is_external,
+ is_hidden, in_unique_section,
+ var.get_locus ());
+ ctx->get_backend ()->global_variable_set_init (static_global, value);
+
+ ctx->insert_var_decl (var.get_mappings ().get_hirid (), static_global);
+ ctx->push_var (static_global);
+
+ reference = ctx->get_backend ()->var_expression (static_global, ref_locus);
+}
+
+void
+CompileItem::visit (HIR::ConstantItem &constant)
+{
+ // resolve the type
+ TyTy::BaseType *resolved_type = nullptr;
+ bool ok
+ = ctx->get_tyctx ()->lookup_type (constant.get_mappings ().get_hirid (),
+ &resolved_type);
+ rust_assert (ok);
+
+ // canonical path
+ const Resolver::CanonicalPath *canonical_path = nullptr;
+ ok = ctx->get_mappings ()->lookup_canonical_path (
+ constant.get_mappings ().get_crate_num (),
+ constant.get_mappings ().get_nodeid (), &canonical_path);
+ rust_assert (ok);
+
+ HIR::Expr *const_value_expr = constant.get_expr ();
+ tree const_expr
+ = compile_constant_item (ctx, resolved_type, canonical_path,
+ const_value_expr, constant.get_locus ());
+
+ ctx->push_const (const_expr);
+ ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
+ reference = const_expr;
+}
+
+void
+CompileItem::visit (HIR::Function &function)
+{
+ TyTy::BaseType *fntype_tyty;
+ if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
+ &fntype_tyty))
+ {
+ rust_fatal_error (function.get_locus (),
+ "failed to lookup function type");
+ return;
+ }
+
+ rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
+ TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
+ if (fntype->has_subsititions_defined ())
+ {
+ // we cant do anything for this only when it is used and a concrete type
+ // is given
+ if (concrete == nullptr)
+ return;
+ else
+ {
+ rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
+ fntype = static_cast<TyTy::FnType *> (concrete);
+ }
+ }
+
+ // items can be forward compiled which means we may not need to invoke this
+ // code. We might also have already compiled this generic function as well.
+ tree lookup = NULL_TREE;
+ if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
+ fntype->get_id (), fntype))
+ {
+ // has this been added to the list then it must be finished
+ if (ctx->function_completed (lookup))
+ {
+ tree dummy = NULL_TREE;
+ if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
+ {
+ ctx->insert_function_decl (fntype, lookup);
+ }
+
+ reference = address_expression (lookup, ref_locus);
+ return;
+ }
+ }
+
+ if (fntype->has_subsititions_defined ())
+ {
+ // override the Hir Lookups for the substituions in this context
+ fntype->override_context ();
+ }
+
+ const Resolver::CanonicalPath *canonical_path = nullptr;
+ bool ok = ctx->get_mappings ()->lookup_canonical_path (
+ function.get_mappings ().get_crate_num (),
+ function.get_mappings ().get_nodeid (), &canonical_path);
+ rust_assert (ok);
+
+ tree fndecl
+ = compile_function (ctx, function.get_function_name (),
+ function.get_self_param (),
+ function.get_function_params (),
+ function.get_qualifiers (), function.get_visibility (),
+ function.get_outer_attrs (), function.get_locus (),
+ function.get_definition ().get (), canonical_path,
+ fntype, function.has_function_return_type ());
+ reference = address_expression (fndecl, ref_locus);
+}
+
+void
+CompileItem::visit (HIR::ImplBlock &impl_block)
+{
+ TyTy::BaseType *self_lookup = nullptr;
+ if (!ctx->get_tyctx ()->lookup_type (
+ impl_block.get_type ()->get_mappings ().get_hirid (), &self_lookup))
+ {
+ rust_error_at (impl_block.get_locus (), "failed to resolve type of impl");
+ return;
+ }
+
+ for (auto &impl_item : impl_block.get_impl_items ())
+ CompileInherentImplItem::Compile (impl_item.get (), ctx);
+}
+
+void
+CompileItem::visit (HIR::ExternBlock &extern_block)
+{
+ for (auto &item : extern_block.get_extern_items ())
+ {
+ CompileExternItem::compile (item.get (), ctx, concrete);
+ }
+}
+
+void
+CompileItem::visit (HIR::Module &module)
+{
+ for (auto &item : module.get_items ())
+ CompileItem::compile (item.get (), ctx);
+}
+
+} // namespace Compile
+} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h
index 70b5415..897fe85 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -20,14 +20,6 @@
#define RUST_COMPILE_ITEM
#include "rust-compile-base.h"
-#include "rust-compile-tyty.h"
-#include "rust-compile-implitem.h"
-#include "rust-compile-var-decl.h"
-#include "rust-compile-stmt.h"
-#include "rust-compile-expr.h"
-#include "rust-compile-fnparam.h"
-#include "rust-compile-extern.h"
-#include "rust-constexpr.h"
namespace Rust {
namespace Compile {
@@ -46,372 +38,24 @@ public:
CompileItem compiler (ctx, concrete, ref_locus);
item->accept_vis (compiler);
- if (is_query_mode
- && ctx->get_backend ()->is_error_expression (compiler.reference))
+ if (is_query_mode && compiler.reference == error_mark_node)
rust_internal_error_at (ref_locus, "failed to compile item: %s",
item->as_string ().c_str ());
return compiler.reference;
}
- void visit (HIR::StaticItem &var) override
- {
- // have we already compiled this?
- Bvariable *static_decl_ref = nullptr;
- if (ctx->lookup_var_decl (var.get_mappings ().get_hirid (),
- &static_decl_ref))
- {
- reference
- = ctx->get_backend ()->var_expression (static_decl_ref, ref_locus);
- return;
- }
-
- TyTy::BaseType *resolved_type = nullptr;
- bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (),
- &resolved_type);
- rust_assert (ok);
-
- tree type = TyTyResolveCompile::compile (ctx, resolved_type);
- tree value = CompileExpr::Compile (var.get_expr (), ctx);
-
- const Resolver::CanonicalPath *canonical_path = nullptr;
- ok = ctx->get_mappings ()->lookup_canonical_path (
- var.get_mappings ().get_crate_num (), var.get_mappings ().get_nodeid (),
- &canonical_path);
- rust_assert (ok);
-
- std::string name = canonical_path->get ();
- std::string asm_name = ctx->mangle_item (resolved_type, *canonical_path);
-
- bool is_external = false;
- bool is_hidden = false;
- bool in_unique_section = true;
-
- Bvariable *static_global
- = ctx->get_backend ()->global_variable (name, asm_name, type, is_external,
- is_hidden, in_unique_section,
- var.get_locus ());
- ctx->get_backend ()->global_variable_set_init (static_global, value);
-
- ctx->insert_var_decl (var.get_mappings ().get_hirid (), static_global);
- ctx->push_var (static_global);
-
- reference = ctx->get_backend ()->var_expression (static_global, ref_locus);
- }
-
- void visit (HIR::ConstantItem &constant) override
- {
- // resolve the type
- TyTy::BaseType *resolved_type = nullptr;
- bool ok
- = ctx->get_tyctx ()->lookup_type (constant.get_mappings ().get_hirid (),
- &resolved_type);
- rust_assert (ok);
-
- // canonical path
- const Resolver::CanonicalPath *canonical_path = nullptr;
- ok = ctx->get_mappings ()->lookup_canonical_path (
- constant.get_mappings ().get_crate_num (),
- constant.get_mappings ().get_nodeid (), &canonical_path);
- rust_assert (ok);
- std::string ident = canonical_path->get ();
-
- // types
- tree type = TyTyResolveCompile::compile (ctx, resolved_type);
- tree const_type = build_qualified_type (type, TYPE_QUAL_CONST);
-
- HIR::Expr *const_value_expr = constant.get_expr ();
- bool is_block_expr
- = const_value_expr->get_expression_type () == HIR::Expr::ExprType::Block;
-
- // compile the expression
- tree folded_expr = error_mark_node;
- if (!is_block_expr)
- {
- tree value = CompileExpr::Compile (constant.get_expr (), ctx);
- folded_expr = ConstCtx::fold (value);
- }
- else
- {
- // 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
- // make a _fake_ function with a block so it can hold onto temps then
- // use our constexpr code to fold it completely or error_mark_node
- Backend::typed_identifier receiver;
- tree compiled_fn_type = ctx->get_backend ()->function_type (
- receiver, {},
- {Backend::typed_identifier ("_", const_type, constant.get_locus ())},
- NULL, constant.get_locus ());
-
- tree fndecl
- = ctx->get_backend ()->function (compiled_fn_type, ident, "", 0,
- constant.get_locus ());
- TREE_READONLY (fndecl) = 1;
-
- tree enclosing_scope = NULL_TREE;
- HIR::BlockExpr *function_body
- = static_cast<HIR::BlockExpr *> (constant.get_expr ());
- Location start_location = function_body->get_locus ();
- Location end_location = function_body->get_end_locus ();
-
- tree code_block
- = ctx->get_backend ()->block (fndecl, enclosing_scope, {},
- start_location, end_location);
- ctx->push_block (code_block);
-
- bool address_is_taken = false;
- tree ret_var_stmt = NULL_TREE;
- Bvariable *return_address = ctx->get_backend ()->temporary_variable (
- fndecl, code_block, const_type, NULL, address_is_taken,
- constant.get_locus (), &ret_var_stmt);
-
- ctx->add_statement (ret_var_stmt);
- ctx->push_fn (fndecl, return_address);
-
- compile_function_body (fndecl, *function_body, true);
-
- ctx->pop_block ();
-
- auto body = ctx->get_backend ()->block_statement (code_block);
- if (!ctx->get_backend ()->function_set_body (fndecl, body))
- {
- rust_error_at (constant.get_locus (),
- "failed to set body to constant function");
- return;
- }
-
- ctx->pop_fn ();
-
- // lets fold it into a call expr
- tree call = build_call_array_loc (constant.get_locus ().gcc_location (),
- const_type, fndecl, 0, NULL);
- folded_expr = ConstCtx::fold (call);
- }
-
- tree const_expr
- = ctx->get_backend ()->named_constant_expression (const_type, ident,
- folded_expr,
- constant.get_locus ());
-
- ctx->push_const (const_expr);
- ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
-
- reference = const_expr;
- }
-
- void visit (HIR::Function &function) override
- {
- TyTy::BaseType *fntype_tyty;
- if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
- &fntype_tyty))
- {
- rust_fatal_error (function.get_locus (),
- "failed to lookup function type");
- return;
- }
-
- rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
- TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
- if (fntype->has_subsititions_defined ())
- {
- // we cant do anything for this only when it is used and a concrete type
- // is given
- if (concrete == nullptr)
- return;
- else
- {
- rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
- fntype = static_cast<TyTy::FnType *> (concrete);
- }
- }
-
- // items can be forward compiled which means we may not need to invoke this
- // code. We might also have already compiled this generic function as well.
- tree lookup = NULL_TREE;
- if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
- fntype->get_id (), fntype))
- {
- // has this been added to the list then it must be finished
- if (ctx->function_completed (lookup))
- {
- tree dummy = NULL_TREE;
- if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
- {
- ctx->insert_function_decl (fntype, lookup);
- }
+ void visit (HIR::StaticItem &var) override;
- reference
- = ctx->get_backend ()->function_code_expression (lookup,
- ref_locus);
- return;
- }
- }
+ void visit (HIR::ConstantItem &constant) override;
- if (fntype->has_subsititions_defined ())
- {
- // override the Hir Lookups for the substituions in this context
- fntype->override_context ();
- }
+ void visit (HIR::Function &function) override;
- tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
+ void visit (HIR::ImplBlock &impl_block) override;
- const Resolver::CanonicalPath *canonical_path = nullptr;
- bool ok = ctx->get_mappings ()->lookup_canonical_path (
- function.get_mappings ().get_crate_num (),
- function.get_mappings ().get_nodeid (), &canonical_path);
- rust_assert (ok);
+ void visit (HIR::ExternBlock &extern_block) override;
- std::string ir_symbol_name
- = canonical_path->get () + fntype->subst_as_string ();
- std::string asm_name = function.get_function_name ();
-
- // we don't mangle the main fn since we haven't implemented the main shim
- // yet
- bool is_main_fn = function.get_function_name ().compare ("main") == 0;
- if (!is_main_fn)
- {
- asm_name = ctx->mangle_item (fntype, *canonical_path);
- }
-
- unsigned int flags = 0;
- tree fndecl
- = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name,
- asm_name, flags, function.get_locus ());
- setup_attributes_on_fndecl (fndecl, is_main_fn, function.has_visibility (),
- function.get_qualifiers (),
- function.get_outer_attrs ());
-
- // insert into the context
- ctx->insert_function_decl (fntype, fndecl);
-
- // setup the params
- TyTy::BaseType *tyret = fntype->get_return_type ();
- std::vector<Bvariable *> param_vars;
-
- size_t i = 0;
- for (auto &it : fntype->get_params ())
- {
- HIR::FunctionParam &referenced_param
- = function.get_function_params ().at (i);
- auto param_tyty = it.second;
- auto compiled_param_type
- = TyTyResolveCompile::compile (ctx, param_tyty);
-
- Location param_locus
- = ctx->get_mappings ()->lookup_location (param_tyty->get_ref ());
- Bvariable *compiled_param_var
- = CompileFnParam::compile (ctx, fndecl, &referenced_param,
- compiled_param_type, param_locus);
- if (compiled_param_var == nullptr)
- {
- rust_error_at (param_locus, "failed to compile parameter variable");
- return;
- }
-
- param_vars.push_back (compiled_param_var);
-
- ctx->insert_var_decl (referenced_param.get_mappings ().get_hirid (),
- compiled_param_var);
- i++;
- }
-
- if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
- {
- rust_fatal_error (function.get_locus (),
- "failed to setup parameter variables");
- return;
- }
-
- // lookup locals
- auto block_expr = function.get_definition ().get ();
- auto body_mappings = block_expr->get_mappings ();
-
- Resolver::Rib *rib = nullptr;
- if (!ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
- &rib))
- {
- rust_fatal_error (function.get_locus (),
- "failed to setup locals per block");
- return;
- }
-
- std::vector<Bvariable *> locals;
- ok = compile_locals_for_block (*rib, fndecl, locals);
- rust_assert (ok);
-
- tree enclosing_scope = NULL_TREE;
- HIR::BlockExpr *function_body = function.get_definition ().get ();
- Location start_location = function_body->get_locus ();
- Location end_location = function_body->get_end_locus ();
-
- tree code_block
- = ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
- start_location, end_location);
- ctx->push_block (code_block);
-
- Bvariable *return_address = nullptr;
- if (function.has_function_return_type ())
- {
- tree return_type = TyTyResolveCompile::compile (ctx, tyret);
-
- bool address_is_taken = false;
- tree ret_var_stmt = NULL_TREE;
-
- return_address = ctx->get_backend ()->temporary_variable (
- fndecl, code_block, return_type, NULL, address_is_taken,
- function.get_locus (), &ret_var_stmt);
-
- ctx->add_statement (ret_var_stmt);
- }
-
- ctx->push_fn (fndecl, return_address);
-
- compile_function_body (fndecl, *function.get_definition ().get (),
- function.has_function_return_type ());
-
- ctx->pop_block ();
- auto body = ctx->get_backend ()->block_statement (code_block);
- if (!ctx->get_backend ()->function_set_body (fndecl, body))
- {
- rust_error_at (function.get_locus (), "failed to set body to function");
- return;
- }
-
- ctx->pop_fn ();
- ctx->push_function (fndecl);
-
- reference
- = ctx->get_backend ()->function_code_expression (fndecl, ref_locus);
- }
-
- void visit (HIR::ImplBlock &impl_block) override
- {
- TyTy::BaseType *self_lookup = nullptr;
- if (!ctx->get_tyctx ()->lookup_type (
- impl_block.get_type ()->get_mappings ().get_hirid (), &self_lookup))
- {
- rust_error_at (impl_block.get_locus (),
- "failed to resolve type of impl");
- return;
- }
-
- for (auto &impl_item : impl_block.get_impl_items ())
- CompileInherentImplItem::Compile (impl_item.get (), ctx);
- }
-
- void visit (HIR::ExternBlock &extern_block) override
- {
- for (auto &item : extern_block.get_extern_items ())
- {
- CompileExternItem::compile (item.get (), ctx, concrete);
- }
- }
-
- void visit (HIR::Module &module) override
- {
- for (auto &item : module.get_items ())
- CompileItem::compile (item.get (), ctx);
- }
+ void visit (HIR::Module &module) override;
protected:
CompileItem (Context *ctx, TyTy::BaseType *concrete, Location ref_locus)
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index c624046..e41ee7f 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -16,10 +16,10 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-#include "rust-linemap.h"
-#include "rust-backend.h"
#include "rust-compile-resolve-path.h"
#include "rust-compile-item.h"
+#include "rust-compile-implitem.h"
+#include "rust-compile-expr.h"
#include "rust-hir-trait-resolve.h"
#include "rust-hir-path-probe.h"
@@ -58,7 +58,7 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
if (!ctx->get_resolver ()->lookup_definition (ref_node_id, &def))
{
rust_error_at (expr_locus, "unknown reference for resolved name");
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
}
ref_node_id = def.parent;
}
@@ -69,22 +69,22 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
{
// it might be an enum data-less enum variant
if (lookup->get_kind () != TyTy::TypeKind::ADT)
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
if (!adt->is_enum ())
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
HirId variant_id;
if (!ctx->get_tyctx ()->lookup_variant_definition (mappings.get_hirid (),
&variant_id))
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
int union_disriminator = -1;
TyTy::VariantDef *variant = nullptr;
if (!adt->lookup_variant_by_id (variant_id, &variant,
&union_disriminator))
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
// this can only be for discriminant variants the others are built up
// using call-expr or struct-init
@@ -111,7 +111,7 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
ref_node_id, &ref))
{
rust_error_at (expr_locus, "reverse call path lookup failure");
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
}
// might be a constant
@@ -131,7 +131,7 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
tree fn = NULL_TREE;
if (ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
{
- return ctx->get_backend ()->function_code_expression (fn, expr_locus);
+ return address_expression (fn, expr_locus);
}
}
@@ -245,8 +245,8 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
associated->setup_associated_types ();
return CompileTraitItem::Compile (
- receiver, trait_item_ref->get_hir_trait_item (), ctx, lookup,
- true, expr_locus);
+ trait_item_ref->get_hir_trait_item (), ctx, lookup, true,
+ expr_locus);
}
else
{
@@ -274,7 +274,7 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
}
}
- return ctx->get_backend ()->error_expression ();
+ return error_mark_node;
}
} // namespace Compile
diff --git a/gcc/rust/backend/rust-compile-resolve-path.h b/gcc/rust/backend/rust-compile-resolve-path.h
index b4161cb..9c9d7c5 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.h
+++ b/gcc/rust/backend/rust-compile-resolve-path.h
@@ -50,7 +50,7 @@ public:
private:
ResolvePathRef (Context *ctx)
- : HIRCompileBase (ctx), resolved (ctx->get_backend ()->error_expression ())
+ : HIRCompileBase (ctx), resolved (error_mark_node)
{}
tree resolve (const HIR::PathIdentSegment &final_segment,
diff --git a/gcc/rust/backend/rust-compile-stmt.h b/gcc/rust/backend/rust-compile-stmt.h
index 10f9a52..24a2084 100644
--- a/gcc/rust/backend/rust-compile-stmt.h
+++ b/gcc/rust/backend/rust-compile-stmt.h
@@ -48,34 +48,6 @@ public:
translated = CompileExpr::Compile (stmt.get_expr (), ctx);
}
- void visit (HIR::ConstantItem &constant) override
- {
- TyTy::BaseType *resolved_type = nullptr;
- bool ok
- = ctx->get_tyctx ()->lookup_type (constant.get_mappings ().get_hirid (),
- &resolved_type);
- rust_assert (ok);
-
- tree type = TyTyResolveCompile::compile (ctx, resolved_type);
- tree value = CompileExpr::Compile (constant.get_expr (), ctx);
-
- const Resolver::CanonicalPath *canonical_path = nullptr;
- ok = ctx->get_mappings ()->lookup_canonical_path (
- constant.get_mappings ().get_crate_num (),
- constant.get_mappings ().get_nodeid (), &canonical_path);
- rust_assert (ok);
-
- std::string ident = canonical_path->get ();
- tree const_expr
- = ctx->get_backend ()->named_constant_expression (type, ident, value,
- constant.get_locus ());
-
- ctx->push_const (const_expr);
- ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
-
- translated = const_expr;
- }
-
void visit (HIR::LetStmt &stmt) override
{
// nothing to do
diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h
index 7bc37eb..c431edd 100644
--- a/gcc/rust/backend/rust-compile-var-decl.h
+++ b/gcc/rust/backend/rust-compile-var-decl.h
@@ -72,8 +72,7 @@ public:
private:
CompileVarDecl (Context *ctx, tree fndecl)
- : HIRCompileBase (ctx), fndecl (fndecl),
- translated_type (ctx->get_backend ()->error_type ()),
+ : HIRCompileBase (ctx), fndecl (fndecl), translated_type (error_mark_node),
compiled_variable (ctx->get_backend ()->error_variable ())
{}
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index 2299ddb..fcbfc05 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -16,14 +16,16 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-#include "rust-compile.h"
-#include "rust-compile-item.h"
-#include "rust-compile-expr.h"
-#include "rust-compile-struct-field-expr.h"
#include "rust-hir-trait-resolve.h"
#include "rust-hir-path-probe.h"
#include "rust-hir-type-bounds.h"
#include "rust-hir-dot-operator.h"
+#include "rust-compile.h"
+#include "rust-compile-item.h"
+#include "rust-compile-implitem.h"
+#include "rust-compile-expr.h"
+#include "rust-compile-struct-field-expr.h"
+#include "rust-compile-stmt.h"
namespace Rust {
namespace Compile {
@@ -67,9 +69,8 @@ CompileBlock::visit (HIR::BlockExpr &expr)
return;
}
- std::vector<Bvariable *> locals;
- bool ok = compile_locals_for_block (*rib, fndecl, locals);
- rust_assert (ok);
+ std::vector<Bvariable *> locals
+ = compile_locals_for_block (ctx, *rib, fndecl);
tree enclosing_scope = ctx->peek_enclosing_scope ();
tree new_block = ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
@@ -203,87 +204,6 @@ CompileStructExprField::visit (HIR::StructExprFieldIdentifier &field)
// Shared methods in compilation
-void
-HIRCompileBase::compile_function_body (tree fndecl,
- HIR::BlockExpr &function_body,
- bool has_return_type)
-{
- for (auto &s : function_body.get_statements ())
- {
- auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
- if (compiled_expr != nullptr)
- {
- tree compiled_stmt
- = ctx->get_backend ()->expression_statement (fndecl, compiled_expr);
- ctx->add_statement (compiled_stmt);
- }
- }
-
- if (function_body.has_expr ())
- {
- // the previous passes will ensure this is a valid return
- // or a valid trailing expression
- tree compiled_expr
- = CompileExpr::Compile (function_body.expr.get (), ctx);
-
- if (compiled_expr != nullptr)
- {
- if (has_return_type)
- {
- std::vector<tree> retstmts;
- retstmts.push_back (compiled_expr);
-
- auto ret = ctx->get_backend ()->return_statement (
- fndecl, retstmts,
- function_body.get_final_expr ()->get_locus ());
- ctx->add_statement (ret);
- }
- else
- {
- tree final_stmt
- = ctx->get_backend ()->expression_statement (fndecl,
- compiled_expr);
- ctx->add_statement (final_stmt);
- }
- }
- }
-}
-
-bool
-HIRCompileBase::compile_locals_for_block (Resolver::Rib &rib, tree fndecl,
- std::vector<Bvariable *> &locals)
-{
- rib.iterate_decls ([&] (NodeId n, Location) mutable -> bool {
- Resolver::Definition d;
- bool ok = ctx->get_resolver ()->lookup_definition (n, &d);
- rust_assert (ok);
-
- HIR::Stmt *decl = nullptr;
- ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
- rust_assert (ok);
-
- // if its a function we extract this out side of this fn context
- // and it is not a local to this function
- bool is_item = ctx->get_mappings ()->lookup_hir_item (
- decl->get_mappings ().get_crate_num (),
- decl->get_mappings ().get_hirid ())
- != nullptr;
- if (is_item)
- {
- HIR::Item *item = static_cast<HIR::Item *> (decl);
- CompileItem::compile (item, ctx);
- return true;
- }
-
- Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
- locals.push_back (compiled);
-
- return true;
- });
-
- return true;
-}
-
tree
HIRCompileBase::coercion_site (tree rvalue, TyTy::BaseType *actual,
TyTy::BaseType *expected, Location lvalue_locus,
@@ -533,8 +453,8 @@ HIRCompileBase::compute_address_for_trait_item (
rust_assert (trait_item_has_definition);
HIR::TraitItem *trait_item = ref->get_hir_trait_item ();
- return CompileTraitItem::Compile (root, trait_item, ctx, trait_item_fntype,
- true, locus);
+ return CompileTraitItem::Compile (trait_item, ctx, trait_item_fntype, true,
+ locus);
}
bool
diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h
index 5fdf806..d1cb762 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -510,7 +510,6 @@ struct FunctionParam
{
std::unique_ptr<Pattern> param_name;
std::unique_ptr<Type> type;
-
Location locus;
Analysis::NodeMapping mappings;
@@ -1226,11 +1225,7 @@ public:
bool is_method () const { return !self.is_error (); }
- SelfParam &get_self_param ()
- {
- rust_assert (is_method ());
- return self;
- }
+ SelfParam &get_self_param () { return self; }
protected:
/* Use covariance to implement clone function as returning this object
@@ -2242,11 +2237,7 @@ public:
bool is_method () const { return !self.is_error (); }
- SelfParam &get_self ()
- {
- rust_assert (is_method ());
- return self;
- }
+ SelfParam &get_self () { return self; }
Identifier get_function_name () const { return function_name; }
@@ -2341,7 +2332,8 @@ public:
return TraitItemKind::FUNC;
}
- AST::AttrVec get_outer_attrs () const { return outer_attrs; }
+ AST::AttrVec &get_outer_attrs () { return outer_attrs; }
+ const AST::AttrVec &get_outer_attrs () const { return outer_attrs; }
protected:
// Clone function implementation as (not pure) virtual method
diff --git a/gcc/rust/rust-backend.h b/gcc/rust/rust-backend.h
index f7a1ac6..fe809c9 100644
--- a/gcc/rust/rust-backend.h
+++ b/gcc/rust/rust-backend.h
@@ -29,12 +29,6 @@
#include "operator.h"
#include "tree.h"
-extern bool
-saw_errors (void);
-
-// TODO: Will have to be significantly modified to work with Rust and current
-// setup of gccrs
-
// Pointers to these types are created by the backend, passed to the
// frontend, and passed back to the backend. The types must be
// defined by the backend using these names.
@@ -76,15 +70,6 @@ public:
// Types.
- // Produce an error type. Actually the backend could probably just
- // crash if this is called.
- virtual tree error_type () = 0;
-
- // Get a void type. This is used in (at least) two ways: 1) as the
- // return type of a function with no result parameters; 2)
- // unsafe.Pointer is represented as *void.
- virtual tree void_type () = 0;
-
// get unit-type
virtual tree unit_type () = 0;
@@ -188,17 +173,6 @@ public:
// converting nil to other types.
virtual tree zero_expression (tree) = 0;
- // Create an error expression. This is used for cases which should
- // not occur in a correct program, in order to keep the compilation
- // going without crashing.
- virtual tree error_expression () = 0;
-
- // return whether this is error_mark_node
- virtual bool is_error_expression (tree) = 0;
-
- // Create a nil pointer expression.
- virtual tree nil_pointer_expression () = 0;
-
virtual tree unit_expression () = 0;
// Create a reference to a variable.
@@ -212,12 +186,6 @@ public:
Location)
= 0;
- // Return an expression that declares a constant named NAME with the
- // constant value VAL in BTYPE.
- virtual tree named_constant_expression (tree btype, const std::string &name,
- tree val, Location)
- = 0;
-
// Return an expression for the multi-precision integer VAL in BTYPE.
virtual tree integer_constant_expression (tree btype, mpz_t val) = 0;
@@ -251,10 +219,6 @@ public:
// Return an expression that converts EXPR to TYPE.
virtual tree convert_expression (tree type, tree expr, Location) = 0;
- // Create an expression for the address of a function. This is used to
- // get the address of the code for a function.
- virtual tree function_code_expression (tree, Location) = 0;
-
// Return an expression for the field at INDEX in BSTRUCT.
virtual tree struct_field_expression (tree bstruct, size_t index, Location)
= 0;
@@ -328,11 +292,6 @@ public:
// Statements.
- // Create an error statement. This is used for cases which should
- // not occur in a correct program, in order to keep the compilation
- // going without crashing.
- virtual tree error_statement () = 0;
-
// Create an expression statement within the specified function.
virtual tree expression_statement (tree, tree) = 0;
@@ -409,10 +368,6 @@ public:
// be empty if there are no statements.
virtual void block_add_statements (tree, const std::vector<tree> &) = 0;
- // Return the block as a statement. This is used to include a block
- // in a list of statements.
- virtual tree block_statement (tree) = 0;
-
// Variables.
// Create an error variable. This is used for cases which should
@@ -512,11 +467,6 @@ public:
// Functions.
- // Create an error function. This is used for cases which should
- // not occur in a correct program, in order to keep the compilation
- // going without crashing.
- virtual tree error_function () = 0;
-
// Bit flags to pass to the function method.
// Set if this is a function declaration rather than a definition;
@@ -561,10 +511,6 @@ public:
const std::vector<Bvariable *> &param_vars)
= 0;
- // Set the function body for FUNCTION using the code in CODE_STMT. Returns
- // true on success, false on failure.
- virtual bool function_set_body (tree function, tree code_stmt) = 0;
-
// Look up a named built-in function in the current backend implementation.
// Returns NULL if no built-in function by that name exists.
virtual tree lookup_gcc_builtin (const std::string &) = 0;
diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index 86a4106..812fd55 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -116,9 +116,6 @@ public:
}
// Types.
- tree error_type () { return error_mark_node; }
-
- tree void_type () { return void_type_node; }
tree unit_type ()
{
@@ -192,21 +189,12 @@ public:
tree zero_expression (tree);
- tree error_expression () { return error_mark_node; }
-
- bool is_error_expression (tree expr) { return expr == error_mark_node; }
-
- tree nil_pointer_expression () { return null_pointer_node; }
-
tree unit_expression () { return integer_zero_node; }
tree var_expression (Bvariable *var, Location);
tree indirect_expression (tree, tree expr, bool known_valid, Location);
- tree named_constant_expression (tree type, const std::string &name, tree val,
- Location);
-
tree integer_constant_expression (tree type, mpz_t val);
tree float_constant_expression (tree type, mpfr_t val);
@@ -229,8 +217,6 @@ public:
tree convert_expression (tree type, tree expr, Location);
- tree function_code_expression (tree, Location);
-
tree struct_field_expression (tree, size_t, Location);
tree compound_expression (tree, tree, Location);
@@ -263,8 +249,6 @@ public:
// Statements.
- tree error_statement () { return error_mark_node; }
-
tree expression_statement (tree, tree);
tree init_statement (tree, Bvariable *var, tree init);
@@ -297,8 +281,6 @@ public:
void block_add_statements (tree, const std::vector<tree> &);
- tree block_statement (tree);
-
// Variables.
Bvariable *error_variable () { return new Bvariable (error_mark_node); }
@@ -332,8 +314,6 @@ public:
// Functions.
- tree error_function () { return error_mark_node; }
-
tree function (tree fntype, const std::string &name,
const std::string &asm_name, unsigned int flags, Location);
@@ -343,8 +323,6 @@ public:
bool function_set_parameters (tree function,
const std::vector<Bvariable *> &);
- bool function_set_body (tree function, tree code_stmt);
-
tree lookup_gcc_builtin (const std::string &);
tree lookup_builtin_by_rust_name (const std::string &);
@@ -717,7 +695,7 @@ tree
Gcc_backend::pointer_type (tree to_type)
{
if (to_type == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
tree type = build_pointer_type (to_type);
return type;
}
@@ -728,7 +706,7 @@ tree
Gcc_backend::reference_type (tree to_type)
{
if (to_type == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
tree type = build_reference_type (to_type);
return type;
}
@@ -739,7 +717,7 @@ tree
Gcc_backend::immutable_type (tree base)
{
if (base == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
tree constified = build_qualified_type (base, TYPE_QUAL_CONST);
return constified;
}
@@ -758,7 +736,7 @@ Gcc_backend::function_type (const typed_identifier &receiver,
{
tree t = receiver.type;
if (t == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
*pp = tree_cons (NULL_TREE, t, NULL_TREE);
pp = &TREE_CHAIN (*pp);
}
@@ -768,7 +746,7 @@ Gcc_backend::function_type (const typed_identifier &receiver,
{
tree t = p->type;
if (t == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
*pp = tree_cons (NULL_TREE, t, NULL_TREE);
pp = &TREE_CHAIN (*pp);
}
@@ -788,7 +766,7 @@ Gcc_backend::function_type (const typed_identifier &receiver,
result = result_struct;
}
if (result == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
// The libffi library cannot represent a zero-sized object. To
// avoid causing confusion on 32-bit SPARC, we treat a function that
@@ -800,7 +778,7 @@ Gcc_backend::function_type (const typed_identifier &receiver,
tree fntype = build_function_type (result, args);
if (fntype == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
return build_pointer_type (fntype);
}
@@ -819,7 +797,7 @@ Gcc_backend::function_type_varadic (
{
tree t = receiver.type;
if (t == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
args[offs++] = t;
}
@@ -829,7 +807,7 @@ Gcc_backend::function_type_varadic (
{
tree t = p->type;
if (t == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
args[offs++] = t;
}
@@ -844,7 +822,7 @@ Gcc_backend::function_type_varadic (
result = result_struct;
}
if (result == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
// The libffi library cannot represent a zero-sized object. To
// avoid causing confusion on 32-bit SPARC, we treat a function that
@@ -856,7 +834,7 @@ Gcc_backend::function_type_varadic (
tree fntype = build_varargs_function_type_array (result, n, args);
if (fntype == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
return build_pointer_type (fntype);
}
@@ -872,7 +850,7 @@ Gcc_backend::function_ptr_type (tree result_type,
for (auto &param : parameters)
{
if (param == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
*pp = tree_cons (NULL_TREE, param, NULL_TREE);
pp = &TREE_CHAIN (*pp);
@@ -886,7 +864,7 @@ Gcc_backend::function_ptr_type (tree result_type,
tree fntype = build_function_type (result, args);
if (fntype == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
return build_pointer_type (fntype);
}
@@ -921,7 +899,7 @@ Gcc_backend::fill_in_fields (tree fill,
tree name_tree = get_identifier_from_string (p->name);
tree type_tree = p->type;
if (type_tree == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
tree field = build_decl (p->location.gcc_location (), FIELD_DECL,
name_tree, type_tree);
DECL_CONTEXT (field) = fill;
@@ -954,7 +932,7 @@ tree
Gcc_backend::fill_in_array (tree fill, tree element_type, tree length_tree)
{
if (element_type == error_mark_node || length_tree == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
gcc_assert (TYPE_SIZE (element_type) != NULL_TREE);
@@ -986,7 +964,7 @@ tree
Gcc_backend::named_type (const std::string &name, tree type, Location location)
{
if (type == error_mark_node)
- return this->error_type ();
+ return error_mark_node;
// The middle-end expects a basic type to have a name. In Rust every
// basic type will have a name. The first time we see a basic type,
@@ -1089,7 +1067,7 @@ Gcc_backend::var_expression (Bvariable *var, Location location)
{
tree ret = var->get_tree (location);
if (ret == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
return ret;
}
@@ -1100,7 +1078,7 @@ Gcc_backend::indirect_expression (tree type_tree, tree expr_tree,
bool known_valid, Location location)
{
if (expr_tree == error_mark_node || type_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
// If the type of EXPR is a recursive pointer type, then we
// need to insert a cast before indirecting.
@@ -1115,34 +1093,13 @@ Gcc_backend::indirect_expression (tree type_tree, tree expr_tree,
return ret;
}
-// Return an expression that declares a constant named NAME with the
-// constant value VAL in BTYPE.
-
-tree
-Gcc_backend::named_constant_expression (tree type_tree, const std::string &name,
- tree const_val, Location location)
-{
- if (type_tree == error_mark_node || const_val == error_mark_node)
- return this->error_expression ();
-
- tree name_tree = get_identifier_from_string (name);
- tree decl
- = build_decl (location.gcc_location (), CONST_DECL, name_tree, type_tree);
- DECL_INITIAL (decl) = const_val;
- TREE_CONSTANT (decl) = 1;
- TREE_READONLY (decl) = 1;
-
- rust_preserve_from_gc (decl);
- return decl;
-}
-
// Return a typed value as a constant integer.
tree
Gcc_backend::integer_constant_expression (tree t, mpz_t val)
{
if (t == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
tree ret = double_int_to_tree (t, mpz_get_double_int (t, val, true));
return ret;
@@ -1155,7 +1112,7 @@ Gcc_backend::float_constant_expression (tree t, mpfr_t val)
{
tree ret;
if (t == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
REAL_VALUE_TYPE r1;
real_from_mpfr (&r1, val, t, GMP_RNDN);
@@ -1172,7 +1129,7 @@ Gcc_backend::complex_constant_expression (tree t, mpc_t val)
{
tree ret;
if (t == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
REAL_VALUE_TYPE r1;
real_from_mpfr (&r1, mpc_realref (val), TREE_TYPE (t), GMP_RNDN);
@@ -1230,7 +1187,7 @@ tree
Gcc_backend::real_part_expression (tree complex_tree, Location location)
{
if (complex_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
gcc_assert (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (complex_tree)));
tree ret
= fold_build1_loc (location.gcc_location (), REALPART_EXPR,
@@ -1244,7 +1201,7 @@ tree
Gcc_backend::imag_part_expression (tree complex_tree, Location location)
{
if (complex_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
gcc_assert (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (complex_tree)));
tree ret
= fold_build1_loc (location.gcc_location (), IMAGPART_EXPR,
@@ -1259,7 +1216,7 @@ Gcc_backend::complex_expression (tree real_tree, tree imag_tree,
Location location)
{
if (real_tree == error_mark_node || imag_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (real_tree))
== TYPE_MAIN_VARIANT (TREE_TYPE (imag_tree)));
gcc_assert (SCALAR_FLOAT_TYPE_P (TREE_TYPE (real_tree)));
@@ -1277,7 +1234,7 @@ Gcc_backend::convert_expression (tree type_tree, tree expr_tree,
{
if (type_tree == error_mark_node || expr_tree == error_mark_node
|| TREE_TYPE (expr_tree) == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
tree ret;
if (this->type_size (type_tree) == 0
@@ -1305,18 +1262,6 @@ Gcc_backend::convert_expression (tree type_tree, tree expr_tree,
return ret;
}
-// Get the address of a function.
-
-tree
-Gcc_backend::function_code_expression (tree func, Location location)
-{
- if (func == error_mark_node)
- return this->error_expression ();
-
- tree ret = build_fold_addr_expr_loc (location.gcc_location (), func);
- return ret;
-}
-
// Return an expression for the field at INDEX in BSTRUCT.
tree
@@ -1325,7 +1270,7 @@ Gcc_backend::struct_field_expression (tree struct_tree, size_t index,
{
if (struct_tree == error_mark_node
|| TREE_TYPE (struct_tree) == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
gcc_assert (TREE_CODE (TREE_TYPE (struct_tree)) == RECORD_TYPE
|| TREE_CODE (TREE_TYPE (struct_tree)) == UNION_TYPE);
tree field = TYPE_FIELDS (TREE_TYPE (struct_tree));
@@ -1333,7 +1278,7 @@ Gcc_backend::struct_field_expression (tree struct_tree, size_t index,
{
// This can happen for a type which refers to itself indirectly
// and then turns out to be erroneous.
- return this->error_expression ();
+ return error_mark_node;
}
for (unsigned int i = index; i > 0; --i)
{
@@ -1341,7 +1286,7 @@ Gcc_backend::struct_field_expression (tree struct_tree, size_t index,
gcc_assert (field != NULL_TREE);
}
if (TREE_TYPE (field) == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
tree ret = fold_build3_loc (location.gcc_location (), COMPONENT_REF,
TREE_TYPE (field), struct_tree, field, NULL_TREE);
if (TREE_CONSTANT (struct_tree))
@@ -1355,7 +1300,7 @@ tree
Gcc_backend::compound_expression (tree stat, tree expr, Location location)
{
if (stat == error_mark_node || expr == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
tree ret = fold_build2_loc (location.gcc_location (), COMPOUND_EXPR,
TREE_TYPE (expr), stat, expr);
return ret;
@@ -1371,7 +1316,7 @@ Gcc_backend::conditional_expression (tree, tree type_tree, tree cond_expr,
{
if (type_tree == error_mark_node || cond_expr == error_mark_node
|| then_expr == error_mark_node || else_expr == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
tree ret = build3_loc (location.gcc_location (), COND_EXPR, type_tree,
cond_expr, then_expr, else_expr);
return ret;
@@ -1483,7 +1428,7 @@ Gcc_backend::negation_expression (NegationOperator op, tree expr_tree,
/* Check if the expression is an error, in which case we return an error
expression. */
if (expr_tree == error_mark_node || TREE_TYPE (expr_tree) == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
/* For negation operators, the resulting type should be the same as its
operand. */
@@ -1522,7 +1467,7 @@ Gcc_backend::arithmetic_or_logical_expression (ArithmeticOrLogicalOperator op,
/* Check if either expression is an error, in which case we return an error
expression. */
if (left_tree == error_mark_node || right_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
/* We need to determine if we're doing floating point arithmetics of integer
arithmetics. */
@@ -1567,7 +1512,7 @@ Gcc_backend::comparison_expression (ComparisonOperator op, tree left_tree,
/* Check if either expression is an error, in which case we return an error
expression. */
if (left_tree == error_mark_node || right_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
/* For comparison operators, the resulting type should be boolean. */
auto tree_type = boolean_type_node;
@@ -1587,7 +1532,7 @@ Gcc_backend::lazy_boolean_expression (LazyBooleanOperator op, tree left_tree,
/* Check if either expression is an error, in which case we return an error
expression. */
if (left_tree == error_mark_node || right_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
/* For lazy boolean operators, the resulting type should be the same as the
rhs operand. */
@@ -1608,7 +1553,7 @@ Gcc_backend::constructor_expression (tree type_tree, bool is_variant,
int union_index, Location location)
{
if (type_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
vec<constructor_elt, va_gc> *init;
vec_alloc (init, vals.size ());
@@ -1652,7 +1597,7 @@ Gcc_backend::constructor_expression (tree type_tree, bool is_variant,
}
if (TREE_TYPE (field) == error_mark_node || val == error_mark_node
|| TREE_TYPE (val) == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
if (int_size_in_bytes (TREE_TYPE (field)) == 0)
{
@@ -1684,7 +1629,7 @@ Gcc_backend::constructor_expression (tree type_tree, bool is_variant,
tree val = (*p);
if (TREE_TYPE (field) == error_mark_node || val == error_mark_node
|| TREE_TYPE (val) == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
if (int_size_in_bytes (TREE_TYPE (field)) == 0)
{
@@ -1724,7 +1669,7 @@ Gcc_backend::array_constructor_expression (
const std::vector<tree> &vals, Location location)
{
if (type_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
gcc_assert (indexes.size () == vals.size ());
@@ -1741,7 +1686,7 @@ Gcc_backend::array_constructor_expression (
tree val = vals[i];
if (index == error_mark_node || val == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
if (element_size == 0)
{
@@ -1780,7 +1725,7 @@ Gcc_backend::pointer_offset_expression (tree base_tree, tree index_tree,
tree element_type_tree = TREE_TYPE (TREE_TYPE (base_tree));
if (base_tree == error_mark_node || TREE_TYPE (base_tree) == error_mark_node
|| index_tree == error_mark_node || element_type_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
tree element_size = TYPE_SIZE_UNIT (element_type_tree);
index_tree
@@ -1800,7 +1745,7 @@ Gcc_backend::array_index_expression (tree array_tree, tree index_tree,
{
if (array_tree == error_mark_node || TREE_TYPE (array_tree) == error_mark_node
|| index_tree == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
// A function call that returns a zero sized object will have been
// changed to return void. If we see void here, assume we are
@@ -1824,7 +1769,7 @@ Gcc_backend::call_expression (tree, // containing fcn for call
tree chain_expr, Location location)
{
if (fn == error_mark_node || TREE_TYPE (fn) == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
gcc_assert (FUNCTION_POINTER_TYPE_P (TREE_TYPE (fn)));
tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
@@ -1835,7 +1780,7 @@ Gcc_backend::call_expression (tree, // containing fcn for call
{
args[i] = fn_args.at (i);
if (args[i] == error_mark_node)
- return this->error_expression ();
+ return error_mark_node;
}
tree fndecl = fn;
@@ -1907,7 +1852,7 @@ Gcc_backend::init_statement (tree, Bvariable *var, tree init_tree)
{
tree var_tree = var->get_decl ();
if (var_tree == error_mark_node || init_tree == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
gcc_assert (TREE_CODE (var_tree) == VAR_DECL);
// To avoid problems with GNU ld, we don't make zero-sized
@@ -1939,7 +1884,7 @@ Gcc_backend::assignment_statement (tree bfn, tree lhs, tree rhs,
Location location)
{
if (lhs == error_mark_node || rhs == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
// To avoid problems with GNU ld, we don't make zero-sized
// externally visible variables. That might lead us to doing an
@@ -1967,10 +1912,10 @@ Gcc_backend::return_statement (tree fntree, const std::vector<tree> &vals,
Location location)
{
if (fntree == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
tree result = DECL_RESULT (fntree);
if (result == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
// If the result size is zero bytes, we have set the function type
// to have a result type of void, so don't return anything.
@@ -1984,7 +1929,7 @@ Gcc_backend::return_statement (tree fntree, const std::vector<tree> &vals,
{
tree val = (*p);
if (val == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
append_to_statement_list (val, &stmt_list);
}
tree ret = fold_build1_loc (location.gcc_location (), RETURN_EXPR,
@@ -2001,7 +1946,7 @@ Gcc_backend::return_statement (tree fntree, const std::vector<tree> &vals,
{
tree val = vals.front ();
if (val == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
tree set = fold_build2_loc (location.gcc_location (), MODIFY_EXPR,
void_type_node, result, vals.front ());
ret = fold_build1_loc (location.gcc_location (), RETURN_EXPR,
@@ -2033,7 +1978,7 @@ Gcc_backend::return_statement (tree fntree, const std::vector<tree> &vals,
TREE_TYPE (field), rettmp, field, NULL_TREE);
tree val = (*p);
if (val == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
tree set = fold_build2_loc (location.gcc_location (), MODIFY_EXPR,
void_type_node, ref, (*p));
append_to_statement_list (set, &stmt_list);
@@ -2061,7 +2006,7 @@ Gcc_backend::exception_handler_statement (tree try_stmt, tree except_stmt,
{
if (try_stmt == error_mark_node || except_stmt == error_mark_node
|| finally_stmt == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
if (except_stmt != NULL_TREE)
try_stmt = build2_loc (location.gcc_location (), TRY_CATCH_EXPR,
@@ -2082,7 +2027,7 @@ Gcc_backend::if_statement (tree, tree cond_tree, tree then_tree, tree else_tree,
{
if (cond_tree == error_mark_node || then_tree == error_mark_node
|| else_tree == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
tree ret = build3_loc (location.gcc_location (), COND_EXPR, void_type_node,
cond_tree, then_tree, else_tree);
return ret;
@@ -2139,7 +2084,7 @@ Gcc_backend::switch_statement (tree decl, tree value,
{
tree t = (*pcv);
if (t == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
location_t loc = EXPR_LOCATION (t);
tree label = create_artificial_label (loc);
tree c = build_case_label ((*pcv), NULL_TREE, label);
@@ -2151,7 +2096,7 @@ Gcc_backend::switch_statement (tree decl, tree value,
{
tree t = (*ps);
if (t == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
append_to_statement_list (t, &stmt_list);
}
}
@@ -2159,7 +2104,7 @@ Gcc_backend::switch_statement (tree decl, tree value,
tree tv = value;
if (tv == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
tree t = build2_loc (switch_location.gcc_location (), SWITCH_EXPR, NULL_TREE,
tv, stmt_list);
return t;
@@ -2173,11 +2118,11 @@ Gcc_backend::compound_statement (tree s1, tree s2)
tree stmt_list = NULL_TREE;
tree t = s1;
if (t == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
append_to_statement_list (t, &stmt_list);
t = s2;
if (t == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
append_to_statement_list (t, &stmt_list);
// If neither statement has any side effects, stmt_list can be NULL
@@ -2199,7 +2144,7 @@ Gcc_backend::statement_list (const std::vector<tree> &statements)
{
tree t = (*p);
if (t == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
append_to_statement_list (t, &stmt_list);
}
return stmt_list;
@@ -2289,15 +2234,6 @@ Gcc_backend::block_add_statements (tree bind_tree,
BIND_EXPR_BODY (bind_tree) = stmt_list;
}
-// Return a block as a statement.
-
-tree
-Gcc_backend::block_statement (tree bind_tree)
-{
- gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
- return bind_tree;
-}
-
// This is not static because we declare it with GTY(()) in rust-c.h.
tree rust_non_zero_struct;
@@ -2541,7 +2477,7 @@ Gcc_backend::temporary_variable (tree fndecl, tree bind_tree, tree type_tree,
if (type_tree == error_mark_node || init_tree == error_mark_node
|| fndecl == error_mark_node)
{
- *pstatement = this->error_statement ();
+ *pstatement = error_mark_node;
return this->error_variable ();
}
@@ -2670,7 +2606,7 @@ Gcc_backend::function (tree functype, const std::string &name,
}
tree id = get_identifier_from_string (name);
if (functype == error_mark_node || id == error_mark_node)
- return this->error_function ();
+ return error_mark_node;
tree decl
= build_decl (location.gcc_location (), FUNCTION_DECL, id, functype);
@@ -2711,7 +2647,7 @@ Gcc_backend::function_defer_statement (tree function, tree undefer_tree,
{
if (undefer_tree == error_mark_node || defer_tree == error_mark_node
|| function == error_mark_node)
- return this->error_statement ();
+ return error_mark_node;
if (DECL_STRUCT_FUNCTION (function) == NULL)
push_struct_function (function);
@@ -2759,17 +2695,6 @@ Gcc_backend::function_set_parameters (
return true;
}
-// Set the function body for FUNCTION using the code in CODE_BLOCK.
-
-bool
-Gcc_backend::function_set_body (tree function, tree code_stmt)
-{
- if (function == error_mark_node || code_stmt == error_mark_node)
- return false;
- DECL_SAVED_TREE (function) = code_stmt;
- return true;
-}
-
// Look up a named built-in function in the current backend implementation.
// Returns NULL if no built-in function by that name exists.
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 0150a48..5529fbf 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -17,32 +17,8 @@
// <http://www.gnu.org/licenses/>.
// #include "rust-session-manager.h"
-#include <fstream>
-#include <sstream>
#include "rust-session-manager.h"
#include "rust-diagnostics.h"
-#include "diagnostic.h"
-#include "input.h"
-
-#include "target.h"
-#include "tm.h"
-#include "memmodel.h"
-#include "tm_p.h"
-
-//#include "rust-target.h"
-/*TODO This isn't (currently?) necessary, but if '#include'd after '#include
- "target.h"', causes: In file included from
- [...]/gcc/rust/rust-session-manager.cc:31:
- [...]/gcc/rust/rust-target.h:23: error: "DEFHOOK" redefined [-Werror]
- 23 | #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) TYPE (*NAME) PARAMS;
- |
- In file included from [...]/gcc/rust/rust-session-manager.cc:27:
- [...]/gcc/target.h:272: note: this is the location of the previous
- definition 272 | #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) TYPE (* NAME)
- PARAMS;
- |
-*/
-
#include "rust-lex.h"
#include "rust-parse.h"
#include "rust-macro-expand.h"
@@ -54,6 +30,13 @@
#include "rust-ast-resolve-unused.h"
#include "rust-compile.h"
+#include "diagnostic.h"
+#include "input.h"
+#include "rust-target.h"
+
+extern bool
+saw_errors (void);
+
extern Linemap *
rust_get_linemap ();
diff --git a/gcc/rust/rust-target.h b/gcc/rust/rust-target.h
index 9669c4a..743ac51 100644
--- a/gcc/rust/rust-target.h
+++ b/gcc/rust/rust-target.h
@@ -18,9 +18,14 @@
#ifndef GCC_RUST_TARGET_H
#define GCC_RUST_TARGET_H
+#include "target.h"
+#include "tm.h"
+#include "memmodel.h"
+#include "tm_p.h"
+
// TODO: find out what this stuff actually does
#define DEFHOOKPOD(NAME, DOC, TYPE, INIT) TYPE NAME;
-#define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) TYPE (*NAME) PARAMS;
+// #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) TYPE (*NAME) PARAMS;
#define DEFHOOK_UNDOC DEFHOOK
#define HOOKSTRUCT(FRAGMENT) FRAGMENT
@@ -39,4 +44,4 @@ extern struct gcc_targetrustm targetrustm;
extern void
rust_add_target_info (const char *key, const char *value);
-#endif \ No newline at end of file
+#endif
diff --git a/gcc/rust/typecheck/rust-tycheck-dump.h b/gcc/rust/typecheck/rust-tycheck-dump.h
index 1b19123..0bcc6c8 100644
--- a/gcc/rust/typecheck/rust-tycheck-dump.h
+++ b/gcc/rust/typecheck/rust-tycheck-dump.h
@@ -22,6 +22,9 @@
#include "rust-hir-type-check-base.h"
#include "rust-hir-full.h"
+#include <fstream>
+#include <sstream>
+
namespace Rust {
namespace Resolver {