From 55c03e504f4130ce888e5c17282e52ab0af227f6 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Tue, 27 Feb 2024 13:43:37 +0100 Subject: [PATCH 01/11] Refactor HIR to reduce the amount of raw pointers Attempt to use references and smart pointers whenever possible. gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::compile_function_body): Remove usage of get function to retrieve a raw pointer. * backend/rust-compile-base.h: Change API usage from raw pointer to a reference. * backend/rust-compile-block.cc (CompileBlock::compile): Likewise. (CompileBlock::visit): Likewise. (CompileConditionalBlocks::visit): Likewise. * backend/rust-compile-block.h: Likewise. * backend/rust-compile-expr.cc (CompileExpr::Compile): Likewise. (CompileExpr::visit): Likewise. (check_match_scrutinee): Likewise. (CompileExpr::array_value_expr): Likewise. (CompileExpr::array_copied_expr): Likewise. (CompileExpr::generate_closure_function): Likewise. (CompileExpr::generate_possible_fn_trait_call): Likewise. * backend/rust-compile-expr.h: Likewise. * backend/rust-compile-fnparam.cc (CompileFnParam::compile): Likewise. (CompileFnParam::visit): Likewise. * backend/rust-compile-fnparam.h: Likewise. * backend/rust-compile-implitem.cc (CompileTraitItem::visit): Likewise. * backend/rust-compile-intrinsic.cc (compile_fn_params): Likewise. * backend/rust-compile-item.cc (CompileItem::visit): Likewise. * backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit): Likewise. (compile_range_pattern_bound): Likewise. (CompilePatternBindings::visit): Likewise. (CompilePatternLet::visit): Likewise. * backend/rust-compile-pattern.h: Likewise. * backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): Likewise. (HIRCompileBase::query_compile): Likewise. * backend/rust-compile-stmt.cc (CompileStmt::visit): Likewise. * backend/rust-compile-struct-field-expr.cc (CompileStructExprField::Compile): Likewise. (CompileStructExprField::visit): Likewise. * backend/rust-compile-struct-field-expr.h: Likewise. * backend/rust-compile-type.cc (TyTyResolveCompile::visit): Likewise. * backend/rust-compile-var-decl.h: Likewise. * backend/rust-compile.cc: Likewise. * backend/rust-mangle-v0.cc (v0_inherent_or_trait_impl_path): Likewise. * checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit): Likewise. * checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h: Likewise. * checks/errors/borrowck/rust-bir-builder-pattern.h: Likewise. * checks/errors/borrowck/rust-bir-builder-struct.h: Likewise. * checks/errors/borrowck/rust-bir-builder.h: Likewise. * checks/errors/borrowck/rust-function-collector.h: Likewise. * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_type_privacy): Likewise. (PrivacyReporter::visit): Likewise. * checks/errors/privacy/rust-privacy-reporter.h: Likewise. * checks/errors/privacy/rust-reachability.cc (ReachabilityVisitor::visit): Likewise. * checks/errors/rust-const-checker.cc (ConstChecker::visit): Likewise. * checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Likewise. * checks/lints/rust-lint-marklive.cc (MarkLive::visit): Likewise. * checks/lints/rust-lint-marklive.h: Likewise. * hir/rust-hir-dump.cc (Dump::visit): Likewise. * hir/tree/rust-hir-expr.h: Likewise. * hir/tree/rust-hir-item.h: Likewise. * hir/tree/rust-hir-path.h: Likewise. * hir/tree/rust-hir-pattern.h: Likewise. * hir/tree/rust-hir-stmt.h: Likewise. * hir/tree/rust-hir-type.h: Likewise. * hir/tree/rust-hir.h: Likewise. * typecheck/rust-autoderef.cc: Likewise. * typecheck/rust-hir-dot-operator.cc (MethodResolver::select): Likewise. * typecheck/rust-hir-inherent-impl-overlap.h: Likewise. * typecheck/rust-hir-path-probe.cc (PathProbeType::process_impl_item_candidate): Likewise. (PathProbeImplTrait::process_trait_impl_items_for_candidates): Likewise. * typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): Likewise. (TraitItemReference::resolve_item): Likewise. * typecheck/rust-hir-type-check-base.cc: Likewise. * typecheck/rust-hir-type-check-base.h: Likewise. * typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::Resolve): Likewise. (TypeCheckEnumItem::visit): Likewise. * typecheck/rust-hir-type-check-enumitem.h: Likewise. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::Resolve): Likewise. (TypeCheckExpr::visit): Likewise. (TypeCheckExpr::resolve_fn_trait_call): Likewise. * typecheck/rust-hir-type-check-expr.h: Likewise. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::Resolve): Likewise. (TypeCheckTopLevelExternItem::visit): Likewise. (TypeCheckImplItem::visit): Likewise. (TypeCheckImplItemWithTrait::visit): Likewise. * typecheck/rust-hir-type-check-implitem.h: Likewise. * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise. (TypeCheckItem::resolve_impl_item): Likewise. (TypeCheckItem::resolve_impl_block_substitutions): Likewise. (TypeCheckItem::resolve_impl_block_self): Likewise. * typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): Likewise. (TypeCheckExpr::resolve_segments): Likewise. * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::Resolve): Likewise. (TypeCheckPattern::visit): Likewise. (ClosureParamInfer::Resolve): Likewise. (ClosureParamInfer::visit): Likewise. * typecheck/rust-hir-type-check-pattern.h: Likewise. * typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::Resolve): Likewise. (TypeCheckStmt::visit): Likewise. * typecheck/rust-hir-type-check-stmt.h: Likewise. * typecheck/rust-hir-type-check-struct-field.h: Likewise. * typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::TypeCheckStructExpr): Likewise. (TypeCheckStructExpr::Resolve): Likewise. (TypeCheckStructExpr::resolve): Likewise. (TypeCheckStructExpr::visit): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckResolveGenericArguments::resolve): Likewise. (TypeCheckType::Resolve): Likewise. (TypeCheckType::visit): Likewise. (TypeCheckType::resolve_root_path): Likewise. (TypeResolveGenericParam::Resolve): Likewise. (TypeResolveGenericParam::visit): Likewise. (ResolveWhereClauseItem::visit): Likewise. * typecheck/rust-hir-type-check-type.h: Likewise. * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise. * typecheck/rust-hir-type-check.h: Likewise. * typecheck/rust-type-util.cc (query_type): Likewise. * typecheck/rust-typecheck-context.cc (TypeCheckContextItem::TypeCheckContextItem): Likewise. * typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::scan): Likewise. (TypeCheckBase::get_predicate_from_bound): Likewise. * typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Likewise. (TypeCheckMethodCallExpr::go): Likewise. (TypeCheckMethodCallExpr::check): Likewise. * typecheck/rust-tyty-subst.cc: Likewise. * typecheck/rust-tyty.cc (BaseType::monomorphized_clone): Likewise. (VariantDef::VariantDef): Remove copy constructor. (VariantDef::operator=): Change to move operator. (VariantDef::get_discriminant): Replace return type to a reference instead of a reference to a unique pointer. (VariantDef::clone): Change to references. (VariantDef::monomorphized_clone): Likewise. (FnType::as_string): Likewise. (FnType::clone): Likewise. * typecheck/rust-tyty.h: Likewise. * util/rust-hir-map.cc (Mappings::insert_hir_impl_block): Likewise. * backend/rust-compile-asm.cc: Use a reference instead of the raw pointer value. * checks/errors/borrowck/rust-bir-builder-pattern.cc: Use references. * checks/errors/rust-hir-pattern-analysis.cc: Likewise. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/backend/rust-compile-asm.cc | 4 +- gcc/rust/backend/rust-compile-base.cc | 35 +-- gcc/rust/backend/rust-compile-base.h | 2 +- gcc/rust/backend/rust-compile-block.cc | 27 +- gcc/rust/backend/rust-compile-block.h | 4 +- gcc/rust/backend/rust-compile-expr.cc | 208 +++++++------ gcc/rust/backend/rust-compile-expr.h | 6 +- gcc/rust/backend/rust-compile-fnparam.cc | 14 +- gcc/rust/backend/rust-compile-fnparam.h | 4 +- gcc/rust/backend/rust-compile-implitem.cc | 4 +- gcc/rust/backend/rust-compile-intrinsic.cc | 2 +- gcc/rust/backend/rust-compile-item.cc | 9 +- gcc/rust/backend/rust-compile-pattern.cc | 77 +++-- gcc/rust/backend/rust-compile-pattern.h | 8 +- gcc/rust/backend/rust-compile-resolve-path.cc | 4 +- gcc/rust/backend/rust-compile-stmt.cc | 12 +- .../backend/rust-compile-struct-field-expr.cc | 16 +- .../backend/rust-compile-struct-field-expr.h | 2 +- gcc/rust/backend/rust-compile-type.cc | 2 +- gcc/rust/backend/rust-compile-var-decl.h | 4 +- gcc/rust/backend/rust-compile.cc | 10 +- gcc/rust/backend/rust-mangle-v0.cc | 4 +- .../borrowck/rust-bir-builder-expr-stmt.cc | 119 ++++---- .../borrowck/rust-bir-builder-lazyboolexpr.h | 8 +- .../borrowck/rust-bir-builder-pattern.cc | 20 +- .../borrowck/rust-bir-builder-pattern.h | 1 + .../errors/borrowck/rust-bir-builder-struct.h | 4 +- .../checks/errors/borrowck/rust-bir-builder.h | 16 +- .../errors/borrowck/rust-function-collector.h | 4 +- .../errors/privacy/rust-privacy-reporter.cc | 130 ++++---- .../errors/privacy/rust-privacy-reporter.h | 2 +- .../errors/privacy/rust-reachability.cc | 2 +- gcc/rust/checks/errors/rust-const-checker.cc | 122 ++++---- .../errors/rust-hir-pattern-analysis.cc | 203 ++++++------- gcc/rust/checks/errors/rust-unsafe-checker.cc | 118 ++++---- gcc/rust/checks/lints/rust-lint-marklive.cc | 16 +- gcc/rust/checks/lints/rust-lint-marklive.h | 72 ++--- gcc/rust/hir/rust-hir-dump.cc | 34 +-- gcc/rust/hir/tree/rust-hir-expr.h | 117 ++++---- gcc/rust/hir/tree/rust-hir-item.h | 59 ++-- gcc/rust/hir/tree/rust-hir-path.h | 21 +- gcc/rust/hir/tree/rust-hir-pattern.h | 18 +- gcc/rust/hir/tree/rust-hir-stmt.h | 8 +- gcc/rust/hir/tree/rust-hir-type.h | 18 +- gcc/rust/hir/tree/rust-hir.h | 7 +- gcc/rust/typecheck/rust-autoderef.cc | 2 +- gcc/rust/typecheck/rust-hir-dot-operator.cc | 179 ++++++----- .../rust-hir-inherent-impl-overlap.h | 2 +- gcc/rust/typecheck/rust-hir-path-probe.cc | 5 +- gcc/rust/typecheck/rust-hir-trait-resolve.cc | 9 +- .../typecheck/rust-hir-type-check-base.cc | 30 +- gcc/rust/typecheck/rust-hir-type-check-base.h | 10 +- .../typecheck/rust-hir-type-check-enumitem.cc | 58 ++-- .../typecheck/rust-hir-type-check-enumitem.h | 2 +- .../typecheck/rust-hir-type-check-expr.cc | 281 +++++++++--------- gcc/rust/typecheck/rust-hir-type-check-expr.h | 2 +- .../typecheck/rust-hir-type-check-implitem.cc | 103 +++---- .../typecheck/rust-hir-type-check-implitem.h | 14 +- .../typecheck/rust-hir-type-check-item.cc | 79 +++-- .../typecheck/rust-hir-type-check-path.cc | 12 +- .../typecheck/rust-hir-type-check-pattern.cc | 73 +++-- .../typecheck/rust-hir-type-check-pattern.h | 11 +- .../typecheck/rust-hir-type-check-stmt.cc | 39 ++- gcc/rust/typecheck/rust-hir-type-check-stmt.h | 2 +- .../rust-hir-type-check-struct-field.h | 4 +- .../typecheck/rust-hir-type-check-struct.cc | 22 +- .../typecheck/rust-hir-type-check-type.cc | 137 ++++----- gcc/rust/typecheck/rust-hir-type-check-type.h | 6 +- gcc/rust/typecheck/rust-hir-type-check.cc | 43 +-- gcc/rust/typecheck/rust-hir-type-check.h | 2 +- gcc/rust/typecheck/rust-type-util.cc | 5 +- gcc/rust/typecheck/rust-typecheck-context.cc | 4 +- gcc/rust/typecheck/rust-tyty-bounds.cc | 31 +- gcc/rust/typecheck/rust-tyty-call.cc | 19 +- gcc/rust/typecheck/rust-tyty-subst.cc | 4 +- gcc/rust/typecheck/rust-tyty.cc | 51 ++-- gcc/rust/typecheck/rust-tyty.h | 23 +- gcc/rust/util/rust-hir-map.cc | 2 +- 78 files changed, 1382 insertions(+), 1461 deletions(-) diff --git a/gcc/rust/backend/rust-compile-asm.cc b/gcc/rust/backend/rust-compile-asm.cc index 87e4266133fc..3a22658d2a25 100644 --- a/gcc/rust/backend/rust-compile-asm.cc +++ b/gcc/rust/backend/rust-compile-asm.cc @@ -85,7 +85,7 @@ CompileAsm::asm_construct_outputs (HIR::InlineAsm &expr) { auto out = output.get_out (); - tree out_tree = CompileExpr::Compile (out.expr.get (), this->ctx); + tree out_tree = CompileExpr::Compile (*out.expr, this->ctx); // expects a tree list // TODO: This assumes that the output is a register std::string expr_name = "=r"; @@ -112,7 +112,7 @@ CompileAsm::asm_construct_inputs (HIR::InlineAsm &expr) { auto in = input.get_in (); - tree in_tree = CompileExpr::Compile (in.expr.get (), this->ctx); + tree in_tree = CompileExpr::Compile (*in.expr, this->ctx); // expects a tree list // TODO: This assumes that the input is a register std::string expr_name = "r"; diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 11129bcb86be..966ff527c15a 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -582,8 +582,9 @@ HIRCompileBase::compile_function_body (tree fndecl, if (function_body.has_expr ()) { - location_t locus = function_body.get_final_expr ()->get_locus (); - tree return_value = CompileExpr::Compile (function_body.expr.get (), ctx); + location_t locus = function_body.get_final_expr ().get_locus (); + tree return_value + = CompileExpr::Compile (function_body.get_final_expr (), ctx); // we can only return this if non unit value return type if (!fn_return_ty->is_unit ()) @@ -709,18 +710,18 @@ HIRCompileBase::compile_function ( size_t i = is_method ? 1 : 0; for (auto &referenced_param : function_params) { - auto tyty_param = fntype->param_at (i++); + auto &tyty_param = fntype->param_at (i++); auto param_tyty = tyty_param.second; auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); location_t param_locus = referenced_param.get_locus (); Bvariable *compiled_param_var - = CompileFnParam::compile (ctx, fndecl, &referenced_param, + = CompileFnParam::compile (ctx, fndecl, referenced_param, compiled_param_type, param_locus); param_vars.push_back (compiled_param_var); - const HIR::Pattern ¶m_pattern = *referenced_param.get_param_name (); + const HIR::Pattern ¶m_pattern = referenced_param.get_param_name (); ctx->insert_var_decl (param_pattern.get_mappings ().get_hirid (), compiled_param_var); } @@ -768,14 +769,14 @@ HIRCompileBase::compile_function ( tree HIRCompileBase::compile_constant_item ( TyTy::BaseType *resolved_type, const Resolver::CanonicalPath &canonical_path, - HIR::Expr *const_value_expr, location_t locus) + HIR::Expr &const_value_expr, location_t 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; + = const_value_expr.get_expression_type () == HIR::Expr::ExprType::Block; // 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 @@ -789,14 +790,14 @@ HIRCompileBase::compile_constant_item ( TREE_READONLY (fndecl) = 1; tree enclosing_scope = NULL_TREE; - location_t start_location = const_value_expr->get_locus (); - location_t end_location = const_value_expr->get_locus (); + location_t start_location = const_value_expr.get_locus (); + location_t end_location = const_value_expr.get_locus (); if (is_block_expr) { - HIR::BlockExpr *function_body - = static_cast (const_value_expr); - start_location = function_body->get_locus (); - end_location = function_body->get_end_locus (); + HIR::BlockExpr &function_body + = static_cast (const_value_expr); + start_location = function_body.get_locus (); + end_location = function_body.get_end_locus (); } tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/, @@ -814,9 +815,9 @@ HIRCompileBase::compile_constant_item ( if (is_block_expr) { - HIR::BlockExpr *function_body - = static_cast (const_value_expr); - compile_function_body (fndecl, *function_body, resolved_type); + HIR::BlockExpr &function_body + = static_cast (const_value_expr); + compile_function_body (fndecl, function_body, resolved_type); } else { @@ -824,7 +825,7 @@ HIRCompileBase::compile_constant_item ( tree return_expr = Backend::return_statement (fndecl, value, - const_value_expr->get_locus ()); + const_value_expr.get_locus ()); ctx->add_statement (return_expr); } diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index 22d4ae6c6f66..bf175d748a0b 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -92,7 +92,7 @@ class HIRCompileBase tree compile_constant_item (TyTy::BaseType *resolved_type, const Resolver::CanonicalPath &canonical_path, - HIR::Expr *const_value_expr, location_t locus); + HIR::Expr &const_value_expr, location_t locus); tree compile_function (const std::string &fn_name, HIR::SelfParam &self_param, std::vector &function_params, diff --git a/gcc/rust/backend/rust-compile-block.cc b/gcc/rust/backend/rust-compile-block.cc index 3f5734378aa5..56d0c417f0ab 100644 --- a/gcc/rust/backend/rust-compile-block.cc +++ b/gcc/rust/backend/rust-compile-block.cc @@ -28,10 +28,10 @@ CompileBlock::CompileBlock (Context *ctx, Bvariable *result) {} tree -CompileBlock::compile (HIR::BlockExpr *expr, Context *ctx, Bvariable *result) +CompileBlock::compile (HIR::BlockExpr &expr, Context *ctx, Bvariable *result) { CompileBlock compiler (ctx, result); - compiler.visit (*expr); + compiler.visit (expr); return compiler.translated; } @@ -60,10 +60,10 @@ CompileBlock::visit (HIR::BlockExpr &expr) if (expr.has_expr ()) { - tree compiled_expr = CompileExpr::Compile (expr.expr.get (), ctx); + tree compiled_expr = CompileExpr::Compile (expr.get_final_expr (), ctx); if (result != nullptr) { - location_t locus = expr.get_final_expr ()->get_locus (); + location_t locus = expr.get_final_expr ().get_locus (); tree result_reference = Backend::var_expression (result, locus); tree assignment @@ -93,10 +93,8 @@ CompileConditionalBlocks::visit (HIR::IfExpr &expr) { fncontext fnctx = ctx->peek_fn (); tree fndecl = fnctx.fndecl; - tree condition_expr - = CompileExpr::Compile (expr.get_if_condition ().get (), ctx); - tree then_block - = CompileBlock::compile (expr.get_if_block ().get (), ctx, result); + tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx); + tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result); translated = Backend::if_statement (fndecl, condition_expr, then_block, NULL, expr.get_locus ()); @@ -107,23 +105,20 @@ CompileConditionalBlocks::visit (HIR::IfExprConseqElse &expr) { fncontext fnctx = ctx->peek_fn (); tree fndecl = fnctx.fndecl; - tree condition_expr - = CompileExpr::Compile (expr.get_if_condition ().get (), ctx); - tree then_block - = CompileBlock::compile (expr.get_if_block ().get (), ctx, result); + tree condition_expr = CompileExpr::Compile (expr.get_if_condition (), ctx); + tree then_block = CompileBlock::compile (expr.get_if_block (), ctx, result); // else block std::vector locals; - location_t start_location = expr.get_else_block ()->get_locus (); - location_t end_location = expr.get_else_block ()->get_locus (); // FIXME + location_t start_location = expr.get_else_block ().get_locus (); + location_t end_location = expr.get_else_block ().get_locus (); // FIXME tree enclosing_scope = ctx->peek_enclosing_scope (); tree else_block = Backend::block (fndecl, enclosing_scope, locals, start_location, end_location); ctx->push_block (else_block); tree else_stmt_decl - = CompileExprWithBlock::compile (expr.get_else_block ().get (), ctx, - result); + = CompileExprWithBlock::compile (&expr.get_else_block (), ctx, result); ctx->add_statement (else_stmt_decl); diff --git a/gcc/rust/backend/rust-compile-block.h b/gcc/rust/backend/rust-compile-block.h index 46a32cfaf867..642ce2cfd15f 100644 --- a/gcc/rust/backend/rust-compile-block.h +++ b/gcc/rust/backend/rust-compile-block.h @@ -28,7 +28,7 @@ namespace Compile { class CompileBlock : private HIRCompileBase { public: - static tree compile (HIR::BlockExpr *expr, Context *ctx, Bvariable *result); + static tree compile (HIR::BlockExpr &expr, Context *ctx, Bvariable *result); protected: void visit (HIR::BlockExpr &expr); @@ -134,7 +134,7 @@ class CompileExprWithBlock : public HIRCompileBase, void visit (HIR::BlockExpr &expr) override { - translated = CompileBlock::compile (&expr, ctx, result); + translated = CompileBlock::compile (expr, ctx, result); } // Empty visit for unused Expression HIR nodes. diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 12c2fc883fca..7d41f635716e 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -40,24 +40,24 @@ CompileExpr::CompileExpr (Context *ctx) {} tree -CompileExpr::Compile (HIR::Expr *expr, Context *ctx) +CompileExpr::Compile (HIR::Expr &expr, Context *ctx) { CompileExpr compiler (ctx); - expr->accept_vis (compiler); + expr.accept_vis (compiler); return compiler.translated; } void CompileExpr::visit (HIR::TupleIndexExpr &expr) { - HIR::Expr *tuple_expr = expr.get_tuple_expr ().get (); + HIR::Expr &tuple_expr = expr.get_tuple_expr (); TupleIndex index = expr.get_tuple_index (); tree receiver_ref = CompileExpr::Compile (tuple_expr, ctx); TyTy::BaseType *tuple_expr_ty = nullptr; bool ok - = ctx->get_tyctx ()->lookup_type (tuple_expr->get_mappings ().get_hirid (), + = ctx->get_tyctx ()->lookup_type (tuple_expr.get_mappings ().get_hirid (), &tuple_expr_ty); rust_assert (ok); @@ -97,7 +97,7 @@ CompileExpr::visit (HIR::TupleExpr &expr) std::vector vals; for (auto &elem : expr.get_tuple_elems ()) { - auto e = CompileExpr::Compile (elem.get (), ctx); + auto e = CompileExpr::Compile (*elem, ctx); vals.push_back (e); } @@ -111,7 +111,7 @@ CompileExpr::visit (HIR::ReturnExpr &expr) auto fncontext = ctx->peek_fn (); tree return_value = expr.has_return_expr () - ? CompileExpr::Compile (expr.return_expr.get (), ctx) + ? CompileExpr::Compile (expr.get_expr (), ctx) : unit_expression (expr.get_locus ()); if (expr.has_return_expr ()) @@ -141,8 +141,8 @@ void CompileExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) { auto op = expr.get_expr_type (); - auto lhs = CompileExpr::Compile (expr.get_lhs ().get (), ctx); - auto rhs = CompileExpr::Compile (expr.get_rhs ().get (), ctx); + auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx); + auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx); // this might be an operator overload situation lets check TyTy::FnType *fntype; @@ -153,8 +153,7 @@ CompileExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) auto lang_item_type = LangItem::OperatorToLangItem (expr.get_expr_type ()); translated = resolve_operator_overload (lang_item_type, expr, lhs, rhs, - expr.get_lhs ().get (), - expr.get_rhs ().get ()); + expr.get_lhs (), expr.get_rhs ()); return; } @@ -185,8 +184,8 @@ void CompileExpr::visit (HIR::CompoundAssignmentExpr &expr) { auto op = expr.get_expr_type (); - auto lhs = CompileExpr::Compile (expr.get_lhs ().get (), ctx); - auto rhs = CompileExpr::Compile (expr.get_rhs ().get (), ctx); + auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx); + auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx); // this might be an operator overload situation lets check TyTy::FnType *fntype; @@ -198,8 +197,7 @@ CompileExpr::visit (HIR::CompoundAssignmentExpr &expr) expr.get_expr_type ()); auto compound_assignment = resolve_operator_overload (lang_item_type, expr, lhs, rhs, - expr.get_lhs ().get (), - expr.get_rhs ().get ()); + expr.get_lhs (), expr.get_rhs ()); ctx->add_statement (compound_assignment); return; @@ -236,18 +234,18 @@ CompileExpr::visit (HIR::NegationExpr &expr) { auto op = expr.get_expr_type (); - const auto literal_expr = expr.get_expr ().get (); + auto &literal_expr = expr.get_expr (); // If it's a negated integer/float literal, we can return early if (op == NegationOperator::NEGATE - && literal_expr->get_expression_type () == HIR::Expr::ExprType::Lit) + && literal_expr.get_expression_type () == HIR::Expr::ExprType::Lit) { - auto new_literal_expr = static_cast (literal_expr); - auto lit_type = new_literal_expr->get_lit_type (); + auto &new_literal_expr = static_cast (literal_expr); + auto lit_type = new_literal_expr.get_lit_type (); if (lit_type == HIR::Literal::LitType::INT || lit_type == HIR::Literal::LitType::FLOAT) { - new_literal_expr->set_negative (); + new_literal_expr.set_negative (); translated = CompileExpr::Compile (literal_expr, ctx); return; } @@ -265,7 +263,7 @@ CompileExpr::visit (HIR::NegationExpr &expr) auto lang_item_type = LangItem::NegationOperatorToLangItem (op); translated = resolve_operator_overload (lang_item_type, expr, negated_expr, - nullptr, expr.get_expr ().get (), nullptr); + nullptr, expr.get_expr (), tl::nullopt); return; } @@ -276,8 +274,8 @@ void CompileExpr::visit (HIR::ComparisonExpr &expr) { auto op = expr.get_expr_type (); - auto lhs = CompileExpr::Compile (expr.get_lhs ().get (), ctx); - auto rhs = CompileExpr::Compile (expr.get_rhs ().get (), ctx); + auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx); + auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx); auto location = expr.get_locus (); translated = Backend::comparison_expression (op, lhs, rhs, location); @@ -287,8 +285,8 @@ void CompileExpr::visit (HIR::LazyBooleanExpr &expr) { auto op = expr.get_expr_type (); - auto lhs = CompileExpr::Compile (expr.get_lhs ().get (), ctx); - auto rhs = CompileExpr::Compile (expr.get_rhs ().get (), ctx); + auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx); + auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx); auto location = expr.get_locus (); translated = Backend::lazy_boolean_expression (op, lhs, rhs, location); @@ -307,14 +305,14 @@ CompileExpr::visit (HIR::TypeCastExpr &expr) TyTy::BaseType *casted_tyty = nullptr; if (!ctx->get_tyctx ()->lookup_type ( - expr.get_casted_expr ()->get_mappings ().get_hirid (), &casted_tyty)) + expr.get_casted_expr ().get_mappings ().get_hirid (), &casted_tyty)) { translated = error_mark_node; return; } auto type_to_cast_to = TyTyResolveCompile::compile (ctx, type_to_cast_to_ty); - auto casted_expr = CompileExpr::Compile (expr.get_casted_expr ().get (), ctx); + auto casted_expr = CompileExpr::Compile (expr.get_casted_expr (), ctx); std::vector *adjustments = nullptr; bool ok = ctx->get_tyctx ()->lookup_cast_autoderef_mappings ( @@ -405,7 +403,7 @@ CompileExpr::visit (HIR::BlockExpr &expr) &ret_var_stmt); ctx->add_statement (ret_var_stmt); - auto block_stmt = CompileBlock::compile (&expr, ctx, tmp); + auto block_stmt = CompileBlock::compile (expr, ctx, tmp); rust_assert (TREE_CODE (block_stmt) == BIND_EXPR); ctx->add_statement (block_stmt); @@ -415,7 +413,7 @@ CompileExpr::visit (HIR::BlockExpr &expr) void CompileExpr::visit (HIR::UnsafeBlockExpr &expr) { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void @@ -487,7 +485,7 @@ CompileExpr::visit (HIR::StructExprStructFields &struct_expr) auto lvalue_locus = ctx->get_mappings ().lookup_location (expected->get_ty_ref ()); auto rvalue_locus = argument->get_locus (); - auto rvalue = CompileStructExprField::Compile (argument.get (), ctx); + auto rvalue = CompileStructExprField::Compile (*argument, ctx); TyTy::BaseType *actual = nullptr; bool ok = ctx->get_tyctx ()->lookup_type ( @@ -519,7 +517,7 @@ CompileExpr::visit (HIR::StructExprStructFields &struct_expr) auto lvalue_locus = ctx->get_mappings ().lookup_location (expected->get_ty_ref ()); auto rvalue_locus = argument->get_locus (); - auto rvalue = CompileStructExprField::Compile (argument.get (), ctx); + auto rvalue = CompileStructExprField::Compile (*argument, ctx); TyTy::BaseType *actual = nullptr; bool ok = ctx->get_tyctx ()->lookup_type ( @@ -544,7 +542,7 @@ CompileExpr::visit (HIR::StructExprStructFields &struct_expr) std::vector ctor_arguments; if (adt->is_enum ()) { - HIR::Expr *discrim_expr = variant->get_discriminant (); + HIR::Expr &discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); tree folded_discrim_expr = fold_expr (discrim_expr_node); tree qualifier = folded_discrim_expr; @@ -563,21 +561,21 @@ CompileExpr::visit (HIR::StructExprStructFields &struct_expr) void CompileExpr::visit (HIR::GroupedExpr &expr) { - translated = CompileExpr::Compile (expr.get_expr_in_parens ().get (), ctx); + translated = CompileExpr::Compile (expr.get_expr_in_parens (), ctx); } void CompileExpr::visit (HIR::FieldAccessExpr &expr) { - HIR::Expr *receiver_expr = expr.get_receiver_expr ().get (); + HIR::Expr &receiver_expr = expr.get_receiver_expr (); tree receiver_ref = CompileExpr::Compile (receiver_expr, ctx); // resolve the receiver back to ADT type TyTy::BaseType *receiver = nullptr; if (!ctx->get_tyctx ()->lookup_type ( - expr.get_receiver_expr ()->get_mappings ().get_hirid (), &receiver)) + expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver)) { - rust_error_at (expr.get_receiver_expr ()->get_locus (), + rust_error_at (expr.get_receiver_expr ().get_locus (), "unresolved type for receiver"); return; } @@ -672,7 +670,7 @@ CompileExpr::visit (HIR::LoopExpr &expr) ctx->push_loop_begin_label (loop_begin_label); tree code_block - = CompileBlock::compile (expr.get_loop_block ().get (), ctx, nullptr); + = CompileBlock::compile (expr.get_loop_block (), ctx, nullptr); tree loop_expr = Backend::loop_expression (code_block, expr.get_locus ()); ctx->add_statement (loop_expr); @@ -699,8 +697,8 @@ CompileExpr::visit (HIR::WhileLoopExpr &expr) } std::vector locals; - location_t start_location = expr.get_loop_block ()->get_locus (); - location_t end_location = expr.get_loop_block ()->get_locus (); // FIXME + location_t start_location = expr.get_loop_block ().get_locus (); + location_t end_location = expr.get_loop_block ().get_locus (); // FIXME tree enclosing_scope = ctx->peek_enclosing_scope (); tree loop_block = Backend::block (fnctx.fndecl, enclosing_scope, locals, @@ -713,15 +711,14 @@ CompileExpr::visit (HIR::WhileLoopExpr &expr) ctx->add_statement (loop_begin_label_decl); ctx->push_loop_begin_label (loop_begin_label); - tree condition - = CompileExpr::Compile (expr.get_predicate_expr ().get (), ctx); + tree condition = CompileExpr::Compile (expr.get_predicate_expr (), ctx); tree exit_condition = fold_build1_loc (expr.get_locus (), TRUTH_NOT_EXPR, boolean_type_node, condition); tree exit_expr = Backend::exit_expression (exit_condition, expr.get_locus ()); ctx->add_statement (exit_expr); tree code_block_stmt - = CompileBlock::compile (expr.get_loop_block ().get (), ctx, nullptr); + = CompileBlock::compile (expr.get_loop_block (), ctx, nullptr); rust_assert (TREE_CODE (code_block_stmt) == BIND_EXPR); ctx->add_statement (code_block_stmt); @@ -737,12 +734,12 @@ CompileExpr::visit (HIR::BreakExpr &expr) { if (expr.has_break_expr ()) { - tree compiled_expr = CompileExpr::Compile (expr.get_expr ().get (), ctx); + tree compiled_expr = CompileExpr::Compile (expr.get_expr (), ctx); Bvariable *loop_result_holder = ctx->peek_loop_context (); tree result_reference = Backend::var_expression (loop_result_holder, - expr.get_expr ()->get_locus ()); + expr.get_expr ().get_locus ()); tree assignment = Backend::assignment_statement (result_reference, compiled_expr, @@ -865,7 +862,7 @@ CompileExpr::visit (HIR::ContinueExpr &expr) void CompileExpr::visit (HIR::BorrowExpr &expr) { - tree main_expr = CompileExpr::Compile (expr.get_expr ().get (), ctx); + tree main_expr = CompileExpr::Compile (expr.get_expr (), ctx); if (RS_DST_FLAG_P (TREE_TYPE (main_expr))) { translated = main_expr; @@ -892,7 +889,7 @@ CompileExpr::visit (HIR::DereferenceExpr &expr) return; } - tree main_expr = CompileExpr::Compile (expr.get_expr ().get (), ctx); + tree main_expr = CompileExpr::Compile (expr.get_expr (), ctx); // this might be an operator overload situation lets check TyTy::FnType *fntype; @@ -903,7 +900,7 @@ CompileExpr::visit (HIR::DereferenceExpr &expr) auto lang_item_type = LangItem::Kind::DEREF; tree operator_overload_call = resolve_operator_overload (lang_item_type, expr, main_expr, nullptr, - expr.get_expr ().get (), nullptr); + expr.get_expr (), tl::nullopt); // rust deref always returns a reference from this overload then we can // actually do the indirection @@ -963,8 +960,8 @@ CompileExpr::visit (HIR::LiteralExpr &expr) void CompileExpr::visit (HIR::AssignmentExpr &expr) { - auto lvalue = CompileExpr::Compile (expr.get_lhs ().get (), ctx); - auto rvalue = CompileExpr::Compile (expr.get_rhs ().get (), ctx); + auto lvalue = CompileExpr::Compile (expr.get_lhs (), ctx); + auto rvalue = CompileExpr::Compile (expr.get_rhs (), ctx); // assignments are coercion sites so lets convert the rvalue if necessary TyTy::BaseType *expected = nullptr; @@ -972,16 +969,16 @@ CompileExpr::visit (HIR::AssignmentExpr &expr) bool ok; ok = ctx->get_tyctx ()->lookup_type ( - expr.get_lhs ()->get_mappings ().get_hirid (), &expected); + expr.get_lhs ().get_mappings ().get_hirid (), &expected); rust_assert (ok); ok = ctx->get_tyctx ()->lookup_type ( - expr.get_rhs ()->get_mappings ().get_hirid (), &actual); + expr.get_rhs ().get_mappings ().get_hirid (), &actual); rust_assert (ok); rvalue = coercion_site (expr.get_mappings ().get_hirid (), rvalue, actual, - expected, expr.get_lhs ()->get_locus (), - expr.get_rhs ()->get_locus ()); + expected, expr.get_lhs ().get_locus (), + expr.get_rhs ().get_locus ()); // rust_debug_loc (expr.get_locus (), "XXXXXX assignment"); // debug_tree (rvalue); @@ -1002,7 +999,7 @@ check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx) { TyTy::BaseType *scrutinee_expr_tyty = nullptr; if (!ctx->get_tyctx ()->lookup_type ( - expr.get_scrutinee_expr ()->get_mappings ().get_hirid (), + expr.get_scrutinee_expr ().get_mappings ().get_hirid (), &scrutinee_expr_tyty)) { return TyTy::TypeKind::ERROR; @@ -1080,7 +1077,7 @@ CompileExpr::visit (HIR::MatchExpr &expr) // lets compile the scrutinee expression tree match_scrutinee_rval - = CompileExpr::Compile (expr.get_scrutinee_expr ().get (), ctx); + = CompileExpr::Compile (expr.get_scrutinee_expr (), ctx); Bvariable *match_scrutinee_tmp_var = Backend::temporary_variable (fnctx.fndecl, enclosing_scope, @@ -1090,7 +1087,7 @@ CompileExpr::visit (HIR::MatchExpr &expr) ctx->add_statement (ret_var_stmt); tree match_scrutinee_expr = match_scrutinee_tmp_var->get_tree ( - expr.get_scrutinee_expr ()->get_locus ()); + expr.get_scrutinee_expr ().get_locus ()); tree assignment = Backend::assignment_statement (match_scrutinee_expr, match_scrutinee_rval, @@ -1123,14 +1120,13 @@ CompileExpr::visit (HIR::MatchExpr &expr) ctx->push_block (arm_body_block); // setup the bindings for the block - CompilePatternBindings::Compile (kase_pattern.get (), - match_scrutinee_expr, ctx); + CompilePatternBindings::Compile (*kase_pattern, match_scrutinee_expr, + ctx); // compile the expr and setup the assignment if required when tmp != // NULL location_t arm_locus = kase_arm.get_locus (); - tree kase_expr_tree - = CompileExpr::Compile (kase.get_expr ().get (), ctx); + tree kase_expr_tree = CompileExpr::Compile (kase.get_expr (), ctx); tree result_reference = Backend::var_expression (tmp, arm_locus); tree assignment = Backend::assignment_statement (result_reference, kase_expr_tree, @@ -1145,7 +1141,7 @@ CompileExpr::visit (HIR::MatchExpr &expr) ctx->pop_block (); tree check_expr - = CompilePatternCheckExpr::Compile (kase_pattern.get (), + = CompilePatternCheckExpr::Compile (*kase_pattern, match_scrutinee_expr, ctx); tree check_stmt @@ -1167,7 +1163,7 @@ CompileExpr::visit (HIR::CallExpr &expr) { TyTy::BaseType *tyty = nullptr; if (!ctx->get_tyctx ()->lookup_type ( - expr.get_fnexpr ()->get_mappings ().get_hirid (), &tyty)) + expr.get_fnexpr ().get_mappings ().get_hirid (), &tyty)) { rust_error_at (expr.get_locus (), "unknown type"); return; @@ -1193,7 +1189,7 @@ CompileExpr::visit (HIR::CallExpr &expr) { HirId variant_id; bool ok = ctx->get_tyctx ()->lookup_variant_definition ( - expr.get_fnexpr ()->get_mappings ().get_hirid (), &variant_id); + expr.get_fnexpr ().get_mappings ().get_hirid (), &variant_id); rust_assert (ok); ok = adt->lookup_variant_by_id (variant_id, &variant, @@ -1207,7 +1203,7 @@ CompileExpr::visit (HIR::CallExpr &expr) for (size_t i = 0; i < expr.get_arguments ().size (); i++) { auto &argument = expr.get_arguments ().at (i); - auto rvalue = CompileExpr::Compile (argument.get (), ctx); + auto rvalue = CompileExpr::Compile (*argument, ctx); // assignments are coercion sites so lets convert the rvalue if // necessary @@ -1236,7 +1232,7 @@ CompileExpr::visit (HIR::CallExpr &expr) std::vector ctor_arguments; if (adt->is_enum ()) { - HIR::Expr *discrim_expr = variant->get_discriminant (); + HIR::Expr &discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); tree folded_discrim_expr = fold_expr (discrim_expr_node); tree qualifier = folded_discrim_expr; @@ -1270,13 +1266,13 @@ CompileExpr::visit (HIR::CallExpr &expr) } const TyTy::FnType *fn = static_cast (base); - auto param = fn->param_at (index); + auto ¶m = fn->param_at (index); *result = param.second; return true; }; - auto fn_address = CompileExpr::Compile (expr.get_fnexpr ().get (), ctx); + auto fn_address = CompileExpr::Compile (expr.get_fnexpr (), ctx); // is this a closure call? bool possible_trait_call @@ -1303,7 +1299,7 @@ CompileExpr::visit (HIR::CallExpr &expr) for (size_t i = 0; i < expr.get_arguments ().size (); i++) { auto &argument = expr.get_arguments ().at (i); - auto rvalue = CompileExpr::Compile (argument.get (), ctx); + auto rvalue = CompileExpr::Compile (*argument, ctx); if (is_variadic && i >= required_num_args) { @@ -1343,7 +1339,7 @@ void CompileExpr::visit (HIR::MethodCallExpr &expr) { // method receiver - tree self = CompileExpr::Compile (expr.get_receiver ().get (), ctx); + tree self = CompileExpr::Compile (expr.get_receiver (), ctx); // lookup the expected function type TyTy::BaseType *lookup_fntype = nullptr; @@ -1383,7 +1379,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr) // lookup the autoderef mappings HirId autoderef_mappings_id - = expr.get_receiver ()->get_mappings ().get_hirid (); + = expr.get_receiver ().get_mappings ().get_hirid (); std::vector *adjustments = nullptr; ok = ctx->get_tyctx ()->lookup_autoderef_mappings (autoderef_mappings_id, &adjustments); @@ -1391,7 +1387,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr) // apply adjustments for the fn call self = resolve_adjustements (*adjustments, self, - expr.get_receiver ()->get_locus ()); + expr.get_receiver ().get_locus ()); std::vector args; args.push_back (self); // adjusted self @@ -1400,7 +1396,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr) for (size_t i = 0; i < expr.get_arguments ().size (); i++) { auto &argument = expr.get_arguments ().at (i); - auto rvalue = CompileExpr::Compile (argument.get (), ctx); + auto rvalue = CompileExpr::Compile (*argument, ctx); // assignments are coercion sites so lets convert the rvalue if // necessary, offset from the already adjusted implicit self @@ -1482,8 +1478,8 @@ CompileExpr::get_receiver_from_dyn (const TyTy::DynamicObjectType *dyn, tree CompileExpr::resolve_operator_overload (LangItem::Kind lang_item_type, HIR::OperatorExprMeta expr, tree lhs, - tree rhs, HIR::Expr *lhs_expr, - HIR::Expr *rhs_expr) + tree rhs, HIR::Expr &lhs_expr, + tl::optional rhs_expr) { TyTy::FnType *fntype; bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload ( @@ -1514,7 +1510,7 @@ CompileExpr::resolve_operator_overload (LangItem::Kind lang_item_type, rust_assert (ok); // apply adjustments for the fn call - tree self = resolve_adjustements (*adjustments, lhs, lhs_expr->get_locus ()); + tree self = resolve_adjustements (*adjustments, lhs, lhs_expr.get_locus ()); std::vector args; args.push_back (self); // adjusted self @@ -1829,7 +1825,7 @@ CompileExpr::visit (HIR::ArrayExpr &expr) const TyTy::ArrayType &array_tyty = static_cast (*tyty); - HIR::ArrayElems &elements = *expr.get_internal_elements (); + HIR::ArrayElems &elements = expr.get_internal_elements (); switch (elements.get_array_expr_type ()) { case HIR::ArrayElems::ArrayExprType::VALUES: { @@ -1858,7 +1854,7 @@ CompileExpr::array_value_expr (location_t expr_locus, size_t i = 0; for (auto &elem : elems.get_values ()) { - tree translated_expr = CompileExpr::Compile (elem.get (), ctx); + tree translated_expr = CompileExpr::Compile (*elem, ctx); constructor.push_back (translated_expr); indexes.push_back (i++); } @@ -1885,8 +1881,7 @@ CompileExpr::array_copied_expr (location_t expr_locus, } ctx->push_const_context (); - tree capacity_expr - = CompileExpr::Compile (elems.get_num_copies_expr ().get (), ctx); + tree capacity_expr = CompileExpr::Compile (elems.get_num_copies_expr (), ctx); ctx->pop_const_context (); if (!TREE_CONSTANT (capacity_expr)) @@ -1896,8 +1891,7 @@ CompileExpr::array_copied_expr (location_t expr_locus, } // get the compiled value - tree translated_expr - = CompileExpr::Compile (elems.get_elem_to_copy ().get (), ctx); + tree translated_expr = CompileExpr::Compile (elems.get_elem_to_copy (), ctx); tree max_domain = TYPE_MAX_VALUE (domain); tree min_domain = TYPE_MIN_VALUE (domain); @@ -2102,8 +2096,8 @@ HIRCompileBase::resolve_unsized_dyn_adjustment ( void CompileExpr::visit (HIR::RangeFromToExpr &expr) { - tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx); - tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx); + tree from = CompileExpr::Compile (expr.get_from_expr (), ctx); + tree to = CompileExpr::Compile (expr.get_to_expr (), ctx); if (from == error_mark_node || to == error_mark_node) { translated = error_mark_node; @@ -2125,7 +2119,7 @@ CompileExpr::visit (HIR::RangeFromToExpr &expr) void CompileExpr::visit (HIR::RangeFromExpr &expr) { - tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx); + tree from = CompileExpr::Compile (expr.get_from_expr (), ctx); if (from == error_mark_node) { translated = error_mark_node; @@ -2147,7 +2141,7 @@ CompileExpr::visit (HIR::RangeFromExpr &expr) void CompileExpr::visit (HIR::RangeToExpr &expr) { - tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx); + tree to = CompileExpr::Compile (expr.get_to_expr (), ctx); if (to == error_mark_node) { translated = error_mark_node; @@ -2182,8 +2176,8 @@ CompileExpr::visit (HIR::RangeFullExpr &expr) void CompileExpr::visit (HIR::RangeFromToInclExpr &expr) { - tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx); - tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx); + tree from = CompileExpr::Compile (expr.get_from_expr (), ctx); + tree to = CompileExpr::Compile (expr.get_to_expr (), ctx); if (from == error_mark_node || to == error_mark_node) { translated = error_mark_node; @@ -2205,9 +2199,8 @@ CompileExpr::visit (HIR::RangeFromToInclExpr &expr) void CompileExpr::visit (HIR::ArrayIndexExpr &expr) { - tree array_reference - = CompileExpr::Compile (expr.get_array_expr ().get (), ctx); - tree index = CompileExpr::Compile (expr.get_index_expr ().get (), ctx); + tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx); + tree index = CompileExpr::Compile (expr.get_index_expr (), ctx); // this might be an core::ops::index lang item situation TyTy::FnType *fntype; @@ -2218,8 +2211,8 @@ CompileExpr::visit (HIR::ArrayIndexExpr &expr) auto lang_item_type = LangItem::Kind::INDEX; tree operator_overload_call = resolve_operator_overload (lang_item_type, expr, array_reference, - index, expr.get_array_expr ().get (), - expr.get_index_expr ().get ()); + index, expr.get_array_expr (), + expr.get_index_expr ()); tree actual_type = TREE_TYPE (operator_overload_call); bool can_indirect = TYPE_PTR_P (actual_type) || TYPE_REF_P (actual_type); @@ -2241,7 +2234,7 @@ CompileExpr::visit (HIR::ArrayIndexExpr &expr) // indirection if required TyTy::BaseType *array_expr_ty = nullptr; bool ok = ctx->get_tyctx ()->lookup_type ( - expr.get_array_expr ()->get_mappings ().get_hirid (), &array_expr_ty); + expr.get_array_expr ().get_mappings ().get_hirid (), &array_expr_ty); rust_assert (ok); // do we need to add an indirect reference @@ -2392,7 +2385,7 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr, = Backend::struct_field_expression (args_param_expr, i, closure_param.get_locus ()); - CompilePatternBindings::Compile (closure_param.get_pattern ().get (), + CompilePatternBindings::Compile (closure_param.get_pattern (), compiled_param_var, ctx); i++; } @@ -2404,13 +2397,13 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr, } // lookup locals - HIR::Expr *function_body = expr.get_expr ().get (); + HIR::Expr &function_body = expr.get_expr (); bool is_block_expr - = function_body->get_expression_type () == HIR::Expr::ExprType::Block; + = function_body.get_expression_type () == HIR::Expr::ExprType::Block; if (is_block_expr) { - auto body_mappings = function_body->get_mappings (); + auto body_mappings = function_body.get_mappings (); if (flag_name_resolution_2_0) { auto nr_ctx @@ -2431,13 +2424,13 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr, } tree enclosing_scope = NULL_TREE; - location_t start_location = function_body->get_locus (); - location_t end_location = function_body->get_locus (); + location_t start_location = function_body.get_locus (); + location_t end_location = function_body.get_locus (); if (is_block_expr) { - HIR::BlockExpr *body = static_cast (function_body); - start_location = body->get_locus (); - end_location = body->get_end_locus (); + auto &body = static_cast (function_body); + start_location = body.get_locus (); + end_location = body.get_end_locus (); } tree code_block = Backend::block (fndecl, enclosing_scope, {} /*locals*/, @@ -2462,15 +2455,14 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr, if (is_block_expr) { - HIR::BlockExpr *body = static_cast (function_body); - compile_function_body (fndecl, *body, tyret); + auto &body = static_cast (function_body); + compile_function_body (fndecl, body, tyret); } else { tree value = CompileExpr::Compile (function_body, ctx); tree return_expr - = Backend::return_statement (fndecl, value, - function_body->get_locus ()); + = Backend::return_statement (fndecl, value, function_body.get_locus ()); ctx->add_statement (return_expr); } @@ -2557,8 +2549,8 @@ CompileExpr::generate_possible_fn_trait_call (HIR::CallExpr &expr, } // need to apply any autoderef's to the self argument - HIR::Expr *fnexpr = expr.get_fnexpr ().get (); - HirId autoderef_mappings_id = fnexpr->get_mappings ().get_hirid (); + HIR::Expr &fnexpr = expr.get_fnexpr (); + HirId autoderef_mappings_id = fnexpr.get_mappings ().get_hirid (); std::vector *adjustments = nullptr; bool ok = ctx->get_tyctx ()->lookup_autoderef_mappings (autoderef_mappings_id, &adjustments); @@ -2571,7 +2563,7 @@ CompileExpr::generate_possible_fn_trait_call (HIR::CallExpr &expr, std::vector tuple_arg_vals; for (auto &argument : expr.get_arguments ()) { - auto rvalue = CompileExpr::Compile (argument.get (), ctx); + auto rvalue = CompileExpr::Compile (*argument, ctx); tuple_arg_vals.push_back (rvalue); } diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 871111bb007c..872b510090b6 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -28,7 +28,7 @@ namespace Compile { class CompileExpr : private HIRCompileBase, protected HIR::HIRExpressionVisitor { public: - static tree Compile (HIR::Expr *expr, Context *ctx); + static tree Compile (HIR::Expr &expr, Context *ctx); void visit (HIR::TupleIndexExpr &expr) override; void visit (HIR::TupleExpr &expr) override; @@ -98,8 +98,8 @@ class CompileExpr : private HIRCompileBase, protected HIR::HIRExpressionVisitor tree resolve_operator_overload (LangItem::Kind lang_item_type, HIR::OperatorExprMeta expr, tree lhs, - tree rhs, HIR::Expr *lhs_expr, - HIR::Expr *rhs_expr); + tree rhs, HIR::Expr &lhs_expr, + tl::optional rhs_expr); tree compile_bool_literal (const HIR::LiteralExpr &expr, const TyTy::BaseType *tyty); diff --git a/gcc/rust/backend/rust-compile-fnparam.cc b/gcc/rust/backend/rust-compile-fnparam.cc index 68066b8463d0..f73e23359252 100644 --- a/gcc/rust/backend/rust-compile-fnparam.cc +++ b/gcc/rust/backend/rust-compile-fnparam.cc @@ -31,20 +31,20 @@ CompileFnParam::CompileFnParam (Context *ctx, tree fndecl, tree decl_type, {} Bvariable * -CompileFnParam::compile (Context *ctx, tree fndecl, HIR::FunctionParam *param, +CompileFnParam::compile (Context *ctx, tree fndecl, HIR::FunctionParam ¶m, tree decl_type, location_t locus) { CompileFnParam compiler (ctx, fndecl, decl_type, locus); - param->get_param_name ()->accept_vis (compiler); + param.get_param_name ().accept_vis (compiler); return compiler.compiled_param; } Bvariable * -CompileFnParam::compile (Context *ctx, tree fndecl, HIR::Pattern *param, +CompileFnParam::compile (Context *ctx, tree fndecl, HIR::Pattern ¶m, tree decl_type, location_t locus) { CompileFnParam compiler (ctx, fndecl, decl_type, locus); - param->accept_vis (compiler); + param.accept_vis (compiler); return compiler.compiled_param; } @@ -72,21 +72,21 @@ void CompileFnParam::visit (HIR::StructPattern &pattern) { tree tmp_param_var = create_tmp_param_var (decl_type); - CompilePatternBindings::Compile (&pattern, tmp_param_var, ctx); + CompilePatternBindings::Compile (pattern, tmp_param_var, ctx); } void CompileFnParam::visit (HIR::TupleStructPattern &pattern) { tree tmp_param_var = create_tmp_param_var (decl_type); - CompilePatternBindings::Compile (&pattern, tmp_param_var, ctx); + CompilePatternBindings::Compile (pattern, tmp_param_var, ctx); } void CompileFnParam::visit (HIR::ReferencePattern &pattern) { tree tmp_param_var = create_tmp_param_var (decl_type); - CompilePatternBindings::Compile (&pattern, tmp_param_var, ctx); + CompilePatternBindings::Compile (pattern, tmp_param_var, ctx); } Bvariable * diff --git a/gcc/rust/backend/rust-compile-fnparam.h b/gcc/rust/backend/rust-compile-fnparam.h index 9b50b1934680..c36f7914f346 100644 --- a/gcc/rust/backend/rust-compile-fnparam.h +++ b/gcc/rust/backend/rust-compile-fnparam.h @@ -29,9 +29,9 @@ class CompileFnParam : private HIRCompileBase, protected HIR::HIRPatternVisitor { public: static Bvariable *compile (Context *ctx, tree fndecl, - HIR::FunctionParam *param, tree decl_type, + HIR::FunctionParam ¶m, tree decl_type, location_t locus); - static Bvariable *compile (Context *ctx, tree fndecl, HIR::Pattern *param, + static Bvariable *compile (Context *ctx, tree fndecl, HIR::Pattern ¶m, tree decl_type, location_t locus); void visit (HIR::IdentifierPattern &pattern) override; diff --git a/gcc/rust/backend/rust-compile-implitem.cc b/gcc/rust/backend/rust-compile-implitem.cc index 73b977f76872..49e18232af6f 100644 --- a/gcc/rust/backend/rust-compile-implitem.cc +++ b/gcc/rust/backend/rust-compile-implitem.cc @@ -44,7 +44,7 @@ CompileTraitItem::visit (HIR::TraitItemConst &constant) rust_assert (canonical_path); - HIR::Expr *const_value_expr = constant.get_expr ().get (); + HIR::Expr &const_value_expr = constant.get_expr (); tree const_expr = compile_constant_item (resolved_type, *canonical_path, const_value_expr, constant.get_locus ()); @@ -114,7 +114,7 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func) 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); + &func.get_block_expr (), *canonical_path, fntype); reference = address_expression (fndecl, ref_locus); } diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index bc99ef942631..0ffc3634e130 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -321,7 +321,7 @@ compile_fn_params (Context *ctx, TyTy::FnType *fntype, tree fndecl, location_t param_locus = referenced_param->get_locus (); Bvariable *compiled_param_var - = CompileFnParam::compile (ctx, fndecl, referenced_param, + = CompileFnParam::compile (ctx, fndecl, *referenced_param, compiled_param_type, param_locus); compiled_param_variables->push_back (compiled_param_var); diff --git a/gcc/rust/backend/rust-compile-item.cc b/gcc/rust/backend/rust-compile-item.cc index 5fdc9cd6a679..8537d818791e 100644 --- a/gcc/rust/backend/rust-compile-item.cc +++ b/gcc/rust/backend/rust-compile-item.cc @@ -60,7 +60,7 @@ CompileItem::visit (HIR::StaticItem &var) rust_assert (canonical_path.has_value ()); - HIR::Expr *const_value_expr = var.get_expr ().get (); + HIR::Expr &const_value_expr = var.get_expr (); ctx->push_const_context (); tree value = compile_constant_item (resolved_type, *canonical_path, const_value_expr, var.get_locus ()); @@ -120,7 +120,7 @@ CompileItem::visit (HIR::ConstantItem &constant) .value (); } - HIR::Expr *const_value_expr = constant.get_expr ().get (); + HIR::Expr &const_value_expr = constant.get_expr (); ctx->push_const_context (); tree const_expr = compile_constant_item (resolved_type, canonical_path, const_value_expr, @@ -222,8 +222,7 @@ CompileItem::visit (HIR::Function &function) 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.get_definition (), canonical_path, fntype); reference = address_expression (fndecl, ref_locus); if (function.get_qualifiers ().is_const ()) @@ -235,7 +234,7 @@ 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)) + impl_block.get_type ().get_mappings ().get_hirid (), &self_lookup)) { rust_error_at (impl_block.get_locus (), "failed to resolve type of impl"); return; diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index c462a6d7a025..a1d2e68914f6 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -67,7 +67,7 @@ CompilePatternCheckExpr::visit (HIR::PathInExpression &pattern) // must be enum match_scrutinee_expr = scrutinee_expr_qualifier_expr; - HIR::Expr *discrim_expr = variant->get_discriminant (); + HIR::Expr &discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); check_expr @@ -80,10 +80,9 @@ void CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern) { // Compile the literal - HIR::LiteralExpr *litexpr - = new HIR::LiteralExpr (pattern.get_mappings (), pattern.get_literal (), - pattern.get_locus (), - std::vector ()); + auto litexpr = Rust::make_unique ( + HIR::LiteralExpr (pattern.get_mappings (), pattern.get_literal (), + pattern.get_locus (), std::vector ())); // Note: Floating point literals are currently accepted but will likely be // forbidden in LiteralPatterns in a future version of Rust. @@ -95,7 +94,7 @@ CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern) rust_sorry_at (pattern.get_locus (), "floating-point literal in pattern"); } - tree lit = CompileExpr::Compile (litexpr, ctx); + tree lit = CompileExpr::Compile (*litexpr, ctx); check_expr = Backend::comparison_expression (ComparisonOperator::EQUAL, match_scrutinee_expr, lit, @@ -103,19 +102,17 @@ CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern) } static tree -compile_range_pattern_bound (HIR::RangePatternBound *bound, +compile_range_pattern_bound (HIR::RangePatternBound &bound, Analysis::NodeMapping mappings, location_t locus, Context *ctx) { tree result = NULL_TREE; - switch (bound->get_bound_type ()) + switch (bound.get_bound_type ()) { case HIR::RangePatternBound::RangePatternBoundType::LITERAL: { - HIR::RangePatternBoundLiteral &ref - = *static_cast (bound); + auto &ref = static_cast (bound); - HIR::LiteralExpr *litexpr - = new HIR::LiteralExpr (mappings, ref.get_literal (), locus, + HIR::LiteralExpr litexpr (mappings, ref.get_literal (), locus, std::vector ()); result = CompileExpr::Compile (litexpr, ctx); @@ -123,8 +120,7 @@ compile_range_pattern_bound (HIR::RangePatternBound *bound, break; case HIR::RangePatternBound::RangePatternBoundType::PATH: { - HIR::RangePatternBoundPath &ref - = *static_cast (bound); + auto &ref = static_cast (bound); result = ResolvePathRef::Compile (ref.get_path (), ctx); @@ -134,8 +130,7 @@ compile_range_pattern_bound (HIR::RangePatternBound *bound, break; case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { - HIR::RangePatternBoundQualPath &ref - = *static_cast (bound); + auto &ref = static_cast (bound); result = ResolvePathRef::Compile (ref.get_qualified_path (), ctx); @@ -150,10 +145,10 @@ compile_range_pattern_bound (HIR::RangePatternBound *bound, void CompilePatternCheckExpr::visit (HIR::RangePattern &pattern) { - tree upper = compile_range_pattern_bound (pattern.get_upper_bound ().get (), + tree upper = compile_range_pattern_bound (pattern.get_upper_bound (), pattern.get_mappings (), pattern.get_locus (), ctx); - tree lower = compile_range_pattern_bound (pattern.get_lower_bound ().get (), + tree lower = compile_range_pattern_bound (pattern.get_lower_bound (), pattern.get_mappings (), pattern.get_locus (), ctx); @@ -175,7 +170,7 @@ CompilePatternCheckExpr::visit (HIR::ReferencePattern &pattern) { match_scrutinee_expr = indirect_expression (match_scrutinee_expr, pattern.get_locus ()); - pattern.get_referenced_pattern ()->accept_vis (*this); + pattern.get_referenced_pattern ().accept_vis (*this); } void @@ -183,14 +178,13 @@ CompilePatternCheckExpr::visit (HIR::AltPattern &pattern) { auto &alts = pattern.get_alts (); - check_expr = CompilePatternCheckExpr::Compile (alts.at (0).get (), + check_expr = CompilePatternCheckExpr::Compile (*alts.at (0), match_scrutinee_expr, ctx); auto end = alts.end (); for (auto i = alts.begin () + 1; i != end; i++) { tree next_expr - = CompilePatternCheckExpr::Compile (i->get (), match_scrutinee_expr, - ctx); + = CompilePatternCheckExpr::Compile (**i, match_scrutinee_expr, ctx); check_expr = Backend::arithmetic_or_logical_expression ( ArithmeticOrLogicalOperator::BITWISE_OR, check_expr, next_expr, (*i)->get_locus ()); @@ -229,7 +223,7 @@ CompilePatternCheckExpr::visit (HIR::StructPattern &pattern) // // would be DECL_QUALIFIER i think. For now this will just access the // // first record field and its respective qualifier because it will // // always be set because this is all a big special union - HIR::Expr *discrim_expr = variant->get_discriminant (); + HIR::Expr &discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); // find discriminant field of scrutinee @@ -282,11 +276,11 @@ CompilePatternCheckExpr::visit (HIR::StructPattern &pattern) ident.get_locus ()); tree check_expr_sub - = CompilePatternCheckExpr::Compile (ident.get_pattern ().get (), + = CompilePatternCheckExpr::Compile (ident.get_pattern (), field_expr, ctx); check_expr = Backend::arithmetic_or_logical_expression ( ArithmeticOrLogicalOperator::BITWISE_AND, check_expr, - check_expr_sub, ident.get_pattern ()->get_locus ()); + check_expr_sub, ident.get_pattern ().get_locus ()); } break; @@ -328,7 +322,7 @@ CompilePatternCheckExpr::visit (HIR::TupleStructPattern &pattern) rust_assert (ok); // find expected discriminant - HIR::Expr *discrim_expr = variant->get_discriminant (); + HIR::Expr &discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); // find discriminant field of scrutinee @@ -357,8 +351,8 @@ CompilePatternCheckExpr::visit (HIR::TupleStructPattern &pattern) tuple_field_index = 0; } - std::unique_ptr &items = pattern.get_items (); - switch (items->get_item_type ()) + HIR::TupleStructItems &items = pattern.get_items (); + switch (items.get_item_type ()) { case HIR::TupleStructItems::RANGED: { // TODO @@ -368,7 +362,7 @@ CompilePatternCheckExpr::visit (HIR::TupleStructPattern &pattern) case HIR::TupleStructItems::MULTIPLE: { HIR::TupleStructItemsNoRange &items_no_range - = static_cast (*items.get ()); + = static_cast (items); rust_assert (items_no_range.get_patterns ().size () == variant->num_fields ()); @@ -381,8 +375,7 @@ CompilePatternCheckExpr::visit (HIR::TupleStructPattern &pattern) pattern->get_locus ()); tree check_expr_sub - = CompilePatternCheckExpr::Compile (pattern.get (), field_expr, - ctx); + = CompilePatternCheckExpr::Compile (*pattern, field_expr, ctx); check_expr = Backend::arithmetic_or_logical_expression ( ArithmeticOrLogicalOperator::BITWISE_AND, check_expr, check_expr_sub, pattern->get_locus ()); @@ -397,7 +390,7 @@ CompilePatternCheckExpr::visit (HIR::TuplePattern &pattern) { check_expr = boolean_true_node; - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TuplePatternItems::RANGED: { // TODO @@ -407,7 +400,7 @@ CompilePatternCheckExpr::visit (HIR::TuplePattern &pattern) case HIR::TuplePatternItems::MULTIPLE: { auto &items = static_cast ( - *pattern.get_items ()); + pattern.get_items ()); size_t tuple_field_index = 0; for (auto &pat : items.get_patterns ()) @@ -418,7 +411,7 @@ CompilePatternCheckExpr::visit (HIR::TuplePattern &pattern) pat->get_locus ()); tree check_expr_sub - = CompilePatternCheckExpr::Compile (pat.get (), field_expr, ctx); + = CompilePatternCheckExpr::Compile (*pat, field_expr, ctx); check_expr = Backend::arithmetic_or_logical_expression ( ArithmeticOrLogicalOperator::BITWISE_AND, check_expr, check_expr_sub, pat->get_locus ()); @@ -459,8 +452,8 @@ CompilePatternBindings::visit (HIR::TupleStructPattern &pattern) rust_assert (variant->get_variant_type () == TyTy::VariantDef::VariantType::TUPLE); - std::unique_ptr &items = pattern.get_items (); - switch (items->get_item_type ()) + HIR::TupleStructItems &items = pattern.get_items (); + switch (items.get_item_type ()) { case HIR::TupleStructItems::RANGED: { // TODO @@ -470,7 +463,7 @@ CompilePatternBindings::visit (HIR::TupleStructPattern &pattern) case HIR::TupleStructItems::MULTIPLE: { HIR::TupleStructItemsNoRange &items_no_range - = static_cast (*items.get ()); + = static_cast (items); rust_assert (items_no_range.get_patterns ().size () == variant->num_fields ()); @@ -609,8 +602,8 @@ CompilePatternBindings::visit (HIR::ReferencePattern &pattern) tree derefed = indirect_expression (match_scrutinee_expr, pattern.get_locus ()); - CompilePatternBindings::Compile (pattern.get_referenced_pattern ().get (), - derefed, ctx); + CompilePatternBindings::Compile (pattern.get_referenced_pattern (), derefed, + ctx); } void @@ -670,12 +663,12 @@ CompilePatternLet::visit (HIR::TuplePattern &pattern) tree access_expr = Backend::var_expression (tmp_var, pattern.get_locus ()); ctx->add_statement (init_stmt); - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TuplePatternItems::ItemType::RANGED: { size_t tuple_idx = 0; auto &items - = static_cast (*pattern.get_items ()); + = static_cast (pattern.get_items ()); auto &items_lower = items.get_lower_patterns (); auto &items_upper = items.get_upper_patterns (); @@ -719,7 +712,7 @@ CompilePatternLet::visit (HIR::TuplePattern &pattern) case HIR::TuplePatternItems::ItemType::MULTIPLE: { size_t tuple_idx = 0; auto &items = static_cast ( - *pattern.get_items ()); + pattern.get_items ()); for (auto &sub : items.get_patterns ()) { diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 17c82fa30501..01fd0973e47a 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -26,11 +26,11 @@ class CompilePatternCheckExpr : public HIRCompileBase, public HIR::HIRPatternVisitor { public: - static tree Compile (HIR::Pattern *pattern, tree match_scrutinee_expr, + static tree Compile (HIR::Pattern &pattern, tree match_scrutinee_expr, Context *ctx) { CompilePatternCheckExpr compiler (ctx, match_scrutinee_expr); - pattern->accept_vis (compiler); + pattern.accept_vis (compiler); rust_assert (compiler.check_expr); return compiler.check_expr; } @@ -71,11 +71,11 @@ class CompilePatternBindings : public HIRCompileBase, public HIR::HIRPatternVisitor { public: - static void Compile (HIR::Pattern *pattern, tree match_scrutinee_expr, + static void Compile (HIR::Pattern &pattern, tree match_scrutinee_expr, Context *ctx) { CompilePatternBindings compiler (ctx, match_scrutinee_expr); - pattern->accept_vis (compiler); + pattern.accept_vis (compiler); } void visit (HIR::StructPattern &pattern) override; diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index 58d9e8e3b100..5f6ba7cce433 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -81,7 +81,7 @@ ResolvePathRef::attempt_constructor_expression_lookup ( tree compiled_adt_type = TyTyResolveCompile::compile (ctx, adt); // make the ctor for the union - HIR::Expr *discrim_expr = variant->get_discriminant (); + HIR::Expr &discrim_expr = variant->get_discriminant (); tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx); tree folded_discrim_expr = fold_expr (discrim_expr_node); tree qualifier = folded_discrim_expr; @@ -301,7 +301,7 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup, TyTy::BaseType *self = nullptr; bool ok = ctx->get_tyctx ()->lookup_type ( - impl->get_type ()->get_mappings ().get_hirid (), &self); + impl->get_type ().get_mappings ().get_hirid (), &self); rust_assert (ok); if (!lookup->has_substitutions_defined ()) diff --git a/gcc/rust/backend/rust-compile-stmt.cc b/gcc/rust/backend/rust-compile-stmt.cc index e7ba370eb8e0..023c2c895a77 100644 --- a/gcc/rust/backend/rust-compile-stmt.cc +++ b/gcc/rust/backend/rust-compile-stmt.cc @@ -40,13 +40,13 @@ CompileStmt::Compile (HIR::Stmt *stmt, Context *ctx) void CompileStmt::visit (HIR::ExprStmt &stmt) { - translated = CompileExpr::Compile (stmt.get_expr ().get (), ctx); + translated = CompileExpr::Compile (stmt.get_expr (), ctx); } void CompileStmt::visit (HIR::LetStmt &stmt) { - HIR::Pattern &stmt_pattern = *stmt.get_pattern (); + HIR::Pattern &stmt_pattern = stmt.get_pattern (); HirId stmt_id = stmt_pattern.get_mappings ().get_hirid (); TyTy::BaseType *ty = nullptr; @@ -68,7 +68,7 @@ CompileStmt::visit (HIR::LetStmt &stmt) if (!stmt.has_init_expr ()) return; - tree init = CompileExpr::Compile (stmt.get_init_expr ().get (), ctx); + tree init = CompileExpr::Compile (stmt.get_init_expr (), ctx); // FIXME use error_mark_node, check that CompileExpr returns error_mark_node // on failure and make this an assertion if (init == nullptr) @@ -76,11 +76,11 @@ CompileStmt::visit (HIR::LetStmt &stmt) TyTy::BaseType *actual = nullptr; bool ok = ctx->get_tyctx ()->lookup_type ( - stmt.get_init_expr ()->get_mappings ().get_hirid (), &actual); + stmt.get_init_expr ().get_mappings ().get_hirid (), &actual); rust_assert (ok); - location_t lvalue_locus = stmt.get_pattern ()->get_locus (); - location_t rvalue_locus = stmt.get_init_expr ()->get_locus (); + location_t lvalue_locus = stmt.get_pattern ().get_locus (); + location_t rvalue_locus = stmt.get_init_expr ().get_locus (); TyTy::BaseType *expected = ty; init = coercion_site (stmt.get_mappings ().get_hirid (), init, actual, expected, lvalue_locus, rvalue_locus); diff --git a/gcc/rust/backend/rust-compile-struct-field-expr.cc b/gcc/rust/backend/rust-compile-struct-field-expr.cc index d642e28a2021..e10ea57c285c 100644 --- a/gcc/rust/backend/rust-compile-struct-field-expr.cc +++ b/gcc/rust/backend/rust-compile-struct-field-expr.cc @@ -27,22 +27,22 @@ CompileStructExprField::CompileStructExprField (Context *ctx) {} tree -CompileStructExprField::Compile (HIR::StructExprField *field, Context *ctx) +CompileStructExprField::Compile (HIR::StructExprField &field, Context *ctx) { CompileStructExprField compiler (ctx); - switch (field->get_kind ()) + switch (field.get_kind ()) { case HIR::StructExprField::StructExprFieldKind::IDENTIFIER: - compiler.visit (static_cast (*field)); + compiler.visit (static_cast (field)); break; case HIR::StructExprField::StructExprFieldKind::IDENTIFIER_VALUE: compiler.visit ( - static_cast (*field)); + static_cast (field)); break; case HIR::StructExprField::StructExprFieldKind::INDEX_VALUE: - compiler.visit (static_cast (*field)); + compiler.visit (static_cast (field)); break; } return compiler.translated; @@ -51,13 +51,13 @@ CompileStructExprField::Compile (HIR::StructExprField *field, Context *ctx) void CompileStructExprField::visit (HIR::StructExprFieldIdentifierValue &field) { - translated = CompileExpr::Compile (field.get_value ().get (), ctx); + translated = CompileExpr::Compile (field.get_value (), ctx); } void CompileStructExprField::visit (HIR::StructExprFieldIndexValue &field) { - translated = CompileExpr::Compile (field.get_value ().get (), ctx); + translated = CompileExpr::Compile (field.get_value (), ctx); } void @@ -74,7 +74,7 @@ CompileStructExprField::visit (HIR::StructExprFieldIdentifier &field) HIR::GenericArgs::create_empty ()); HIR::PathInExpression expr (mappings_copy2, {seg}, field.get_locus (), false, {}); - translated = CompileExpr::Compile (&expr, ctx); + translated = CompileExpr::Compile (expr, ctx); } } // namespace Compile diff --git a/gcc/rust/backend/rust-compile-struct-field-expr.h b/gcc/rust/backend/rust-compile-struct-field-expr.h index 1ee4d1b15939..af1f3676e1bf 100644 --- a/gcc/rust/backend/rust-compile-struct-field-expr.h +++ b/gcc/rust/backend/rust-compile-struct-field-expr.h @@ -27,7 +27,7 @@ namespace Compile { class CompileStructExprField : private HIRCompileBase { public: - static tree Compile (HIR::StructExprField *field, Context *ctx); + static tree Compile (HIR::StructExprField &field, Context *ctx); protected: void visit (HIR::StructExprFieldIdentifierValue &field); diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index c8fe1cd9d629..e01335247143 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -428,7 +428,7 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type) = TyTyResolveCompile::compile (ctx, type.get_element_type ()); ctx->push_const_context (); - tree capacity_expr = CompileExpr::Compile (&type.get_capacity_expr (), ctx); + tree capacity_expr = CompileExpr::Compile (type.get_capacity_expr (), ctx); ctx->pop_const_context (); tree folded_capacity_expr = fold_expr (capacity_expr); diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index 45ca01d4f709..eda233599f6f 100644 --- a/gcc/rust/backend/rust-compile-var-decl.h +++ b/gcc/rust/backend/rust-compile-var-decl.h @@ -68,12 +68,12 @@ class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor void visit (HIR::TuplePattern &pattern) override { - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TuplePatternItems::ItemType::MULTIPLE: { rust_assert (TREE_CODE (translated_type) == RECORD_TYPE); auto &items = static_cast ( - *pattern.get_items ()); + pattern.get_items ()); size_t offs = 0; for (auto &sub : items.get_patterns ()) diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index c67f9d690591..11831cfb8e42 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -259,17 +259,17 @@ HIRCompileBase::compute_address_for_trait_item ( HIR::ImplBlock *impl_block = item.second; rust_assert (impl_block != nullptr); - // Lookup type for potentially associated impl. - std::unique_ptr &self_type_path = impl_block->get_type (); - // Checks for empty impl blocks, triggered by Sized trait. - if (self_type_path == nullptr) + if (!impl_block->has_type ()) continue; + // Lookup type for potentially associated impl. + HIR::Type &self_type_path = impl_block->get_type (); + // Convert HIR::Type to TyTy::BaseType TyTy::BaseType *self = nullptr; bool ok = ctx->get_tyctx ()->lookup_type ( - self_type_path->get_mappings ().get_hirid (), &self); + self_type_path.get_mappings ().get_hirid (), &self); rust_assert (ok); diff --git a/gcc/rust/backend/rust-mangle-v0.cc b/gcc/rust/backend/rust-mangle-v0.cc index 685236b2da49..ee6ae8c8e5a9 100644 --- a/gcc/rust/backend/rust-mangle-v0.cc +++ b/gcc/rust/backend/rust-mangle-v0.cc @@ -328,7 +328,7 @@ v0_inherent_or_trait_impl_path (Rust::Compile::Context *ctx, // lookup impl type TyTy::BaseType *impl_ty = nullptr; ok = ctx->get_tyctx ()->lookup_type ( - impl_block->get_type ()->get_mappings ().get_hirid (), &impl_ty); + impl_block->get_type ().get_mappings ().get_hirid (), &impl_ty); rust_assert (ok); // FIXME: dummy value for now @@ -342,7 +342,7 @@ v0_inherent_or_trait_impl_path (Rust::Compile::Context *ctx, TyTy::BaseType *trait_ty = nullptr; ok = ctx->get_tyctx ()->lookup_type ( - impl_block->get_trait_ref ()->get_mappings ().get_hirid (), &trait_ty); + impl_block->get_trait_ref ().get_mappings ().get_hirid (), &trait_ty); rust_assert (ok); v0path.trait_type = v0_type_prefix (ctx, trait_ty); diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc index 3ae58843f1fb..d8e00e38519e 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc @@ -135,7 +135,7 @@ ExprStmtBuilder::visit (HIR::LiteralExpr &expr) void ExprStmtBuilder::visit (HIR::BorrowExpr &expr) { - auto operand = visit_expr (*expr.get_expr ()); + auto operand = visit_expr (expr.get_expr ()); if (ctx.place_db[operand].is_constant ()) { // Cannot borrow a constant, must create a temporary copy. @@ -150,7 +150,7 @@ ExprStmtBuilder::visit (HIR::BorrowExpr &expr) void ExprStmtBuilder::visit (HIR::DereferenceExpr &expr) { - auto operand = visit_expr (*expr.get_expr ()); + auto operand = visit_expr (expr.get_expr ()); return_place (ctx.place_db.lookup_or_add_path (Place::DEREF, lookup_type (expr), operand), expr.get_locus ()); @@ -166,30 +166,31 @@ ExprStmtBuilder::visit (HIR::ErrorPropagationExpr &expr) void ExprStmtBuilder::visit (HIR::NegationExpr &expr) { - PlaceId operand = visit_expr (*expr.get_expr ()); - return_expr (new Operator<1> ({move_place (operand, expr.get_locus ())}), + PlaceId operand = visit_expr (expr.get_expr ()); + return_expr (new Operator<1> ( + {move_place (operand, expr.get_expr ().get_locus ())}), lookup_type (expr), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::ArithmeticOrLogicalExpr &expr) { - PlaceId lhs = visit_expr (*expr.get_lhs ()); - PlaceId rhs = visit_expr (*expr.get_rhs ()); + PlaceId lhs = visit_expr (expr.get_lhs ()); + PlaceId rhs = visit_expr (expr.get_rhs ()); return_expr (new Operator<2> ( - {move_place (lhs, expr.get_lhs ()->get_locus ()), - move_place (rhs, expr.get_rhs ()->get_locus ())}), + {move_place (lhs, expr.get_lhs ().get_locus ()), + move_place (rhs, expr.get_rhs ().get_locus ())}), lookup_type (expr), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::ComparisonExpr &expr) { - PlaceId lhs = visit_expr (*expr.get_lhs ()); - PlaceId rhs = visit_expr (*expr.get_rhs ()); + PlaceId lhs = visit_expr (expr.get_lhs ()); + PlaceId rhs = visit_expr (expr.get_rhs ()); return_expr (new Operator<2> ( - {move_place (lhs, expr.get_lhs ()->get_locus ()), - move_place (rhs, expr.get_rhs ()->get_locus ())}), + {move_place (lhs, expr.get_lhs ().get_locus ()), + move_place (rhs, expr.get_rhs ().get_locus ())}), lookup_type (expr), expr.get_locus ()); } @@ -205,7 +206,7 @@ ExprStmtBuilder::visit (HIR::LazyBooleanExpr &expr) void ExprStmtBuilder::visit (HIR::TypeCastExpr &expr) { - auto operand = visit_expr (*expr.get_expr ()); + auto operand = visit_expr (expr.get_expr ()); return_expr (new Operator<1> ({operand}), lookup_type (expr), expr.get_locus ()); } @@ -213,8 +214,8 @@ ExprStmtBuilder::visit (HIR::TypeCastExpr &expr) void ExprStmtBuilder::visit (HIR::AssignmentExpr &expr) { - auto lhs = visit_expr (*expr.get_lhs ()); - auto rhs = visit_expr (*expr.get_rhs ()); + auto lhs = visit_expr (expr.get_lhs ()); + auto rhs = visit_expr (expr.get_rhs ()); push_assignment (lhs, rhs, expr.get_locus ()); translated = INVALID_PLACE; } @@ -222,25 +223,25 @@ ExprStmtBuilder::visit (HIR::AssignmentExpr &expr) void ExprStmtBuilder::visit (HIR::CompoundAssignmentExpr &expr) { - auto lhs = visit_expr (*expr.get_lhs ()); - auto rhs = visit_expr (*expr.get_rhs ()); + auto lhs = visit_expr (expr.get_lhs ()); + auto rhs = visit_expr (expr.get_rhs ()); push_assignment (lhs, new Operator<2> ({lhs, rhs}), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::GroupedExpr &expr) { - return_place (visit_expr (*expr.get_expr_in_parens ()), expr.get_locus ()); + return_place (visit_expr (expr.get_expr_in_parens ()), expr.get_locus ()); } void ExprStmtBuilder::visit (HIR::ArrayExpr &expr) { auto &elems = expr.get_internal_elements (); - switch (elems->get_array_expr_type ()) + switch (elems.get_array_expr_type ()) { case HIR::ArrayElems::VALUES: { - auto &elem_vals = (static_cast (*elems)); + auto &elem_vals = (static_cast (elems)); auto init_values = visit_list (elem_vals.get_values ()); // collect locations std::vector value_locations; @@ -254,8 +255,8 @@ ExprStmtBuilder::visit (HIR::ArrayExpr &expr) break; } case HIR::ArrayElems::COPIED: { - auto &elem_copied = (static_cast (*elems)); - auto init = visit_expr (*elem_copied.get_elem_to_copy ()); + auto &elem_copied = (static_cast (elems)); + auto init = visit_expr (elem_copied.get_elem_to_copy ()); return_expr (new InitializerExpr ({init}), lookup_type (expr), expr.get_locus ()); break; @@ -266,8 +267,8 @@ ExprStmtBuilder::visit (HIR::ArrayExpr &expr) void ExprStmtBuilder::visit (HIR::ArrayIndexExpr &expr) { - auto lhs = visit_expr (*expr.get_array_expr ()); - auto rhs = visit_expr (*expr.get_index_expr ()); + auto lhs = visit_expr (expr.get_array_expr ()); + auto rhs = visit_expr (expr.get_index_expr ()); // The index is not tracked in BIR. std::ignore = rhs; return_place (ctx.place_db.lookup_or_add_path (Place::INDEX, @@ -286,7 +287,7 @@ ExprStmtBuilder::visit (HIR::TupleExpr &expr) void ExprStmtBuilder::visit (HIR::TupleIndexExpr &expr) { - auto tuple = visit_expr (*expr.get_tuple_expr ()); + auto tuple = visit_expr (expr.get_tuple_expr ()); return_place (ctx.place_db.lookup_or_add_path (Place::FIELD, lookup_type (expr), tuple, expr.get_tuple_index ()), @@ -296,7 +297,7 @@ ExprStmtBuilder::visit (HIR::TupleIndexExpr &expr) void ExprStmtBuilder::visit (HIR::CallExpr &expr) { - PlaceId fn = visit_expr (*expr.get_fnexpr ()); + PlaceId fn = visit_expr (expr.get_fnexpr ()); std::vector arguments = visit_list (expr.get_arguments ()); const auto fn_type @@ -330,7 +331,7 @@ ExprStmtBuilder::visit (HIR::MethodCallExpr &expr) void ExprStmtBuilder::visit (HIR::FieldAccessExpr &expr) { - auto receiver = visit_expr (*expr.get_receiver_expr ()); + auto receiver = visit_expr (expr.get_receiver_expr ()); auto type = autoderef (receiver); rust_assert (type->get_kind () == TyTy::ADT); auto adt = type->as (); @@ -383,7 +384,7 @@ ExprStmtBuilder::visit (HIR::BlockExpr &block) if (block.has_expr () && !unreachable) { push_assignment (block_ctx.label_var, - visit_expr (*block.get_final_expr ()), + visit_expr (block.get_final_expr ()), block.get_start_locus ()); } if (!ctx.get_current_bb ().is_terminated ()) @@ -397,9 +398,9 @@ ExprStmtBuilder::visit (HIR::BlockExpr &block) } else if (block.has_expr () && !unreachable) { - return_place (visit_expr (*block.get_final_expr (), + return_place (visit_expr (block.get_final_expr (), take_or_create_return_place ( - lookup_type (*block.get_final_expr ()))), + lookup_type (block.get_final_expr ()))), block.get_start_locus ()); } @@ -426,7 +427,7 @@ ExprStmtBuilder::visit (HIR::BreakExpr &brk) LoopAndLabelCtx info = brk.has_label () ? get_label_ctx (brk.get_label ()) : get_unnamed_loop_ctx (); if (brk.has_break_expr ()) - push_assignment (info.label_var, visit_expr (*brk.get_expr ()), + push_assignment (info.label_var, visit_expr (brk.get_expr ()), brk.get_locus ()); start_new_consecutive_bb (); @@ -438,8 +439,8 @@ ExprStmtBuilder::visit (HIR::BreakExpr &brk) void ExprStmtBuilder::visit (HIR::RangeFromToExpr &range) { - auto from = visit_expr (*range.get_from_expr ()); - auto to = visit_expr (*range.get_to_expr ()); + auto from = visit_expr (range.get_from_expr ()); + auto to = visit_expr (range.get_to_expr ()); return_expr (new InitializerExpr ({from, to}), lookup_type (range), range.get_locus ()); } @@ -447,7 +448,7 @@ ExprStmtBuilder::visit (HIR::RangeFromToExpr &range) void ExprStmtBuilder::visit (HIR::RangeFromExpr &expr) { - auto from = visit_expr (*expr.get_from_expr ()); + auto from = visit_expr (expr.get_from_expr ()); return_expr (new InitializerExpr ({from}), lookup_type (expr), expr.get_locus ()); } @@ -455,7 +456,7 @@ ExprStmtBuilder::visit (HIR::RangeFromExpr &expr) void ExprStmtBuilder::visit (HIR::RangeToExpr &expr) { - auto to = visit_expr (*expr.get_to_expr ()); + auto to = visit_expr (expr.get_to_expr ()); return_expr (new InitializerExpr ({to}), lookup_type (expr), expr.get_locus ()); } @@ -469,8 +470,8 @@ ExprStmtBuilder::visit (HIR::RangeFullExpr &expr) void ExprStmtBuilder::visit (HIR::RangeFromToInclExpr &expr) { - auto from = visit_expr (*expr.get_from_expr ()); - auto to = visit_expr (*expr.get_to_expr ()); + auto from = visit_expr (expr.get_from_expr ()); + auto to = visit_expr (expr.get_to_expr ()); return_expr (new InitializerExpr ({from, to}), lookup_type (expr), expr.get_locus ()); } @@ -478,7 +479,7 @@ ExprStmtBuilder::visit (HIR::RangeFromToInclExpr &expr) void ExprStmtBuilder::visit (HIR::RangeToInclExpr &expr) { - auto to = visit_expr (*expr.get_to_expr ()); + auto to = visit_expr (expr.get_to_expr ()); return_expr (new InitializerExpr ({to}), lookup_type (expr), expr.get_locus ()); } @@ -489,9 +490,9 @@ ExprStmtBuilder::visit (HIR::ReturnExpr &ret) if (ret.has_return_expr ()) { push_assignment (RETURN_VALUE_PLACE, - move_place (visit_expr (*ret.get_expr ()), - ret.get_expr ()->get_locus ()), - ret.get_expr ()->get_locus ()); + move_place (visit_expr (ret.get_expr ()), + ret.get_expr ().get_locus ()), + ret.get_expr ().get_locus ()); } unwind_until (ROOT_SCOPE); push_return (ret.get_locus ()); @@ -509,7 +510,7 @@ ExprStmtBuilder::visit (HIR::LoopExpr &expr) { auto loop = setup_loop (expr); - std::ignore = visit_expr (*expr.get_loop_block ()); + std::ignore = visit_expr (expr.get_loop_block ()); if (!ctx.get_current_bb ().is_terminated ()) push_goto (loop.continue_bb); @@ -521,12 +522,12 @@ ExprStmtBuilder::visit (HIR::WhileLoopExpr &expr) { auto loop = setup_loop (expr); - auto cond_val = visit_expr (*expr.get_predicate_expr ()); + auto cond_val = visit_expr (expr.get_predicate_expr ()); auto body_bb = new_bb (); push_switch (cond_val, expr.get_locus (), {body_bb, loop.break_bb}); ctx.current_bb = body_bb; - std::ignore = visit_expr (*expr.get_loop_block ()); + std::ignore = visit_expr (expr.get_loop_block ()); push_goto (loop.continue_bb); ctx.current_bb = loop.break_bb; @@ -544,15 +545,15 @@ ExprStmtBuilder::visit (HIR::IfExpr &expr) { // If without else cannot return a non-unit value (see [E0317]). - if (expr.get_if_block ()->statements.empty ()) + if (expr.get_if_block ().statements.empty ()) return; - push_switch (visit_expr (*expr.get_if_condition ()), expr.get_locus ()); + push_switch (visit_expr (expr.get_if_condition ()), expr.get_locus ()); BasicBlockId if_block = ctx.current_bb; ctx.current_bb = new_bb (); BasicBlockId then_start_block = ctx.current_bb; - std::ignore = visit_expr (*expr.get_if_block ()); + std::ignore = visit_expr (expr.get_if_block ()); if (!ctx.get_current_bb ().is_terminated ()) push_goto (INVALID_BB); // Resolved later. BasicBlockId then_end_block = ctx.current_bb; @@ -573,8 +574,8 @@ ExprStmtBuilder::visit (HIR::IfExpr &expr) void ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr) { - push_switch (move_place (visit_expr (*expr.get_if_condition ()), - expr.get_if_condition ()->get_locus ()), + push_switch (move_place (visit_expr (expr.get_if_condition ()), + expr.get_if_condition ().get_locus ()), expr.get_locus ()); BasicBlockId if_end_bb = ctx.current_bb; @@ -582,14 +583,14 @@ ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr) ctx.current_bb = new_bb (); BasicBlockId then_start_bb = ctx.current_bb; - std::ignore = visit_expr (*expr.get_if_block (), result); + std::ignore = visit_expr (expr.get_if_block (), result); if (!ctx.get_current_bb ().is_terminated ()) push_goto (INVALID_BB); // Resolved later. BasicBlockId then_end_bb = ctx.current_bb; ctx.current_bb = new_bb (); BasicBlockId else_start_bb = ctx.current_bb; - std::ignore = visit_expr (*expr.get_else_block (), result); + std::ignore = visit_expr (expr.get_else_block (), result); if (!ctx.get_current_bb ().is_terminated ()) push_goto (INVALID_BB); // Resolved later. BasicBlockId else_end_bb = ctx.current_bb; @@ -690,35 +691,35 @@ ExprStmtBuilder::visit (HIR::LetStmt &stmt) tl::optional type_annotation; if (stmt.has_type ()) - type_annotation = lookup_type (*stmt.get_type ()); + type_annotation = lookup_type (stmt.get_type ()); - if (stmt.get_pattern ()->get_pattern_type () == HIR::Pattern::IDENTIFIER) + if (stmt.get_pattern ().get_pattern_type () == HIR::Pattern::IDENTIFIER) { // Only if a pattern is just an identifier, no destructuring is needed. // Hoverer PatternBindingBuilder cannot change existing temporary // (init expr is evaluated before pattern binding) into a // variable, so it would emit extra assignment. - auto var = declare_variable (stmt.get_pattern ()->get_mappings ()); + auto var = declare_variable (stmt.get_pattern ().get_mappings ()); if (stmt.has_type ()) - push_user_type_ascription (var, lookup_type (*stmt.get_type ())); + push_user_type_ascription (var, lookup_type (stmt.get_type ())); if (stmt.has_init_expr ()) - std::ignore = visit_expr (*stmt.get_init_expr (), var); + std::ignore = visit_expr (stmt.get_init_expr (), var); } else { if (stmt.has_init_expr ()) - init = visit_expr (*stmt.get_init_expr ()); + init = visit_expr (stmt.get_init_expr ()); PatternBindingBuilder (ctx, init, type_annotation) - .go (*stmt.get_pattern ()); + .go (stmt.get_pattern ()); } } void ExprStmtBuilder::visit (HIR::ExprStmt &stmt) { - PlaceId result = visit_expr (*stmt.get_expr ()); + PlaceId result = visit_expr (stmt.get_expr ()); // We must read the value for current liveness and we must not store it into // the same place. if (result != INVALID_PLACE) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h index 926ba692e2ca..71cf710a4111 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-lazyboolexpr.h @@ -63,16 +63,16 @@ class LazyBooleanExprBuilder : public AbstractExprBuilder protected: void visit (HIR::LazyBooleanExpr &expr) override { - auto lhs = visit_expr (*expr.get_lhs ()); - push_switch (move_place (lhs, expr.get_lhs ()->get_locus ()), + auto lhs = visit_expr (expr.get_lhs ()); + push_switch (move_place (lhs, expr.get_lhs ().get_locus ()), expr.get_locus (), {short_circuit_bb}); start_new_consecutive_bb (); - return_place (visit_expr (*expr.get_rhs ()), expr.get_locus ()); + return_place (visit_expr (expr.get_rhs ()), expr.get_locus ()); } void visit (HIR::GroupedExpr &expr) override { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } protected: diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc index 723ff7334b62..ee37bb09bc43 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.cc @@ -50,7 +50,7 @@ PatternBindingBuilder::visit (HIR::ReferencePattern &pattern) return ty->as ()->get_base (); }); - pattern.get_referenced_pattern ()->accept_vis (*this); + pattern.get_referenced_pattern ().accept_vis (*this); } void @@ -107,7 +107,7 @@ PatternBindingBuilder::visit (HIR::StructPattern &pattern) init = init.map ([&] (PlaceId id) { return ctx.place_db.lookup_or_add_path ( - Place::FIELD, lookup_type (*tuple->get_tuple_pattern ()), id, + Place::FIELD, lookup_type (tuple->get_tuple_pattern ()), id, tuple->get_index ()); }); @@ -120,7 +120,7 @@ PatternBindingBuilder::visit (HIR::StructPattern &pattern) ->get_field_type (); }); - tuple->get_tuple_pattern ()->accept_vis (*this); + tuple->get_tuple_pattern ().accept_vis (*this); break; } case HIR::StructPatternField::IDENT_PAT: { @@ -136,7 +136,7 @@ PatternBindingBuilder::visit (HIR::StructPattern &pattern) field_ty->get_field_type (), saved.init.value (), field_index); - ident_field->get_pattern ()->accept_vis (*this); + ident_field->get_pattern ().accept_vis (*this); break; } case HIR::StructPatternField::IDENT: { @@ -197,17 +197,17 @@ PatternBindingBuilder::visit (HIR::TuplePattern &pattern) SavedState saved (this); size_t index = 0; - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TuplePatternItems::MULTIPLE: { auto &items = static_cast ( - *pattern.get_items ()); + pattern.get_items ()); visit_tuple_fields (items.get_patterns (), saved, index); break; } case HIR::TuplePatternItems::RANGED: { auto &items - = static_cast (*pattern.get_items ()); + = static_cast (pattern.get_items ()); auto tyty = ctx.place_db[init.value ()].tyty; rust_assert (tyty->get_kind () == TyTy::TUPLE); @@ -242,11 +242,11 @@ PatternBindingBuilder::visit (HIR::TupleStructPattern &pattern) }); size_t index = 0; - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TupleStructItems::RANGED: { auto &items - = static_cast (*pattern.get_items ()); + = static_cast (pattern.get_items ()); rust_assert (type->get_kind () == TyTy::ADT); auto adt_ty = static_cast (type); @@ -263,7 +263,7 @@ PatternBindingBuilder::visit (HIR::TupleStructPattern &pattern) } case HIR::TupleStructItems::MULTIPLE: { auto &items - = static_cast (*pattern.get_items ()); + = static_cast (pattern.get_items ()); visit_tuple_fields (items.get_patterns (), saved, index); break; } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h index 698607459f68..ca35318c766e 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-pattern.h @@ -91,6 +91,7 @@ class PatternBindingBuilder : protected AbstractBuilder, void visit (HIR::QualifiedPathInExpression &expression) override {} void visit (HIR::RangePattern &pattern) override {} }; + } // namespace BIR } // namespace Rust diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h index 351b4c8a3758..da5e965fe8da 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h @@ -52,12 +52,12 @@ class StructBuilder : public AbstractBuilder, public HIR::HIRFullVisitor } void visit (HIR::StructExprFieldIdentifierValue &field) override { - auto value = ExprStmtBuilder (ctx).build (*field.get_value ()); + auto value = ExprStmtBuilder (ctx).build (field.get_value ()); handle_named_field (field, value); } void visit (HIR::StructExprFieldIndexValue &field) override { - auto value = ExprStmtBuilder (ctx).build (*field.get_value ()); + auto value = ExprStmtBuilder (ctx).build (field.get_value ()); coercion_site (value, struct_ty->get_field_at_index (field.get_tuple_index ()) ->get_field_type ()); diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h index 19d8ce557233..100f753f56f2 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h @@ -49,7 +49,7 @@ class Builder final : public AbstractBuilder for (auto ¶m : function.get_function_params ()) handle_param (param); - handle_body (*function.get_definition ()); + handle_body (function.get_definition ()); auto region_hir_map = map_region_to_hir (function.get_generic_params (), ctx.fn_free_regions); @@ -118,14 +118,14 @@ class Builder final : public AbstractBuilder void handle_param (HIR::FunctionParam ¶m) { - auto param_type = lookup_type (*param.get_param_name ()); + auto param_type = lookup_type (param.get_param_name ()); auto &pattern = param.get_param_name (); - if (pattern->get_pattern_type () == HIR::Pattern::IDENTIFIER - && !static_cast (*pattern).get_is_ref ()) + if (pattern.get_pattern_type () == HIR::Pattern::IDENTIFIER + && !static_cast (pattern).get_is_ref ()) { // Avoid useless temporary variable for parameter to look like MIR. - translated = declare_variable (pattern->get_mappings ()); + translated = declare_variable (pattern.get_mappings ()); ctx.arguments.push_back (translated); } else @@ -133,11 +133,9 @@ class Builder final : public AbstractBuilder translated = ctx.place_db.add_temporary (param_type); ctx.arguments.push_back (translated); PatternBindingBuilder (ctx, translated, tl::nullopt) - .go (*param.get_param_name ()); + .go (param.get_param_name ()); } - rust_assert (param.get_type () != nullptr); - // Set parameter place to use functions regions, not the fresh ones. ctx.place_db[translated].regions = bind_regions (Resolver::TypeCheckContext::get () @@ -159,7 +157,7 @@ class Builder final : public AbstractBuilder body.get_end_locus ()); } auto return_location = body.has_expr () - ? body.get_final_expr ()->get_locus () + ? body.get_final_expr ().get_locus () : body.get_end_locus (); push_return (return_location); } diff --git a/gcc/rust/checks/errors/borrowck/rust-function-collector.h b/gcc/rust/checks/errors/borrowck/rust-function-collector.h index 1f7cf48e412b..e35757c7fea0 100644 --- a/gcc/rust/checks/errors/borrowck/rust-function-collector.h +++ b/gcc/rust/checks/errors/borrowck/rust-function-collector.h @@ -56,13 +56,13 @@ class FunctionCollector : public HIR::HIRFullVisitor void visit (HIR::Function &function) override { functions.push_back (&function); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); } void visit (HIR::ClosureExpr &closure) override { closures.push_back (&closure); - closure.get_expr ()->accept_vis (*this); + closure.get_expr ().accept_vis (*this); } // TODO: recurse for nested closures and functions. diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index b7abed256290..a5e2f85418f7 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -267,16 +267,13 @@ PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings, } void -PrivacyReporter::check_type_privacy (const HIR::Type *type) +PrivacyReporter::check_type_privacy (const HIR::Type &type) { - rust_assert (type); - TyTy::BaseType *lookup = nullptr; - rust_assert ( - ty_ctx.lookup_type (type->get_mappings ().get_hirid (), &lookup)); + rust_assert (ty_ctx.lookup_type (type.get_mappings ().get_hirid (), &lookup)); - auto node_mappings = type->get_mappings (); - return check_base_type_privacy (node_mappings, lookup, type->get_locus ()); + auto node_mappings = type.get_mappings (); + return check_base_type_privacy (node_mappings, lookup, type.get_locus ()); } void @@ -326,100 +323,98 @@ PrivacyReporter::visit (HIR::LiteralExpr &) void PrivacyReporter::visit (HIR::BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::DereferenceExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PrivacyReporter::visit (HIR::GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ArrayExpr &expr) { - HIR::ArrayElems &elements = *expr.get_internal_elements (); + HIR::ArrayElems &elements = expr.get_internal_elements (); switch (elements.get_array_expr_type ()) { case HIR::ArrayElems::ArrayExprType::VALUES: { - HIR::ArrayElemsValues &elems - = static_cast (elements); + auto &elems = static_cast (elements); for (auto &value : elems.get_values ()) value->accept_vis (*this); } return; case HIR::ArrayElems::ArrayExprType::COPIED: - HIR::ArrayElemsCopied &elems - = static_cast (elements); - elems.get_elem_to_copy ()->accept_vis (*this); + auto &elems = static_cast (elements); + elems.get_elem_to_copy ().accept_vis (*this); } } void PrivacyReporter::visit (HIR::ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -432,7 +427,7 @@ PrivacyReporter::visit (HIR::TupleExpr &expr) void PrivacyReporter::visit (HIR::TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -448,13 +443,13 @@ PrivacyReporter::visit (HIR::StructExprFieldIdentifier &) void PrivacyReporter::visit (HIR::StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void PrivacyReporter::visit (HIR::StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -467,7 +462,7 @@ PrivacyReporter::visit (HIR::StructExprStructFields &expr) void PrivacyReporter::visit (HIR::CallExpr &expr) { - expr.get_fnexpr ()->accept_vis (*this); + expr.get_fnexpr ().accept_vis (*this); for (auto ¶m : expr.get_arguments ()) param->accept_vis (*this); @@ -476,7 +471,7 @@ PrivacyReporter::visit (HIR::CallExpr &expr) void PrivacyReporter::visit (HIR::MethodCallExpr &expr) { - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto ¶m : expr.get_arguments ()) param->accept_vis (*this); @@ -485,7 +480,7 @@ PrivacyReporter::visit (HIR::MethodCallExpr &expr) void PrivacyReporter::visit (HIR::FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); // FIXME: We should also check if the field is public? } @@ -503,8 +498,7 @@ PrivacyReporter::visit (HIR::BlockExpr &expr) stmt->accept_vis (*this); auto &last_expr = expr.get_final_expr (); - if (last_expr) - last_expr->accept_vis (*this); + last_expr.accept_vis (*this); } void @@ -515,27 +509,26 @@ void PrivacyReporter::visit (HIR::BreakExpr &expr) { auto &break_expr = expr.get_expr (); - if (break_expr) - break_expr->accept_vis (*this); + break_expr.accept_vis (*this); } void PrivacyReporter::visit (HIR::RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -545,8 +538,8 @@ PrivacyReporter::visit (HIR::RangeFullExpr &) void PrivacyReporter::visit (HIR::RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -558,55 +551,54 @@ PrivacyReporter::visit (HIR::RangeToInclExpr &) void PrivacyReporter::visit (HIR::ReturnExpr &expr) { - if (expr.get_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::UnsafeBlockExpr &expr) { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void PrivacyReporter::visit (HIR::MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); } void @@ -649,9 +641,9 @@ void PrivacyReporter::visit (HIR::Function &function) { for (auto ¶m : function.get_function_params ()) - check_type_privacy (param.get_type ().get ()); + check_type_privacy (param.get_type ()); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); } void @@ -708,14 +700,14 @@ void PrivacyReporter::visit (HIR::ConstantItem &const_item) { // TODO: We need to visit the type - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::StaticItem &static_item) { // TODO: We need to visit the type - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); } void @@ -748,17 +740,15 @@ PrivacyReporter::visit (HIR::EmptyStmt &) void PrivacyReporter::visit (HIR::LetStmt &stmt) { - if (stmt.get_type ()) - check_type_privacy (stmt.get_type ().get ()); + check_type_privacy (stmt.get_type ()); - if (stmt.get_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } void PrivacyReporter::visit (HIR::ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } } // namespace Privacy diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h index f90f5a2fc8ba..505c8673c571 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h @@ -75,7 +75,7 @@ types * @param type Reference to an explicit type used in a statement, expression * or parameter */ - void check_type_privacy (const HIR::Type *type); + void check_type_privacy (const HIR::Type &type); virtual void visit (HIR::StructExprFieldIdentifier &field); virtual void visit (HIR::StructExprFieldIdentifierValue &field); diff --git a/gcc/rust/checks/errors/privacy/rust-reachability.cc b/gcc/rust/checks/errors/privacy/rust-reachability.cc index 0afd056a1b70..a6ef6c451440 100644 --- a/gcc/rust/checks/errors/privacy/rust-reachability.cc +++ b/gcc/rust/checks/errors/privacy/rust-reachability.cc @@ -132,7 +132,7 @@ ReachabilityVisitor::visit (HIR::StructStruct &struct_item) { for (auto &field : struct_item.get_fields ()) if (field.get_visibility ().is_public ()) - ctx.update_reachability (field.get_field_type ()->get_mappings (), + ctx.update_reachability (field.get_field_type ().get_mappings (), struct_reach); } diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index f858a2233495..5579027217e2 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -161,72 +161,72 @@ ConstChecker::visit (LiteralExpr &) void ConstChecker::visit (BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (DereferenceExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void ConstChecker::visit (GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void @@ -239,11 +239,11 @@ ConstChecker::visit (ArrayElemsValues &elems) void ConstChecker::visit (ArrayElemsCopied &elems) { - elems.get_elem_to_copy ()->accept_vis (*this); + elems.get_elem_to_copy ().accept_vis (*this); const_context.enter (elems.get_mappings ().get_hirid ()); - elems.get_num_copies_expr ()->accept_vis (*this); + elems.get_num_copies_expr ().accept_vis (*this); const_context.exit (); } @@ -251,14 +251,14 @@ ConstChecker::visit (ArrayElemsCopied &elems) void ConstChecker::visit (ArrayExpr &expr) { - expr.get_internal_elements ()->accept_vis (*this); + expr.get_internal_elements ().accept_vis (*this); } void ConstChecker::visit (ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -271,7 +271,7 @@ ConstChecker::visit (TupleExpr &expr) void ConstChecker::visit (TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -285,13 +285,13 @@ ConstChecker::visit (StructExprFieldIdentifier &) void ConstChecker::visit (StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void ConstChecker::visit (StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -352,10 +352,10 @@ ConstChecker::check_function_call (HirId fn_id, location_t locus) void ConstChecker::visit (CallExpr &expr) { - if (!expr.get_fnexpr ()) + if (!expr.has_fnexpr ()) return; - NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid (); + NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; if (flag_name_resolution_2_0) @@ -388,7 +388,7 @@ ConstChecker::visit (CallExpr &expr) void ConstChecker::visit (MethodCallExpr &expr) { - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto &arg : expr.get_arguments ()) arg->accept_vis (*this); @@ -397,13 +397,13 @@ ConstChecker::visit (MethodCallExpr &expr) void ConstChecker::visit (FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); } void ConstChecker::visit (ClosureExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -413,7 +413,7 @@ ConstChecker::visit (BlockExpr &expr) stmt->accept_vis (*this); if (expr.has_expr ()) - expr.get_final_expr ()->accept_vis (*this); + expr.get_final_expr ().accept_vis (*this); } void @@ -424,26 +424,26 @@ void ConstChecker::visit (BreakExpr &expr) { if (expr.has_break_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void ConstChecker::visit (RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void ConstChecker::visit (RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -453,8 +453,8 @@ ConstChecker::visit (RangeFullExpr &) void ConstChecker::visit (RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -467,57 +467,57 @@ void ConstChecker::visit (ReturnExpr &expr) { if (expr.has_return_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void ConstChecker::visit (UnsafeBlockExpr &expr) { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void ConstChecker::visit (LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void ConstChecker::visit (WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void ConstChecker::visit (WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void ConstChecker::visit (IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void ConstChecker::visit (IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void ConstChecker::visit (MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); for (auto &match_arm : expr.get_match_cases ()) - match_arm.get_expr ()->accept_vis (*this); + match_arm.get_expr ().accept_vis (*this); } void @@ -590,9 +590,9 @@ ConstChecker::visit (Function &function) ConstGenericCtx::Function); for (auto ¶m : function.get_function_params ()) - param.get_type ()->accept_vis (*this); + param.get_type ().accept_vis (*this); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); if (const_fn) const_context.exit (); @@ -636,7 +636,7 @@ ConstChecker::visit (EnumItemDiscriminant &item) { const_context.enter (item.get_mappings ().get_hirid ()); - item.get_discriminant_expression ()->accept_vis (*this); + item.get_discriminant_expression ().accept_vis (*this); const_context.exit (); } @@ -660,7 +660,7 @@ ConstChecker::visit (ConstantItem &const_item) { const_context.enter (const_item.get_mappings ().get_hirid ()); - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); const_context.exit (); } @@ -670,7 +670,7 @@ ConstChecker::visit (StaticItem &static_item) { const_context.enter (static_item.get_mappings ().get_hirid ()); - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); const_context.exit (); } @@ -679,14 +679,14 @@ void ConstChecker::visit (TraitItemFunc &item) { if (item.has_block_defined ()) - item.get_block_expr ()->accept_vis (*this); + item.get_block_expr ().accept_vis (*this); } void ConstChecker::visit (TraitItemConst &item) { if (item.has_expr ()) - item.get_expr ()->accept_vis (*this); + item.get_expr ().accept_vis (*this); } void @@ -821,13 +821,13 @@ void ConstChecker::visit (LetStmt &stmt) { if (stmt.has_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } void ConstChecker::visit (ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } void @@ -875,7 +875,7 @@ ConstChecker::visit (ArrayType &type) { const_context.enter (type.get_mappings ().get_hirid ()); - type.get_size_expr ()->accept_vis (*this); + type.get_size_expr ().accept_vis (*this); const_context.exit (); } diff --git a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc index 8fb795b7b837..617d754c1819 100644 --- a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc +++ b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc @@ -92,72 +92,72 @@ PatternChecker::visit (LiteralExpr &) void PatternChecker::visit (BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (DereferenceExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void PatternChecker::visit (GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void @@ -170,20 +170,20 @@ PatternChecker::visit (ArrayElemsValues &elems) void PatternChecker::visit (ArrayElemsCopied &elems) { - elems.get_elem_to_copy ()->accept_vis (*this); + elems.get_elem_to_copy ().accept_vis (*this); } void PatternChecker::visit (ArrayExpr &expr) { - expr.get_internal_elements ()->accept_vis (*this); + expr.get_internal_elements ().accept_vis (*this); } void PatternChecker::visit (ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -196,7 +196,7 @@ PatternChecker::visit (TupleExpr &expr) void PatternChecker::visit (TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -210,13 +210,13 @@ PatternChecker::visit (StructExprFieldIdentifier &) void PatternChecker::visit (StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void PatternChecker::visit (StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -233,10 +233,10 @@ PatternChecker::visit (StructExprStructBase &) void PatternChecker::visit (CallExpr &expr) { - if (!expr.get_fnexpr ()) + if (!expr.has_fnexpr ()) return; - NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid (); + NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; if (flag_name_resolution_2_0) { @@ -266,7 +266,7 @@ PatternChecker::visit (CallExpr &expr) void PatternChecker::visit (MethodCallExpr &expr) { - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto &arg : expr.get_arguments ()) arg->accept_vis (*this); @@ -275,13 +275,13 @@ PatternChecker::visit (MethodCallExpr &expr) void PatternChecker::visit (FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); } void PatternChecker::visit (ClosureExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -291,7 +291,7 @@ PatternChecker::visit (BlockExpr &expr) stmt->accept_vis (*this); if (expr.has_expr ()) - expr.get_final_expr ()->accept_vis (*this); + expr.get_final_expr ().accept_vis (*this); } void @@ -302,26 +302,26 @@ void PatternChecker::visit (BreakExpr &expr) { if (expr.has_break_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PatternChecker::visit (RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void PatternChecker::visit (RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -331,76 +331,76 @@ PatternChecker::visit (RangeFullExpr &) void PatternChecker::visit (RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PatternChecker::visit (RangeToInclExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void PatternChecker::visit (ReturnExpr &expr) { if (expr.has_return_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void PatternChecker::visit (UnsafeBlockExpr &expr) { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void PatternChecker::visit (LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PatternChecker::visit (WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PatternChecker::visit (WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void PatternChecker::visit (IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void PatternChecker::visit (IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void PatternChecker::visit (MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); for (auto &match_arm : expr.get_match_cases ()) - match_arm.get_expr ()->accept_vis (*this); + match_arm.get_expr ().accept_vis (*this); // match expressions are only an entrypoint TyTy::BaseType *scrutinee_ty; bool ok = tyctx.lookup_type ( - expr.get_scrutinee_expr ()->get_mappings ().get_hirid (), &scrutinee_ty); + expr.get_scrutinee_expr ().get_mappings ().get_hirid (), &scrutinee_ty); rust_assert (ok); check_match_usefulness (&tyctx, scrutinee_ty, expr); @@ -468,7 +468,7 @@ PatternChecker::visit (UseDeclaration &) void PatternChecker::visit (Function &function) { - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); } void @@ -510,27 +510,27 @@ PatternChecker::visit (Union &) void PatternChecker::visit (ConstantItem &const_item) { - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); } void PatternChecker::visit (StaticItem &static_item) { - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); } void PatternChecker::visit (TraitItemFunc &item) { if (item.has_block_defined ()) - item.get_block_expr ()->accept_vis (*this); + item.get_block_expr ().accept_vis (*this); } void PatternChecker::visit (TraitItemConst &item) { if (item.has_expr ()) - item.get_expr ()->accept_vis (*this); + item.get_expr ().accept_vis (*this); } void @@ -659,13 +659,13 @@ void PatternChecker::visit (LetStmt &stmt) { if (stmt.has_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } void PatternChecker::visit (ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } void @@ -1161,33 +1161,34 @@ WitnessMatrix::extend (const WitnessMatrix &other) // forward declarations static DeconstructedPat -lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, +lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern &pattern, TyTy::BaseType *scrutinee_ty); static DeconstructedPat lower_tuple_pattern (Resolver::TypeCheckContext *ctx, - HIR::TupleStructPattern *pattern, + HIR::TupleStructPattern &pattern, TyTy::VariantDef *variant, Constructor &ctor) { int arity = variant->get_fields ().size (); - HIR::TupleStructItems *elems = pattern->get_items ().get (); + HIR::TupleStructItems &elems = pattern.get_items (); std::vector fields; - switch (elems->get_item_type ()) + switch (elems.get_item_type ()) { case HIR::TupleStructItems::ItemType::MULTIPLE: { - HIR::TupleStructItemsNoRange *multiple - = static_cast (elems); + HIR::TupleStructItemsNoRange &multiple + = static_cast (elems); rust_assert (variant->get_fields ().size () - == multiple->get_patterns ().size ()); - for (size_t i = 0; i < multiple->get_patterns ().size (); i++) + == multiple.get_patterns ().size ()); + + for (size_t i = 0; i < multiple.get_patterns ().size (); i++) { fields.push_back ( - lower_pattern (ctx, multiple->get_patterns ().at (i).get (), + lower_pattern (ctx, *multiple.get_patterns ().at (i), variant->get_fields ().at (i)->get_field_type ())); } - return DeconstructedPat (ctor, arity, fields, pattern->get_locus ()); + return DeconstructedPat (ctor, arity, fields, pattern.get_locus ()); } break; case HIR::TupleStructItems::ItemType::RANGED: { @@ -1203,7 +1204,7 @@ lower_tuple_pattern (Resolver::TypeCheckContext *ctx, static DeconstructedPat lower_struct_pattern (Resolver::TypeCheckContext *ctx, - HIR::StructPattern *pattern, TyTy::VariantDef *variant, + HIR::StructPattern &pattern, TyTy::VariantDef *variant, Constructor ctor) { int arity = variant->get_fields ().size (); @@ -1211,7 +1212,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, // Initialize all field patterns to wildcard. std::vector fields = std::vector (arity, DeconstructedPat::make_wildcard ( - pattern->get_locus ())); + pattern.get_locus ())); std::map field_map; for (int i = 0; i < arity; i++) @@ -1221,7 +1222,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, } // Fill in the fields with the present patterns. - HIR::StructPatternElements elems = pattern->get_struct_pattern_elems (); + HIR::StructPatternElements elems = pattern.get_struct_pattern_elems (); for (auto &elem : elems.get_struct_pattern_fields ()) { switch (elem->get_item_type ()) @@ -1232,7 +1233,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, int field_idx = field_map.at (ident->get_identifier ().as_string ()); fields.at (field_idx) - = DeconstructedPat::make_wildcard (pattern->get_locus ()); + = DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::StructPatternField::ItemType::IDENT_PAT: { @@ -1241,7 +1242,7 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, int field_idx = field_map.at (ident_pat->get_identifier ().as_string ()); fields.at (field_idx) = lower_pattern ( - ctx, ident_pat->get_pattern ().get (), + ctx, ident_pat->get_pattern (), variant->get_fields ().at (field_idx)->get_field_type ()); } break; @@ -1256,19 +1257,19 @@ lower_struct_pattern (Resolver::TypeCheckContext *ctx, } } - return DeconstructedPat{ctor, arity, fields, pattern->get_locus ()}; + return DeconstructedPat{ctor, arity, fields, pattern.get_locus ()}; }; static DeconstructedPat -lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, +lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern &pattern, TyTy::BaseType *scrutinee_ty) { - HIR::Pattern::PatternType pat_type = pattern->get_pattern_type (); + HIR::Pattern::PatternType pat_type = pattern.get_pattern_type (); switch (pat_type) { case HIR::Pattern::PatternType::WILDCARD: case HIR::Pattern::PatternType::IDENTIFIER: { - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::PATH: { @@ -1276,12 +1277,12 @@ lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, // structs // https://doc.rust-lang.org/reference/patterns.html#path-patterns // unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::REFERENCE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::STRUCT: @@ -1289,15 +1290,15 @@ lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, HirId path_id = UNKNOWN_HIRID; if (pat_type == HIR::Pattern::PatternType::STRUCT) { - HIR::StructPattern *struct_pattern - = static_cast (pattern); - path_id = struct_pattern->get_path ().get_mappings ().get_hirid (); + HIR::StructPattern &struct_pattern + = static_cast (pattern); + path_id = struct_pattern.get_path ().get_mappings ().get_hirid (); } else { - HIR::TupleStructPattern *tuple_pattern - = static_cast (pattern); - path_id = tuple_pattern->get_path ().get_mappings ().get_hirid (); + HIR::TupleStructPattern &tuple_pattern + = static_cast (pattern); + path_id = tuple_pattern.get_path ().get_mappings ().get_hirid (); } rust_assert (scrutinee_ty->get_kind () == TyTy::TypeKind::ADT); @@ -1330,46 +1331,46 @@ lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern *pattern, if (pat_type == HIR::Pattern::PatternType::STRUCT) { - HIR::StructPattern *struct_pattern - = static_cast (pattern); + HIR::StructPattern &struct_pattern + = static_cast (pattern); return lower_struct_pattern (ctx, struct_pattern, variant, ctor); } else { - HIR::TupleStructPattern *tuple_pattern - = static_cast (pattern); + HIR::TupleStructPattern &tuple_pattern + = static_cast (pattern); return lower_tuple_pattern (ctx, tuple_pattern, variant, ctor); } } break; case HIR::Pattern::PatternType::TUPLE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::SLICE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::ALT: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::LITERAL: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::RANGE: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; case HIR::Pattern::PatternType::GROUPED: { // TODO: unimplemented. Treat this pattern as wildcard for now. - return DeconstructedPat::make_wildcard (pattern->get_locus ()); + return DeconstructedPat::make_wildcard (pattern.get_locus ()); } break; default: { @@ -1385,8 +1386,7 @@ lower_arm (Resolver::TypeCheckContext *ctx, HIR::MatchCase &arm, rust_assert (arm.get_arm ().get_patterns ().size () > 0); DeconstructedPat pat - = lower_pattern (ctx, arm.get_arm ().get_patterns ().at (0).get (), - scrutinee_ty); + = lower_pattern (ctx, *arm.get_arm ().get_patterns ().at (0), scrutinee_ty); return MatchArm (pat, arm.get_arm ().has_match_arm_guard ()); } @@ -1495,8 +1495,9 @@ emit_exhaustiveness_error (Resolver::TypeCheckContext *ctx, HIR::MatchExpr &expr, WitnessMatrix &witness) { TyTy::BaseType *scrutinee_ty; - bool ok = ctx->lookup_type ( - expr.get_scrutinee_expr ()->get_mappings ().get_hirid (), &scrutinee_ty); + bool ok + = ctx->lookup_type (expr.get_scrutinee_expr ().get_mappings ().get_hirid (), + &scrutinee_ty); rust_assert (ok); if (!witness.empty ()) @@ -1514,7 +1515,7 @@ emit_exhaustiveness_error (Resolver::TypeCheckContext *ctx, if (i != witness.get_stacks ().size () - 1) buf << " and "; } - rust_error_at (expr.get_scrutinee_expr ()->get_locus (), + rust_error_at (expr.get_scrutinee_expr ().get_locus (), "non-exhaustive patterns: %s not covered", buf.str ().c_str ()); } diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 667e5257b86b..10181b7574f7 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -279,14 +279,14 @@ UnsafeChecker::visit (LiteralExpr &) void UnsafeChecker::visit (BorrowExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (DereferenceExpr &expr) { TyTy::BaseType *to_deref_type; - auto to_deref = expr.get_expr ()->get_mappings ().get_hirid (); + auto to_deref = expr.get_expr ().get_mappings ().get_hirid (); rust_assert (context.lookup_type (to_deref, &to_deref_type)); @@ -299,60 +299,60 @@ UnsafeChecker::visit (DereferenceExpr &expr) void UnsafeChecker::visit (ErrorPropagationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (NegationExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (ArithmeticOrLogicalExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (ComparisonExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (LazyBooleanExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (TypeCastExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (AssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (CompoundAssignmentExpr &expr) { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void UnsafeChecker::visit (GroupedExpr &expr) { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void @@ -365,20 +365,20 @@ UnsafeChecker::visit (ArrayElemsValues &elems) void UnsafeChecker::visit (ArrayElemsCopied &elems) { - elems.get_elem_to_copy ()->accept_vis (*this); + elems.get_elem_to_copy ().accept_vis (*this); } void UnsafeChecker::visit (ArrayExpr &expr) { - expr.get_internal_elements ()->accept_vis (*this); + expr.get_internal_elements ().accept_vis (*this); } void UnsafeChecker::visit (ArrayIndexExpr &expr) { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void @@ -391,7 +391,7 @@ UnsafeChecker::visit (TupleExpr &expr) void UnsafeChecker::visit (TupleIndexExpr &expr) { - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -405,13 +405,13 @@ UnsafeChecker::visit (StructExprFieldIdentifier &) void UnsafeChecker::visit (StructExprFieldIdentifierValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void UnsafeChecker::visit (StructExprFieldIndexValue &field) { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void @@ -428,10 +428,10 @@ UnsafeChecker::visit (StructExprStructBase &) void UnsafeChecker::visit (CallExpr &expr) { - if (!expr.get_fnexpr ()) + if (!expr.has_fnexpr ()) return; - NodeId ast_node_id = expr.get_fnexpr ()->get_mappings ().get_nodeid (); + NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); NodeId ref_node_id; // There are no unsafe types, and functions are defined in the name resolver. @@ -489,7 +489,7 @@ UnsafeChecker::visit (MethodCallExpr &expr) check_unsafe_call (static_cast (method->first), expr.get_locus (), "method"); - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); for (auto &arg : expr.get_arguments ()) arg->accept_vis (*this); @@ -498,14 +498,14 @@ UnsafeChecker::visit (MethodCallExpr &expr) void UnsafeChecker::visit (FieldAccessExpr &expr) { - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); if (unsafe_context.is_in_context ()) return; TyTy::BaseType *receiver_ty; auto ok = context.lookup_type ( - expr.get_receiver_expr ()->get_mappings ().get_hirid (), &receiver_ty); + expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver_ty); rust_assert (ok); if (receiver_ty->get_kind () == TyTy::TypeKind::ADT) @@ -521,7 +521,7 @@ UnsafeChecker::visit (FieldAccessExpr &expr) void UnsafeChecker::visit (ClosureExpr &expr) { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -531,7 +531,7 @@ UnsafeChecker::visit (BlockExpr &expr) stmt->accept_vis (*this); if (expr.has_expr ()) - expr.get_final_expr ()->accept_vis (*this); + expr.get_final_expr ().accept_vis (*this); } void @@ -542,26 +542,26 @@ void UnsafeChecker::visit (BreakExpr &expr) { if (expr.has_break_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeFromToExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeFromExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeToExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void @@ -571,21 +571,21 @@ UnsafeChecker::visit (RangeFullExpr &) void UnsafeChecker::visit (RangeFromToInclExpr &expr) { - expr.get_from_expr ()->accept_vis (*this); - expr.get_to_expr ()->accept_vis (*this); + expr.get_from_expr ().accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void UnsafeChecker::visit (RangeToInclExpr &expr) { - expr.get_to_expr ()->accept_vis (*this); + expr.get_to_expr ().accept_vis (*this); } void UnsafeChecker::visit (ReturnExpr &expr) { if (expr.has_return_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void @@ -593,7 +593,7 @@ UnsafeChecker::visit (UnsafeBlockExpr &expr) { unsafe_context.enter (expr.get_mappings ().get_hirid ()); - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); unsafe_context.exit (); } @@ -601,45 +601,45 @@ UnsafeChecker::visit (UnsafeBlockExpr &expr) void UnsafeChecker::visit (LoopExpr &expr) { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void UnsafeChecker::visit (WhileLoopExpr &expr) { - expr.get_predicate_expr ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void UnsafeChecker::visit (WhileLetLoopExpr &expr) { - expr.get_cond ()->accept_vis (*this); - expr.get_loop_block ()->accept_vis (*this); + expr.get_cond ().accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void UnsafeChecker::visit (IfExpr &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void UnsafeChecker::visit (IfExprConseqElse &expr) { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void UnsafeChecker::visit (MatchExpr &expr) { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); for (auto &match_arm : expr.get_match_cases ()) - match_arm.get_expr ()->accept_vis (*this); + match_arm.get_expr ().accept_vis (*this); } void @@ -716,7 +716,7 @@ UnsafeChecker::visit (Function &function) if (is_unsafe_fn) unsafe_context.enter (function.get_mappings ().get_hirid ()); - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); if (is_unsafe_fn) unsafe_context.exit (); @@ -764,27 +764,27 @@ UnsafeChecker::visit (Union &) void UnsafeChecker::visit (ConstantItem &const_item) { - const_item.get_expr ()->accept_vis (*this); + const_item.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (StaticItem &static_item) { - static_item.get_expr ()->accept_vis (*this); + static_item.get_expr ().accept_vis (*this); } void UnsafeChecker::visit (TraitItemFunc &item) { if (item.has_block_defined ()) - item.get_block_expr ()->accept_vis (*this); + item.get_block_expr ().accept_vis (*this); } void UnsafeChecker::visit (TraitItemConst &item) { if (item.has_expr ()) - item.get_expr ()->accept_vis (*this); + item.get_expr ().accept_vis (*this); } void @@ -931,13 +931,13 @@ void UnsafeChecker::visit (LetStmt &stmt) { if (stmt.has_init_expr ()) - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } void UnsafeChecker::visit (ExprStmt &stmt) { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } void diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc b/gcc/rust/checks/lints/rust-lint-marklive.cc index 00fefbb6182e..91550c7a568f 100644 --- a/gcc/rust/checks/lints/rust-lint-marklive.cc +++ b/gcc/rust/checks/lints/rust-lint-marklive.cc @@ -124,7 +124,7 @@ MarkLive::visit (HIR::PathInExpression &expr) void MarkLive::visit (HIR::MethodCallExpr &expr) { - expr.get_receiver ()->accept_vis (*this); + expr.get_receiver ().accept_vis (*this); visit_path_segment (expr.get_method_name ()); for (auto &argument : expr.get_arguments ()) argument->accept_vis (*this); @@ -182,14 +182,14 @@ void MarkLive::visit (HIR::FieldAccessExpr &expr) { // visit receiver at first - expr.get_receiver_expr ()->accept_vis (*this); + expr.get_receiver_expr ().accept_vis (*this); // resolve the receiver back to ADT type TyTy::BaseType *receiver = nullptr; if (!tyctx->lookup_type ( - expr.get_receiver_expr ()->get_mappings ().get_hirid (), &receiver)) + expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver)) { - rust_error_at (expr.get_receiver_expr ()->get_locus (), + rust_error_at (expr.get_receiver_expr ().get_locus (), "unresolved type for receiver"); } @@ -221,7 +221,7 @@ MarkLive::visit (HIR::FieldAccessExpr &expr) rust_assert (ok); if (index >= variant->num_fields ()) { - rust_error_at (expr.get_receiver_expr ()->get_locus (), + rust_error_at (expr.get_receiver_expr ().get_locus (), "cannot access struct %s by index: %lu", adt->get_name ().c_str (), (unsigned long) index); return; @@ -236,7 +236,7 @@ void MarkLive::visit (HIR::TupleIndexExpr &expr) { // TODO: unused tuple field detection - expr.get_tuple_expr ()->accept_vis (*this); + expr.get_tuple_expr ().accept_vis (*this); } void @@ -249,13 +249,13 @@ MarkLive::visit (HIR::TypeAlias &alias) = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); if (auto id = nr_ctx.lookup ( - alias.get_type_aliased ()->get_mappings ().get_nodeid ())) + alias.get_type_aliased ().get_mappings ().get_nodeid ())) ast_node_id = *id; } else { resolver->lookup_resolved_type ( - alias.get_type_aliased ()->get_mappings ().get_nodeid (), &ast_node_id); + alias.get_type_aliased ().get_mappings ().get_nodeid (), &ast_node_id); } if (auto hid = mappings.lookup_node_to_hir (ast_node_id)) diff --git a/gcc/rust/checks/lints/rust-lint-marklive.h b/gcc/rust/checks/lints/rust-lint-marklive.h index 585a7271c561..a1e8c110a3d3 100644 --- a/gcc/rust/checks/lints/rust-lint-marklive.h +++ b/gcc/rust/checks/lints/rust-lint-marklive.h @@ -43,44 +43,44 @@ class MarkLive : public MarkLiveBase void visit (HIR::BorrowExpr &expr) override { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void visit (HIR::DereferenceExpr &expr) override { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void visit (HIR::NegationExpr &expr) override { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void visit (HIR::LazyBooleanExpr &expr) override { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void visit (HIR::TypeCastExpr &expr) override { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void visit (HIR::GroupedExpr &expr) override { - expr.get_expr_in_parens ()->accept_vis (*this); + expr.get_expr_in_parens ().accept_vis (*this); } void visit (HIR::ArrayExpr &expr) override { - expr.get_internal_elements ()->accept_vis (*this); + expr.get_internal_elements ().accept_vis (*this); } void visit (HIR::ArrayIndexExpr &expr) override { - expr.get_array_expr ()->accept_vis (*this); - expr.get_index_expr ()->accept_vis (*this); + expr.get_array_expr ().accept_vis (*this); + expr.get_index_expr ().accept_vis (*this); } void visit (HIR::ArrayElemsValues &expr) override @@ -107,57 +107,57 @@ class MarkLive : public MarkLiveBase } if (expr.has_expr ()) { - expr.get_final_expr ()->accept_vis (*this); + expr.get_final_expr ().accept_vis (*this); } } void visit (HIR::UnsafeBlockExpr &expr) override { - expr.get_block_expr ()->accept_vis (*this); + expr.get_block_expr ().accept_vis (*this); } void visit (HIR::LoopExpr &expr) override { - expr.get_loop_block ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); } void visit (HIR::BreakExpr &expr) override { if (expr.has_break_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void visit (HIR::WhileLoopExpr &expr) override { - expr.get_loop_block ()->accept_vis (*this); - expr.get_predicate_expr ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); + expr.get_predicate_expr ().accept_vis (*this); } void visit (HIR::Function &function) override { - function.get_definition ()->accept_vis (*this); + function.get_definition ().accept_vis (*this); } void visit (HIR::ReturnExpr &expr) override { if (expr.has_return_expr ()) - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } void visit (HIR::WhileLetLoopExpr &expr) override { - expr.get_loop_block ()->accept_vis (*this); - expr.get_cond ()->accept_vis (*this); + expr.get_loop_block ().accept_vis (*this); + expr.get_cond ().accept_vis (*this); } void visit (HIR::ExprStmt &stmt) override { - stmt.get_expr ()->accept_vis (*this); + stmt.get_expr ().accept_vis (*this); } void visit (HIR::CallExpr &expr) override { - expr.get_fnexpr ()->accept_vis (*this); + expr.get_fnexpr ().accept_vis (*this); for (auto &argument : expr.get_arguments ()) argument->accept_vis (*this); } @@ -169,8 +169,8 @@ class MarkLive : public MarkLiveBase } void visit (HIR::ComparisonExpr &expr) override { - expr.get_lhs ()->accept_vis (*this); - expr.get_rhs ()->accept_vis (*this); + expr.get_lhs ().accept_vis (*this); + expr.get_rhs ().accept_vis (*this); } void visit (HIR::AssignmentExpr &expr) override @@ -187,33 +187,33 @@ class MarkLive : public MarkLiveBase void visit (HIR::IfExpr &expr) override { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); } void visit (HIR::IfExprConseqElse &expr) override { - expr.get_if_condition ()->accept_vis (*this); - expr.get_if_block ()->accept_vis (*this); - expr.get_else_block ()->accept_vis (*this); + expr.get_if_condition ().accept_vis (*this); + expr.get_if_block ().accept_vis (*this); + expr.get_else_block ().accept_vis (*this); } void visit (HIR::MatchExpr &expr) override { - expr.get_scrutinee_expr ()->accept_vis (*this); + expr.get_scrutinee_expr ().accept_vis (*this); std::vector &cases = expr.get_match_cases (); for (auto &&caz : cases) { auto case_arm = caz.get_arm (); if (case_arm.has_match_arm_guard ()) - case_arm.get_guard_expr ()->accept_vis (*this); - caz.get_expr ()->accept_vis (*this); + case_arm.get_guard_expr ().accept_vis (*this); + caz.get_expr ().accept_vis (*this); } } void visit (HIR::TraitItemFunc &item) override { - item.get_block_expr ()->accept_vis (*this); + item.get_block_expr ().accept_vis (*this); } void visit (HIR::ImplBlock &impl) override @@ -228,7 +228,7 @@ class MarkLive : public MarkLiveBase { if (stmt.has_init_expr ()) { - stmt.get_init_expr ()->accept_vis (*this); + stmt.get_init_expr ().accept_vis (*this); } } @@ -253,7 +253,7 @@ class MarkLive : public MarkLiveBase virtual void visit (HIR::StructExprFieldIdentifierValue &field) override { - field.get_value ()->accept_vis (*this); + field.get_value ().accept_vis (*this); } void visit (HIR::StructExprStructBase &stct) override @@ -269,7 +269,7 @@ class MarkLive : public MarkLiveBase void visit (HIR::ClosureExpr &expr) override { - expr.get_expr ()->accept_vis (*this); + expr.get_expr ().accept_vis (*this); } private: diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc index 8ebdd3de6251..1f56d5e2539f 100644 --- a/gcc/rust/hir/rust-hir-dump.cc +++ b/gcc/rust/hir/rust-hir-dump.cc @@ -800,7 +800,7 @@ Dump::visit (QualifiedPathInType &e) end_field ("path_type"); begin_field ("associated_segment"); - do_typepathsegment (*e.get_associated_segment ()); + do_typepathsegment (e.get_associated_segment ()); end_field ("associated_segment"); visit_collection ("segments", e.get_segments ()); @@ -911,7 +911,7 @@ Dump::visit (ArithmeticOrLogicalExpr &e) } put_field ("expr_type", str); do_operatorexpr (e); - visit_field ("right_expr", *e.get_rhs ()); + visit_field ("right_expr", e.get_rhs ()); end ("ArithmeticOrLogicalExpr"); } @@ -946,7 +946,7 @@ Dump::visit (ComparisonExpr &e) } put_field ("expr_type", str); do_operatorexpr (e); - visit_field ("right_expr", *e.get_rhs ()); + visit_field ("right_expr", e.get_rhs ()); end ("ComparisonExpr"); } @@ -969,7 +969,7 @@ Dump::visit (LazyBooleanExpr &e) } do_operatorexpr (e); - visit_field ("right_expr", *e.get_rhs ()); + visit_field ("right_expr", e.get_rhs ()); end ("LazyBooleanExpr"); } @@ -987,7 +987,7 @@ Dump::visit (AssignmentExpr &e) { begin ("AssignmentExpr"); do_operatorexpr (e); - visit_field ("right_expr", *e.get_rhs ()); + visit_field ("right_expr", e.get_rhs ()); end ("AssignmentExpr"); } @@ -997,7 +997,7 @@ Dump::visit (CompoundAssignmentExpr &e) begin ("CompoundAssignmentExpr"); do_operatorexpr (e); - visit_field ("right_expr", *e.get_rhs ()); + visit_field ("right_expr", e.get_rhs ()); std::string str; @@ -1684,7 +1684,7 @@ Dump::visit (TypeAlias &e) else put_field ("where clause", e.get_where_clause ().as_string ()); - put_field ("type", e.get_type_aliased ()->as_string ()); + put_field ("type", e.get_type_aliased ().as_string ()); end ("TypeAlias"); } @@ -2050,7 +2050,7 @@ Dump::visit (IdentifierPattern &e) put_field ("mut", std::to_string (e.is_mut ())); if (e.has_pattern_to_bind ()) - put_field ("to_bind", e.get_to_bind ()->as_string ()); + put_field ("to_bind", e.get_to_bind ().as_string ()); else put_field ("to_bind", "none"); @@ -2094,8 +2094,8 @@ Dump::visit (RangePattern &e) { begin ("RangePattern"); do_mappings (e.get_mappings ()); - put_field ("lower", e.get_lower_bound ()->as_string ()); - put_field ("upper", e.get_upper_bound ()->as_string ()); + put_field ("lower", e.get_lower_bound ().as_string ()); + put_field ("upper", e.get_upper_bound ().as_string ()); put_field ("has_ellipsis_syntax", std::to_string (e.get_has_ellipsis_syntax ())); end ("RangePattern"); @@ -2107,7 +2107,7 @@ Dump::visit (ReferencePattern &e) begin ("ReferencePattern"); do_mappings (e.get_mappings ()); put_field ("mut", std::to_string (e.is_mut ())); - put_field ("pattern", e.get_referenced_pattern ()->as_string ()); + put_field ("pattern", e.get_referenced_pattern ().as_string ()); end ("ReferencePattern"); } @@ -2119,7 +2119,7 @@ Dump::visit (StructPatternFieldTuplePat &e) auto oa = e.get_outer_attrs (); do_outer_attrs (oa); put_field ("index", std::to_string (e.get_index ())); - put_field ("tuple_pattern", e.get_tuple_pattern ()->as_string ()); + put_field ("tuple_pattern", e.get_tuple_pattern ().as_string ()); end ("StructPatternFieldTuplePat"); } @@ -2130,7 +2130,7 @@ Dump::visit (StructPatternFieldIdentPat &e) auto oa = e.get_outer_attrs (); do_outer_attrs (oa); put_field ("ident", e.get_identifier ().as_string ()); - put_field ("ident_pattern", e.get_pattern ()->as_string ()); + put_field ("ident_pattern", e.get_pattern ().as_string ()); end ("StructPatternFieldIdentPat"); } @@ -2248,7 +2248,7 @@ Dump::visit (LetStmt &e) auto oa = e.get_outer_attrs (); do_outer_attrs (oa); - put_field ("variable_pattern", e.get_pattern ()->as_string ()); + put_field ("variable_pattern", e.get_pattern ().as_string ()); visit_field ("type", e.get_type ()); visit_field ("init_expr", e.get_init_expr ()); @@ -2309,7 +2309,7 @@ Dump::visit (ParenthesisedType &e) { begin ("ParenthesisedType"); do_type (e); - put_field ("type_in_parens", e.get_type_in_parens ()->as_string ()); + put_field ("type_in_parens", e.get_type_in_parens ().as_string ()); end ("ParenthesisedType"); } @@ -2345,7 +2345,7 @@ Dump::visit (RawPointerType &e) begin ("RawPointerType"); do_type (e); put_field ("mut", Rust::enum_to_str (e.get_mut ())); - put_field ("type", e.get_type ()->as_string ()); + put_field ("type", e.get_type ().as_string ()); end ("RawPointerType"); } @@ -2356,7 +2356,7 @@ Dump::visit (ReferenceType &e) do_type (e); put_field ("lifetime", e.get_lifetime ().as_string ()); put_field ("mut", enum_to_str (e.get_mut ())); - put_field ("type", e.get_base_type ()->as_string ()); + put_field ("type", e.get_base_type ().as_string ()); end ("ReferenceType"); } diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index cb6993974474..c4a821e98643 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -209,7 +209,7 @@ class OperatorExpr : public ExprWithoutBlock public: location_t get_locus () const override final { return locus; } - std::unique_ptr &get_expr () { return main_or_left_expr; } + Expr &get_expr () { return *main_or_left_expr; } ExprType get_expression_type () const override final { @@ -423,8 +423,8 @@ class ArithmeticOrLogicalExpr : public OperatorExpr void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); } void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); } - std::unique_ptr &get_lhs () { return main_or_left_expr; } - std::unique_ptr &get_rhs () { return right_expr; } + Expr &get_lhs () { return *main_or_left_expr; } + Expr &get_rhs () { return *right_expr; } std::string get_operator_str () const; @@ -497,8 +497,8 @@ class ComparisonExpr : public OperatorExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_lhs () { return main_or_left_expr; } - std::unique_ptr &get_rhs () { return right_expr; } + Expr &get_lhs () { return *main_or_left_expr; } + Expr &get_rhs () { return *right_expr; } ExprType get_kind () { return expr_type; } @@ -571,8 +571,8 @@ class LazyBooleanExpr : public OperatorExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_lhs () { return main_or_left_expr; } - std::unique_ptr &get_rhs () { return right_expr; } + Expr &get_lhs () { return *main_or_left_expr; } + Expr &get_rhs () { return *right_expr; } protected: /* Use covariance to implement clone function as returning this object rather @@ -633,12 +633,9 @@ class TypeCastExpr : public OperatorExpr void accept_vis (HIRExpressionVisitor &vis) override; // FIXME: isn't it the same as get_expr() from parent? - std::unique_ptr &get_casted_expr () { return main_or_left_expr; } + Expr &get_casted_expr () { return *main_or_left_expr; } - std::unique_ptr &get_type_to_convert_to () - { - return type_to_convert_to; - } + Type &get_type_to_convert_to () { return *type_to_convert_to; } protected: /* Use covariance to implement clone function as returning this object rather @@ -700,8 +697,8 @@ class AssignmentExpr : public OperatorExpr void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); } void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); } - std::unique_ptr &get_lhs () { return main_or_left_expr; } - std::unique_ptr &get_rhs () { return right_expr; } + Expr &get_lhs () { return *main_or_left_expr; } + Expr &get_rhs () { return *right_expr; } protected: /* Use covariance to implement clone function as returning this object rather @@ -770,9 +767,9 @@ class CompoundAssignmentExpr : public OperatorExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_lhs () { return main_or_left_expr; } + Expr &get_lhs () { return *main_or_left_expr; } - std::unique_ptr &get_rhs () { return right_expr; } + Expr &get_rhs () { return *right_expr; } void visit_lhs (HIRFullVisitor &vis) { main_or_left_expr->accept_vis (vis); } void visit_rhs (HIRFullVisitor &vis) { right_expr->accept_vis (vis); } @@ -834,7 +831,7 @@ class GroupedExpr : public ExprWithoutBlock, public WithInnerAttrs void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_expr_in_parens () { return expr_in_parens; } + Expr &get_expr_in_parens () { return *expr_in_parens; } ExprType get_expression_type () const override final { @@ -986,9 +983,9 @@ class ArrayElemsCopied : public ArrayElems void accept_vis (HIRFullVisitor &vis) override; - std::unique_ptr &get_elem_to_copy () { return elem_to_copy; } + Expr &get_elem_to_copy () { return *elem_to_copy; } - std::unique_ptr &get_num_copies_expr () { return num_copies; } + Expr &get_num_copies_expr () { return *num_copies; } ArrayElems::ArrayExprType get_array_expr_type () const override final { @@ -1056,10 +1053,7 @@ class ArrayExpr : public ExprWithoutBlock, public WithInnerAttrs void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_internal_elements () - { - return internal_elements; - }; + ArrayElems &get_internal_elements () { return *internal_elements; }; ExprType get_expression_type () const override final { @@ -1125,8 +1119,8 @@ class ArrayIndexExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_array_expr () { return array_expr; } - std::unique_ptr &get_index_expr () { return index_expr; } + Expr &get_array_expr () { return *array_expr; } + Expr &get_index_expr () { return *index_expr; } ExprType get_expression_type () const override final { @@ -1279,7 +1273,7 @@ class TupleIndexExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_tuple_expr () { return tuple_expr; } + Expr &get_tuple_expr () { return *tuple_expr; } ExprType get_expression_type () const override final { @@ -1524,7 +1518,7 @@ class StructExprFieldWithVal : public StructExprField public: std::string as_string () const override; - std::unique_ptr &get_value () { return value; } + Expr &get_value () { return *value; } }; // Identifier and value variant of StructExprField HIR node @@ -1797,7 +1791,8 @@ class CallExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_fnexpr () { return function; } + bool has_fnexpr () const { return function != nullptr; } + Expr &get_fnexpr () { return *function; } size_t num_params () const { return params.size (); } @@ -1884,7 +1879,7 @@ class MethodCallExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_receiver () { return receiver; } + Expr &get_receiver () { return *receiver; } PathExprSegment &get_method_name () { return method_name; }; const PathExprSegment &get_method_name () const { return method_name; }; @@ -1968,7 +1963,7 @@ class FieldAccessExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_receiver_expr () { return receiver; } + Expr &get_receiver_expr () { return *receiver; } Identifier get_field_name () const { return field; } @@ -2058,9 +2053,9 @@ struct ClosureParam } std::vector &get_outer_attrs () { return outer_attrs; } - std::unique_ptr &get_pattern () { return pattern; } + Pattern &get_pattern () { return *pattern; } - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } location_t get_locus () const { return locus; } }; @@ -2128,8 +2123,8 @@ class ClosureExpr : public ExprWithoutBlock bool has_return_type () const { return return_type != nullptr; } - std::unique_ptr &get_return_type () { return return_type; }; - std::unique_ptr &get_expr () { return expr; } + Type &get_return_type () { return *return_type; }; + Expr &get_expr () { return *expr; } bool has_params () const { return !params.empty (); } std::vector &get_params () { return params; } @@ -2243,7 +2238,7 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; } - std::unique_ptr &get_final_expr () { return expr; } + Expr &get_final_expr () { return *expr; } std::vector > &get_statements () { return statements; } @@ -2388,7 +2383,7 @@ class BreakExpr : public ExprWithoutBlock Lifetime &get_label () { return label; } - std::unique_ptr &get_expr () { return break_expr; } + Expr &get_expr () { return *break_expr; } ExprType get_expression_type () const override final { @@ -2468,8 +2463,8 @@ class RangeFromToExpr : public RangeExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_from_expr () { return from; } - std::unique_ptr &get_to_expr () { return to; } + Expr &get_from_expr () { return *from; } + Expr &get_to_expr () { return *to; } protected: /* Use covariance to implement clone function as returning this object rather @@ -2522,7 +2517,7 @@ class RangeFromExpr : public RangeExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_from_expr () { return from; } + Expr &get_from_expr () { return *from; } protected: /* Use covariance to implement clone function as returning this object rather @@ -2576,7 +2571,7 @@ class RangeToExpr : public RangeExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_to_expr () { return to; } + Expr &get_to_expr () { return *to; } protected: /* Use covariance to implement clone function as returning this object rather @@ -2666,8 +2661,8 @@ class RangeFromToInclExpr : public RangeExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_from_expr () { return from; } - std::unique_ptr &get_to_expr () { return to; } + Expr &get_from_expr () { return *from; } + Expr &get_to_expr () { return *to; } protected: /* Use covariance to implement clone function as returning this object rather @@ -2721,7 +2716,7 @@ class RangeToInclExpr : public RangeExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_to_expr () { return to; }; + Expr &get_to_expr () { return *to; }; protected: /* Use covariance to implement clone function as returning this object rather @@ -2790,7 +2785,7 @@ class ReturnExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_expr () { return return_expr; } + Expr &get_expr () { return *return_expr; } ExprType get_expression_type () const override final { @@ -2856,7 +2851,7 @@ class UnsafeBlockExpr : public ExprWithBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_block_expr () { return expr; } + BlockExpr &get_block_expr () { return *expr; } ExprType get_expression_type () const override final { @@ -2932,7 +2927,7 @@ class BaseLoopExpr : public ExprWithBlock location_t get_locus () const override final { return locus; } - std::unique_ptr &get_loop_block () { return loop_block; }; + HIR::BlockExpr &get_loop_block () { return *loop_block; }; LoopLabel &get_loop_label () { return loop_label; } }; @@ -3010,7 +3005,7 @@ class WhileLoopExpr : public BaseLoopExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_predicate_expr () { return condition; } + Expr &get_predicate_expr () { return *condition; } protected: /* Use covariance to implement clone function as returning this object rather @@ -3086,7 +3081,7 @@ class WhileLetLoopExpr : public BaseLoopExpr void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_cond () { return condition; } + Expr &get_cond () { return *condition; } std::vector > &get_patterns () { return match_arm_patterns; @@ -3166,8 +3161,8 @@ class IfExpr : public ExprWithBlock void vis_if_condition (HIRFullVisitor &vis) { condition->accept_vis (vis); } void vis_if_block (HIRFullVisitor &vis) { if_block->accept_vis (vis); } - std::unique_ptr &get_if_condition () { return condition; } - std::unique_ptr &get_if_block () { return if_block; } + Expr &get_if_condition () { return *condition; } + BlockExpr &get_if_block () { return *if_block; } ExprType get_expression_type () const final override { return ExprType::If; } @@ -3230,7 +3225,7 @@ class IfExprConseqElse : public IfExpr void vis_else_block (HIRFullVisitor &vis) { else_block->accept_vis (vis); } - std::unique_ptr &get_else_block () { return else_block; } + ExprWithBlock &get_else_block () { return *else_block; } protected: /* Use covariance to implement clone function as returning this object rather @@ -3330,7 +3325,7 @@ struct MatchArm return match_arm_patterns; } - std::unique_ptr &get_guard_expr () { return guard_expr; } + Expr &get_guard_expr () { return *guard_expr; } location_t get_locus () const { return locus; } }; @@ -3374,7 +3369,7 @@ struct MatchCase Analysis::NodeMapping get_mappings () const { return mappings; } MatchArm &get_arm () { return arm; } - std::unique_ptr &get_expr () { return expr; } + Expr &get_expr () { return *expr; } }; // Match expression HIR node @@ -3435,7 +3430,7 @@ class MatchExpr : public ExprWithBlock, public WithInnerAttrs void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_scrutinee_expr () { return branch_value; } + Expr &get_scrutinee_expr () { return *branch_value; } AST::AttrVec get_inner_attrs () const { return inner_attrs; } const std::vector &get_match_cases () const { return match_arms; } std::vector &get_match_cases () { return match_arms; } @@ -3499,7 +3494,7 @@ class AwaitExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::unique_ptr &get_awaited_expr () { return awaited_expr; } + Expr &get_awaited_expr () { return *awaited_expr; } ExprType get_expression_type () const final override { @@ -3556,7 +3551,7 @@ class AsyncBlockExpr : public ExprWithBlock location_t get_locus () const override final { return locus; } bool get_has_move () const { return has_move; } - std::unique_ptr &get_block_expr () { return block_expr; } + BlockExpr &get_block_expr () { return *block_expr; } void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; @@ -3581,31 +3576,31 @@ class OperatorExprMeta public: OperatorExprMeta (HIR::CompoundAssignmentExpr &expr) : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ()->get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), locus (expr.get_locus ()) {} OperatorExprMeta (HIR::ArithmeticOrLogicalExpr &expr) : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ()->get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), locus (expr.get_locus ()) {} OperatorExprMeta (HIR::NegationExpr &expr) : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ()->get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), locus (expr.get_locus ()) {} OperatorExprMeta (HIR::DereferenceExpr &expr) : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ()->get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), locus (expr.get_locus ()) {} OperatorExprMeta (HIR::ArrayIndexExpr &expr) : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_array_expr ()->get_mappings ()), + lvalue_mappings (expr.get_array_expr ().get_mappings ()), locus (expr.get_locus ()) {} diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 86fdabd74b33..ba554d545c2a 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -118,7 +118,7 @@ class TypeParam : public GenericParam Identifier get_type_representation () const { return type_representation; } - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } Analysis::NodeMapping get_type_mappings () const { @@ -277,7 +277,7 @@ class TypeBoundWhereClauseItem : public WhereClauseItem std::vector &get_for_lifetimes () { return for_lifetimes; } - std::unique_ptr &get_bound_type () { return bound_type; } + Type &get_bound_type () { return *bound_type; } std::vector> &get_type_param_bounds () { @@ -452,7 +452,7 @@ struct SelfParam ImplicitSelfKind get_self_kind () const { return self_kind; } - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } Analysis::NodeMapping get_mappings () { return mappings; } @@ -547,9 +547,11 @@ struct FunctionParam location_t get_locus () const { return locus; } - std::unique_ptr &get_param_name () { return param_name; } + Pattern &get_param_name () { return *param_name; } - std::unique_ptr &get_type () { return type; } + std::unique_ptr take_param_name () { return std::move (param_name); } + + Type &get_type () { return *type; } const Analysis::NodeMapping &get_mappings () const { return mappings; } }; @@ -1056,7 +1058,7 @@ class UseDeclaration : public VisItem location_t get_locus () const override final { return locus; } ItemKind get_item_kind () const override { return ItemKind::UseDeclaration; } - std::unique_ptr &get_use_tree () { return use_tree; } + UseTree &get_use_tree () { return *use_tree; } void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRStmtVisitor &vis) override; void accept_vis (HIRVisItemVisitor &vis) override; @@ -1210,7 +1212,7 @@ class Function : public VisItem, public ImplItem } // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr &get_definition () { return function_body; } + BlockExpr &get_definition () { return *function_body; } const FunctionQualifiers &get_qualifiers () const { return qualifiers; } @@ -1222,7 +1224,7 @@ class Function : public VisItem, public ImplItem bool has_return_type () const { return return_type != nullptr; } // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr &get_return_type () { return return_type; } + Type &get_return_type () { return *return_type; } bool is_method () const { return !self.is_error (); } @@ -1337,7 +1339,7 @@ class TypeAlias : public VisItem, public ImplItem WhereClause &get_where_clause () { return where_clause; } - std::unique_ptr &get_type_aliased () { return existing_type; } + Type &get_type_aliased () { return *existing_type; } Identifier get_new_type_name () const { return new_type_name; } @@ -1503,7 +1505,7 @@ class StructField Identifier get_field_name () const { return field_name; } - std::unique_ptr &get_field_type () { return field_type; } + Type &get_field_type () { return *field_type; } Analysis::NodeMapping get_mappings () const { return mappings; } @@ -1640,7 +1642,7 @@ class TupleField location_t get_locus () const { return locus; } AST::AttrVec &get_outer_attrs () { return outer_attrs; } - std::unique_ptr &get_field_type () { return field_type; } + HIR::Type &get_field_type () { return *field_type; } }; // Rust tuple declared using struct keyword HIR node @@ -1855,7 +1857,12 @@ class EnumItemDiscriminant : public EnumItem void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRStmtVisitor &vis) override; - std::unique_ptr &get_discriminant_expression () { return expression; } + Expr &get_discriminant_expression () { return *expression; } + + std::unique_ptr take_discriminant_expression () + { + return std::move (expression); + } protected: // Clone function implementation as (not pure) virtual method @@ -2126,9 +2133,9 @@ class ConstantItem : public VisItem, public ImplItem void accept_vis (HIRImplVisitor &vis) override; void accept_vis (HIRVisItemVisitor &vis) override; - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } - std::unique_ptr &get_expr () { return const_expr; } + Expr &get_expr () { return *const_expr; } Identifier get_identifier () const { return identifier; } @@ -2222,9 +2229,9 @@ class StaticItem : public VisItem bool is_mut () const { return mut == Mutability::Mut; } - std::unique_ptr &get_expr () { return expr; } + Expr &get_expr () { return *expr; } - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } ItemKind get_item_kind () const override { return ItemKind::Static; } @@ -2324,7 +2331,7 @@ class TraitFunctionDecl return generic_params; } - std::unique_ptr &get_return_type () { return return_type; } + Type &get_return_type () { return *return_type; } std::vector &get_function_params () { return function_params; } @@ -2391,7 +2398,7 @@ class TraitItemFunc : public TraitItem bool has_block_defined () const { return block_expr != nullptr; } - std::unique_ptr &get_block_expr () { return block_expr; } + BlockExpr &get_block_expr () { return *block_expr; } const std::string trait_identifier () const override final { @@ -2476,9 +2483,9 @@ class TraitItemConst : public TraitItem bool has_expr () const { return expr != nullptr; } - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } - std::unique_ptr &get_expr () { return expr; } + Expr &get_expr () { return *expr; } const std::string trait_identifier () const override final { @@ -2828,7 +2835,9 @@ class ImplBlock : public VisItem, public WithInnerAttrs location_t get_locus () const override final { return locus; } - std::unique_ptr &get_type () { return impl_type; }; + Type &get_type () { return *impl_type; }; + + bool has_type () { return impl_type == nullptr; } std::vector> &get_generic_params () { @@ -2837,7 +2846,7 @@ class ImplBlock : public VisItem, public WithInnerAttrs bool has_trait_ref () const { return trait_ref != nullptr; } - std::unique_ptr &get_trait_ref () { return trait_ref; } + TypePath &get_trait_ref () { return *trait_ref; } WhereClause &get_where_clause () { return where_clause; } @@ -2976,7 +2985,7 @@ class ExternalStaticItem : public ExternalItem Mutability get_mut () { return mut; } - std::unique_ptr &get_item_type () { return item_type; } + Type &get_item_type () { return *item_type; } ExternKind get_extern_kind () override { return ExternKind::Static; } @@ -3033,7 +3042,7 @@ struct NamedFunctionParam Identifier get_param_name () const { return name; } - std::unique_ptr &get_type () { return param_type; } + Type &get_type () { return *param_type; } Analysis::NodeMapping get_mappings () const { return mappings; } }; @@ -3132,7 +3141,7 @@ class ExternalFunctionItem : public ExternalItem return generic_params; } - std::unique_ptr &get_return_type () { return return_type; } + Type &get_return_type () { return *return_type; } std::vector &get_function_params () { diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index 0566e2e2a1cb..b561c30c49dc 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -107,8 +107,8 @@ class GenericArgsBinding Identifier &get_identifier () { return identifier; } const Identifier &get_identifier () const { return identifier; } - std::unique_ptr &get_type () { return type; } - const std::unique_ptr &get_type () const { return type; } + Type &get_type () { return *type; } + const Type &get_type () const { return *type; } location_t get_locus () const { return locus; } }; @@ -574,7 +574,7 @@ class TypePathFunction TypePathFunction (TypePathFunction const &other) { return_type = other.has_return_type () - ? other.get_return_type ()->clone_type () + ? other.get_return_type ().clone_type () : nullptr; inputs.reserve (other.inputs.size ()); @@ -588,7 +588,7 @@ class TypePathFunction TypePathFunction &operator= (TypePathFunction const &other) { return_type = other.has_return_type () - ? other.get_return_type ()->clone_type () + ? other.get_return_type ().clone_type () : nullptr; inputs.reserve (other.inputs.size ()); @@ -610,8 +610,8 @@ class TypePathFunction }; std::vector > &get_params () { return inputs; }; - const std::unique_ptr &get_return_type () const { return return_type; }; - std::unique_ptr &get_return_type () { return return_type; }; + const Type &get_return_type () const { return *return_type; }; + Type &get_return_type () { return *return_type; }; }; // Segment used in type path with a function argument @@ -811,9 +811,9 @@ class QualifiedPathType Analysis::NodeMapping get_mappings () const { return mappings; } - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } - std::unique_ptr &get_trait () { return trait; } + TypePath &get_trait () { return *trait; } bool trait_has_generic_args () const { @@ -963,10 +963,7 @@ class QualifiedPathInType : public TypeNoBounds QualifiedPathType &get_path_type () { return path_type; } - std::unique_ptr &get_associated_segment () - { - return associated_segment; - } + TypePathSegment &get_associated_segment () { return *associated_segment; } std::vector > &get_segments () { diff --git a/gcc/rust/hir/tree/rust-hir-pattern.h b/gcc/rust/hir/tree/rust-hir-pattern.h index e65a62f20a84..bedc6de51f69 100644 --- a/gcc/rust/hir/tree/rust-hir-pattern.h +++ b/gcc/rust/hir/tree/rust-hir-pattern.h @@ -132,7 +132,7 @@ class IdentifierPattern : public Pattern bool is_mut () const { return mut == Mutability::Mut; } bool get_is_ref () const { return is_ref; } - std::unique_ptr &get_to_bind () { return to_bind; } + Pattern &get_to_bind () { return *to_bind; } void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRPatternVisitor &vis) override; @@ -405,9 +405,9 @@ class RangePattern : public Pattern return PatternType::RANGE; } - std::unique_ptr &get_lower_bound () { return lower; } + RangePatternBound &get_lower_bound () { return *lower; } - std::unique_ptr &get_upper_bound () { return upper; } + RangePatternBound &get_upper_bound () { return *upper; } protected: /* Use covariance to implement clone function as returning this object rather @@ -476,7 +476,7 @@ class ReferencePattern : public Pattern return PatternType::REFERENCE; } - std::unique_ptr &get_referenced_pattern () { return pattern; } + Pattern &get_referenced_pattern () { return *pattern; } protected: /* Use covariance to implement clone function as returning this object rather @@ -572,7 +572,7 @@ class StructPatternFieldTuplePat : public StructPatternField void accept_vis (HIRFullVisitor &vis) override; TupleIndex get_index () { return index; } - std::unique_ptr &get_tuple_pattern () { return tuple_pattern; } + Pattern &get_tuple_pattern () { return *tuple_pattern; } ItemType get_item_type () const override final { return ItemType::TUPLE_PAT; } @@ -630,7 +630,7 @@ class StructPatternFieldIdentPat : public StructPatternField Identifier get_identifier () const { return ident; } - std::unique_ptr &get_pattern () { return ident_pattern; } + Pattern &get_pattern () { return *ident_pattern; } protected: /* Use covariance to implement clone function as returning this object rather @@ -1002,7 +1002,7 @@ class TupleStructPattern : public Pattern PathInExpression &get_path () { return path; } - std::unique_ptr &get_items () { return items; } + TupleStructItems &get_items () { return *items; } const Analysis::NodeMapping &get_mappings () const override final { @@ -1221,8 +1221,8 @@ class TuplePattern : public Pattern return PatternType::TUPLE; } - std::unique_ptr &get_items () { return items; } - const std::unique_ptr &get_items () const { return items; } + TuplePatternItems &get_items () { return *items; } + const TuplePatternItems &get_items () const { return *items; } protected: /* Use covariance to implement clone function as returning this object rather diff --git a/gcc/rust/hir/tree/rust-hir-stmt.h b/gcc/rust/hir/tree/rust-hir-stmt.h index a51dab7b82af..f63717732020 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.h +++ b/gcc/rust/hir/tree/rust-hir-stmt.h @@ -144,11 +144,11 @@ class LetStmt : public Stmt } std::vector &get_outer_attrs () { return outer_attrs; } - std::unique_ptr &get_type () { return type; } + HIR::Type &get_type () { return *type; } - std::unique_ptr &get_init_expr () { return init_expr; } + HIR::Expr &get_init_expr () { return *init_expr; } - std::unique_ptr &get_pattern () { return variables_pattern; } + HIR::Pattern &get_pattern () { return *variables_pattern; } bool is_item () const override final { return false; } @@ -186,7 +186,7 @@ class ExprStmt : public Stmt bool is_item () const override final { return false; } - std::unique_ptr &get_expr () { return expr; } + Expr &get_expr () { return *expr; } // Copy constructor with clone ExprStmt (ExprStmt const &other) diff --git a/gcc/rust/hir/tree/rust-hir-type.h b/gcc/rust/hir/tree/rust-hir-type.h index 8f068ef230e0..a8ac4a1b076e 100644 --- a/gcc/rust/hir/tree/rust-hir-type.h +++ b/gcc/rust/hir/tree/rust-hir-type.h @@ -271,7 +271,7 @@ class ParenthesisedType : public TypeNoBounds * parenthesised type, it must be in parentheses. */ return type_in_parens->to_trait_bound (true); } - std::unique_ptr &get_type_in_parens () { return type_in_parens; } + Type &get_type_in_parens () { return *type_in_parens; } void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRTypeVisitor &vis) override; }; @@ -439,7 +439,7 @@ class RawPointerType : public TypeNoBounds void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRTypeVisitor &vis) override; - std::unique_ptr &get_type () { return type; } + Type &get_type () { return *type; } Mutability get_mut () const { return mut; } @@ -447,7 +447,7 @@ class RawPointerType : public TypeNoBounds bool is_const () const { return mut == Mutability::Imm; } - std::unique_ptr &get_base_type () { return type; } + Type &get_base_type () { return *type; } protected: /* Use covariance to implement clone function as returning this object rather @@ -520,7 +520,7 @@ class ReferenceType : public TypeNoBounds Mutability get_mut () const { return mut; } - std::unique_ptr &get_base_type () { return type; } + Type &get_base_type () { return *type; } protected: /* Use covariance to implement clone function as returning this object rather @@ -578,9 +578,9 @@ class ArrayType : public TypeNoBounds void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRTypeVisitor &vis) override; - std::unique_ptr &get_element_type () { return elem_type; } + Type &get_element_type () { return *elem_type; } - std::unique_ptr &get_size_expr () { return size; } + Expr &get_size_expr () { return *size; } protected: /* Use covariance to implement clone function as returning this object rather @@ -633,7 +633,7 @@ class SliceType : public TypeNoBounds void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRTypeVisitor &vis) override; - std::unique_ptr &get_element_type () { return elem_type; } + Type &get_element_type () { return *elem_type; } protected: /* Use covariance to implement clone function as returning this object rather @@ -743,7 +743,7 @@ struct MaybeNamedParam location_t get_locus () const { return locus; } - std::unique_ptr &get_type () { return param_type; } + Type &get_type () { return *param_type; } ParamKind get_param_kind () const { return param_kind; } @@ -828,7 +828,7 @@ class BareFunctionType : public TypeNoBounds } // TODO: would a "vis_type" be better? - std::unique_ptr &get_return_type () { return return_type; } + Type &get_return_type () { return *return_type; } protected: /* Use covariance to implement clone function as returning this object rather diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 38d35eea6e38..44dcc8e768e2 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -765,11 +765,8 @@ class ConstGenericParam : public GenericParam bool has_default_expression () { return default_expression != nullptr; } std::string get_name () { return name; } - std::unique_ptr &get_type () { return type; } - std::unique_ptr &get_default_expression () - { - return default_expression; - } + Type &get_type () { return *type; } + Expr &get_default_expression () { return *default_expression; } protected: /* Use covariance to implement clone function as returning this object rather diff --git a/gcc/rust/typecheck/rust-autoderef.cc b/gcc/rust/typecheck/rust-autoderef.cc index 285857323225..a0a2b50019bc 100644 --- a/gcc/rust/typecheck/rust-autoderef.cc +++ b/gcc/rust/typecheck/rust-autoderef.cc @@ -209,7 +209,7 @@ resolve_operator_overload_fn ( == 0) { TraitReference *trait_reference - = TraitResolver::Lookup (*parent->get_trait_ref ().get ()); + = TraitResolver::Lookup (parent->get_trait_ref ()); if (!trait_reference->is_error ()) { TyTy::BaseType *lookup = nullptr; diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.cc b/gcc/rust/typecheck/rust-hir-dot-operator.cc index 2c8e53e9bc4d..38535ccfd743 100644 --- a/gcc/rust/typecheck/rust-hir-dot-operator.cc +++ b/gcc/rust/typecheck/rust-hir-dot-operator.cc @@ -200,104 +200,103 @@ MethodResolver::select (TyTy::BaseType &receiver) }; std::vector trait_fns; - mappings.iterate_impl_blocks ( - [&] (HirId id, HIR::ImplBlock *impl) mutable -> bool { - bool is_trait_impl = impl->has_trait_ref (); - if (!is_trait_impl) - return true; - - // look for impl implementation else lookup the associated trait item - for (auto &impl_item : impl->get_impl_items ()) - { - bool is_fn = impl_item->get_impl_item_type () - == HIR::ImplItem::ImplItemType::FUNCTION; - if (!is_fn) - continue; - - HIR::Function *func = static_cast (impl_item.get ()); - if (!func->is_method ()) - continue; - - bool name_matches = func->get_function_name ().as_string ().compare ( - segment_name.as_string ()) - == 0; - if (!name_matches) - continue; - - TyTy::BaseType *ty = nullptr; - if (!query_type (func->get_mappings ().get_hirid (), &ty)) - continue; - if (ty->get_kind () == TyTy::TypeKind::ERROR) - continue; - - rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF); - TyTy::FnType *fnty = static_cast (ty); - const TyTy::BaseType *impl_self - = TypeCheckItem::ResolveImplBlockSelf (*impl); - - // see: - // https://gcc-rust.zulipchat.com/#narrow/stream/266897-general/topic/Method.20Resolution/near/338646280 - // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L650-L660 - bool impl_self_is_ptr - = impl_self->get_kind () == TyTy::TypeKind::POINTER; - bool impl_self_is_ref = impl_self->get_kind () == TyTy::TypeKind::REF; - if (receiver_is_raw_ptr && impl_self_is_ptr) - { - const TyTy::PointerType &sptr - = *static_cast (impl_self); - const TyTy::PointerType &ptr - = *static_cast (raw); - - // we could do this via lang-item assemblies if we refactor this - bool mut_match = sptr.mutability () == ptr.mutability (); - if (!mut_match) - continue; - } - else if (receiver_is_ref && impl_self_is_ref) - { - const TyTy::ReferenceType &sptr - = *static_cast (impl_self); - const TyTy::ReferenceType &ptr - = *static_cast (raw); - - // we could do this via lang-item assemblies if we refactor this - bool mut_match = sptr.mutability () == ptr.mutability (); - if (!mut_match) - continue; - } + mappings.iterate_impl_blocks ([&] (HirId id, + HIR::ImplBlock *impl) mutable -> bool { + bool is_trait_impl = impl->has_trait_ref (); + if (!is_trait_impl) + return true; - inherent_impl_fns.push_back ({func, impl, fnty}); - return true; - } + // look for impl implementation else lookup the associated trait item + for (auto &impl_item : impl->get_impl_items ()) + { + bool is_fn = impl_item->get_impl_item_type () + == HIR::ImplItem::ImplItemType::FUNCTION; + if (!is_fn) + continue; + + HIR::Function *func = static_cast (impl_item.get ()); + if (!func->is_method ()) + continue; + + bool name_matches = func->get_function_name ().as_string ().compare ( + segment_name.as_string ()) + == 0; + if (!name_matches) + continue; + + TyTy::BaseType *ty = nullptr; + if (!query_type (func->get_mappings ().get_hirid (), &ty)) + continue; + if (ty->get_kind () == TyTy::TypeKind::ERROR) + continue; + + rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF); + TyTy::FnType *fnty = static_cast (ty); + const TyTy::BaseType *impl_self + = TypeCheckItem::ResolveImplBlockSelf (*impl); + + // see: + // https://gcc-rust.zulipchat.com/#narrow/stream/266897-general/topic/Method.20Resolution/near/338646280 + // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/method/probe.rs#L650-L660 + bool impl_self_is_ptr + = impl_self->get_kind () == TyTy::TypeKind::POINTER; + bool impl_self_is_ref = impl_self->get_kind () == TyTy::TypeKind::REF; + if (receiver_is_raw_ptr && impl_self_is_ptr) + { + const TyTy::PointerType &sptr + = *static_cast (impl_self); + const TyTy::PointerType &ptr + = *static_cast (raw); + + // we could do this via lang-item assemblies if we refactor this + bool mut_match = sptr.mutability () == ptr.mutability (); + if (!mut_match) + continue; + } + else if (receiver_is_ref && impl_self_is_ref) + { + const TyTy::ReferenceType &sptr + = *static_cast (impl_self); + const TyTy::ReferenceType &ptr + = *static_cast (raw); + + // we could do this via lang-item assemblies if we refactor this + bool mut_match = sptr.mutability () == ptr.mutability (); + if (!mut_match) + continue; + } + + inherent_impl_fns.push_back ({func, impl, fnty}); + return true; + } - TraitReference *trait_ref - = TraitResolver::Resolve (*impl->get_trait_ref ().get ()); - rust_assert (!trait_ref->is_error ()); + TraitReference *trait_ref = TraitResolver::Resolve (impl->get_trait_ref ()); + rust_assert (!trait_ref->is_error ()); - auto item_ref - = trait_ref->lookup_trait_item (segment_name.as_string (), - TraitItemReference::TraitItemType::FN); - if (item_ref->is_error ()) - return true; + auto item_ref + = trait_ref->lookup_trait_item (segment_name.as_string (), + TraitItemReference::TraitItemType::FN); + if (item_ref->is_error ()) + return true; - const HIR::Trait *trait = trait_ref->get_hir_trait_ref (); - HIR::TraitItem *item = item_ref->get_hir_trait_item (); - if (item->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC) - return true; + const HIR::Trait *trait = trait_ref->get_hir_trait_ref (); + HIR::TraitItem *item = item_ref->get_hir_trait_item (); + if (item->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC) + return true; - HIR::TraitItemFunc *func = static_cast (item); - if (!func->get_decl ().is_method ()) - return true; + HIR::TraitItemFunc *func = static_cast (item); + if (!func->get_decl ().is_method ()) + return true; - TyTy::BaseType *ty = item_ref->get_tyty (); - rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF); - TyTy::FnType *fnty = static_cast (ty); + TyTy::BaseType *ty = item_ref->get_tyty (); + rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF); + TyTy::FnType *fnty = static_cast (ty); - trait_item_candidate candidate{func, trait, fnty, trait_ref, item_ref}; - trait_fns.push_back (candidate); + trait_item_candidate candidate{func, trait, fnty, trait_ref, item_ref}; + trait_fns.push_back (candidate); - return true; - }); + return true; + }); // lookup specified bounds for an associated item struct precdicate_candidate diff --git a/gcc/rust/typecheck/rust-hir-inherent-impl-overlap.h b/gcc/rust/typecheck/rust-hir-inherent-impl-overlap.h index a9edc42f1a62..b390d00f031b 100644 --- a/gcc/rust/typecheck/rust-hir-inherent-impl-overlap.h +++ b/gcc/rust/typecheck/rust-hir-inherent-impl-overlap.h @@ -54,7 +54,7 @@ class OverlappingImplItemPass : public TypeCheckBase // impl-type -> [ (item, name), ... ] // } - HirId impl_type_id = impl->get_type ()->get_mappings ().get_hirid (); + HirId impl_type_id = impl->get_type ().get_mappings ().get_hirid (); TyTy::BaseType *impl_type = nullptr; bool ok = query_type (impl_type_id, &impl_type); if (!ok) diff --git a/gcc/rust/typecheck/rust-hir-path-probe.cc b/gcc/rust/typecheck/rust-hir-path-probe.cc index aa74f2e56d55..fa3ef47ebeaa 100644 --- a/gcc/rust/typecheck/rust-hir-path-probe.cc +++ b/gcc/rust/typecheck/rust-hir-path-probe.cc @@ -297,7 +297,7 @@ PathProbeType::process_impl_item_candidate (HirId id, HIR::ImplItem *item, HIR::ImplBlock *impl) { current_impl = impl; - HirId impl_ty_id = impl->get_type ()->get_mappings ().get_hirid (); + HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid (); TyTy::BaseType *impl_block_ty = nullptr; if (!query_type (impl_ty_id, &impl_block_ty)) return; @@ -472,8 +472,7 @@ PathProbeImplTrait::process_trait_impl_items_for_candidates () if (!impl->has_trait_ref ()) return true; - TraitReference *resolved - = TraitResolver::Lookup (*(impl->get_trait_ref ().get ())); + TraitReference *resolved = TraitResolver::Lookup (impl->get_trait_ref ()); if (!trait_reference->is_equal (*resolved)) return true; diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index 3e66e7591c7d..03614200ba25 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -216,8 +216,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) // The one exception is the implicit Self type of a trait bool apply_sized = !is_self; auto param_type - = TypeResolveGenericParam::Resolve (generic_param.get (), - apply_sized); + = TypeResolveGenericParam::Resolve (*generic_param, apply_sized); context->insert_type (generic_param->get_mappings (), param_type); substitutions.push_back ( TyTy::SubstitutionParamMapping (typaram, param_type)); @@ -268,7 +267,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) auto predicate = get_predicate_from_bound ( b->get_path (), - nullptr /*this will setup a PLACEHOLDER for self*/); + tl::nullopt /*this will setup a PLACEHOLDER for self*/); if (predicate.is_error ()) return &TraitReference::error_node (); @@ -384,11 +383,11 @@ TraitItemReference::resolve_item (HIR::TraitItemFunc &func) auto expected_ret_tyty = resolved_fn_type->get_return_type (); context->push_return_type (TypeCheckContextItem (&func), expected_ret_tyty); - auto block_expr_ty = TypeCheckExpr::Resolve (func.get_block_expr ().get ()); + auto block_expr_ty = TypeCheckExpr::Resolve (func.get_block_expr ()); location_t fn_return_locus = func.get_decl ().has_return_type () - ? func.get_decl ().get_return_type ()->get_locus () + ? func.get_decl ().get_return_type ().get_locus () : func.get_locus (); coercion_site (func.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index 82782c68f418..c1700fafdfc9 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc @@ -374,22 +374,21 @@ TypeCheckBase::resolve_generic_params ( break; case HIR::GenericParam::GenericKind::CONST: { - auto param - = static_cast (generic_param.get ()); - auto specified_type - = TypeCheckType::Resolve (param->get_type ().get ()); + auto ¶m + = static_cast (*generic_param); + auto specified_type = TypeCheckType::Resolve (param.get_type ()); - if (param->has_default_expression ()) + if (param.has_default_expression ()) { - auto expr_type = TypeCheckExpr::Resolve ( - param->get_default_expression ().get ()); - - coercion_site ( - param->get_mappings ().get_hirid (), - TyTy::TyWithLocation (specified_type), - TyTy::TyWithLocation ( - expr_type, param->get_default_expression ()->get_locus ()), - param->get_locus ()); + auto expr_type + = TypeCheckExpr::Resolve (param.get_default_expression ()); + + coercion_site (param.get_mappings ().get_hirid (), + TyTy::TyWithLocation (specified_type), + TyTy::TyWithLocation ( + expr_type, + param.get_default_expression ().get_locus ()), + param.get_locus ()); } context->insert_type (generic_param->get_mappings (), @@ -398,8 +397,7 @@ TypeCheckBase::resolve_generic_params ( break; case HIR::GenericParam::GenericKind::TYPE: { - auto param_type - = TypeResolveGenericParam::Resolve (generic_param.get ()); + auto param_type = TypeResolveGenericParam::Resolve (*generic_param); context->insert_type (generic_param->get_mappings (), param_type); substitutions.push_back (TyTy::SubstitutionParamMapping ( diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.h b/gcc/rust/typecheck/rust-hir-type-check-base.h index b085b4b7a4e3..8b071949eb4f 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.h +++ b/gcc/rust/typecheck/rust-hir-type-check-base.h @@ -37,10 +37,10 @@ class TypeCheckBase TraitReference *resolve_trait_path (HIR::TypePath &); - TyTy::TypeBoundPredicate - get_predicate_from_bound (HIR::TypePath &path, HIR::Type *associated_self, - BoundPolarity polarity - = BoundPolarity::RegularBound); + TyTy::TypeBoundPredicate get_predicate_from_bound ( + HIR::TypePath &path, + tl::optional> associated_self, + BoundPolarity polarity = BoundPolarity::RegularBound); bool check_for_unconstrained ( const std::vector ¶ms_to_constrain, @@ -55,7 +55,7 @@ class TypeCheckBase location_t locus); void resolve_generic_params ( - const std::vector > &generic_params, + const std::vector> &generic_params, std::vector &substitutions); TyTy::TypeBoundPredicate get_marker_predicate (LangItem::Kind item_type, diff --git a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc index 521e7977f99b..5e6087d78b22 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // . +#include "rust-hir-expr.h" #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-enumitem.h" @@ -29,25 +30,25 @@ namespace Rust { namespace Resolver { TyTy::VariantDef * -TypeCheckEnumItem::Resolve (HIR::EnumItem *item, int64_t last_discriminant) +TypeCheckEnumItem::Resolve (HIR::EnumItem &item, int64_t last_discriminant) { TypeCheckEnumItem resolver (last_discriminant); - switch (item->get_enum_item_kind ()) + switch (item.get_enum_item_kind ()) { case HIR::EnumItem::EnumItemKind::Named: - resolver.visit (static_cast (*item)); + resolver.visit (static_cast (item)); break; case HIR::EnumItem::EnumItemKind::Tuple: - resolver.visit (static_cast (*item)); + resolver.visit (static_cast (item)); break; case HIR::EnumItem::EnumItemKind::Struct: - resolver.visit (static_cast (*item)); + resolver.visit (static_cast (item)); break; case HIR::EnumItem::EnumItemKind::Discriminant: - resolver.visit (static_cast (*item)); + resolver.visit (static_cast (item)); break; } return resolver.variant; @@ -68,11 +69,10 @@ TypeCheckEnumItem::visit (HIR::EnumItem &item) mappings.get_next_hir_id ( item.get_mappings ().get_crate_num ()), item.get_mappings ().get_local_defid ()); - HIR::LiteralExpr *discim_expr - = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant), - HIR::Literal::LitType::INT, - PrimitiveCoreType::CORETYPE_I64, item.get_locus (), - {}); + auto discim_expr = Rust::make_unique ( + HIR::LiteralExpr (mapping, std::to_string (last_discriminant), + HIR::Literal::LitType::INT, + PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {})); TyTy::BaseType *isize = nullptr; bool ok = context->lookup_builtin ("isize", &isize); @@ -101,7 +101,7 @@ TypeCheckEnumItem::visit (HIR::EnumItem &item) variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), item.get_mappings ().get_defid (), item.get_identifier ().as_string (), ident, - discim_expr); + std::move (discim_expr)); } void @@ -111,13 +111,13 @@ TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item) rust_error_at (item.get_locus (), "discriminant too big"); auto &discriminant = item.get_discriminant_expression (); - auto capacity_type = TypeCheckExpr::Resolve (discriminant.get ()); + auto capacity_type = TypeCheckExpr::Resolve (discriminant); if (capacity_type->get_kind () == TyTy::TypeKind::ERROR) return; TyTy::ISizeType *expected_ty - = new TyTy::ISizeType (discriminant->get_mappings ().get_hirid ()); - context->insert_type (discriminant->get_mappings (), expected_ty); + = new TyTy::ISizeType (discriminant.get_mappings ().get_hirid ()); + context->insert_type (discriminant.get_mappings (), expected_ty); unify_site (item.get_mappings ().get_hirid (), TyTy::TyWithLocation (expected_ty), @@ -145,7 +145,7 @@ TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item) variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), item.get_mappings ().get_defid (), item.get_identifier ().as_string (), ident, - item.get_discriminant_expression ().get ()); + item.take_discriminant_expression ()); } void @@ -159,7 +159,7 @@ TypeCheckEnumItem::visit (HIR::EnumItemTuple &item) for (auto &field : item.get_tuple_fields ()) { TyTy::BaseType *field_type - = TypeCheckType::Resolve (field.get_field_type ().get ()); + = TypeCheckType::Resolve (field.get_field_type ()); TyTy::StructFieldType *ty_field = new TyTy::StructFieldType (field.get_mappings ().get_hirid (), std::to_string (idx), field_type, @@ -174,11 +174,10 @@ TypeCheckEnumItem::visit (HIR::EnumItemTuple &item) mappings.get_next_hir_id ( item.get_mappings ().get_crate_num ()), item.get_mappings ().get_local_defid ()); - HIR::LiteralExpr *discim_expr - = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant), - HIR::Literal::LitType::INT, - PrimitiveCoreType::CORETYPE_I64, item.get_locus (), - {}); + auto discim_expr = Rust::make_unique ( + HIR::LiteralExpr (mapping, std::to_string (last_discriminant), + HIR::Literal::LitType::INT, + PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {})); TyTy::BaseType *isize = nullptr; bool ok = context->lookup_builtin ("isize", &isize); @@ -208,7 +207,7 @@ TypeCheckEnumItem::visit (HIR::EnumItemTuple &item) item.get_mappings ().get_defid (), item.get_identifier ().as_string (), ident, TyTy::VariantDef::VariantType::TUPLE, - discim_expr, fields); + std::move (discim_expr), fields); } void @@ -221,7 +220,7 @@ TypeCheckEnumItem::visit (HIR::EnumItemStruct &item) for (auto &field : item.get_struct_fields ()) { TyTy::BaseType *field_type - = TypeCheckType::Resolve (field.get_field_type ().get ()); + = TypeCheckType::Resolve (field.get_field_type ()); TyTy::StructFieldType *ty_field = new TyTy::StructFieldType (field.get_mappings ().get_hirid (), field.get_field_name ().as_string (), @@ -235,11 +234,10 @@ TypeCheckEnumItem::visit (HIR::EnumItemStruct &item) mappings.get_next_hir_id ( item.get_mappings ().get_crate_num ()), item.get_mappings ().get_local_defid ()); - HIR::LiteralExpr *discrim_expr - = new HIR::LiteralExpr (mapping, std::to_string (last_discriminant), - HIR::Literal::LitType::INT, - PrimitiveCoreType::CORETYPE_I64, item.get_locus (), - {}); + auto discrim_expr = Rust::make_unique ( + HIR::LiteralExpr (mapping, std::to_string (last_discriminant), + HIR::Literal::LitType::INT, + PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {})); TyTy::BaseType *isize = nullptr; bool ok = context->lookup_builtin ("isize", &isize); @@ -269,7 +267,7 @@ TypeCheckEnumItem::visit (HIR::EnumItemStruct &item) item.get_mappings ().get_defid (), item.get_identifier ().as_string (), ident, TyTy::VariantDef::VariantType::STRUCT, - discrim_expr, fields); + std::move (discrim_expr), fields); } } // namespace Resolver diff --git a/gcc/rust/typecheck/rust-hir-type-check-enumitem.h b/gcc/rust/typecheck/rust-hir-type-check-enumitem.h index 5f68d8e9869a..baed198d4343 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-enumitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-enumitem.h @@ -28,7 +28,7 @@ namespace Resolver { class TypeCheckEnumItem : public TypeCheckBase { public: - static TyTy::VariantDef *Resolve (HIR::EnumItem *item, + static TyTy::VariantDef *Resolve (HIR::EnumItem &item, int64_t last_discriminant); protected: diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 8217e57e05a8..f9dace10c8c7 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -36,17 +36,17 @@ TypeCheckExpr::TypeCheckExpr () : TypeCheckBase (), infered (nullptr) {} // Perform type checking on expr. Also runs type unification algorithm. // Returns the unified type of expr TyTy::BaseType * -TypeCheckExpr::Resolve (HIR::Expr *expr) +TypeCheckExpr::Resolve (HIR::Expr &expr) { TypeCheckExpr resolver; - expr->accept_vis (resolver); + expr.accept_vis (resolver); if (resolver.infered == nullptr) - return new TyTy::ErrorType (expr->get_mappings ().get_hirid ()); + return new TyTy::ErrorType (expr.get_mappings ().get_hirid ()); - auto ref = expr->get_mappings ().get_hirid (); + auto ref = expr.get_mappings ().get_hirid (); resolver.infered->set_ref (ref); - resolver.context->insert_type (expr->get_mappings (), resolver.infered); + resolver.context->insert_type (expr.get_mappings (), resolver.infered); return resolver.infered; } @@ -54,10 +54,10 @@ TypeCheckExpr::Resolve (HIR::Expr *expr) void TypeCheckExpr::visit (HIR::TupleIndexExpr &expr) { - auto resolved = TypeCheckExpr::Resolve (expr.get_tuple_expr ().get ()); + auto resolved = TypeCheckExpr::Resolve (expr.get_tuple_expr ()); if (resolved->get_kind () == TyTy::TypeKind::ERROR) { - rust_error_at (expr.get_tuple_expr ()->get_locus (), + rust_error_at (expr.get_tuple_expr ().get_locus (), "failed to resolve TupleIndexExpr receiver"); return; } @@ -73,7 +73,7 @@ TypeCheckExpr::visit (HIR::TupleIndexExpr &expr) || resolved->get_kind () == TyTy::TypeKind::TUPLE; if (!is_valid_type) { - rust_error_at (expr.get_tuple_expr ()->get_locus (), + rust_error_at (expr.get_tuple_expr ().get_locus (), "Expected Tuple or ADT got: %s", resolved->as_string ().c_str ()); return; @@ -138,7 +138,7 @@ TypeCheckExpr::visit (HIR::TupleExpr &expr) std::vector fields; for (auto &elem : expr.get_tuple_elems ()) { - auto field_ty = TypeCheckExpr::Resolve (elem.get ()); + auto field_ty = TypeCheckExpr::Resolve (*elem); fields.push_back (TyTy::TyVar (field_ty->get_ref ())); } infered = new TyTy::TupleType (expr.get_mappings ().get_hirid (), @@ -158,10 +158,11 @@ TypeCheckExpr::visit (HIR::ReturnExpr &expr) auto fn_return_tyty = context->peek_return_type (); location_t expr_locus = expr.has_return_expr () - ? expr.get_expr ()->get_locus () + ? expr.get_expr ().get_locus () : expr.get_locus (); + TyTy::BaseType *expr_ty = expr.has_return_expr () - ? TypeCheckExpr::Resolve (expr.get_expr ().get ()) + ? TypeCheckExpr::Resolve (expr.get_expr ()) : TyTy::TupleType::get_unit_type (); coercion_site (expr.get_mappings ().get_hirid (), @@ -174,8 +175,7 @@ TypeCheckExpr::visit (HIR::ReturnExpr &expr) void TypeCheckExpr::visit (HIR::CallExpr &expr) { - TyTy::BaseType *function_tyty - = TypeCheckExpr::Resolve (expr.get_fnexpr ().get ()); + TyTy::BaseType *function_tyty = TypeCheckExpr::Resolve (expr.get_fnexpr ()); rust_debug_loc (expr.get_locus (), "resolved_call_expr to: {%s}", function_tyty->get_name ().c_str ()); @@ -189,7 +189,7 @@ TypeCheckExpr::visit (HIR::CallExpr &expr) // lookup variant id HirId variant_id; bool ok = context->lookup_variant_definition ( - expr.get_fnexpr ()->get_mappings ().get_hirid (), &variant_id); + expr.get_fnexpr ().get_mappings ().get_hirid (), &variant_id); if (!ok) { @@ -203,12 +203,12 @@ TypeCheckExpr::visit (HIR::CallExpr &expr) ok = adt->lookup_variant_by_id (variant_id, &lookup_variant); rust_assert (ok); - variant = *lookup_variant; + variant = std::move (*lookup_variant); } else { rust_assert (adt->number_of_variants () == 1); - variant = *adt->get_variants ().at (0); + variant = std::move (*adt->get_variants ().at (0)); } infered @@ -238,12 +238,12 @@ TypeCheckExpr::visit (HIR::AssignmentExpr &expr) { infered = TyTy::TupleType::get_unit_type (); - auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ().get ()); - auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ().get ()); + auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); + auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); coercion_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (lhs, expr.get_lhs ()->get_locus ()), - TyTy::TyWithLocation (rhs, expr.get_rhs ()->get_locus ()), + TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), + TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), expr.get_locus ()); } @@ -252,14 +252,14 @@ TypeCheckExpr::visit (HIR::CompoundAssignmentExpr &expr) { infered = TyTy::TupleType::get_unit_type (); - auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ().get ()); - auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ().get ()); + auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); + auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); // we dont care about the result of the unify from a compound assignment // since this is a unit-type expr coercion_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (lhs, expr.get_lhs ()->get_locus ()), - TyTy::TyWithLocation (rhs, expr.get_rhs ()->get_locus ()), + TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), + TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), expr.get_locus ()); auto lang_item_type @@ -292,8 +292,8 @@ TypeCheckExpr::visit (HIR::LiteralExpr &expr) void TypeCheckExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) { - auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ().get ()); - auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ().get ()); + auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); + auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); auto lang_item_type = LangItem::OperatorToLangItem (expr.get_expr_type ()); bool operator_overloaded @@ -317,8 +317,8 @@ TypeCheckExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) { case ArithmeticOrLogicalOperator::LEFT_SHIFT: case ArithmeticOrLogicalOperator::RIGHT_SHIFT: { - TyTy::TyWithLocation from (rhs, expr.get_rhs ()->get_locus ()); - TyTy::TyWithLocation to (lhs, expr.get_lhs ()->get_locus ()); + TyTy::TyWithLocation from (rhs, expr.get_rhs ().get_locus ()); + TyTy::TyWithLocation to (lhs, expr.get_lhs ().get_locus ()); infered = cast_site (expr.get_mappings ().get_hirid (), from, to, expr.get_locus ()); } @@ -327,8 +327,8 @@ TypeCheckExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) default: { infered = unify_site ( expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (lhs, expr.get_lhs ()->get_locus ()), - TyTy::TyWithLocation (rhs, expr.get_rhs ()->get_locus ()), + TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), + TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), expr.get_locus ()); } break; @@ -338,12 +338,12 @@ TypeCheckExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) void TypeCheckExpr::visit (HIR::ComparisonExpr &expr) { - auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ().get ()); - auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ().get ()); + auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); + auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); unify_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (lhs, expr.get_lhs ()->get_locus ()), - TyTy::TyWithLocation (rhs, expr.get_rhs ()->get_locus ()), + TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), + TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), expr.get_locus ()); bool ok = context->lookup_builtin ("bool", &infered); @@ -353,8 +353,8 @@ TypeCheckExpr::visit (HIR::ComparisonExpr &expr) void TypeCheckExpr::visit (HIR::LazyBooleanExpr &expr) { - auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ().get ()); - auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ().get ()); + auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ()); + auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ()); // we expect the lhs and rhs must be bools at this point TyTy::BaseType *boolean_node = nullptr; @@ -364,27 +364,27 @@ TypeCheckExpr::visit (HIR::LazyBooleanExpr &expr) // verify the lhs and rhs before unifying together lhs = unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (boolean_node, - expr.get_lhs ()->get_locus ()), - TyTy::TyWithLocation (lhs, expr.get_lhs ()->get_locus ()), + expr.get_lhs ().get_locus ()), + TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), expr.get_locus ()); rhs = unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (boolean_node, - expr.get_rhs ()->get_locus ()), - TyTy::TyWithLocation (rhs, expr.get_rhs ()->get_locus ()), + expr.get_rhs ().get_locus ()), + TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), expr.get_locus ()); infered = unify_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (lhs, expr.get_lhs ()->get_locus ()), - TyTy::TyWithLocation (rhs, expr.get_rhs ()->get_locus ()), + TyTy::TyWithLocation (lhs, expr.get_lhs ().get_locus ()), + TyTy::TyWithLocation (rhs, expr.get_rhs ().get_locus ()), expr.get_locus ()); } void TypeCheckExpr::visit (HIR::NegationExpr &expr) { - auto negated_expr_ty = TypeCheckExpr::Resolve (expr.get_expr ().get ()); + auto negated_expr_ty = TypeCheckExpr::Resolve (expr.get_expr ()); // check for operator overload auto lang_item_type @@ -449,15 +449,14 @@ TypeCheckExpr::visit (HIR::IfExpr &expr) bool ok = context->lookup_builtin ("bool", &bool_ty); rust_assert (ok); - TyTy::BaseType *cond_type - = TypeCheckExpr::Resolve (expr.get_if_condition ().get ()); + TyTy::BaseType *cond_type = TypeCheckExpr::Resolve (expr.get_if_condition ()); unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (bool_ty), TyTy::TyWithLocation (cond_type, - expr.get_if_condition ()->get_locus ()), + expr.get_if_condition ().get_locus ()), expr.get_locus ()); - TypeCheckExpr::Resolve (expr.get_if_block ().get ()); + TypeCheckExpr::Resolve (expr.get_if_block ()); infered = TyTy::TupleType::get_unit_type (); } @@ -469,17 +468,15 @@ TypeCheckExpr::visit (HIR::IfExprConseqElse &expr) bool ok = context->lookup_builtin ("bool", &bool_ty); rust_assert (ok); - TyTy::BaseType *cond_type - = TypeCheckExpr::Resolve (expr.get_if_condition ().get ()); + TyTy::BaseType *cond_type = TypeCheckExpr::Resolve (expr.get_if_condition ()); unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (bool_ty), TyTy::TyWithLocation (cond_type, - expr.get_if_condition ()->get_locus ()), + expr.get_if_condition ().get_locus ()), expr.get_locus ()); - auto if_blk_resolved = TypeCheckExpr::Resolve (expr.get_if_block ().get ()); - auto else_blk_resolved - = TypeCheckExpr::Resolve (expr.get_else_block ().get ()); + auto if_blk_resolved = TypeCheckExpr::Resolve (expr.get_if_block ()); + auto else_blk_resolved = TypeCheckExpr::Resolve (expr.get_else_block ()); if (if_blk_resolved->get_kind () == TyTy::NEVER) infered = else_blk_resolved; @@ -487,20 +484,20 @@ TypeCheckExpr::visit (HIR::IfExprConseqElse &expr) infered = if_blk_resolved; else { - infered = unify_site ( - expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (if_blk_resolved, - expr.get_if_block ()->get_locus ()), - TyTy::TyWithLocation (else_blk_resolved, - expr.get_else_block ()->get_locus ()), - expr.get_locus ()); + infered + = unify_site (expr.get_mappings ().get_hirid (), + TyTy::TyWithLocation (if_blk_resolved, + expr.get_if_block ().get_locus ()), + TyTy::TyWithLocation ( + else_blk_resolved, expr.get_else_block ().get_locus ()), + expr.get_locus ()); } } void TypeCheckExpr::visit (HIR::UnsafeBlockExpr &expr) { - infered = TypeCheckExpr::Resolve (expr.get_block_expr ().get ()); + infered = TypeCheckExpr::Resolve (expr.get_block_expr ()); } void @@ -515,7 +512,7 @@ TypeCheckExpr::visit (HIR::BlockExpr &expr) if (!s->is_item ()) continue; - TypeCheckStmt::Resolve (s.get ()); + TypeCheckStmt::Resolve (*s); } for (auto &s : expr.get_statements ()) @@ -523,7 +520,7 @@ TypeCheckExpr::visit (HIR::BlockExpr &expr) if (s->is_item ()) continue; - auto resolved = TypeCheckStmt::Resolve (s.get ()); + auto resolved = TypeCheckStmt::Resolve (*s); if (resolved == nullptr) { rust_error_at (s->get_locus (), "failure to resolve type"); @@ -541,7 +538,7 @@ TypeCheckExpr::visit (HIR::BlockExpr &expr) } if (expr.has_expr ()) - infered = TypeCheckExpr::Resolve (expr.get_final_expr ().get ())->clone (); + infered = TypeCheckExpr::Resolve (expr.get_final_expr ())->clone (); else if (expr.is_tail_reachable ()) infered = TyTy::TupleType::get_unit_type (); else if (expr.has_label ()) @@ -595,14 +592,13 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr) // resolve the range expressions and these types must unify then we use that // type to substitute into the ADT - TyTy::BaseType *from_ty - = TypeCheckExpr::Resolve (expr.get_from_expr ().get ()); - TyTy::BaseType *to_ty = TypeCheckExpr::Resolve (expr.get_to_expr ().get ()); + TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_from_expr ()); + TyTy::BaseType *to_ty = TypeCheckExpr::Resolve (expr.get_to_expr ()); TyTy::BaseType *unified = unify_site ( expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (from_ty, expr.get_from_expr ()->get_locus ()), - TyTy::TyWithLocation (to_ty, expr.get_to_expr ()->get_locus ()), + TyTy::TyWithLocation (from_ty, expr.get_from_expr ().get_locus ()), + TyTy::TyWithLocation (to_ty, expr.get_to_expr ().get_locus ()), expr.get_locus ()); // substitute it in @@ -647,8 +643,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr) // resolve the range expressions and these types must unify then we use that // type to substitute into the ADT - TyTy::BaseType *from_ty - = TypeCheckExpr::Resolve (expr.get_from_expr ().get ()); + TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_from_expr ()); // substitute it in std::vector subst_mappings; @@ -692,7 +687,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr) // resolve the range expressions and these types must unify then we use that // type to substitute into the ADT - TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_to_expr ().get ()); + TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_to_expr ()); // substitute it in std::vector subst_mappings; @@ -716,38 +711,38 @@ typecheck_inline_asm_operand (HIR::InlineAsm &expr) { case RegisterType::In: { auto in = operand.get_in (); - TypeCheckExpr::Resolve (in.expr.get ()); + TypeCheckExpr::Resolve (*in.expr); break; } case RegisterType::Out: { auto out = operand.get_out (); - TypeCheckExpr::Resolve (out.expr.get ()); + TypeCheckExpr::Resolve (*out.expr); break; } case RegisterType::InOut: { auto in_out = operand.get_in_out (); - TypeCheckExpr::Resolve (in_out.expr.get ()); + TypeCheckExpr::Resolve (*in_out.expr); break; } case RegisterType::SplitInOut: { auto split_in_out = operand.get_split_in_out (); - TypeCheckExpr::Resolve (split_in_out.in_expr.get ()); - TypeCheckExpr::Resolve (split_in_out.out_expr.get ()); + TypeCheckExpr::Resolve (*split_in_out.in_expr); + TypeCheckExpr::Resolve (*split_in_out.out_expr); break; } case RegisterType::Const: { auto anon_const = operand.get_const ().anon_const; - TypeCheckExpr::Resolve (anon_const.expr.get ()); + TypeCheckExpr::Resolve (*anon_const.expr); break; } case RegisterType::Sym: { auto sym = operand.get_sym (); - TypeCheckExpr::Resolve (sym.expr.get ()); + TypeCheckExpr::Resolve (*sym.expr); break; } case RegisterType::Label: { auto label = operand.get_label (); - TypeCheckExpr::Resolve (label.expr.get ()); + TypeCheckExpr::Resolve (*label.expr); break; } } @@ -826,13 +821,12 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr) // resolve the range expressions and these types must unify then we use that // type to substitute into the ADT - TyTy::BaseType *from_ty - = TypeCheckExpr::Resolve (expr.get_from_expr ().get ()); - TyTy::BaseType *to_ty = TypeCheckExpr::Resolve (expr.get_to_expr ().get ()); + TyTy::BaseType *from_ty = TypeCheckExpr::Resolve (expr.get_from_expr ()); + TyTy::BaseType *to_ty = TypeCheckExpr::Resolve (expr.get_to_expr ()); TyTy::BaseType *unified = unify_site ( expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (from_ty, expr.get_from_expr ()->get_locus ()), - TyTy::TyWithLocation (to_ty, expr.get_to_expr ()->get_locus ()), + TyTy::TyWithLocation (from_ty, expr.get_from_expr ().get_locus ()), + TyTy::TyWithLocation (to_ty, expr.get_to_expr ().get_locus ()), expr.get_locus ()); // substitute it in @@ -849,11 +843,11 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr) void TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr) { - auto array_expr_ty = TypeCheckExpr::Resolve (expr.get_array_expr ().get ()); + auto array_expr_ty = TypeCheckExpr::Resolve (expr.get_array_expr ()); if (array_expr_ty->get_kind () == TyTy::TypeKind::ERROR) return; - auto index_expr_ty = TypeCheckExpr::Resolve (expr.get_index_expr ().get ()); + auto index_expr_ty = TypeCheckExpr::Resolve (expr.get_index_expr ()); if (index_expr_ty->get_kind () == TyTy::TypeKind::ERROR) return; @@ -876,10 +870,10 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr) if (maybe_simple_array_access && direct_array_expr_ty->get_kind () == TyTy::TypeKind::ARRAY) { - unify_site (expr.get_index_expr ()->get_mappings ().get_hirid (), + unify_site (expr.get_index_expr ().get_mappings ().get_hirid (), TyTy::TyWithLocation (size_ty), TyTy::TyWithLocation (index_expr_ty, - expr.get_index_expr ()->get_locus ()), + expr.get_index_expr ().get_locus ()), expr.get_locus ()); TyTy::ArrayType *array_type @@ -906,8 +900,8 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr) // error[E0277]: the type `[{integer}]` cannot be indexed by `u32` rich_location r (line_table, expr.get_locus ()); - r.add_range (expr.get_array_expr ()->get_locus ()); - r.add_range (expr.get_index_expr ()->get_locus ()); + r.add_range (expr.get_array_expr ().get_locus ()); + r.add_range (expr.get_index_expr ().get_locus ()); rust_error_at (r, ErrorCode::E0277, "the type %<%s%> cannot be indexed by %<%s%>", array_expr_ty->get_name ().c_str (), @@ -917,7 +911,7 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr) void TypeCheckExpr::visit (HIR::ArrayExpr &expr) { - HIR::ArrayElems &elements = *expr.get_internal_elements (); + auto &elements = expr.get_internal_elements (); HIR::Expr *capacity_expr = nullptr; TyTy::BaseType *element_type = nullptr; @@ -926,25 +920,24 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr) case HIR::ArrayElems::ArrayExprType::COPIED: { HIR::ArrayElemsCopied &elems = static_cast (elements); - element_type - = TypeCheckExpr::Resolve (elems.get_elem_to_copy ().get ()); + element_type = TypeCheckExpr::Resolve (elems.get_elem_to_copy ()); auto capacity_type - = TypeCheckExpr::Resolve (elems.get_num_copies_expr ().get ()); + = TypeCheckExpr::Resolve (elems.get_num_copies_expr ()); TyTy::BaseType *expected_ty = nullptr; bool ok = context->lookup_builtin ("usize", &expected_ty); rust_assert (ok); - context->insert_type (elems.get_num_copies_expr ()->get_mappings (), + context->insert_type (elems.get_num_copies_expr ().get_mappings (), expected_ty); - unify_site ( - expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (expected_ty), - TyTy::TyWithLocation (capacity_type, - elems.get_num_copies_expr ()->get_locus ()), - expr.get_locus ()); + unify_site (expr.get_mappings ().get_hirid (), + TyTy::TyWithLocation (expected_ty), + TyTy::TyWithLocation ( + capacity_type, elems.get_num_copies_expr ().get_locus ()), + expr.get_locus ()); - capacity_expr = elems.get_num_copies_expr ().get (); + capacity_expr = &elems.get_num_copies_expr (); } break; @@ -955,7 +948,7 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr) std::vector types; for (auto &elem : elems.get_values ()) { - types.push_back (TypeCheckExpr::Resolve (elem.get ())); + types.push_back (TypeCheckExpr::Resolve (*elem)); } // this is a LUB @@ -999,7 +992,7 @@ void TypeCheckExpr::visit (HIR::StructExprStruct &struct_expr) { TyTy::BaseType *struct_path_ty - = TypeCheckExpr::Resolve (&struct_expr.get_struct_name ()); + = TypeCheckExpr::Resolve (struct_expr.get_struct_name ()); if (struct_path_ty->get_kind () != TyTy::TypeKind::ADT) { rust_error_at (struct_expr.get_struct_name ().get_locus (), @@ -1031,19 +1024,19 @@ TypeCheckExpr::visit (HIR::StructExprStruct &struct_expr) void TypeCheckExpr::visit (HIR::StructExprStructFields &struct_expr) { - infered = TypeCheckStructExpr::Resolve (&struct_expr); + infered = TypeCheckStructExpr::Resolve (struct_expr); } void TypeCheckExpr::visit (HIR::GroupedExpr &expr) { - infered = TypeCheckExpr::Resolve (expr.get_expr_in_parens ().get ()); + infered = TypeCheckExpr::Resolve (expr.get_expr_in_parens ()); } void TypeCheckExpr::visit (HIR::FieldAccessExpr &expr) { - auto struct_base = TypeCheckExpr::Resolve (expr.get_receiver_expr ().get ()); + auto struct_base = TypeCheckExpr::Resolve (expr.get_receiver_expr ()); // FIXME does this require autoderef here? if (struct_base->get_kind () == TyTy::TypeKind::REF) @@ -1085,10 +1078,10 @@ TypeCheckExpr::visit (HIR::FieldAccessExpr &expr) void TypeCheckExpr::visit (HIR::MethodCallExpr &expr) { - auto receiver_tyty = TypeCheckExpr::Resolve (expr.get_receiver ().get ()); + auto receiver_tyty = TypeCheckExpr::Resolve (expr.get_receiver ()); if (receiver_tyty->get_kind () == TyTy::TypeKind::ERROR) { - rust_error_at (expr.get_receiver ()->get_locus (), + rust_error_at (expr.get_receiver ().get_locus (), "failed to resolve receiver in MethodCallExpr"); return; } @@ -1147,7 +1140,7 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr) // stored onto the receiver to so as we don't trigger duplicate deref mappings // ICE when an argument is a method call HirId autoderef_mappings_id - = expr.get_receiver ()->get_mappings ().get_hirid (); + = expr.get_receiver ().get_mappings ().get_hirid (); context->insert_autoderef_mappings (autoderef_mappings_id, std::move (candidate.adjustments)); @@ -1195,7 +1188,7 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr) if (impl_self_infer->get_kind () == TyTy::TypeKind::ERROR) { rich_location r (line_table, expr.get_locus ()); - r.add_range (impl.get_type ()->get_locus ()); + r.add_range (impl.get_type ().get_locus ()); rust_error_at ( r, "failed to resolve impl type for method call resolution"); return; @@ -1267,11 +1260,10 @@ TypeCheckExpr::visit (HIR::LoopExpr &expr) { context->push_new_loop_context (expr.get_mappings ().get_hirid (), expr.get_locus ()); - TyTy::BaseType *block_expr - = TypeCheckExpr::Resolve (expr.get_loop_block ().get ()); + TyTy::BaseType *block_expr = TypeCheckExpr::Resolve (expr.get_loop_block ()); if (!block_expr->is_unit ()) { - rust_error_at (expr.get_loop_block ()->get_locus (), + rust_error_at (expr.get_loop_block ().get_locus (), "expected %<()%> got %s", block_expr->as_string ().c_str ()); return; @@ -1294,13 +1286,12 @@ TypeCheckExpr::visit (HIR::WhileLoopExpr &expr) { context->push_new_while_loop_context (expr.get_mappings ().get_hirid ()); - TypeCheckExpr::Resolve (expr.get_predicate_expr ().get ()); - TyTy::BaseType *block_expr - = TypeCheckExpr::Resolve (expr.get_loop_block ().get ()); + TypeCheckExpr::Resolve (expr.get_predicate_expr ()); + TyTy::BaseType *block_expr = TypeCheckExpr::Resolve (expr.get_loop_block ()); if (!block_expr->is_unit ()) { - rust_error_at (expr.get_loop_block ()->get_locus (), + rust_error_at (expr.get_loop_block ().get_locus (), "expected %<()%> got %s", block_expr->as_string ().c_str ()); return; @@ -1323,7 +1314,7 @@ TypeCheckExpr::visit (HIR::BreakExpr &expr) if (expr.has_break_expr ()) { TyTy::BaseType *break_expr_tyty - = TypeCheckExpr::Resolve (expr.get_expr ().get ()); + = TypeCheckExpr::Resolve (expr.get_expr ()); TyTy::BaseType *loop_context = context->peek_loop_context (); if (loop_context->get_kind () == TyTy::TypeKind::ERROR) @@ -1338,7 +1329,7 @@ TypeCheckExpr::visit (HIR::BreakExpr &expr) = unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (loop_context), TyTy::TyWithLocation (break_expr_tyty, - expr.get_expr ()->get_locus ()), + expr.get_expr ().get_locus ()), expr.get_locus ()); context->swap_head_loop_context (unified_ty); } @@ -1362,8 +1353,7 @@ TypeCheckExpr::visit (HIR::ContinueExpr &expr) void TypeCheckExpr::visit (HIR::BorrowExpr &expr) { - TyTy::BaseType *resolved_base - = TypeCheckExpr::Resolve (expr.get_expr ().get ()); + TyTy::BaseType *resolved_base = TypeCheckExpr::Resolve (expr.get_expr ()); // In Rust this is valid because of DST's // @@ -1393,8 +1383,7 @@ TypeCheckExpr::visit (HIR::BorrowExpr &expr) void TypeCheckExpr::visit (HIR::DereferenceExpr &expr) { - TyTy::BaseType *resolved_base - = TypeCheckExpr::Resolve (expr.get_expr ().get ()); + TyTy::BaseType *resolved_base = TypeCheckExpr::Resolve (expr.get_expr ()); rust_debug_loc (expr.get_locus (), "attempting deref operator overload"); auto lang_item_type = LangItem::Kind::DEREF; @@ -1435,14 +1424,14 @@ void TypeCheckExpr::visit (HIR::TypeCastExpr &expr) { TyTy::BaseType *expr_to_convert - = TypeCheckExpr::Resolve (expr.get_casted_expr ().get ()); + = TypeCheckExpr::Resolve (expr.get_casted_expr ()); TyTy::BaseType *tyty_to_convert_to - = TypeCheckType::Resolve (expr.get_type_to_convert_to ().get ()); + = TypeCheckType::Resolve (expr.get_type_to_convert_to ()); TyTy::TyWithLocation from (expr_to_convert, - expr.get_casted_expr ()->get_locus ()); + expr.get_casted_expr ().get_locus ()); TyTy::TyWithLocation to (tyty_to_convert_to, - expr.get_type_to_convert_to ()->get_locus ()); + expr.get_type_to_convert_to ().get_locus ()); infered = cast_site (expr.get_mappings ().get_hirid (), from, to, expr.get_locus ()); } @@ -1453,7 +1442,7 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr) // this needs to perform a least upper bound coercion on the blocks and then // unify the scruintee and arms TyTy::BaseType *scrutinee_tyty - = TypeCheckExpr::Resolve (expr.get_scrutinee_expr ().get ()); + = TypeCheckExpr::Resolve (expr.get_scrutinee_expr ()); bool saw_error = false; std::vector kase_block_tys; @@ -1464,7 +1453,7 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr) for (auto &pattern : kase_arm.get_patterns ()) { TyTy::BaseType *kase_arm_ty - = TypeCheckPattern::Resolve (pattern.get (), scrutinee_tyty); + = TypeCheckPattern::Resolve (*pattern, scrutinee_tyty); if (kase_arm_ty->get_kind () == TyTy ::TypeKind::ERROR) { saw_error = true; @@ -1474,7 +1463,7 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr) TyTy::BaseType *checked_kase = unify_site ( expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (scrutinee_tyty, - expr.get_scrutinee_expr ()->get_locus ()), + expr.get_scrutinee_expr ().get_locus ()), TyTy::TyWithLocation (kase_arm_ty, pattern->get_locus ()), expr.get_locus ()); if (checked_kase->get_kind () == TyTy::TypeKind::ERROR) @@ -1485,8 +1474,7 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr) } // check the kase type - TyTy::BaseType *kase_block_ty - = TypeCheckExpr::Resolve (kase.get_expr ().get ()); + TyTy::BaseType *kase_block_ty = TypeCheckExpr::Resolve (kase.get_expr ()); kase_block_tys.push_back (kase_block_ty); } if (saw_error) @@ -1529,17 +1517,17 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr) TyTy::BaseType *param_tyty = nullptr; if (p.has_type_given ()) { - param_tyty = TypeCheckType::Resolve (p.get_type ().get ()); + param_tyty = TypeCheckType::Resolve (p.get_type ()); } else { - param_tyty = ClosureParamInfer::Resolve (p.get_pattern ().get ()); + param_tyty = ClosureParamInfer::Resolve (p.get_pattern ()); } TyTy::TyVar param_ty (param_tyty->get_ref ()); parameter_types.push_back (param_ty); - TypeCheckPattern::Resolve (p.get_pattern ().get (), param_ty.get_tyty ()); + TypeCheckPattern::Resolve (p.get_pattern (), param_ty.get_tyty ()); } // we generate an implicit hirid for the closure args @@ -1550,18 +1538,17 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr) context->insert_implicit_type (closure_args); location_t result_type_locus = expr.has_return_type () - ? expr.get_return_type ()->get_locus () + ? expr.get_return_type ().get_locus () : expr.get_locus (); TyTy::TyVar result_type = expr.has_return_type () ? TyTy::TyVar ( - TypeCheckType::Resolve (expr.get_return_type ().get ())->get_ref ()) + TypeCheckType::Resolve (expr.get_return_type ())->get_ref ()) : TyTy::TyVar::get_implicit_infer_var (expr.get_locus ()); // resolve the block - location_t closure_expr_locus = expr.get_expr ()->get_locus (); - TyTy::BaseType *closure_expr_ty - = TypeCheckExpr::Resolve (expr.get_expr ().get ()); + location_t closure_expr_locus = expr.get_expr ().get_locus (); + TyTy::BaseType *closure_expr_ty = TypeCheckExpr::Resolve (expr.get_expr ()); coercion_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (result_type.get_tyty (), result_type_locus), @@ -1764,7 +1751,7 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, if (parent->has_trait_ref () && is_recursive_op) { TraitReference *trait_reference - = TraitResolver::Lookup (*parent->get_trait_ref ().get ()); + = TraitResolver::Lookup (parent->get_trait_ref ()); if (!trait_reference->is_error ()) { TyTy::BaseType *lookup = nullptr; @@ -1804,7 +1791,7 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, // type check the arguments if required TyTy::FnType *type = static_cast (lookup); rust_assert (type->num_params () > 0); - auto fnparam = type->param_at (0); + auto &fnparam = type->param_at (0); // typecheck the self unify_site (expr.get_mappings ().get_hirid (), @@ -1817,7 +1804,7 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, else { rust_assert (type->num_params () == 2); - auto fnparam = type->param_at (1); + auto &fnparam = type->param_at (1); unify_site (expr.get_mappings ().get_hirid (), TyTy::TyWithLocation (fnparam.second), TyTy::TyWithLocation (rhs), expr.get_locus ()); @@ -1933,8 +1920,8 @@ TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr, // store the adjustments for code-generation to know what to do which must be // stored onto the receiver to so as we don't trigger duplicate deref mappings // ICE when an argument is a method call - HIR::Expr *fnexpr = expr.get_fnexpr ().get (); - HirId autoderef_mappings_id = fnexpr->get_mappings ().get_hirid (); + HIR::Expr &fnexpr = expr.get_fnexpr (); + HirId autoderef_mappings_id = fnexpr.get_mappings ().get_hirid (); context->insert_autoderef_mappings (autoderef_mappings_id, std::move (candidate.adjustments)); context->insert_receiver (expr.get_mappings ().get_hirid (), receiver_tyty); @@ -1971,7 +1958,7 @@ TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr, std::vector call_args; for (auto &arg : expr.get_arguments ()) { - TyTy::BaseType *a = TypeCheckExpr::Resolve (arg.get ()); + TyTy::BaseType *a = TypeCheckExpr::Resolve (*arg); call_args.push_back (TyTy::TyVar (a->get_ref ())); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index e8137306b7c5..51fdd934da5d 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -29,7 +29,7 @@ namespace Resolver { class TypeCheckExpr : private TypeCheckBase, private HIR::HIRExpressionVisitor { public: - static TyTy::BaseType *Resolve (HIR::Expr *expr); + static TyTy::BaseType *Resolve (HIR::Expr &expr); void visit (HIR::TupleIndexExpr &expr) override; void visit (HIR::TupleExpr &expr) override; diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index d2895ad7a146..79afbd528da2 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -18,6 +18,8 @@ #include "rust-hir-type-check-implitem.h" #include "rust-diagnostics.h" +#include "rust-hir-full-decls.h" +#include "rust-hir-pattern.h" #include "rust-hir-type-check-base.h" #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-expr.h" @@ -38,27 +40,26 @@ TypeCheckTopLevelExternItem::TypeCheckTopLevelExternItem ( {} TyTy::BaseType * -TypeCheckTopLevelExternItem::Resolve (HIR::ExternalItem *item, +TypeCheckTopLevelExternItem::Resolve (HIR::ExternalItem &item, const HIR::ExternBlock &parent) { // is it already resolved? auto context = TypeCheckContext::get (); TyTy::BaseType *resolved = nullptr; bool already_resolved - = context->lookup_type (item->get_mappings ().get_hirid (), &resolved); + = context->lookup_type (item.get_mappings ().get_hirid (), &resolved); if (already_resolved) return resolved; TypeCheckTopLevelExternItem resolver (parent); - item->accept_vis (resolver); + item.accept_vis (resolver); return resolver.resolved; } void TypeCheckTopLevelExternItem::visit (HIR::ExternalStaticItem &item) { - TyTy::BaseType *actual_type - = TypeCheckType::Resolve (item.get_item_type ().get ()); + TyTy::BaseType *actual_type = TypeCheckType::Resolve (item.get_item_type ()); context->insert_type (item.get_mappings (), actual_type); resolved = actual_type; @@ -89,7 +90,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) case HIR::GenericParam::GenericKind::TYPE: { auto param_type - = TypeResolveGenericParam::Resolve (generic_param.get ()); + = TypeResolveGenericParam::Resolve (*generic_param); context->insert_type (generic_param->get_mappings (), param_type); @@ -106,7 +107,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) { for (auto &where_clause_item : function.get_where_clause ().get_items ()) { - ResolveWhereClauseItem::Resolve (*where_clause_item.get (), + ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints); } } @@ -116,8 +117,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) ret_type = TyTy::TupleType::get_unit_type (); else { - auto resolved - = TypeCheckType::Resolve (function.get_return_type ().get ()); + auto resolved = TypeCheckType::Resolve (function.get_return_type ()); if (resolved == nullptr) { rust_error_at (function.get_locus (), @@ -127,14 +127,15 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) ret_type = resolved->clone (); ret_type->set_ref ( - function.get_return_type ()->get_mappings ().get_hirid ()); + function.get_return_type ().get_mappings ().get_hirid ()); } std::vector > params; + std::unique_ptr param_pattern = nullptr; for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on - auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ()); + auto param_tyty = TypeCheckType::Resolve (param.get_type ()); // these are implicit mappings and not used auto crate_num = mappings.get_current_crate (); @@ -142,13 +143,13 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - HIR::IdentifierPattern *param_pattern - = new HIR::IdentifierPattern (mapping, param.get_param_name (), - UNDEF_LOCATION, false, Mutability::Imm, - std::unique_ptr (nullptr)); + param_pattern = Rust::make_unique ( + HIR::IdentifierPattern (mapping, param.get_param_name (), + UNDEF_LOCATION, false, Mutability::Imm, + std::unique_ptr (nullptr))); params.push_back ( - std::pair (param_pattern, + std::pair (param_pattern.get (), param_tyty)); context->insert_type (param.get_mappings (), param_tyty); @@ -316,7 +317,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalTypeItem &type) } TypeCheckImplItem::TypeCheckImplItem ( - HIR::ImplBlock *parent, TyTy::BaseType *self, + HIR::ImplBlock &parent, TyTy::BaseType *self, std::vector substitutions) : TypeCheckBase (), parent (parent), self (self), substitutions (substitutions) @@ -324,20 +325,20 @@ TypeCheckImplItem::TypeCheckImplItem ( TyTy::BaseType * TypeCheckImplItem::Resolve ( - HIR::ImplBlock *parent, HIR::ImplItem *item, TyTy::BaseType *self, + HIR::ImplBlock &parent, HIR::ImplItem &item, TyTy::BaseType *self, std::vector substitutions) { // is it already resolved? auto context = TypeCheckContext::get (); TyTy::BaseType *resolved = nullptr; bool already_resolved - = context->lookup_type (item->get_impl_mappings ().get_hirid (), &resolved); + = context->lookup_type (item.get_impl_mappings ().get_hirid (), &resolved); if (already_resolved) return resolved; // resolve TypeCheckImplItem resolver (parent, self, substitutions); - item->accept_vis (resolver); + item.accept_vis (resolver); return resolver.result; } @@ -361,8 +362,7 @@ TypeCheckImplItem::visit (HIR::Function &function) ret_type = TyTy::TupleType::get_unit_type (); else { - auto resolved - = TypeCheckType::Resolve (function.get_return_type ().get ()); + auto resolved = TypeCheckType::Resolve (function.get_return_type ()); if (resolved == nullptr) { rust_error_at (function.get_locus (), @@ -372,10 +372,11 @@ TypeCheckImplItem::visit (HIR::Function &function) ret_type = resolved->clone (); ret_type->set_ref ( - function.get_return_type ()->get_mappings ().get_hirid ()); + function.get_return_type ().get_mappings ().get_hirid ()); } std::vector > params; + std::unique_ptr self_pattern = nullptr; if (function.is_method ()) { // these are implicit mappings and not used @@ -389,16 +390,17 @@ TypeCheckImplItem::visit (HIR::Function &function) // reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self_param (); // FIXME: which location should be used for Rust::Identifier for `self`? - HIR::IdentifierPattern *self_pattern = new HIR::IdentifierPattern ( - mapping, {"self"}, self_param.get_locus (), self_param.is_ref (), - self_param.get_mut (), std::unique_ptr (nullptr)); + self_pattern = Rust::make_unique ( + HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (), + self_param.is_ref (), self_param.get_mut (), + std::unique_ptr (nullptr))); // might have a specified type TyTy::BaseType *self_type = nullptr; if (self_param.has_type ()) { - std::unique_ptr &specified_type = self_param.get_type (); - self_type = TypeCheckType::Resolve (specified_type.get ()); + auto &specified_type = self_param.get_type (); + self_type = TypeCheckType::Resolve (specified_type); } else { @@ -449,18 +451,21 @@ TypeCheckImplItem::visit (HIR::Function &function) context->insert_type (self_param.get_mappings (), self_type); params.push_back ( - std::pair (self_pattern, self_type)); + std::pair (self_pattern.get (), + self_type)); } for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on - auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ()); - params.push_back (std::pair ( - param.get_param_name ().get (), param_tyty)); + auto param_tyty = TypeCheckType::Resolve (param.get_type ()); context->insert_type (param.get_mappings (), param_tyty); - TypeCheckPattern::Resolve (param.get_param_name ().get (), param_tyty); + TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); + + params.push_back ( + std::pair (¶m.get_param_name (), + param_tyty)); } tl::optional canonical_path; @@ -502,17 +507,16 @@ TypeCheckImplItem::visit (HIR::Function &function) context->push_return_type (TypeCheckContextItem (parent, &function), expected_ret_tyty); - auto block_expr_ty - = TypeCheckExpr::Resolve (function.get_definition ().get ()); + auto block_expr_ty = TypeCheckExpr::Resolve (function.get_definition ()); location_t fn_return_locus = function.has_function_return_type () - ? function.get_return_type ()->get_locus () + ? function.get_return_type ().get_locus () : function.get_locus (); - coercion_site (function.get_definition ()->get_mappings ().get_hirid (), + coercion_site (function.get_definition ().get_mappings ().get_hirid (), TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus), TyTy::TyWithLocation (block_expr_ty), - function.get_definition ()->get_locus ()); + function.get_definition ().get_locus ()); context->pop_return_type (); } @@ -520,14 +524,13 @@ TypeCheckImplItem::visit (HIR::Function &function) void TypeCheckImplItem::visit (HIR::ConstantItem &constant) { - TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ().get ()); - TyTy::BaseType *expr_type - = TypeCheckExpr::Resolve (constant.get_expr ().get ()); + TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ()); + TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ()); TyTy::BaseType *unified = unify_site ( constant.get_mappings ().get_hirid (), - TyTy::TyWithLocation (type, constant.get_type ()->get_locus ()), - TyTy::TyWithLocation (expr_type, constant.get_expr ()->get_locus ()), + TyTy::TyWithLocation (type, constant.get_type ().get_locus ()), + TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()), constant.get_locus ()); context->insert_type (constant.get_mappings (), unified); result = unified; @@ -542,7 +545,7 @@ TypeCheckImplItem::visit (HIR::TypeAlias &alias) resolve_generic_params (alias.get_generic_params (), substitutions); TyTy::BaseType *actual_type - = TypeCheckType::Resolve (alias.get_type_aliased ().get ()); + = TypeCheckType::Resolve (alias.get_type_aliased ()); context->insert_type (alias.get_mappings (), actual_type); result = actual_type; @@ -555,7 +558,7 @@ TypeCheckImplItem::visit (HIR::TypeAlias &alias) } TypeCheckImplItemWithTrait::TypeCheckImplItemWithTrait ( - HIR::ImplBlock *parent, TyTy::BaseType *self, + HIR::ImplBlock &parent, TyTy::BaseType *self, TyTy::TypeBoundPredicate &trait_reference, std::vector substitutions) : TypeCheckBase (), trait_reference (trait_reference), @@ -567,13 +570,13 @@ TypeCheckImplItemWithTrait::TypeCheckImplItemWithTrait ( TyTy::TypeBoundPredicateItem TypeCheckImplItemWithTrait::Resolve ( - HIR::ImplBlock *parent, HIR::ImplItem *item, TyTy::BaseType *self, + HIR::ImplBlock &parent, HIR::ImplItem &item, TyTy::BaseType *self, TyTy::TypeBoundPredicate &trait_reference, std::vector substitutions) { TypeCheckImplItemWithTrait resolver (parent, self, trait_reference, substitutions); - item->accept_vis (resolver); + item.accept_vis (resolver); return resolver.resolved_trait_item; } @@ -582,7 +585,7 @@ TypeCheckImplItemWithTrait::visit (HIR::ConstantItem &constant) { // normal resolution of the item TyTy::BaseType *lookup - = TypeCheckImplItem::Resolve (parent, &constant, self, substitutions); + = TypeCheckImplItem::Resolve (parent, constant, self, substitutions); // map the impl item to the associated trait item const auto tref = trait_reference.get (); @@ -635,7 +638,7 @@ TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type) { // normal resolution of the item TyTy::BaseType *lookup - = TypeCheckImplItem::Resolve (parent, &type, self, substitutions); + = TypeCheckImplItem::Resolve (parent, type, self, substitutions); // map the impl item to the associated trait item const auto tref = trait_reference.get (); @@ -696,7 +699,7 @@ TypeCheckImplItemWithTrait::visit (HIR::Function &function) { // normal resolution of the item TyTy::BaseType *lookup - = TypeCheckImplItem::Resolve (parent, &function, self, substitutions); + = TypeCheckImplItem::Resolve (parent, function, self, substitutions); // map the impl item to the associated trait item const auto tref = trait_reference.get (); diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.h b/gcc/rust/typecheck/rust-hir-type-check-implitem.h index 64eb208d15d2..872d9d04b841 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.h @@ -29,7 +29,7 @@ class TypeCheckTopLevelExternItem : public TypeCheckBase, public HIR::HIRExternalItemVisitor { public: - static TyTy::BaseType *Resolve (HIR::ExternalItem *item, + static TyTy::BaseType *Resolve (HIR::ExternalItem &item, const HIR::ExternBlock &parent); void visit (HIR::ExternalStaticItem &item) override; @@ -47,7 +47,7 @@ class TypeCheckImplItem : public TypeCheckBase, public HIR::HIRImplVisitor { public: static TyTy::BaseType * - Resolve (HIR::ImplBlock *parent, HIR::ImplItem *item, TyTy::BaseType *self, + Resolve (HIR::ImplBlock &parent, HIR::ImplItem &item, TyTy::BaseType *self, std::vector substitutions); void visit (HIR::Function &function) override; @@ -55,10 +55,10 @@ class TypeCheckImplItem : public TypeCheckBase, public HIR::HIRImplVisitor void visit (HIR::TypeAlias &type_alias) override; protected: - TypeCheckImplItem (HIR::ImplBlock *parent, TyTy::BaseType *self, + TypeCheckImplItem (HIR::ImplBlock &parent, TyTy::BaseType *self, std::vector substitutions); - HIR::ImplBlock *parent; + HIR::ImplBlock &parent; TyTy::BaseType *self; std::vector substitutions; @@ -70,7 +70,7 @@ class TypeCheckImplItemWithTrait : public TypeCheckBase, { public: static TyTy::TypeBoundPredicateItem - Resolve (HIR::ImplBlock *parent, HIR::ImplItem *item, TyTy::BaseType *self, + Resolve (HIR::ImplBlock &parent, HIR::ImplItem &item, TyTy::BaseType *self, TyTy::TypeBoundPredicate &trait_reference, std::vector substitutions); @@ -86,7 +86,7 @@ class TypeCheckImplItemWithTrait : public TypeCheckBase, private: TypeCheckImplItemWithTrait ( - HIR::ImplBlock *parent, TyTy::BaseType *self, + HIR::ImplBlock &parent, TyTy::BaseType *self, TyTy::TypeBoundPredicate &trait_reference, std::vector substitutions); @@ -95,7 +95,7 @@ class TypeCheckImplItemWithTrait : public TypeCheckBase, TyTy::TypeBoundPredicate &trait_reference; TyTy::TypeBoundPredicateItem resolved_trait_item; - HIR::ImplBlock *parent; + HIR::ImplBlock &parent; TyTy::BaseType *self; std::vector substitutions; TyTy::RegionConstraints region_constraints; diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index ce8e7595c6d7..2a5922c10d7f 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -150,7 +150,7 @@ void TypeCheckItem::visit (HIR::TypeAlias &alias) { TyTy::BaseType *actual_type - = TypeCheckType::Resolve (alias.get_type_aliased ().get ()); + = TypeCheckType::Resolve (alias.get_type_aliased ()); context->insert_type (alias.get_mappings (), actual_type); @@ -182,7 +182,7 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl) for (auto &field : struct_decl.get_fields ()) { TyTy::BaseType *field_type - = TypeCheckType::Resolve (field.get_field_type ().get ()); + = TypeCheckType::Resolve (field.get_field_type ()); auto *ty_field = new TyTy::StructFieldType (field.get_mappings ().get_hirid (), std::to_string (idx), field_type, @@ -265,7 +265,7 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl) for (auto &field : struct_decl.get_fields ()) { TyTy::BaseType *field_type - = TypeCheckType::Resolve (field.get_field_type ().get ()); + = TypeCheckType::Resolve (field.get_field_type ()); auto *ty_field = new TyTy::StructFieldType (field.get_mappings ().get_hirid (), field.get_field_name ().as_string (), @@ -341,7 +341,7 @@ TypeCheckItem::visit (HIR::Enum &enum_decl) for (auto &variant : enum_decl.get_variants ()) { TyTy::VariantDef *field_type - = TypeCheckEnumItem::Resolve (variant.get (), discriminant_value); + = TypeCheckEnumItem::Resolve (*variant, discriminant_value); discriminant_value++; variants.push_back (field_type); @@ -400,7 +400,7 @@ TypeCheckItem::visit (HIR::Union &union_decl) for (auto &variant : union_decl.get_variants ()) { TyTy::BaseType *variant_type - = TypeCheckType::Resolve (variant.get_field_type ().get ()); + = TypeCheckType::Resolve (variant.get_field_type ()); auto *ty_variant = new TyTy::StructFieldType (variant.get_mappings ().get_hirid (), variant.get_field_name ().as_string (), @@ -456,14 +456,14 @@ TypeCheckItem::visit (HIR::Union &union_decl) void TypeCheckItem::visit (HIR::StaticItem &var) { - TyTy::BaseType *type = TypeCheckType::Resolve (var.get_type ().get ()); - TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (var.get_expr ().get ()); + TyTy::BaseType *type = TypeCheckType::Resolve (var.get_type ()); + TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (var.get_expr ()); TyTy::BaseType *unified = coercion_site (var.get_mappings ().get_hirid (), - TyTy::TyWithLocation (type, var.get_type ()->get_locus ()), + TyTy::TyWithLocation (type, var.get_type ().get_locus ()), TyTy::TyWithLocation (expr_type, - var.get_expr ()->get_locus ()), + var.get_expr ().get_locus ()), var.get_locus ()); context->insert_type (var.get_mappings (), unified); infered = unified; @@ -472,14 +472,13 @@ TypeCheckItem::visit (HIR::StaticItem &var) void TypeCheckItem::visit (HIR::ConstantItem &constant) { - TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ().get ()); - TyTy::BaseType *expr_type - = TypeCheckExpr::Resolve (constant.get_expr ().get ()); + TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ()); + TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ()); TyTy::BaseType *unified = unify_site ( constant.get_mappings ().get_hirid (), - TyTy::TyWithLocation (type, constant.get_type ()->get_locus ()), - TyTy::TyWithLocation (expr_type, constant.get_expr ()->get_locus ()), + TyTy::TyWithLocation (type, constant.get_type ().get_locus ()), + TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()), constant.get_locus ()); context->insert_type (constant.get_mappings (), unified); infered = unified; @@ -493,8 +492,8 @@ TypeCheckItem::visit (HIR::ImplBlock &impl_block) TraitReference *trait_reference = &TraitReference::error_node (); if (impl_block.has_trait_ref ()) { - std::unique_ptr &ref = impl_block.get_trait_ref (); - trait_reference = TraitResolver::Resolve (*ref); + HIR::TypePath &ref = impl_block.get_trait_ref (); + trait_reference = TraitResolver::Resolve (ref); if (trait_reference->is_error ()) return; } @@ -515,8 +514,7 @@ TypeCheckItem::visit (HIR::ImplBlock &impl_block) // resolve each impl_item for (auto &impl_item : impl_block.get_impl_items ()) { - TypeCheckImplItem::Resolve (&impl_block, impl_item.get (), self, - substitutions); + TypeCheckImplItem::Resolve (impl_block, *impl_item, self, substitutions); } // validate the impl items @@ -540,7 +538,7 @@ TypeCheckItem::resolve_impl_item (HIR::ImplBlock &impl_block, TyTy::BaseType *self = resolve_impl_block_self (impl_block); - return TypeCheckImplItem::Resolve (&impl_block, &item, self, substitutions); + return TypeCheckImplItem::Resolve (impl_block, item, self, substitutions); } void @@ -563,8 +561,7 @@ TypeCheckItem::visit (HIR::Function &function) ret_type = TyTy::TupleType::get_unit_type (); else { - auto resolved - = TypeCheckType::Resolve (function.get_return_type ().get ()); + auto resolved = TypeCheckType::Resolve (function.get_return_type ()); if (resolved->get_kind () == TyTy::TypeKind::ERROR) { rust_error_at (function.get_locus (), @@ -574,18 +571,18 @@ TypeCheckItem::visit (HIR::Function &function) ret_type = resolved->clone (); ret_type->set_ref ( - function.get_return_type ()->get_mappings ().get_hirid ()); + function.get_return_type ().get_mappings ().get_hirid ()); } std::vector> params; for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on - auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ()); - params.emplace_back (param.get_param_name ().get (), param_tyty); + auto param_tyty = TypeCheckType::Resolve (param.get_type ()); + params.emplace_back (¶m.get_param_name (), param_tyty); context->insert_type (param.get_mappings (), param_tyty); - TypeCheckPattern::Resolve (param.get_param_name ().get (), param_tyty); + TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); } auto path = CanonicalPath::create_empty (); @@ -628,16 +625,15 @@ TypeCheckItem::visit (HIR::Function &function) expected_ret_tyty); context->switch_to_fn_body (); - auto block_expr_ty - = TypeCheckExpr::Resolve (function.get_definition ().get ()); + auto block_expr_ty = TypeCheckExpr::Resolve (function.get_definition ()); location_t fn_return_locus = function.has_function_return_type () - ? function.get_return_type ()->get_locus () + ? function.get_return_type ().get_locus () : function.get_locus (); - coercion_site (function.get_definition ()->get_mappings ().get_hirid (), + coercion_site (function.get_definition ().get_mappings ().get_hirid (), TyTy::TyWithLocation (expected_ret_tyty, fn_return_locus), TyTy::TyWithLocation (block_expr_ty), - function.get_definition ()->get_locus ()); + function.get_definition ().get_locus ()); context->pop_return_type (); @@ -691,7 +687,7 @@ TypeCheckItem::visit (HIR::ExternBlock &extern_block) { for (auto &item : extern_block.get_extern_items ()) { - TypeCheckTopLevelExternItem::Resolve (item.get (), extern_block); + TypeCheckTopLevelExternItem::Resolve (*item, extern_block); } } @@ -713,17 +709,17 @@ TypeCheckItem::resolve_impl_block_substitutions (HIR::ImplBlock &impl_block, TraitReference *trait_reference = &TraitReference::error_node (); if (impl_block.has_trait_ref ()) { - std::unique_ptr &ref = impl_block.get_trait_ref (); - trait_reference = TraitResolver::Resolve (*ref); + auto &ref = impl_block.get_trait_ref (); + trait_reference = TraitResolver::Resolve (ref); rust_assert (!trait_reference->is_error ()); // we don't error out here see: gcc/testsuite/rust/compile/traits2.rs // for example - specified_bound - = get_predicate_from_bound (*ref, impl_block.get_type ().get ()); + specified_bound = get_predicate_from_bound (ref, impl_block.get_type ()); } - TyTy::BaseType *self = TypeCheckType::Resolve (impl_block.get_type ().get ()); + TyTy::BaseType *self = TypeCheckType::Resolve (impl_block.get_type ()); + if (self->is ()) { // we cannot check for unconstrained type arguments when the Self type is @@ -758,14 +754,14 @@ TypeCheckItem::validate_trait_impl_block ( auto specified_bound = TyTy::TypeBoundPredicate::error (); if (impl_block.has_trait_ref ()) { - std::unique_ptr &ref = impl_block.get_trait_ref (); + auto &ref = impl_block.get_trait_ref (); + trait_reference = TraitResolver::Resolve (ref); if (trait_reference->is_error ()) return; // we don't error out here see: gcc/testsuite/rust/compile/traits2.rs // for example - specified_bound - = get_predicate_from_bound (*ref, impl_block.get_type ().get ()); + specified_bound = get_predicate_from_bound (ref, impl_block.get_type ()); } bool is_trait_impl_block = !trait_reference->is_error (); @@ -775,8 +771,7 @@ TypeCheckItem::validate_trait_impl_block ( if (!specified_bound.is_error ()) { auto trait_item_ref - = TypeCheckImplItemWithTrait::Resolve (&impl_block, - impl_item.get (), self, + = TypeCheckImplItemWithTrait::Resolve (impl_block, *impl_item, self, specified_bound, substitutions); if (!trait_item_ref.is_error ()) @@ -849,7 +844,7 @@ TypeCheckItem::validate_trait_impl_block ( TyTy::BaseType * TypeCheckItem::resolve_impl_block_self (HIR::ImplBlock &impl_block) { - return TypeCheckType::Resolve (impl_block.get_type ().get ()); + return TypeCheckType::Resolve (impl_block.get_type ()); } } // namespace Resolver diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc index 617671c19c57..120852d159a8 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-path.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc @@ -34,8 +34,7 @@ void TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr) { HIR::QualifiedPathType qual_path_type = expr.get_path_type (); - TyTy::BaseType *root - = TypeCheckType::Resolve (qual_path_type.get_type ().get ()); + TyTy::BaseType *root = TypeCheckType::Resolve (qual_path_type.get_type ()); if (root->get_kind () == TyTy::TypeKind::ERROR) return; @@ -48,8 +47,8 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr) } // Resolve the trait now - std::unique_ptr &trait_path_ref = qual_path_type.get_trait (); - TraitReference *trait_ref = TraitResolver::Resolve (*trait_path_ref.get ()); + HIR::TypePath &trait_path_ref = qual_path_type.get_trait (); + TraitReference *trait_ref = TraitResolver::Resolve (trait_path_ref); if (trait_ref->is_error ()) return; @@ -64,8 +63,7 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr) // get the predicate for the bound auto specified_bound - = get_predicate_from_bound (*trait_path_ref.get (), - qual_path_type.get_type ().get ()); + = get_predicate_from_bound (trait_path_ref, qual_path_type.get_type ()); if (specified_bound.is_error ()) return; @@ -457,7 +455,7 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id, { // we need to setup with apropriate bounds HIR::TypePath &bound_path - = *associated->get_impl_block ()->get_trait_ref ().get (); + = associated->get_impl_block ()->get_trait_ref (); const auto &trait_ref = *TraitResolver::Resolve (bound_path); rust_assert (!trait_ref.is_error ()); diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc index e5859edca79e..52d125354d53 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc @@ -33,15 +33,15 @@ TypeCheckPattern::TypeCheckPattern (TyTy::BaseType *parent) {} TyTy::BaseType * -TypeCheckPattern::Resolve (HIR::Pattern *pattern, TyTy::BaseType *parent) +TypeCheckPattern::Resolve (HIR::Pattern &pattern, TyTy::BaseType *parent) { TypeCheckPattern resolver (parent); - pattern->accept_vis (resolver); + pattern.accept_vis (resolver); if (resolver.infered == nullptr) - return new TyTy::ErrorType (pattern->get_mappings ().get_hirid ()); + return new TyTy::ErrorType (pattern.get_mappings ().get_hirid ()); - resolver.context->insert_type (pattern->get_mappings (), resolver.infered); + resolver.context->insert_type (pattern.get_mappings (), resolver.infered); return resolver.infered; } @@ -49,7 +49,7 @@ void TypeCheckPattern::visit (HIR::PathInExpression &pattern) { // Pattern must be enum variants, sturcts, constants, or associated constansts - TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (&pattern); + TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (pattern); NodeId ref_node_id = UNKNOWN_NODEID; bool maybe_item = false; @@ -161,7 +161,7 @@ TypeCheckPattern::visit (HIR::PathInExpression &pattern) void TypeCheckPattern::visit (HIR::TupleStructPattern &pattern) { - TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (&pattern.get_path ()); + TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (pattern.get_path ()); if (pattern_ty->get_kind () != TyTy::TypeKind::ADT) { rust_error_at ( @@ -210,8 +210,8 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern) // error[E0023]: this pattern has 0 fields, but the corresponding tuple // variant has 1 field - std::unique_ptr &items = pattern.get_items (); - switch (items->get_item_type ()) + auto &items = pattern.get_items (); + switch (items.get_item_type ()) { case HIR::TupleStructItems::RANGED: { // TODO @@ -221,7 +221,7 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern) case HIR::TupleStructItems::MULTIPLE: { HIR::TupleStructItemsNoRange &items_no_range - = static_cast (*items.get ()); + = static_cast (items); if (items_no_range.get_patterns ().size () != variant->num_fields ()) { @@ -247,7 +247,7 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern) // setup the type on this pattern type context->insert_type (pattern->get_mappings (), fty); - TypeCheckPattern::Resolve (pattern.get (), fty); + TypeCheckPattern::Resolve (*pattern, fty); } } break; @@ -266,7 +266,7 @@ emit_invalid_field_error (location_t loc, Rust::TyTy::VariantDef *variant, void TypeCheckPattern::visit (HIR::StructPattern &pattern) { - TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (&pattern.get_path ()); + TyTy::BaseType *pattern_ty = TypeCheckExpr::Resolve (pattern.get_path ()); if (pattern_ty->get_kind () != TyTy::TypeKind::ADT) { rust_error_at (pattern.get_locus (), @@ -324,7 +324,7 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern) case HIR::StructPatternField::ItemType::IDENT_PAT: { HIR::StructPatternFieldIdentPat &ident - = static_cast (*field.get ()); + = static_cast (*field); TyTy::StructFieldType *field = nullptr; if (!variant->lookup_field (ident.get_identifier ().as_string (), @@ -337,13 +337,13 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern) named_fields.push_back (ident.get_identifier ().as_string ()); TyTy::BaseType *fty = field->get_field_type (); - TypeCheckPattern::Resolve (ident.get_pattern ().get (), fty); + TypeCheckPattern::Resolve (ident.get_pattern (), fty); } break; case HIR::StructPatternField::ItemType::IDENT: { HIR::StructPatternFieldIdent &ident - = static_cast (*field.get ()); + = static_cast (*field); TyTy::StructFieldType *field = nullptr; if (!variant->lookup_field (ident.get_identifier ().as_string (), @@ -440,12 +440,11 @@ void TypeCheckPattern::visit (HIR::TuplePattern &pattern) { std::unique_ptr items; - switch (pattern.get_items ()->get_item_type ()) + switch (pattern.get_items ().get_item_type ()) { case HIR::TuplePatternItems::ItemType::MULTIPLE: { - HIR::TuplePatternItemsMultiple &ref - = *static_cast ( - pattern.get_items ().get ()); + auto &ref = static_cast ( + pattern.get_items ()); auto resolved_parent = parent->destructure (); if (resolved_parent->get_kind () != TyTy::TUPLE) @@ -474,8 +473,7 @@ TypeCheckPattern::visit (HIR::TuplePattern &pattern) auto &p = patterns[i]; TyTy::BaseType *par_type = par.get_field (i); - TyTy::BaseType *elem - = TypeCheckPattern::Resolve (p.get (), par_type); + TyTy::BaseType *elem = TypeCheckPattern::Resolve (*p, par_type); pattern_elems.push_back (TyTy::TyVar (elem->get_ref ())); } infered = new TyTy::TupleType (pattern.get_mappings ().get_hirid (), @@ -543,10 +541,10 @@ TypeCheckPattern::visit (HIR::ReferencePattern &pattern) return; } - TyTy::ReferenceType *ref_ty_ty = static_cast (parent); + auto &ref_ty_ty = static_cast (*parent); TyTy::BaseType *infered_base - = TypeCheckPattern::Resolve (pattern.get_referenced_pattern ().get (), - ref_ty_ty->get_base ()); + = TypeCheckPattern::Resolve (pattern.get_referenced_pattern (), + ref_ty_ty.get_base ()); infered = new TyTy::ReferenceType (pattern.get_mappings ().get_hirid (), TyTy::TyVar (infered_base->get_ref ()), pattern.is_mut () ? Mutability::Mut @@ -578,15 +576,14 @@ TypeCheckPattern::emit_pattern_size_error (const HIR::Pattern &pattern, TyTy::BaseType * TypeCheckPattern::typecheck_range_pattern_bound ( - std::unique_ptr &bound, - Analysis::NodeMapping mappings, location_t locus) + Rust::HIR::RangePatternBound &bound, Analysis::NodeMapping mappings, + location_t locus) { TyTy::BaseType *resolved_bound = nullptr; - switch (bound->get_bound_type ()) + switch (bound.get_bound_type ()) { case HIR::RangePatternBound::RangePatternBoundType::LITERAL: { - HIR::RangePatternBoundLiteral &ref - = *static_cast (bound.get ()); + auto &ref = static_cast (bound); HIR::Literal lit = ref.get_literal (); @@ -595,18 +592,16 @@ TypeCheckPattern::typecheck_range_pattern_bound ( break; case HIR::RangePatternBound::RangePatternBoundType::PATH: { - HIR::RangePatternBoundPath &ref - = *static_cast (bound.get ()); + auto &ref = static_cast (bound); - resolved_bound = TypeCheckExpr::Resolve (&ref.get_path ()); + resolved_bound = TypeCheckExpr::Resolve (ref.get_path ()); } break; case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { - HIR::RangePatternBoundQualPath &ref - = *static_cast (bound.get ()); + auto &ref = static_cast (bound); - resolved_bound = TypeCheckExpr::Resolve (&ref.get_qualified_path ()); + resolved_bound = TypeCheckExpr::Resolve (ref.get_qualified_path ()); } break; } @@ -623,7 +618,7 @@ TypeCheckPattern::visit (HIR::AltPattern &pattern) std::vector types; for (auto &alt_pattern : alts) { - types.push_back (TypeCheckPattern::Resolve (alt_pattern.get (), parent)); + types.push_back (TypeCheckPattern::Resolve (*alt_pattern, parent)); } TyTy::BaseType *alt_pattern_type @@ -642,16 +637,16 @@ TypeCheckPattern::visit (HIR::AltPattern &pattern) } TyTy::BaseType * -ClosureParamInfer::Resolve (HIR::Pattern *pattern) +ClosureParamInfer::Resolve (HIR::Pattern &pattern) { ClosureParamInfer resolver; - pattern->accept_vis (resolver); + pattern.accept_vis (resolver); if (resolver.infered->get_kind () != TyTy::TypeKind::ERROR) { resolver.context->insert_implicit_type (resolver.infered); resolver.mappings.insert_location (resolver.infered->get_ref (), - pattern->get_locus ()); + pattern.get_locus ()); } return resolver.infered; } @@ -682,7 +677,7 @@ void ClosureParamInfer::visit (HIR::ReferencePattern &pattern) { TyTy::BaseType *element - = ClosureParamInfer::Resolve (pattern.get_referenced_pattern ().get ()); + = ClosureParamInfer::Resolve (pattern.get_referenced_pattern ()); HirId id = pattern.get_mappings ().get_hirid (); infered = new TyTy::ReferenceType (id, TyTy::TyVar (element->get_ref ()), diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.h b/gcc/rust/typecheck/rust-hir-type-check-pattern.h index 4820b7406df2..a4de0aae1936 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.h +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.h @@ -28,7 +28,7 @@ namespace Resolver { class TypeCheckPattern : public TypeCheckBase, public HIR::HIRPatternVisitor { public: - static TyTy::BaseType *Resolve (HIR::Pattern *pattern, + static TyTy::BaseType *Resolve (HIR::Pattern &pattern, TyTy::BaseType *parent); void visit (HIR::PathInExpression &pattern) override; @@ -47,9 +47,10 @@ class TypeCheckPattern : public TypeCheckBase, public HIR::HIRPatternVisitor private: TypeCheckPattern (TyTy::BaseType *parent); - TyTy::BaseType *typecheck_range_pattern_bound ( - std::unique_ptr &bound, - Analysis::NodeMapping mappings, location_t locus); + TyTy::BaseType * + typecheck_range_pattern_bound (Rust::HIR::RangePatternBound &bound, + Analysis::NodeMapping mappings, + location_t locus); void emit_pattern_size_error (const HIR::Pattern &pattern, size_t expected_field_count, @@ -62,7 +63,7 @@ class TypeCheckPattern : public TypeCheckBase, public HIR::HIRPatternVisitor class ClosureParamInfer : private TypeCheckBase, private HIR::HIRPatternVisitor { public: - static TyTy::BaseType *Resolve (HIR::Pattern *pattern); + static TyTy::BaseType *Resolve (HIR::Pattern &pattern); void visit (HIR::PathInExpression &pattern) override; void visit (HIR::StructPattern &pattern) override; diff --git a/gcc/rust/typecheck/rust-hir-type-check-stmt.cc b/gcc/rust/typecheck/rust-hir-type-check-stmt.cc index 7e62bfdb5cba..87a7733848a1 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-stmt.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-stmt.cc @@ -28,17 +28,17 @@ namespace Rust { namespace Resolver { TyTy::BaseType * -TypeCheckStmt::Resolve (HIR::Stmt *stmt) +TypeCheckStmt::Resolve (HIR::Stmt &stmt) { TypeCheckStmt resolver; - stmt->accept_vis (resolver); + stmt.accept_vis (resolver); return resolver.infered; } void TypeCheckStmt::visit (HIR::ExprStmt &stmt) { - infered = TypeCheckExpr::Resolve (stmt.get_expr ().get ()); + infered = TypeCheckExpr::Resolve (stmt.get_expr ()); } void @@ -52,21 +52,20 @@ TypeCheckStmt::visit (HIR::ExternBlock &extern_block) { for (auto &item : extern_block.get_extern_items ()) { - TypeCheckTopLevelExternItem::Resolve (item.get (), extern_block); + TypeCheckTopLevelExternItem::Resolve (*item, extern_block); } } void TypeCheckStmt::visit (HIR::ConstantItem &constant) { - TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ().get ()); - TyTy::BaseType *expr_type - = TypeCheckExpr::Resolve (constant.get_expr ().get ()); + TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ()); + TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ()); infered = coercion_site ( constant.get_mappings ().get_hirid (), - TyTy::TyWithLocation (type, constant.get_type ()->get_locus ()), - TyTy::TyWithLocation (expr_type, constant.get_expr ()->get_locus ()), + TyTy::TyWithLocation (type, constant.get_type ().get_locus ()), + TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()), constant.get_locus ()); context->insert_type (constant.get_mappings (), infered); } @@ -76,13 +75,13 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt) { infered = TyTy::TupleType::get_unit_type (); - HIR::Pattern &stmt_pattern = *stmt.get_pattern (); + auto &stmt_pattern = stmt.get_pattern (); TyTy::BaseType *init_expr_ty = nullptr; location_t init_expr_locus = UNKNOWN_LOCATION; if (stmt.has_init_expr ()) { - init_expr_locus = stmt.get_init_expr ()->get_locus (); - init_expr_ty = TypeCheckExpr::Resolve (stmt.get_init_expr ().get ()); + init_expr_locus = stmt.get_init_expr ().get_locus (); + init_expr_ty = TypeCheckExpr::Resolve (stmt.get_init_expr ()); if (init_expr_ty->get_kind () == TyTy::TypeKind::ERROR) return; @@ -94,8 +93,8 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt) location_t specified_ty_locus; if (stmt.has_type ()) { - specified_ty = TypeCheckType::Resolve (stmt.get_type ().get ()); - specified_ty_locus = stmt.get_type ()->get_locus (); + specified_ty = TypeCheckType::Resolve (stmt.get_type ()); + specified_ty_locus = stmt.get_type ().get_locus (); } // let x:i32 = 123; @@ -105,19 +104,19 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt) TyTy::TyWithLocation (specified_ty, specified_ty_locus), TyTy::TyWithLocation (init_expr_ty, init_expr_locus), stmt.get_locus ()); - TypeCheckPattern::Resolve (&stmt_pattern, specified_ty); + TypeCheckPattern::Resolve (stmt_pattern, specified_ty); } else { // let x:i32; if (specified_ty != nullptr) { - TypeCheckPattern::Resolve (&stmt_pattern, specified_ty); + TypeCheckPattern::Resolve (stmt_pattern, specified_ty); } // let x = 123; else if (init_expr_ty != nullptr) { - TypeCheckPattern::Resolve (&stmt_pattern, init_expr_ty); + TypeCheckPattern::Resolve (stmt_pattern, init_expr_ty); } // let x; else @@ -127,7 +126,7 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt) TyTy::InferType::InferTypeKind::GENERAL, TyTy::InferType::TypeHint::Default (), stmt.get_locus ()); - TypeCheckPattern::Resolve (&stmt_pattern, infer); + TypeCheckPattern::Resolve (stmt_pattern, infer); } } } @@ -135,12 +134,12 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt) void TypeCheckStmt::visit (HIR::TypePath &path) { - infered = TypeCheckType::Resolve (&path); + infered = TypeCheckType::Resolve (path); } void TypeCheckStmt::visit (HIR::QualifiedPathInType &path) { - infered = TypeCheckType::Resolve (&path); + infered = TypeCheckType::Resolve (path); } void diff --git a/gcc/rust/typecheck/rust-hir-type-check-stmt.h b/gcc/rust/typecheck/rust-hir-type-check-stmt.h index 138780f9035e..7df221f56e51 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-stmt.h +++ b/gcc/rust/typecheck/rust-hir-type-check-stmt.h @@ -28,7 +28,7 @@ namespace Resolver { class TypeCheckStmt : private TypeCheckBase, private HIR::HIRStmtVisitor { public: - static TyTy::BaseType *Resolve (HIR::Stmt *stmt); + static TyTy::BaseType *Resolve (HIR::Stmt &stmt); void visit (HIR::ExprStmt &stmt) override; void visit (HIR::EmptyStmt &stmt) override; diff --git a/gcc/rust/typecheck/rust-hir-type-check-struct-field.h b/gcc/rust/typecheck/rust-hir-type-check-struct-field.h index 792eebf3ff5e..1d4d1fa5e147 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-struct-field.h +++ b/gcc/rust/typecheck/rust-hir-type-check-struct-field.h @@ -33,7 +33,7 @@ namespace Resolver { class TypeCheckStructExpr : public TypeCheckBase { public: - static TyTy::BaseType *Resolve (HIR::StructExprStructFields *expr); + static TyTy::BaseType *Resolve (HIR::StructExprStructFields &expr); // Helper for making any errors static Error @@ -49,7 +49,7 @@ class TypeCheckStructExpr : public TypeCheckBase bool visit (HIR::StructExprFieldIdentifier &field); private: - TypeCheckStructExpr (HIR::Expr *e); + TypeCheckStructExpr (HIR::Expr &e); // result TyTy::BaseType *resolved; diff --git a/gcc/rust/typecheck/rust-hir-type-check-struct.cc b/gcc/rust/typecheck/rust-hir-type-check-struct.cc index 5999b2dcbc39..47881021b699 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-struct.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-struct.cc @@ -24,18 +24,18 @@ namespace Rust { namespace Resolver { -TypeCheckStructExpr::TypeCheckStructExpr (HIR::Expr *e) +TypeCheckStructExpr::TypeCheckStructExpr (HIR::Expr &e) : TypeCheckBase (), - resolved (new TyTy::ErrorType (e->get_mappings ().get_hirid ())), + resolved (new TyTy::ErrorType (e.get_mappings ().get_hirid ())), struct_path_resolved (nullptr), variant (&TyTy::VariantDef::get_error_node ()) {} TyTy::BaseType * -TypeCheckStructExpr::Resolve (HIR::StructExprStructFields *expr) +TypeCheckStructExpr::Resolve (HIR::StructExprStructFields &expr) { TypeCheckStructExpr resolver (expr); - resolver.resolve (*expr); + resolver.resolve (expr); return resolver.resolved; } @@ -43,7 +43,7 @@ void TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr) { TyTy::BaseType *struct_path_ty - = TypeCheckExpr::Resolve (&struct_expr.get_struct_name ()); + = TypeCheckExpr::Resolve (struct_expr.get_struct_name ()); if (struct_path_ty->get_kind () != TyTy::TypeKind::ADT) { rust_error_at (struct_expr.get_struct_name ().get_locus (), @@ -56,7 +56,7 @@ TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr) if (struct_expr.has_struct_base ()) { TyTy::BaseType *base_resolved - = TypeCheckExpr::Resolve (struct_expr.struct_base->base_struct.get ()); + = TypeCheckExpr::Resolve (*struct_expr.struct_base->base_struct); TyTy::BaseType *base_unify = unify_site ( struct_expr.struct_base->base_struct->get_mappings ().get_hirid (), TyTy::TyWithLocation (struct_path_resolved), @@ -284,8 +284,8 @@ TypeCheckStructExpr::visit (HIR::StructExprFieldIdentifierValue &field) return false; } - TyTy::BaseType *value = TypeCheckExpr::Resolve (field.get_value ().get ()); - location_t value_locus = field.get_value ()->get_locus (); + TyTy::BaseType *value = TypeCheckExpr::Resolve (field.get_value ()); + location_t value_locus = field.get_value ().get_locus (); HirId coercion_site_id = field.get_mappings ().get_hirid (); resolved_field_value_expr @@ -330,8 +330,8 @@ TypeCheckStructExpr::visit (HIR::StructExprFieldIndexValue &field) return false; } - TyTy::BaseType *value = TypeCheckExpr::Resolve (field.get_value ().get ()); - location_t value_locus = field.get_value ()->get_locus (); + TyTy::BaseType *value = TypeCheckExpr::Resolve (field.get_value ()); + location_t value_locus = field.get_value ().get_locus (); HirId coercion_site_id = field.get_mappings ().get_hirid (); resolved_field_value_expr @@ -385,7 +385,7 @@ TypeCheckStructExpr::visit (HIR::StructExprFieldIdentifier &field) HIR::GenericArgs::create_empty ()); HIR::PathInExpression expr (mappings_copy2, {seg}, field.get_locus (), false, {}); - TyTy::BaseType *value = TypeCheckExpr::Resolve (&expr); + TyTy::BaseType *value = TypeCheckExpr::Resolve (expr); location_t value_locus = expr.get_locus (); HirId coercion_site_id = field.get_mappings ().get_hirid (); diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index f3aee8a97210..099a08910e8d 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -18,6 +18,7 @@ #include "rust-hir-type-check-type.h" #include "options.h" +#include "optional.h" #include "rust-hir-trait-resolve.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-path-probe.h" @@ -26,18 +27,19 @@ #include "rust-mapping-common.h" #include "rust-substitution-mapper.h" #include "rust-type-util.h" +#include namespace Rust { namespace Resolver { HIR::GenericArgs -TypeCheckResolveGenericArguments::resolve (HIR::TypePathSegment *segment) +TypeCheckResolveGenericArguments::resolve (HIR::TypePathSegment &segment) { - TypeCheckResolveGenericArguments resolver (segment->get_locus ()); - switch (segment->get_type ()) + TypeCheckResolveGenericArguments resolver (segment.get_locus ()); + switch (segment.get_type ()) { case HIR::TypePathSegment::SegmentType::GENERIC: - resolver.visit (static_cast (*segment)); + resolver.visit (static_cast (segment)); break; default: @@ -53,20 +55,20 @@ TypeCheckResolveGenericArguments::visit (HIR::TypePathSegmentGeneric &generic) } TyTy::BaseType * -TypeCheckType::Resolve (HIR::Type *type) +TypeCheckType::Resolve (HIR::Type &type) { // is it already resolved? auto context = TypeCheckContext::get (); TyTy::BaseType *resolved = nullptr; bool already_resolved - = context->lookup_type (type->get_mappings ().get_hirid (), &resolved); + = context->lookup_type (type.get_mappings ().get_hirid (), &resolved); if (already_resolved) return resolved; - TypeCheckType resolver (type->get_mappings ().get_hirid ()); - type->accept_vis (resolver); + TypeCheckType resolver (type.get_mappings ().get_hirid ()); + type.accept_vis (resolver); rust_assert (resolver.translated != nullptr); - resolver.context->insert_type (type->get_mappings (), resolver.translated); + resolver.context->insert_type (type.get_mappings (), resolver.translated); return resolver.translated; } @@ -82,7 +84,7 @@ TypeCheckType::visit (HIR::BareFunctionType &fntype) TyTy::BaseType *return_type; if (fntype.has_return_type ()) { - return_type = TypeCheckType::Resolve (fntype.get_return_type ().get ()); + return_type = TypeCheckType::Resolve (fntype.get_return_type ()); } else { @@ -95,7 +97,7 @@ TypeCheckType::visit (HIR::BareFunctionType &fntype) std::vector params; for (auto ¶m : fntype.get_function_params ()) { - TyTy::BaseType *ptype = TypeCheckType::Resolve (param.get_type ().get ()); + TyTy::BaseType *ptype = TypeCheckType::Resolve (param.get_type ()); params.push_back (TyTy::TyVar (ptype->get_ref ())); } @@ -121,7 +123,7 @@ TypeCheckType::visit (HIR::TupleType &tuple) std::vector fields; for (auto &elem : tuple.get_elems ()) { - auto field_ty = TypeCheckType::Resolve (elem.get ()); + auto field_ty = TypeCheckType::Resolve (*elem); fields.push_back (TyTy::TyVar (field_ty->get_ref ())); } @@ -163,8 +165,7 @@ void TypeCheckType::visit (HIR::QualifiedPathInType &path) { HIR::QualifiedPathType qual_path_type = path.get_path_type (); - TyTy::BaseType *root - = TypeCheckType::Resolve (qual_path_type.get_type ().get ()); + TyTy::BaseType *root = TypeCheckType::Resolve (qual_path_type.get_type ()); if (root->get_kind () == TyTy::TypeKind::ERROR) { rust_debug_loc (path.get_locus (), "failed to resolve the root"); @@ -176,7 +177,7 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) // then this is just a normal path-in-expression NodeId root_resolved_node_id = UNKNOWN_NODEID; bool ok = resolver->lookup_resolved_type ( - qual_path_type.get_type ()->get_mappings ().get_nodeid (), + qual_path_type.get_type ().get_mappings ().get_nodeid (), &root_resolved_node_id); rust_assert (ok); @@ -189,8 +190,8 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) } // Resolve the trait now - std::unique_ptr &trait_path_ref = qual_path_type.get_trait (); - TraitReference *trait_ref = TraitResolver::Resolve (*trait_path_ref.get ()); + auto &trait_path_ref = qual_path_type.get_trait (); + TraitReference *trait_ref = TraitResolver::Resolve (trait_path_ref); if (trait_ref->is_error ()) return; @@ -203,9 +204,8 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) } // get the predicate for the bound - auto specified_bound - = get_predicate_from_bound (*qual_path_type.get_trait ().get (), - qual_path_type.get_type ().get ()); + auto specified_bound = get_predicate_from_bound (qual_path_type.get_trait (), + qual_path_type.get_type ()); if (specified_bound.is_error ()) return; @@ -213,18 +213,17 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) root->inherit_bounds ({specified_bound}); // lookup the associated item from the specified bound - std::unique_ptr &item_seg - = path.get_associated_segment (); - HIR::PathIdentSegment item_seg_identifier = item_seg->get_ident_segment (); + HIR::TypePathSegment &item_seg = path.get_associated_segment (); + HIR::PathIdentSegment item_seg_identifier = item_seg.get_ident_segment (); TyTy::TypeBoundPredicateItem item = specified_bound.lookup_associated_item (item_seg_identifier.as_string ()); if (item.is_error ()) { std::string item_seg_ident_name, rich_msg; - item_seg_ident_name = qual_path_type.get_trait ()->as_string (); + item_seg_ident_name = qual_path_type.get_trait ().as_string (); rich_msg = "not found in `" + item_seg_ident_name + "`"; - rich_location richloc (line_table, item_seg->get_locus ()); + rich_location richloc (line_table, item_seg.get_locus ()); richloc.add_fixit_replace (rich_msg.c_str ()); rust_error_at (richloc, ErrorCode::E0576, @@ -296,17 +295,16 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path) } // turbo-fish segment path:: - if (item_seg->get_type () == HIR::TypePathSegment::SegmentType::GENERIC) + if (item_seg.get_type () == HIR::TypePathSegment::SegmentType::GENERIC) { - HIR::TypePathSegmentGeneric &generic_seg - = static_cast (*item_seg.get ()); + auto &generic_seg = static_cast (item_seg); // turbo-fish segment path:: if (generic_seg.has_generic_args ()) { if (!translated->has_substitutions_defined ()) { - rust_error_at (item_seg->get_locus (), + rust_error_at (item_seg.get_locus (), "substitutions not supported for %s", translated->as_string ().c_str ()); translated @@ -454,13 +452,13 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset, // turbo-fish segment path:: if (seg->is_generic_segment ()) { - HIR::TypePathSegmentGeneric *generic_segment - = static_cast (seg.get ()); + auto &generic_segment + = static_cast (*seg); auto regions = context->regions_from_generic_args ( - generic_segment->get_generic_args ()); + generic_segment.get_generic_args ()); lookup = SubstMapper::Resolve (lookup, path.get_locus (), - &generic_segment->get_generic_args (), + &generic_segment.get_generic_args (), regions); if (lookup->get_kind () == TyTy::TypeKind::ERROR) return new TyTy::ErrorType (seg->get_mappings ().get_hirid ()); @@ -569,12 +567,12 @@ TypeCheckType::resolve_segments ( if (seg->is_generic_segment ()) { - auto *generic_segment - = static_cast (seg.get ()); + auto &generic_segment + = static_cast (*seg); std::vector regions; for (auto &lifetime : - generic_segment->get_generic_args ().get_lifetime_args ()) + generic_segment.get_generic_args ().get_lifetime_args ()) { auto region = context->lookup_and_resolve_lifetime (lifetime); if (!region.has_value ()) @@ -587,7 +585,7 @@ TypeCheckType::resolve_segments ( } tyseg = SubstMapper::Resolve (tyseg, expr_locus, - &generic_segment->get_generic_args (), + &generic_segment.get_generic_args (), regions); if (tyseg->get_kind () == TyTy::TypeKind::ERROR) return new TyTy::ErrorType (expr_id); @@ -651,7 +649,7 @@ TypeCheckType::visit (HIR::TraitObjectType &type) TyTy::TypeBoundPredicate predicate = get_predicate_from_bound ( trait_bound.get_path (), - nullptr /*this will setup a PLACEHOLDER for self*/); + tl::nullopt /*this will setup a PLACEHOLDER for self*/); if (!predicate.is_error () && predicate.is_object_safe (true, type.get_locus ())) @@ -667,33 +665,31 @@ TypeCheckType::visit (HIR::TraitObjectType &type) void TypeCheckType::visit (HIR::ArrayType &type) { - auto capacity_type = TypeCheckExpr::Resolve (type.get_size_expr ().get ()); + auto capacity_type = TypeCheckExpr::Resolve (type.get_size_expr ()); if (capacity_type->get_kind () == TyTy::TypeKind::ERROR) return; TyTy::BaseType *expected_ty = nullptr; bool ok = context->lookup_builtin ("usize", &expected_ty); rust_assert (ok); - context->insert_type (type.get_size_expr ()->get_mappings (), expected_ty); + context->insert_type (type.get_size_expr ().get_mappings (), expected_ty); - unify_site (type.get_size_expr ()->get_mappings ().get_hirid (), + unify_site (type.get_size_expr ().get_mappings ().get_hirid (), TyTy::TyWithLocation (expected_ty), TyTy::TyWithLocation (capacity_type, - type.get_size_expr ()->get_locus ()), - type.get_size_expr ()->get_locus ()); + type.get_size_expr ().get_locus ()), + type.get_size_expr ().get_locus ()); - TyTy::BaseType *base - = TypeCheckType::Resolve (type.get_element_type ().get ()); + TyTy::BaseType *base = TypeCheckType::Resolve (type.get_element_type ()); translated = new TyTy::ArrayType (type.get_mappings ().get_hirid (), - type.get_locus (), *type.get_size_expr (), + type.get_locus (), type.get_size_expr (), TyTy::TyVar (base->get_ref ())); } void TypeCheckType::visit (HIR::SliceType &type) { - TyTy::BaseType *base - = TypeCheckType::Resolve (type.get_element_type ().get ()); + TyTy::BaseType *base = TypeCheckType::Resolve (type.get_element_type ()); translated = new TyTy::SliceType (type.get_mappings ().get_hirid (), type.get_locus (), TyTy::TyVar (base->get_ref ())); @@ -701,7 +697,7 @@ TypeCheckType::visit (HIR::SliceType &type) void TypeCheckType::visit (HIR::ReferenceType &type) { - TyTy::BaseType *base = TypeCheckType::Resolve (type.get_base_type ().get ()); + TyTy::BaseType *base = TypeCheckType::Resolve (type.get_base_type ()); rust_assert (type.has_lifetime ()); auto region = context->lookup_and_resolve_lifetime (type.get_lifetime ()); if (!region.has_value ()) @@ -718,7 +714,7 @@ TypeCheckType::visit (HIR::ReferenceType &type) void TypeCheckType::visit (HIR::RawPointerType &type) { - TyTy::BaseType *base = TypeCheckType::Resolve (type.get_base_type ().get ()); + TyTy::BaseType *base = TypeCheckType::Resolve (type.get_base_type ()); translated = new TyTy::PointerType (type.get_mappings ().get_hirid (), TyTy::TyVar (base->get_ref ()), type.get_mut ()); @@ -744,21 +740,21 @@ TypeCheckType::visit (HIR::NeverType &type) } TyTy::ParamType * -TypeResolveGenericParam::Resolve (HIR::GenericParam *param, bool apply_sized) +TypeResolveGenericParam::Resolve (HIR::GenericParam ¶m, bool apply_sized) { TypeResolveGenericParam resolver (apply_sized); - switch (param->get_kind ()) + switch (param.get_kind ()) { case HIR::GenericParam::GenericKind::TYPE: - resolver.visit (static_cast (*param)); + resolver.visit (static_cast (param)); break; case HIR::GenericParam::GenericKind::CONST: - resolver.visit (static_cast (*param)); + resolver.visit (static_cast (param)); break; case HIR::GenericParam::GenericKind::LIFETIME: - resolver.visit (static_cast (*param)); + resolver.visit (static_cast (param)); break; } return resolver.resolved; @@ -780,9 +776,9 @@ void TypeResolveGenericParam::visit (HIR::TypeParam ¶m) { if (param.has_type ()) - TypeCheckType::Resolve (param.get_type ().get ()); + TypeCheckType::Resolve (param.get_type ()); - HIR::Type *implicit_self_bound = nullptr; + std::unique_ptr implicit_self_bound = nullptr; if (param.has_type_param_bounds ()) { // We need two possible parameter types. One with no Bounds and one with @@ -800,8 +796,8 @@ TypeResolveGenericParam::visit (HIR::TypeParam ¶m) param.get_mappings ().get_nodeid (), implicit_id, param.get_mappings ().get_local_defid ()); - implicit_self_bound - = new HIR::TypePath (mappings, {}, BUILTINS_LOCATION, false); + implicit_self_bound = Rust::make_unique ( + HIR::TypePath (mappings, {}, BUILTINS_LOCATION, false)); } std::map> predicates; @@ -830,13 +826,11 @@ TypeResolveGenericParam::visit (HIR::TypeParam ¶m) switch (bound->get_bound_type ()) { case HIR::TypeParamBound::BoundType::TRAITBOUND: { - HIR::TraitBound *b - = static_cast (bound.get ()); + HIR::TraitBound &b = static_cast (*bound); - TyTy::TypeBoundPredicate predicate - = get_predicate_from_bound (b->get_path (), - implicit_self_bound, - b->get_polarity ()); + TyTy::TypeBoundPredicate predicate = get_predicate_from_bound ( + b.get_path (), tl::optional (std::ref (*implicit_self_bound)), + b.get_polarity ()); if (!predicate.is_error ()) { switch (predicate.get_polarity ()) @@ -849,7 +843,7 @@ TypeResolveGenericParam::visit (HIR::TypeParam ¶m) else { // emit error message - rich_location r (line_table, b->get_locus ()); + rich_location r (line_table, b.get_locus ()); r.add_range (predicate.get ()->get_locus ()); rust_error_at ( r, "antibound for %s is not applied here", @@ -947,7 +941,7 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item) } auto &binding_type_path = item.get_bound_type (); - TyTy::BaseType *binding = TypeCheckType::Resolve (binding_type_path.get ()); + TyTy::BaseType *binding = TypeCheckType::Resolve (binding_type_path); // FIXME double check there might be a trait cycle here see TypeParam handling @@ -960,8 +954,7 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item) auto *b = static_cast (bound.get ()); TyTy::TypeBoundPredicate predicate - = get_predicate_from_bound (b->get_path (), - binding_type_path.get ()); + = get_predicate_from_bound (b->get_path (), binding_type_path); if (!predicate.is_error ()) specified_bounds.push_back (std::move (predicate)); } @@ -991,7 +984,7 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item) // When we apply these bounds we must lookup which type this binding // resolves to, as this is the type which will be used during resolution // of the block. - NodeId ast_node_id = binding_type_path->get_mappings ().get_nodeid (); + NodeId ast_node_id = binding_type_path.get_mappings ().get_nodeid (); // then lookup the reference_node_id NodeId ref_node_id = UNKNOWN_NODEID; @@ -1000,7 +993,7 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item) // FIXME rust_error_at (UNDEF_LOCATION, "Failed to lookup type reference for node: %s", - binding_type_path->as_string ().c_str ()); + binding_type_path.as_string ().c_str ()); return; } @@ -1013,7 +1006,7 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item) { rust_error_at (mappings.lookup_location (*hid), "Failed to resolve where-clause binding type: %s", - binding_type_path->as_string ().c_str ()); + binding_type_path.as_string ().c_str ()); return; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.h b/gcc/rust/typecheck/rust-hir-type-check-type.h index 3083a94f97b2..814903f316bd 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.h +++ b/gcc/rust/typecheck/rust-hir-type-check-type.h @@ -31,7 +31,7 @@ namespace Resolver { class TypeCheckResolveGenericArguments : public TypeCheckBase { public: - static HIR::GenericArgs resolve (HIR::TypePathSegment *segment); + static HIR::GenericArgs resolve (HIR::TypePathSegment &segment); void visit (HIR::TypePathSegmentGeneric &generic); @@ -46,7 +46,7 @@ class TypeCheckResolveGenericArguments : public TypeCheckBase class TypeCheckType : public TypeCheckBase, public HIR::HIRTypeVisitor { public: - static TyTy::BaseType *Resolve (HIR::Type *type); + static TyTy::BaseType *Resolve (HIR::Type &type); void visit (HIR::BareFunctionType &fntype) override; void visit (HIR::TupleType &tuple) override; @@ -96,7 +96,7 @@ class TypeCheckType : public TypeCheckBase, public HIR::HIRTypeVisitor class TypeResolveGenericParam : public TypeCheckBase { public: - static TyTy::ParamType *Resolve (HIR::GenericParam *param, + static TyTy::ParamType *Resolve (HIR::GenericParam ¶m, bool apply_sized = true); protected: diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index 2b0549f4b1dc..48e9c0579515 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -19,10 +19,12 @@ #include "rust-hir-type-check.h" #include "rust-hir-full.h" #include "rust-hir-inherent-impl-overlap.h" +#include "rust-hir-pattern.h" #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-item.h" #include "rust-hir-type-check-pattern.h" #include "rust-hir-type-check-struct-field.h" +#include "rust-make-unique.h" #include "rust-immutable-name-resolution-context.h" // for flag_name_resolution_2_0 @@ -140,11 +142,10 @@ TyTy::BaseType * TraitItemReference::get_type_from_constant ( /*const*/ HIR::TraitItemConst &constant) const { - TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ().get ()); + TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ()); if (constant.has_expr ()) { - TyTy::BaseType *expr - = TypeCheckExpr::Resolve (constant.get_expr ().get ()); + TyTy::BaseType *expr = TypeCheckExpr::Resolve (constant.get_expr ()); return unify_site (constant.get_mappings ().get_hirid (), TyTy::TyWithLocation (type), @@ -185,7 +186,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const case HIR::GenericParam::GenericKind::TYPE: { auto param_type - = TypeResolveGenericParam::Resolve (generic_param.get ()); + = TypeResolveGenericParam::Resolve (*generic_param); context->insert_type (generic_param->get_mappings (), param_type); @@ -209,8 +210,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const ret_type = TyTy::TupleType::get_unit_type (); else { - auto resolved - = TypeCheckType::Resolve (function.get_return_type ().get ()); + auto resolved = TypeCheckType::Resolve (function.get_return_type ()); if (resolved->get_kind () == TyTy::TypeKind::ERROR) { rust_error_at (fn.get_locus (), "failed to resolve return type"); @@ -219,10 +219,12 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const ret_type = resolved->clone (); ret_type->set_ref ( - function.get_return_type ()->get_mappings ().get_hirid ()); + function.get_return_type ().get_mappings ().get_hirid ()); } std::vector > params; + + std::unique_ptr self_pattern = nullptr; if (function.is_method ()) { // these are implicit mappings and not used @@ -236,16 +238,17 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const // for compilation to know parameter names. The types are ignored // but we reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self (); - HIR::IdentifierPattern *self_pattern = new HIR::IdentifierPattern ( - mapping, {"self"}, self_param.get_locus (), self_param.is_ref (), - self_param.is_mut () ? Mutability::Mut : Mutability::Imm, - std::unique_ptr (nullptr)); + self_pattern + = Rust::make_unique (HIR::IdentifierPattern ( + mapping, {"self"}, self_param.get_locus (), self_param.is_ref (), + self_param.is_mut () ? Mutability::Mut : Mutability::Imm, + std::unique_ptr (nullptr))); // might have a specified type TyTy::BaseType *self_type = nullptr; if (self_param.has_type ()) { - std::unique_ptr &specified_type = self_param.get_type (); - self_type = TypeCheckType::Resolve (specified_type.get ()); + HIR::Type &specified_type = self_param.get_type (); + self_type = TypeCheckType::Resolve (specified_type); } else { @@ -288,18 +291,20 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const context->insert_type (self_param.get_mappings (), self_type); params.push_back ( - std::pair (self_pattern, self_type)); + std::pair (self_pattern.get (), + self_type)); } for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on - auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ()); - params.push_back (std::pair ( - param.get_param_name ().get (), param_tyty)); + auto param_tyty = TypeCheckType::Resolve (param.get_type ()); + params.push_back ( + std::pair (¶m.get_param_name (), + param_tyty)); context->insert_type (param.get_mappings (), param_tyty); - TypeCheckPattern::Resolve (param.get_param_name ().get (), param_tyty); + TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); } auto &mappings = Analysis::Mappings::get (); @@ -327,7 +332,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const function.get_function_name ().as_string (), ident, function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG : TyTy::FnType::FNTYPE_DEFAULT_FLAGS, - ABI::RUST, std::move (params), ret_type, substitutions, + ABI::RUST, params, ret_type, substitutions, TyTy::SubstitutionArgumentMappings::empty ( context->get_lifetime_resolver ().get_num_bound_regions ()), region_constraints); diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h index 9c9523a5f8a0..5a78f62221fd 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.h +++ b/gcc/rust/typecheck/rust-hir-type-check.h @@ -42,7 +42,7 @@ class TypeCheckContextItem }; TypeCheckContextItem (HIR::Function *item); - TypeCheckContextItem (HIR::ImplBlock *impl_block, HIR::Function *item); + TypeCheckContextItem (HIR::ImplBlock &impl_block, HIR::Function *item); TypeCheckContextItem (HIR::TraitItemFunc *trait_item); TypeCheckContextItem (const TypeCheckContextItem &other); diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc index 079bd956d313..50892a3365c9 100644 --- a/gcc/rust/typecheck/rust-type-util.cc +++ b/gcc/rust/typecheck/rust-type-util.cc @@ -99,8 +99,9 @@ query_type (HirId reference, TyTy::BaseType **result) auto block = mappings.lookup_hir_extern_block (extern_item->second); rust_assert (block.has_value ()); - *result = TypeCheckTopLevelExternItem::Resolve (extern_item->first, - *block.value ()); + *result + = TypeCheckTopLevelExternItem::Resolve (*extern_item.value ().first, + *block.value ()); context->query_completed (reference); return true; } diff --git a/gcc/rust/typecheck/rust-typecheck-context.cc b/gcc/rust/typecheck/rust-typecheck-context.cc index d79ddf4aa6ae..418abd3421c4 100644 --- a/gcc/rust/typecheck/rust-typecheck-context.cc +++ b/gcc/rust/typecheck/rust-typecheck-context.cc @@ -646,9 +646,9 @@ TypeCheckContextItem::TypeCheckContextItem (HIR::Function *item) : type (ItemType::ITEM), item (item) {} -TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock *impl_block, +TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock &impl_block, HIR::Function *item) - : type (ItemType::IMPL_ITEM), item (impl_block, item) + : type (ItemType::IMPL_ITEM), item (&impl_block, item) {} TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item) diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index 4734b6746fc5..b3c19efadee9 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -70,7 +70,7 @@ TypeBoundsProbe::scan () if (!impl->has_trait_ref ()) return true; - HirId impl_ty_id = impl->get_type ()->get_mappings ().get_hirid (); + HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid (); TyTy::BaseType *impl_type = nullptr; if (!query_type (impl_ty_id, &impl_type)) return true; @@ -81,7 +81,7 @@ TypeBoundsProbe::scan () return true; } - possible_trait_paths.push_back ({impl->get_trait_ref ().get (), impl}); + possible_trait_paths.push_back ({&impl->get_trait_ref (), impl}); return true; }); @@ -182,9 +182,10 @@ TypeCheckBase::resolve_trait_path (HIR::TypePath &path) } TyTy::TypeBoundPredicate -TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path, - HIR::Type *associated_self, - BoundPolarity polarity) +TypeCheckBase::get_predicate_from_bound ( + HIR::TypePath &type_path, + tl::optional> associated_self, + BoundPolarity polarity) { TyTy::TypeBoundPredicate lookup = TyTy::TypeBoundPredicate::error (); bool already_resolved @@ -215,9 +216,9 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path, break; case HIR::TypePathSegment::SegmentType::FUNCTION: { - auto final_function_seg - = static_cast (final_seg.get ()); - auto &fn = final_function_seg->get_function_path (); + auto &final_function_seg + = static_cast (*final_seg); + auto &fn = final_function_seg.get_function_path (); // we need to make implicit generic args which must be an implicit // Tuple @@ -243,7 +244,7 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path, // resolve the fn_once_output type which assumes there must be an output // set rust_assert (fn.has_return_type ()); - TypeCheckType::Resolve (fn.get_return_type ().get ()); + TypeCheckType::Resolve (fn.get_return_type ()); HIR::TraitItem *trait_item = mappings @@ -252,10 +253,10 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path, .value (); std::vector bindings; - location_t output_locus = fn.get_return_type ()->get_locus (); + location_t output_locus = fn.get_return_type ().get_locus (); HIR::GenericArgsBinding binding (Identifier ( trait_item->trait_identifier ()), - fn.get_return_type ()->clone_type (), + fn.get_return_type ().clone_type (), output_locus); bindings.push_back (std::move (binding)); @@ -271,11 +272,11 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path, break; } - if (associated_self != nullptr) + if (associated_self.has_value ()) { std::vector> type_args; - type_args.push_back ( - std::unique_ptr (associated_self->clone_type ())); + type_args.push_back (std::unique_ptr ( + associated_self.value ().get ().clone_type ())); for (auto &arg : args.get_type_args ()) { type_args.push_back (std::unique_ptr (arg->clone_type ())); @@ -292,7 +293,7 @@ TypeCheckBase::get_predicate_from_bound (HIR::TypePath &type_path, if (!args.is_empty () || predicate.requires_generic_args ()) { // this is applying generic arguments to a trait reference - predicate.apply_generic_arguments (&args, associated_self != nullptr); + predicate.apply_generic_arguments (&args, associated_self.has_value ()); } context->insert_resolved_predicate (type_path.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index f5d12b80464e..36a6b3d858d8 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -80,7 +80,7 @@ TypeCheckCallExpr::visit (ADTType &type) BaseType *field_tyty = field->get_field_type (); location_t arg_locus = argument->get_locus (); - BaseType *arg = Resolver::TypeCheckExpr::Resolve (argument.get ()); + BaseType *arg = Resolver::TypeCheckExpr::Resolve (*argument); if (arg->get_kind () == TyTy::TypeKind::ERROR) { rust_error_at (argument->get_locus (), @@ -139,8 +139,7 @@ TypeCheckCallExpr::visit (FnType &type) for (auto &argument : call.get_arguments ()) { location_t arg_locus = argument->get_locus (); - auto argument_expr_tyty - = Resolver::TypeCheckExpr::Resolve (argument.get ()); + auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument); if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) { rust_error_at ( @@ -152,8 +151,8 @@ TypeCheckCallExpr::visit (FnType &type) // it might be a variadic function if (i < type.num_params ()) { - auto fnparam = type.param_at (i); - HIR::Pattern *fn_param_pattern = fnparam.first; + auto &fnparam = type.param_at (i); + auto &fn_param_pattern = fnparam.first; BaseType *param_ty = fnparam.second; location_t param_locus = fn_param_pattern == nullptr @@ -272,8 +271,7 @@ TypeCheckCallExpr::visit (FnPtr &type) { location_t arg_locus = argument->get_locus (); BaseType *fnparam = type.get_param_type_at (i); - auto argument_expr_tyty - = Resolver::TypeCheckExpr::Resolve (argument.get ()); + auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*argument); if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) { rust_error_at ( @@ -322,8 +320,7 @@ TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call, std::vector args; for (auto &arg : call.get_arguments ()) { - BaseType *argument_expr_tyty - = Resolver::TypeCheckExpr::Resolve (arg.get ()); + BaseType *argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (*arg); if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) { rust_error_at (arg->get_locus (), @@ -337,7 +334,7 @@ TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call, TypeCheckMethodCallExpr checker (call.get_mappings (), args, call.get_locus (), - call.get_receiver ()->get_locus (), + call.get_receiver ().get_locus (), adjusted_self, context); return checker.check (*ref); } @@ -377,7 +374,7 @@ TypeCheckMethodCallExpr::check (FnType &type) { location_t arg_locus = argument.get_locus (); - auto fnparam = type.param_at (i); + auto &fnparam = type.param_at (i); HIR::Pattern *fn_param_pattern = fnparam.first; BaseType *param_ty = fnparam.second; location_t param_locus diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc index 83d0e355f35d..976f6af12a55 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.cc +++ b/gcc/rust/typecheck/rust-tyty-subst.cc @@ -630,7 +630,7 @@ SubstitutionRef::get_mappings_from_generic_args ( for (auto &binding : args.get_binding_args ()) { BaseType *resolved - = Resolver::TypeCheckType::Resolve (binding.get_type ().get ()); + = Resolver::TypeCheckType::Resolve (binding.get_type ()); if (resolved == nullptr || resolved->get_kind () == TyTy::TypeKind::ERROR) { @@ -696,7 +696,7 @@ SubstitutionRef::get_mappings_from_generic_args ( std::vector mappings = used_arguments.get_mappings (); for (auto &arg : args.get_type_args ()) { - BaseType *resolved = Resolver::TypeCheckType::Resolve (arg.get ()); + BaseType *resolved = Resolver::TypeCheckType::Resolve (*arg); if (resolved == nullptr || resolved->get_kind () == TyTy::TypeKind::ERROR) { return SubstitutionArgumentMappings::error (); diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 660931f8b945..19196f72c479 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -31,6 +31,7 @@ #include "rust-hir-type-bounds.h" #include "options.h" +#include namespace Rust { namespace TyTy { @@ -595,7 +596,8 @@ BaseType::monomorphized_clone () const { std::vector> cloned_params; for (auto &p : fn->get_params ()) - cloned_params.push_back ({p.first, p.second->monomorphized_clone ()}); + cloned_params.push_back (std::pair ( + p.first, p.second->monomorphized_clone ())); BaseType *retty = fn->get_return_type ()->monomorphized_clone (); return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (), @@ -1341,9 +1343,10 @@ VariantDef::variant_type_string (VariantType type) } VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, - RustIdent ident, HIR::Expr *discriminant) + RustIdent ident, + std::unique_ptr &&discriminant) : id (id), defid (defid), identifier (identifier), ident (ident), - discriminant (discriminant) + discriminant (std::move (discriminant)) { type = VariantType::NUM; @@ -1352,34 +1355,15 @@ VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, VariantType type, - HIR::Expr *discriminant, + std::unique_ptr &&discriminant, std::vector fields) : id (id), defid (defid), identifier (identifier), ident (ident), type (type), - discriminant (discriminant), fields (fields) + discriminant (std::move (discriminant)), fields (fields) { rust_assert ((type == VariantType::NUM && fields.empty ()) || (type == VariantType::TUPLE || type == VariantType::STRUCT)); } -VariantDef::VariantDef (const VariantDef &other) - : id (other.id), defid (other.defid), identifier (other.identifier), - ident (other.ident), type (other.type), discriminant (other.discriminant), - fields (other.fields) -{} - -VariantDef & -VariantDef::operator= (const VariantDef &other) -{ - id = other.id; - identifier = other.identifier; - type = other.type; - discriminant = other.discriminant; - fields = other.fields; - ident = other.ident; - - return *this; -} - VariantDef & VariantDef::get_error_node () { @@ -1475,11 +1459,11 @@ VariantDef::lookup_field (const std::string &lookup, return false; } -HIR::Expr * -VariantDef::get_discriminant () const +HIR::Expr & +VariantDef::get_discriminant () { rust_assert (discriminant != nullptr); - return discriminant; + return *discriminant; } std::string @@ -1533,8 +1517,8 @@ VariantDef::clone () const for (auto &f : fields) cloned_fields.push_back ((StructFieldType *) f->clone ()); - return new VariantDef (id, defid, identifier, ident, type, discriminant, - cloned_fields); + return new VariantDef (id, defid, identifier, ident, type, + discriminant->clone_expr (), cloned_fields); } VariantDef * @@ -1544,8 +1528,8 @@ VariantDef::monomorphized_clone () const for (auto &f : fields) cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ()); - return new VariantDef (id, defid, identifier, ident, type, discriminant, - cloned_fields); + return new VariantDef (id, defid, identifier, ident, type, + discriminant->clone_expr (), cloned_fields); } const RustIdent & @@ -1895,7 +1879,7 @@ FnType::as_string () const std::string params_str = ""; for (auto ¶m : params) { - auto pattern = param.first; + auto &pattern = param.first; auto ty = param.second; params_str += pattern->as_string () + " " + ty->as_string (); params_str += ","; @@ -1965,7 +1949,8 @@ FnType::clone () const { std::vector> cloned_params; for (auto &p : params) - cloned_params.push_back ({p.first, p.second->clone ()}); + cloned_params.push_back ( + std::pair (p.first, p.second->clone ())); return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (), ident, flags, abi, std::move (cloned_params), diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index d96502d67d05..c20a172f2b35 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -28,6 +28,9 @@ #include "rust-tyty-subst.h" #include "rust-tyty-region.h" #include "rust-system.h" +#include "rust-hir.h" + +#include namespace Rust { @@ -567,16 +570,12 @@ class VariantDef static std::string variant_type_string (VariantType type); VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, - HIR::Expr *discriminant); + std::unique_ptr &&discriminant); VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, - VariantType type, HIR::Expr *discriminant, + VariantType type, std::unique_ptr &&discriminant, std::vector fields); - VariantDef (const VariantDef &other); - - VariantDef &operator= (const VariantDef &other); - static VariantDef &get_error_node (); bool is_error () const; @@ -597,7 +596,7 @@ class VariantDef bool lookup_field (const std::string &lookup, StructFieldType **field_lookup, size_t *index) const; - HIR::Expr *get_discriminant () const; + HIR::Expr &get_discriminant (); std::string as_string () const; @@ -616,7 +615,7 @@ class VariantDef RustIdent ident; VariantType type; // can either be a structure or a discriminant value - HIR::Expr *discriminant; + std::unique_ptr discriminant; std::vector fields; }; @@ -787,8 +786,8 @@ class FnType : public CallableTypeInterface, public SubstitutionRef : CallableTypeInterface (ref, ref, TypeKind::FNDEF, ident, refs), SubstitutionRef (std::move (subst_refs), substitution_argument_mappings, region_constraints), - params (std::move (params)), type (type), flags (flags), - identifier (identifier), id (id), abi (abi) + params (params), type (type), flags (flags), identifier (identifier), + id (id), abi (abi) { LocalDefId local_def_id = id.localDefId; rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID); @@ -804,8 +803,8 @@ class FnType : public CallableTypeInterface, public SubstitutionRef : CallableTypeInterface (ref, ty_ref, TypeKind::FNDEF, ident, refs), SubstitutionRef (std::move (subst_refs), substitution_argument_mappings, region_constraints), - params (params), type (type), flags (flags), identifier (identifier), - id (id), abi (abi) + params (std::move (params)), type (type), flags (flags), + identifier (identifier), id (id), abi (abi) { LocalDefId local_def_id = id.localDefId; rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 16a838d9c07c..65981095e699 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -459,7 +459,7 @@ Mappings::insert_hir_impl_block (HIR::ImplBlock *item) auto id = item->get_mappings ().get_hirid (); rust_assert (!lookup_hir_impl_block (id)); - HirId impl_type_id = item->get_type ()->get_mappings ().get_hirid (); + HirId impl_type_id = item->get_type ().get_mappings ().get_hirid (); hirImplBlockMappings[id] = item; hirImplBlockTypeMappings[impl_type_id] = item; insert_node_to_hir (item->get_mappings ().get_nodeid (), id); From 2bdd72a6ea74877933f06c02294e563a0aca6f37 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Fri, 8 Mar 2024 14:42:10 +0100 Subject: [PATCH 02/11] Refactor HIR with optionals, references & newtypes The HIR made heavy use of pair and other unamed types which can be difficult to read. gcc/rust/ChangeLog: * backend/rust-compile-base.cc: Use FnParam getter. * backend/rust-compile-expr.cc (CompileExpr::visit): Likewise. * backend/rust-compile-intrinsic.cc: Likewise. * backend/rust-compile-type.cc: Likewise. * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Only visit childrens if not missing. * checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Use a reference instead of a raw pointer. * hir/tree/rust-hir-expr.h: Add presence function for return expression. * hir/tree/rust-hir-item.h: Remove take_param_name. * hir/tree/rust-hir.h: Make mapping getter const. * typecheck/rust-hir-dot-operator.cc (MethodResolver::Select): Use getter. * typecheck/rust-hir-type-check-expr.cc: Likewise. * typecheck/rust-hir-type-check-implitem.cc: Use FnParam vector instead of std::pair of Pattern and BaseType. * typecheck/rust-hir-type-check-item.cc: Likewise. * typecheck/rust-hir-type-check.cc: Likewise. * typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Use getters. (TypeCheckMethodCallExpr::check): Likewise. * typecheck/rust-tyty-cmp.h: Likewise. * typecheck/rust-tyty.cc: Use FnParam. * typecheck/rust-tyty.h (class FnParam): Add FnParam to handle function parameters instead of handling std::pairs. * typecheck/rust-unify.cc (UnifyRules::expect_fndef): Use getters. (UnifyRules::expect_fnptr): Likewise. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/backend/rust-compile-base.cc | 2 +- gcc/rust/backend/rust-compile-expr.cc | 4 +- gcc/rust/backend/rust-compile-intrinsic.cc | 17 ++--- gcc/rust/backend/rust-compile-type.cc | 4 +- .../errors/privacy/rust-privacy-reporter.cc | 11 +-- gcc/rust/checks/errors/rust-unsafe-checker.cc | 2 +- gcc/rust/hir/tree/rust-hir-expr.h | 2 + gcc/rust/hir/tree/rust-hir-item.h | 2 - gcc/rust/hir/tree/rust-hir.h | 5 +- gcc/rust/typecheck/rust-hir-dot-operator.cc | 2 +- .../typecheck/rust-hir-type-check-expr.cc | 4 +- .../typecheck/rust-hir-type-check-implitem.cc | 28 +++----- .../typecheck/rust-hir-type-check-item.cc | 6 +- gcc/rust/typecheck/rust-hir-type-check.cc | 18 ++--- gcc/rust/typecheck/rust-tyty-call.cc | 16 ++--- gcc/rust/typecheck/rust-tyty-cmp.h | 6 +- gcc/rust/typecheck/rust-tyty.cc | 30 ++++---- gcc/rust/typecheck/rust-tyty.h | 69 ++++++++++++------- gcc/rust/typecheck/rust-unify.cc | 6 +- 19 files changed, 122 insertions(+), 112 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 966ff527c15a..d272374cadc3 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -711,7 +711,7 @@ HIRCompileBase::compile_function ( for (auto &referenced_param : function_params) { auto &tyty_param = fntype->param_at (i++); - auto param_tyty = tyty_param.second; + auto param_tyty = tyty_param.get_type (); auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); location_t param_locus = referenced_param.get_locus (); diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 7d41f635716e..35a6f7044090 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -1267,7 +1267,7 @@ CompileExpr::visit (HIR::CallExpr &expr) const TyTy::FnType *fn = static_cast (base); auto ¶m = fn->param_at (index); - *result = param.second; + *result = param.get_type (); return true; }; @@ -1401,7 +1401,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr) // assignments are coercion sites so lets convert the rvalue if // necessary, offset from the already adjusted implicit self bool ok; - TyTy::BaseType *expected = fntype->param_at (i + 1).second; + TyTy::BaseType *expected = fntype->param_at (i + 1).get_type (); TyTy::BaseType *actual = nullptr; ok = ctx->get_tyctx ()->lookup_type ( diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index 0ffc3634e130..08b64439dab6 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -315,13 +315,13 @@ compile_fn_params (Context *ctx, TyTy::FnType *fntype, tree fndecl, { for (auto &parm : fntype->get_params ()) { - auto &referenced_param = parm.first; - auto ¶m_tyty = parm.second; + auto &referenced_param = parm.get_pattern (); + auto param_tyty = parm.get_type (); auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); - location_t param_locus = referenced_param->get_locus (); + location_t param_locus = referenced_param.get_locus (); Bvariable *compiled_param_var - = CompileFnParam::compile (ctx, fndecl, *referenced_param, + = CompileFnParam::compile (ctx, fndecl, referenced_param, compiled_param_type, param_locus); compiled_param_variables->push_back (compiled_param_var); @@ -496,9 +496,10 @@ transmute_handler (Context *ctx, TyTy::FnType *fntype) rust_error_at (fntype->get_locus (), "cannot transmute between types of different sizes, or " "dependently-sized types"); - rust_inform (fntype->get_ident ().locus, "source type: %qs (%lu bits)", - fntype->get_params ().at (0).second->as_string ().c_str (), - (unsigned long) source_size); + rust_inform ( + fntype->get_ident ().locus, "source type: %qs (%lu bits)", + fntype->get_params ().at (0).get_type ()->as_string ().c_str (), + (unsigned long) source_size); rust_inform (fntype->get_ident ().locus, "target type: %qs (%lu bits)", fntype->get_return_type ()->as_string ().c_str (), (unsigned long) target_size); @@ -1226,7 +1227,7 @@ assume_handler (Context *ctx, TyTy::FnType *fntype) // TODO: make sure this is actually helping the compiler optimize rust_assert (fntype->get_params ().size () == 1); - rust_assert (fntype->param_at (0).second->get_kind () + rust_assert (fntype->param_at (0).get_type ()->get_kind () == TyTy::TypeKind::BOOL); tree lookup = NULL_TREE; diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index e01335247143..c6d249e341dd 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -208,12 +208,12 @@ TyTyResolveCompile::visit (const TyTy::FnType &type) for (auto ¶m_pair : type.get_params ()) { - auto param_tyty = param_pair.second; + auto param_tyty = param_pair.get_type (); auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty, trait_object_mode); auto compiled_param = Backend::typed_identifier ( - param_pair.first->as_string (), compiled_param_type, + param_pair.get_pattern ().as_string (), compiled_param_type, ctx->get_mappings ().lookup_location (param_tyty->get_ref ())); parameters.push_back (compiled_param); diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index a5e2f85418f7..39212bf97f51 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -497,8 +497,8 @@ PrivacyReporter::visit (HIR::BlockExpr &expr) for (auto &stmt : expr.get_statements ()) stmt->accept_vis (*this); - auto &last_expr = expr.get_final_expr (); - last_expr.accept_vis (*this); + if (expr.has_final_expr ()) + expr.get_final_expr ().accept_vis (*this); } void @@ -508,8 +508,8 @@ PrivacyReporter::visit (HIR::ContinueExpr &) void PrivacyReporter::visit (HIR::BreakExpr &expr) { - auto &break_expr = expr.get_expr (); - break_expr.accept_vis (*this); + if (expr.has_break_expr ()) + expr.get_expr ().accept_vis (*this); } void @@ -551,7 +551,8 @@ PrivacyReporter::visit (HIR::RangeToInclExpr &) void PrivacyReporter::visit (HIR::ReturnExpr &expr) { - expr.get_expr ().accept_vis (*this); + if (expr.has_expr ()) + expr.get_expr ().accept_vis (*this); } void diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 10181b7574f7..d1ebd15197bd 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -482,7 +482,7 @@ UnsafeChecker::visit (MethodCallExpr &expr) context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (), &method_type); - auto fn = *static_cast (method_type); + auto &fn = static_cast (*method_type); auto method = mappings.lookup_hir_implitem (fn.get_ref ()); if (!unsafe_context.is_in_context () && method) diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index c4a821e98643..c0d10c2b97f1 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -2238,6 +2238,7 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; } + bool has_final_expr () { return expr != nullptr; } Expr &get_final_expr () { return *expr; } std::vector > &get_statements () { return statements; } @@ -2785,6 +2786,7 @@ class ReturnExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; + bool has_expr () { return return_expr != nullptr; } Expr &get_expr () { return *return_expr; } ExprType get_expression_type () const override final diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index ba554d545c2a..92e8fc3712e1 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -549,8 +549,6 @@ struct FunctionParam Pattern &get_param_name () { return *param_name; } - std::unique_ptr take_param_name () { return std::move (param_name); } - Type &get_type () { return *type; } const Analysis::NodeMapping &get_mappings () const { return mappings; } diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 44dcc8e768e2..05d855c0a7db 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -465,7 +465,10 @@ class Type : public Node, public FullVisitable virtual void accept_vis (HIRTypeVisitor &vis) = 0; - virtual Analysis::NodeMapping get_mappings () const { return mappings; } + virtual const Analysis::NodeMapping &get_mappings () const + { + return mappings; + } virtual location_t get_locus () const { return locus; } protected: diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.cc b/gcc/rust/typecheck/rust-hir-dot-operator.cc index 38535ccfd743..31034eed2fbf 100644 --- a/gcc/rust/typecheck/rust-hir-dot-operator.cc +++ b/gcc/rust/typecheck/rust-hir-dot-operator.cc @@ -62,7 +62,7 @@ MethodResolver::Select (std::set &candidates, for (size_t i = 0; i < arguments.size (); i++) { TyTy::BaseType *arg = arguments.at (i); - TyTy::BaseType *param = fn.get_params ().at (i + 1).second; + TyTy::BaseType *param = fn.get_params ().at (i + 1).get_type (); TyTy::BaseType *coerced = try_coercion (0, TyTy::TyWithLocation (param), TyTy::TyWithLocation (arg), UNDEF_LOCATION); diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index f9dace10c8c7..8bb5058d3fc1 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -1795,7 +1795,7 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, // typecheck the self unify_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (fnparam.second), + TyTy::TyWithLocation (fnparam.get_type ()), TyTy::TyWithLocation (adjusted_self), expr.get_locus ()); if (rhs == nullptr) { @@ -1806,7 +1806,7 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type, rust_assert (type->num_params () == 2); auto &fnparam = type->param_at (1); unify_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (fnparam.second), + TyTy::TyWithLocation (fnparam.get_type ()), TyTy::TyWithLocation (rhs), expr.get_locus ()); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index 79afbd528da2..6a748f4666b2 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -130,8 +130,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector > params; - std::unique_ptr param_pattern = nullptr; + std::vector params; for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on @@ -143,14 +142,12 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - param_pattern = Rust::make_unique ( + auto param_pattern = Rust::make_unique ( HIR::IdentifierPattern (mapping, param.get_param_name (), UNDEF_LOCATION, false, Mutability::Imm, std::unique_ptr (nullptr))); - params.push_back ( - std::pair (param_pattern.get (), - param_tyty)); + params.push_back (TyTy::FnParam (std::move (param_pattern), param_tyty)); context->insert_type (param.get_mappings (), param_tyty); @@ -375,8 +372,7 @@ TypeCheckImplItem::visit (HIR::Function &function) function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector > params; - std::unique_ptr self_pattern = nullptr; + std::vector params; if (function.is_method ()) { // these are implicit mappings and not used @@ -390,10 +386,11 @@ TypeCheckImplItem::visit (HIR::Function &function) // reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self_param (); // FIXME: which location should be used for Rust::Identifier for `self`? - self_pattern = Rust::make_unique ( - HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (), - self_param.is_ref (), self_param.get_mut (), - std::unique_ptr (nullptr))); + std::unique_ptr self_pattern + = Rust::make_unique ( + HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (), + self_param.is_ref (), self_param.get_mut (), + std::unique_ptr (nullptr))); // might have a specified type TyTy::BaseType *self_type = nullptr; @@ -450,9 +447,7 @@ TypeCheckImplItem::visit (HIR::Function &function) } context->insert_type (self_param.get_mappings (), self_type); - params.push_back ( - std::pair (self_pattern.get (), - self_type)); + params.push_back (TyTy::FnParam (std::move (self_pattern), self_type)); } for (auto ¶m : function.get_function_params ()) @@ -464,8 +459,7 @@ TypeCheckImplItem::visit (HIR::Function &function) TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); params.push_back ( - std::pair (¶m.get_param_name (), - param_tyty)); + TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); } tl::optional canonical_path; diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index 2a5922c10d7f..12784d2650c5 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -574,15 +574,15 @@ TypeCheckItem::visit (HIR::Function &function) function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector> params; + std::vector params; for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on auto param_tyty = TypeCheckType::Resolve (param.get_type ()); - params.emplace_back (¶m.get_param_name (), param_tyty); - context->insert_type (param.get_mappings (), param_tyty); TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); + params.push_back ( + TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); } auto path = CanonicalPath::create_empty (); diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index 48e9c0579515..a30408c0b7f7 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -222,9 +222,8 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector > params; + std::vector params; - std::unique_ptr self_pattern = nullptr; if (function.is_method ()) { // these are implicit mappings and not used @@ -238,7 +237,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const // for compilation to know parameter names. The types are ignored // but we reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self (); - self_pattern + std::unique_ptr self_pattern = Rust::make_unique (HIR::IdentifierPattern ( mapping, {"self"}, self_param.get_locus (), self_param.is_ref (), self_param.is_mut () ? Mutability::Mut : Mutability::Imm, @@ -290,21 +289,18 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const } context->insert_type (self_param.get_mappings (), self_type); - params.push_back ( - std::pair (self_pattern.get (), - self_type)); + params.push_back (TyTy::FnParam (std::move (self_pattern), self_type)); } for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on auto param_tyty = TypeCheckType::Resolve (param.get_type ()); - params.push_back ( - std::pair (¶m.get_param_name (), - param_tyty)); - context->insert_type (param.get_mappings (), param_tyty); TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); + // FIXME: Should we take the name ? Use a shared pointer instead ? + params.push_back ( + TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); } auto &mappings = Analysis::Mappings::get (); @@ -332,7 +328,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const function.get_function_name ().as_string (), ident, function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG : TyTy::FnType::FNTYPE_DEFAULT_FLAGS, - ABI::RUST, params, ret_type, substitutions, + ABI::RUST, std::move (params), ret_type, substitutions, TyTy::SubstitutionArgumentMappings::empty ( context->get_lifetime_resolver ().get_num_bound_regions ()), region_constraints); diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 36a6b3d858d8..da1c383b2ca2 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -152,12 +152,12 @@ TypeCheckCallExpr::visit (FnType &type) if (i < type.num_params ()) { auto &fnparam = type.param_at (i); - auto &fn_param_pattern = fnparam.first; - BaseType *param_ty = fnparam.second; + auto &fn_param_pattern = fnparam.get_pattern (); + BaseType *param_ty = fnparam.get_type (); location_t param_locus - = fn_param_pattern == nullptr + = fnparam.has_pattern () ? mappings.lookup_location (param_ty->get_ref ()) - : fn_param_pattern->get_locus (); + : fn_param_pattern.get_locus (); HirId coercion_side_id = argument->get_mappings ().get_hirid (); auto resolved_argument_type @@ -375,12 +375,12 @@ TypeCheckMethodCallExpr::check (FnType &type) location_t arg_locus = argument.get_locus (); auto &fnparam = type.param_at (i); - HIR::Pattern *fn_param_pattern = fnparam.first; - BaseType *param_ty = fnparam.second; + HIR::Pattern &fn_param_pattern = fnparam.get_pattern (); + BaseType *param_ty = fnparam.get_type (); location_t param_locus - = fn_param_pattern == nullptr + = fnparam.has_pattern () ? mappings.lookup_location (param_ty->get_ref ()) - : fn_param_pattern->get_locus (); + : fn_param_pattern.get_locus (); auto argument_expr_tyty = argument.get_argument_type (); HirId coercion_side_id = argument.get_mappings ().get_hirid (); diff --git a/gcc/rust/typecheck/rust-tyty-cmp.h b/gcc/rust/typecheck/rust-tyty-cmp.h index d4552153dea8..95cbb6f6418d 100644 --- a/gcc/rust/typecheck/rust-tyty-cmp.h +++ b/gcc/rust/typecheck/rust-tyty-cmp.h @@ -735,8 +735,8 @@ class FnCmp : public BaseCmp for (size_t i = 0; i < base->num_params (); i++) { - auto a = base->param_at (i).second; - auto b = type.param_at (i).second; + auto a = base->param_at (i).get_type (); + auto b = type.param_at (i).get_type (); if (!a->can_eq (b, emit_error_flag)) { @@ -831,7 +831,7 @@ class FnptrCmp : public BaseCmp for (size_t i = 0; i < base->num_params (); i++) { auto this_param = base->get_param_type_at (i); - auto other_param = type.param_at (i).second; + auto other_param = type.param_at (i).get_type (); if (!this_param->can_eq (other_param, emit_error_flag)) { BaseCmp::visit (type); diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 19196f72c479..ec910b2e6740 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -594,10 +594,9 @@ BaseType::monomorphized_clone () const } else if (auto fn = x->try_as ()) { - std::vector> cloned_params; + std::vector cloned_params; for (auto &p : fn->get_params ()) - cloned_params.push_back (std::pair ( - p.first, p.second->monomorphized_clone ())); + cloned_params.push_back (p.clone ()); BaseType *retty = fn->get_return_type ()->monomorphized_clone (); return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (), @@ -688,7 +687,7 @@ BaseType::is_concrete () const { for (const auto ¶m : fn->get_params ()) { - if (!param.second->is_concrete ()) + if (!param.get_type ()->is_concrete ()) return false; } return fn->get_return_type ()->is_concrete (); @@ -1879,9 +1878,9 @@ FnType::as_string () const std::string params_str = ""; for (auto ¶m : params) { - auto &pattern = param.first; - auto ty = param.second; - params_str += pattern->as_string () + " " + ty->as_string (); + auto &pattern = param.get_pattern (); + auto ty = param.get_type (); + params_str += pattern.as_string () + " " + ty->as_string (); params_str += ","; } @@ -1902,7 +1901,7 @@ FnType::is_equal (const BaseType &other) const if (get_kind () != other.get_kind ()) return false; - auto other2 = static_cast (other); + auto &other2 = static_cast (other); if (get_identifier ().compare (other2.get_identifier ()) != 0) return false; @@ -1936,8 +1935,8 @@ FnType::is_equal (const BaseType &other) const for (size_t i = 0; i < num_params (); i++) { - auto lhs = param_at (i).second; - auto rhs = other2.param_at (i).second; + auto lhs = param_at (i).get_type (); + auto rhs = other2.param_at (i).get_type (); if (!lhs->is_equal (*rhs)) return false; } @@ -1947,10 +1946,9 @@ FnType::is_equal (const BaseType &other) const BaseType * FnType::clone () const { - std::vector> cloned_params; + std::vector cloned_params; for (auto &p : params) - cloned_params.push_back ( - std::pair (p.first, p.second->clone ())); + cloned_params.push_back (p.clone ()); return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (), ident, flags, abi, std::move (cloned_params), @@ -2024,7 +2022,7 @@ FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) for (auto ¶m : fn->get_params ()) { - auto fty = param.second; + auto fty = param.get_type (); bool is_param_ty = fty->get_kind () == TypeKind::PARAM; if (is_param_ty) @@ -2043,7 +2041,7 @@ FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) { auto new_field = argt->clone (); new_field->set_ref (fty->get_ref ()); - param.second = new_field; + param.set_type (new_field); } else { @@ -2067,7 +2065,7 @@ FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) auto new_field = concrete->clone (); new_field->set_ref (fty->get_ref ()); - param.second = new_field; + param.set_type (new_field); } } diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index c20a172f2b35..2a8786169821 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -766,6 +766,34 @@ class ADTType : public BaseType, public SubstitutionRef ReprOptions repr; }; +class FnParam +{ +public: + FnParam (std::unique_ptr pattern, BaseType *type) + : pattern (std::move (pattern)), type (type) + {} + + FnParam (const FnParam &) = delete; + FnParam (FnParam &&) = default; + FnParam &operator= (FnParam &&) = default; + + HIR::Pattern &get_pattern () { return *pattern; } + const HIR::Pattern &get_pattern () const { return *pattern; } + + bool has_pattern () { return pattern != nullptr; } + BaseType *get_type () const { return type; } + void set_type (BaseType *new_type) { type = new_type; } + + FnParam clone () const + { + return FnParam (pattern->clone_pattern (), type->monomorphized_clone ()); + } + +private: + std::unique_ptr pattern; + BaseType *type; +}; + class FnType : public CallableTypeInterface, public SubstitutionRef { public: @@ -777,25 +805,23 @@ class FnType : public CallableTypeInterface, public SubstitutionRef static const uint8_t FNTYPE_IS_VARADIC_FLAG = 0X04; FnType (HirId ref, DefId id, std::string identifier, RustIdent ident, - uint8_t flags, ABI abi, - std::vector> params, - BaseType *type, std::vector subst_refs, + uint8_t flags, ABI abi, std::vector params, BaseType *type, + std::vector subst_refs, SubstitutionArgumentMappings substitution_argument_mappings, RegionConstraints region_constraints, std::set refs = std::set ()) : CallableTypeInterface (ref, ref, TypeKind::FNDEF, ident, refs), SubstitutionRef (std::move (subst_refs), substitution_argument_mappings, region_constraints), - params (params), type (type), flags (flags), identifier (identifier), - id (id), abi (abi) + params (std::move (params)), type (type), flags (flags), + identifier (identifier), id (id), abi (abi) { LocalDefId local_def_id = id.localDefId; rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID); } FnType (HirId ref, HirId ty_ref, DefId id, std::string identifier, - RustIdent ident, uint8_t flags, ABI abi, - std::vector> params, + RustIdent ident, uint8_t flags, ABI abi, std::vector params, BaseType *type, std::vector subst_refs, SubstitutionArgumentMappings substitution_argument_mappings, RegionConstraints region_constraints, @@ -810,6 +836,9 @@ class FnType : public CallableTypeInterface, public SubstitutionRef rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID); } + FnType (const FnType &) = delete; + FnType (FnType &&) = default; + void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -843,28 +872,16 @@ class FnType : public CallableTypeInterface, public SubstitutionRef BaseType *get_self_type () const { rust_assert (is_method ()); - return param_at (0).second; + return param_at (0).get_type (); } - std::vector> &get_params () - { - return params; - } + std::vector &get_params () { return params; } - const std::vector> &get_params () const - { - return params; - } + const std::vector &get_params () const { return params; } - std::pair ¶m_at (size_t idx) - { - return params.at (idx); - } + FnParam ¶m_at (size_t idx) { return params.at (idx); } - const std::pair ¶m_at (size_t idx) const - { - return params.at (idx); - } + const FnParam ¶m_at (size_t idx) const { return params.at (idx); } BaseType *clone () const final override; @@ -881,7 +898,7 @@ class FnType : public CallableTypeInterface, public SubstitutionRef WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override { - return param_at (index).second; + return param_at (index).get_type (); } WARN_UNUSED_RESULT BaseType *get_return_type () const override @@ -890,7 +907,7 @@ class FnType : public CallableTypeInterface, public SubstitutionRef } private: - std::vector> params; + std::vector params; BaseType *type; uint8_t flags; std::string identifier; diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc index 8163d55b3c3a..d5344036fcc1 100644 --- a/gcc/rust/typecheck/rust-unify.cc +++ b/gcc/rust/typecheck/rust-unify.cc @@ -929,8 +929,8 @@ UnifyRules::expect_fndef (TyTy::FnType *ltype, TyTy::BaseType *rtype) for (size_t i = 0; i < ltype->num_params (); i++) { - auto a = ltype->param_at (i).second; - auto b = type.param_at (i).second; + auto a = ltype->param_at (i).get_type (); + auto b = type.param_at (i).get_type (); auto unified_param = UnifyRules::Resolve (TyTy::TyWithLocation (a), @@ -1069,7 +1069,7 @@ UnifyRules::expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype) for (size_t i = 0; i < ltype->num_params (); i++) { auto this_param = ltype->get_param_type_at (i); - auto other_param = type.param_at (i).second; + auto other_param = type.param_at (i).get_type (); auto unified_param = UnifyRules::Resolve (TyTy::TyWithLocation (this_param), From 0c4a3b1842d8bc149271562e47a64ca6acfaeb47 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Tue, 15 Oct 2024 15:22:56 +0200 Subject: [PATCH 03/11] Refactor hir to avoid raw pointers and unneeded fwd Refactor the hir tree files to remove raw pointer usage and most forward declarations. Move implementation out of headers and split headers into smaller and more manageable units. gcc/rust/ChangeLog: * Make-lang.in: Add new files. * hir/tree/rust-hir-item.h: Move Item definition and remove implementations to their corresponding cc file. * hir/tree/rust-hir-expr.h: Move implementation to the corresponding cc file. * hir/tree/rust-hir-path.h: Likewise. * hir/tree/rust-hir-pattern.h: Likewise. * hir/tree/rust-hir-stmt.h: Likewise. * hir/tree/rust-hir-type.h: Likewise. * hir/tree/rust-hir-visitor.h: Likewise. * hir/tree/rust-hir.h: Likewise. * hir/tree/rust-hir.cc (Crate::Crate): Add implementations from Crate and remove ConstGenericParam implementations to move them to their own file. * hir/tree/rust-hir-attrs.h: New file. * hir/tree/rust-hir-bound-abstract.h: New file. * hir/tree/rust-hir-bound.h: New file. * hir/tree/rust-hir-expr-abstract.h: New file. * hir/tree/rust-hir-expr.cc: New file. * hir/tree/rust-hir-generic-param.cc: New file. * hir/tree/rust-hir-generic-param.h: New file. * hir/tree/rust-hir-item.cc: New file. * hir/tree/rust-hir-literal.h: New file. * hir/tree/rust-hir-node.h: New file. * hir/tree/rust-hir-path.cc: New file. * hir/tree/rust-hir-pattern-abstract.h: New file. * hir/tree/rust-hir-simple-path.h: New file. * hir/tree/rust-hir-stmt.cc: New file. * hir/tree/rust-hir-trait-bound.h: New file. * hir/tree/rust-hir-type-abstract.cc: New file. * hir/tree/rust-hir-type-abstract.h: New file. * hir/tree/rust-hir-type-no-bounds.h: New file. * hir/tree/rust-hir-type.cc: New file. * hir/tree/rust-hir-visibility.h: New file. * hir/tree/rust-hir-visitable.h: New file. * checks/lints/rust-lint-marklive.h: Use References. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Reformat vectors. * hir/rust-hir-dump.cc (Dump::visit): Use reference. * typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve): Use references. * typecheck/rust-tyty-bounds.cc: Likewise. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/Make-lang.in | 7 + gcc/rust/checks/lints/rust-lint-marklive.h | 4 +- gcc/rust/hir/rust-ast-lower-expr.cc | 19 +- gcc/rust/hir/rust-hir-dump.cc | 4 +- gcc/rust/hir/tree/rust-hir-attrs.h | 56 + gcc/rust/hir/tree/rust-hir-bound-abstract.h | 65 + gcc/rust/hir/tree/rust-hir-bound.h | 94 ++ gcc/rust/hir/tree/rust-hir-expr-abstract.h | 174 ++ gcc/rust/hir/tree/rust-hir-expr.cc | 1478 +++++++++++++++++ gcc/rust/hir/tree/rust-hir-expr.h | 1353 +++------------ gcc/rust/hir/tree/rust-hir-generic-param.cc | 95 ++ gcc/rust/hir/tree/rust-hir-generic-param.h | 186 +++ gcc/rust/hir/tree/rust-hir-item.cc | 1017 ++++++++++++ gcc/rust/hir/tree/rust-hir-item.h | 977 ++--------- gcc/rust/hir/tree/rust-hir-literal.h | 78 + gcc/rust/hir/tree/rust-hir-node.h | 63 + gcc/rust/hir/tree/rust-hir-path.cc | 383 +++++ gcc/rust/hir/tree/rust-hir-path.h | 346 +--- gcc/rust/hir/tree/rust-hir-pattern-abstract.h | 82 + gcc/rust/hir/tree/rust-hir-pattern.h | 5 +- gcc/rust/hir/tree/rust-hir-simple-path.h | 64 + gcc/rust/hir/tree/rust-hir-stmt.cc | 98 ++ gcc/rust/hir/tree/rust-hir-stmt.h | 105 +- gcc/rust/hir/tree/rust-hir-trait-bound.h | 87 + gcc/rust/hir/tree/rust-hir-type-abstract.cc | 32 + gcc/rust/hir/tree/rust-hir-type-abstract.h | 80 + gcc/rust/hir/tree/rust-hir-type-no-bounds.h | 58 + gcc/rust/hir/tree/rust-hir-type.cc | 289 ++++ gcc/rust/hir/tree/rust-hir-type.h | 306 +--- gcc/rust/hir/tree/rust-hir-visibility.h | 80 + gcc/rust/hir/tree/rust-hir-visitable.h | 41 + gcc/rust/hir/tree/rust-hir-visitor.h | 1 - gcc/rust/hir/tree/rust-hir.cc | 56 +- gcc/rust/hir/tree/rust-hir.h | 816 +-------- .../typecheck/rust-hir-type-check-struct.cc | 30 +- gcc/rust/typecheck/rust-tyty-bounds.cc | 27 +- 36 files changed, 5214 insertions(+), 3442 deletions(-) create mode 100644 gcc/rust/hir/tree/rust-hir-attrs.h create mode 100644 gcc/rust/hir/tree/rust-hir-bound-abstract.h create mode 100644 gcc/rust/hir/tree/rust-hir-bound.h create mode 100644 gcc/rust/hir/tree/rust-hir-expr-abstract.h create mode 100644 gcc/rust/hir/tree/rust-hir-expr.cc create mode 100644 gcc/rust/hir/tree/rust-hir-generic-param.cc create mode 100644 gcc/rust/hir/tree/rust-hir-generic-param.h create mode 100644 gcc/rust/hir/tree/rust-hir-item.cc create mode 100644 gcc/rust/hir/tree/rust-hir-literal.h create mode 100644 gcc/rust/hir/tree/rust-hir-node.h create mode 100644 gcc/rust/hir/tree/rust-hir-path.cc create mode 100644 gcc/rust/hir/tree/rust-hir-pattern-abstract.h create mode 100644 gcc/rust/hir/tree/rust-hir-simple-path.h create mode 100644 gcc/rust/hir/tree/rust-hir-stmt.cc create mode 100644 gcc/rust/hir/tree/rust-hir-trait-bound.h create mode 100644 gcc/rust/hir/tree/rust-hir-type-abstract.cc create mode 100644 gcc/rust/hir/tree/rust-hir-type-abstract.h create mode 100644 gcc/rust/hir/tree/rust-hir-type-no-bounds.h create mode 100644 gcc/rust/hir/tree/rust-hir-type.cc create mode 100644 gcc/rust/hir/tree/rust-hir-visibility.h create mode 100644 gcc/rust/hir/tree/rust-hir-visitable.h diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 7f04136fe637..6de0a20b6a0a 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -178,6 +178,13 @@ GRS_OBJS = \ rust/rust-polonius.o\ rust/rust-hir-dot-operator.o \ rust/rust-hir-path-probe.o \ + rust/rust-hir-path.o \ + rust/rust-hir-type.o \ + rust/rust-hir-expr.o \ + rust/rust-hir-type-abstract.o \ + rust/rust-hir-item.o \ + rust/rust-hir-stmt.o \ + rust/rust-hir-generic-param.o \ rust/rust-type-util.o \ rust/rust-coercion.o \ rust/rust-casts.o \ diff --git a/gcc/rust/checks/lints/rust-lint-marklive.h b/gcc/rust/checks/lints/rust-lint-marklive.h index a1e8c110a3d3..8cb65ef957fe 100644 --- a/gcc/rust/checks/lints/rust-lint-marklive.h +++ b/gcc/rust/checks/lints/rust-lint-marklive.h @@ -247,7 +247,7 @@ class MarkLive : public MarkLiveBase stct.get_struct_name ().accept_vis (*this); if (stct.has_struct_base ()) { - stct.struct_base->base_struct->accept_vis (*this); + stct.get_struct_base ().get_base ().accept_vis (*this); } } @@ -258,7 +258,7 @@ class MarkLive : public MarkLiveBase void visit (HIR::StructExprStructBase &stct) override { - stct.get_struct_base ()->base_struct->accept_vis (*this); + stct.get_struct_base ().get_base ().accept_vis (*this); } void visit (HIR::Module &module) override diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc b/gcc/rust/hir/rust-ast-lower-expr.cc index 9bb20ca73f18..fb24a72efa70 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.cc +++ b/gcc/rust/hir/rust-ast-lower-expr.cc @@ -17,6 +17,7 @@ // . #include "rust-ast-lower-expr.h" +#include "optional.h" #include "rust-ast-lower-base.h" #include "rust-ast-lower-block.h" #include "rust-ast-lower-struct-field-expr.h" @@ -77,7 +78,7 @@ ASTLoweringExpr::visit (AST::TupleIndexExpr &expr) void ASTLoweringExpr::visit (AST::TupleExpr &expr) { - std::vector > tuple_elements; + std::vector> tuple_elements; for (auto &e : expr.get_tuple_elems ()) { HIR::Expr *t = ASTLoweringExpr::translate (*e); @@ -174,7 +175,7 @@ ASTLoweringExpr::visit (AST::CallExpr &expr) HIR::Expr *func = ASTLoweringExpr::translate (expr.get_function_expr ()); auto const &in_params = expr.get_params (); - std::vector > params; + std::vector> params; for (auto ¶m : in_params) { auto trans = ASTLoweringExpr::translate (*param); @@ -200,7 +201,7 @@ ASTLoweringExpr::visit (AST::MethodCallExpr &expr) HIR::Expr *receiver = ASTLoweringExpr::translate (expr.get_receiver_expr ()); auto const &in_params = expr.get_params (); - std::vector > params; + std::vector> params; for (auto ¶m : in_params) { auto trans = ASTLoweringExpr::translate (*param); @@ -290,7 +291,7 @@ ASTLoweringExpr::visit (AST::ArrayIndexExpr &expr) void ASTLoweringExpr::visit (AST::ArrayElemsValues &elems) { - std::vector > elements; + std::vector> elements; for (auto &elem : elems.get_values ()) { HIR::Expr *translated_elem = ASTLoweringExpr::translate (*elem); @@ -511,16 +512,17 @@ ASTLoweringExpr::visit (AST::StructExprStructFields &struct_expr) HIR::PathInExpression copied_path (*path); delete path; - HIR::StructBase *base = nullptr; + tl::optional> base = tl::nullopt; if (struct_expr.has_struct_base ()) { HIR::Expr *translated_base = ASTLoweringExpr::translate ( struct_expr.get_struct_base ().get_base_struct ()); - base = new HIR::StructBase (std::unique_ptr (translated_base)); + base = tl::optional (Rust::make_unique ( + std::unique_ptr (translated_base))); } auto const &in_fields = struct_expr.get_fields (); - std::vector > fields; + std::vector> fields; for (auto &field : in_fields) { HIR::StructExprField *translated @@ -535,7 +537,8 @@ ASTLoweringExpr::visit (AST::StructExprStructFields &struct_expr) translated = new HIR::StructExprStructFields (mapping, copied_path, std::move (fields), - struct_expr.get_locus (), base, + struct_expr.get_locus (), + std::move (base), struct_expr.get_inner_attrs (), struct_expr.get_outer_attrs ()); } diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc index 1f56d5e2539f..f84e1dac808f 100644 --- a/gcc/rust/hir/rust-hir-dump.cc +++ b/gcc/rust/hir/rust-hir-dump.cc @@ -1171,7 +1171,7 @@ Dump::visit (StructExprStructFields &e) if (!e.has_struct_base ()) put_field ("struct_base", "none"); else - put_field ("struct_base", e.get_struct_base ()->as_string ()); + put_field ("struct_base", e.get_struct_base ().as_string ()); end ("StructExprStructFields"); } @@ -1182,7 +1182,7 @@ Dump::visit (StructExprStructBase &e) begin ("StructExprStructBase"); do_structexprstruct (e); - put_field ("struct_base", e.get_struct_base ()->as_string ()); + put_field ("struct_base", e.get_struct_base ().as_string ()); end ("StructExprStructBase"); } diff --git a/gcc/rust/hir/tree/rust-hir-attrs.h b/gcc/rust/hir/tree/rust-hir-attrs.h new file mode 100644 index 000000000000..3e2b1d87cf8d --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-attrs.h @@ -0,0 +1,56 @@ + +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_ATTRS_H +#define RUST_HIR_ATTRS_H + +#include "rust-ast.h" + +namespace Rust { +namespace HIR { + +class WithOuterAttrs +{ +protected: + AST::AttrVec outer_attrs; + +public: + AST::AttrVec &get_outer_attrs () { return outer_attrs; } + const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } + + WithOuterAttrs (AST::AttrVec outer_attrs) + : outer_attrs (std::move (outer_attrs)){}; +}; + +class WithInnerAttrs +{ +protected: + AST::AttrVec inner_attrs; + +public: + AST::AttrVec get_inner_attrs () const { return inner_attrs; } + + WithInnerAttrs (AST::AttrVec inner_attrs) + : inner_attrs (std::move (inner_attrs)){}; +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-bound-abstract.h b/gcc/rust/hir/tree/rust-hir-bound-abstract.h new file mode 100644 index 000000000000..ffc915be919d --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-bound-abstract.h @@ -0,0 +1,65 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_BOUND_ABSTRACT_H +#define RUST_HIR_BOUND_ABSTRACT_H + +#include "rust-hir-visitable.h" +#include "rust-system.h" +#include "rust-hir-map.h" + +namespace Rust { +namespace HIR { + +/* Abstract base class representing a type param bound - Lifetime and TraitBound + * extends it */ +class TypeParamBound : public FullVisitable +{ +public: + using FullVisitable::accept_vis; + enum BoundType + { + LIFETIME, + TRAITBOUND + }; + + virtual ~TypeParamBound () {} + + // Unique pointer custom clone function + std::unique_ptr clone_type_param_bound () const + { + return std::unique_ptr (clone_type_param_bound_impl ()); + } + + virtual std::string as_string () const = 0; + + virtual Analysis::NodeMapping get_mappings () const = 0; + + virtual location_t get_locus () const = 0; + + virtual BoundType get_bound_type () const = 0; + +protected: + // Clone function implementation as pure virtual method + virtual TypeParamBound *clone_type_param_bound_impl () const = 0; +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-bound.h b/gcc/rust/hir/tree/rust-hir-bound.h new file mode 100644 index 000000000000..78bb133f6d4b --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-bound.h @@ -0,0 +1,94 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_BOUND_H +#define RUST_HIR_BOUND_H + +#include "rust-hir-bound-abstract.h" +#include "rust-common.h" +#include "rust-hir-path.h" + +namespace Rust { +namespace HIR { + +// Represents a lifetime (and is also a kind of type param bound) +class Lifetime : public TypeParamBound +{ +private: + AST::Lifetime::LifetimeType lifetime_type; + std::string lifetime_name; + location_t locus; + Analysis::NodeMapping mappings; + +public: + // Constructor + Lifetime (Analysis::NodeMapping mapping, AST::Lifetime::LifetimeType type, + std::string name, location_t locus) + : lifetime_type (type), lifetime_name (std::move (name)), locus (locus), + mappings (mapping) + {} + + // Returns true if the lifetime is in an error state. + bool is_error () const + { + return lifetime_type == AST::Lifetime::LifetimeType::NAMED + && lifetime_name.empty (); + } + + static Lifetime error () + { + return Lifetime (Analysis::NodeMapping::get_error (), + AST::Lifetime::LifetimeType::NAMED, "", UNDEF_LOCATION); + } + + std::string as_string () const override; + + void accept_vis (HIRFullVisitor &vis) override; + + WARN_UNUSED_RESULT const std::string &get_name () const + { + return lifetime_name; + } + + AST::Lifetime::LifetimeType get_lifetime_type () const + { + return lifetime_type; + } + + location_t get_locus () const override final { return locus; } + + Analysis::NodeMapping get_mappings () const override final + { + return mappings; + } + + BoundType get_bound_type () const final override { return LIFETIME; } + +protected: + /* Use covariance to implement clone function as returning this object rather + * than base */ + Lifetime *clone_type_param_bound_impl () const override + { + return new Lifetime (*this); + } +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-expr-abstract.h b/gcc/rust/hir/tree/rust-hir-expr-abstract.h new file mode 100644 index 000000000000..ecf9bd1af5d1 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-expr-abstract.h @@ -0,0 +1,174 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_EXPR_ABSTRACT_H +#define RUST_HIR_EXPR_ABSTRACT_H + +#include "rust-ast.h" +#include "rust-hir-visitable.h" +#include "rust-hir-node.h" + +namespace Rust { +namespace HIR { + +// Base expression HIR node - abstract +class Expr : public Node, virtual public FullVisitable +{ +public: + using FullVisitable::accept_vis; + +protected: + AST::AttrVec outer_attrs; + Analysis::NodeMapping mappings; + +public: + enum BlockType + { + WITH_BLOCK, + WITHOUT_BLOCK, + }; + + enum ExprType + { + Lit, + Operator, + Grouped, + Array, + ArrayIndex, + Tuple, + TupleIdx, + Struct, + Call, + MethodCall, + FieldAccess, + Closure, + Block, + Continue, + Break, + Range, + Return, + UnsafeBlock, + BaseLoop, + If, + IfLet, + Match, + Await, + AsyncBlock, + Path, + InlineAsm, + }; + + BaseKind get_hir_kind () override final { return Node::BaseKind::EXPR; } + + const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } + + // Unique pointer custom clone function + std::unique_ptr clone_expr () const + { + return std::unique_ptr (clone_expr_impl ()); + } + + // TODO: make pure virtual if move out outer attributes to derived classes + virtual std::string as_string () const; + + virtual ~Expr () {} + + virtual location_t get_locus () const = 0; + + const Analysis::NodeMapping &get_mappings () const { return mappings; } + + // Clone function implementation as pure virtual method + virtual Expr *clone_expr_impl () const = 0; + + virtual BlockType get_block_expr_type () const = 0; + + virtual ExprType get_expression_type () const = 0; + + virtual void accept_vis (HIRExpressionVisitor &vis) = 0; + +protected: + // Constructor + Expr (Analysis::NodeMapping mappings, + AST::AttrVec outer_attribs = AST::AttrVec ()); + + // TODO: think of less hacky way to implement this kind of thing + // Sets outer attributes. + void set_outer_attrs (AST::AttrVec outer_attrs_to_set) + { + outer_attrs = std::move (outer_attrs_to_set); + } +}; + +// HIR node for an expression without an accompanying block - abstract +class ExprWithoutBlock : public Expr +{ +protected: + // Constructor + ExprWithoutBlock (Analysis::NodeMapping mappings, + AST::AttrVec outer_attribs = AST::AttrVec ()); + + // pure virtual clone implementation + virtual ExprWithoutBlock *clone_expr_without_block_impl () const = 0; + + /* Save having to specify two clone methods in derived classes by making expr + * clone return exprwithoutblock clone. Hopefully won't affect performance too + * much. */ + ExprWithoutBlock *clone_expr_impl () const override + { + return clone_expr_without_block_impl (); + } + +public: + // Unique pointer custom clone function + std::unique_ptr clone_expr_without_block () const + { + return std::unique_ptr (clone_expr_without_block_impl ()); + } + + BlockType get_block_expr_type () const final override + { + return BlockType::WITHOUT_BLOCK; + }; +}; + +// Base path expression HIR node - abstract +class PathExpr : public ExprWithoutBlock +{ +protected: + PathExpr (Analysis::NodeMapping mappings, AST::AttrVec outer_attribs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)) + {} + +public: + /* Replaces the outer attributes of this path expression with the given outer + * attributes. */ + void replace_outer_attrs (AST::AttrVec outer_attrs) + { + set_outer_attrs (std::move (outer_attrs)); + } + + ExprType get_expression_type () const final override + { + return ExprType::Path; + } +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-expr.cc b/gcc/rust/hir/tree/rust-hir-expr.cc new file mode 100644 index 000000000000..6c9a7a475e71 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-expr.cc @@ -0,0 +1,1478 @@ +// Copyright (C) 2020-2024 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 +// . + +#include "rust-hir-expr.h" +#include "rust-operators.h" +#include "rust-hir-stmt.h" + +namespace Rust { +namespace HIR { + +Expr::Expr (Analysis::NodeMapping mappings, AST::AttrVec outer_attribs) + : outer_attrs (std::move (outer_attribs)), mappings (std::move (mappings)) +{} + +ExprWithoutBlock::ExprWithoutBlock (Analysis::NodeMapping mappings, + AST::AttrVec outer_attribs) + : Expr (std::move (mappings), std::move (outer_attribs)) +{} + +LoopLabel::LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label, + location_t locus) + : label (std::move (loop_label)), locus (locus), mappings (mapping) +{} + +ExprWithBlock::ExprWithBlock (Analysis::NodeMapping mappings, + AST::AttrVec outer_attrs) + : Expr (std::move (mappings), std::move (outer_attrs)) +{} + +LiteralExpr::LiteralExpr (Analysis::NodeMapping mappings, + std::string value_as_string, Literal::LitType type, + PrimitiveCoreType type_hint, location_t locus, + AST::AttrVec outer_attrs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), + literal (std::move (value_as_string), type, type_hint), locus (locus) +{} + +LiteralExpr::LiteralExpr (Analysis::NodeMapping mappings, Literal literal, + location_t locus, AST::AttrVec outer_attrs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), + literal (std::move (literal)), locus (locus) +{} + +OperatorExpr::OperatorExpr (Analysis::NodeMapping mappings, + std::unique_ptr main_or_left_expr, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + locus (locus), main_or_left_expr (std::move (main_or_left_expr)) +{} + +OperatorExpr::OperatorExpr (OperatorExpr const &other) + : ExprWithoutBlock (other), locus (other.locus), + main_or_left_expr (other.main_or_left_expr->clone_expr ()) +{} + +OperatorExpr & +OperatorExpr::operator= (OperatorExpr const &other) +{ + ExprWithoutBlock::operator= (other); + main_or_left_expr = other.main_or_left_expr->clone_expr (); + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +BorrowExpr::BorrowExpr (Analysis::NodeMapping mappings, + std::unique_ptr borrow_lvalue, Mutability mut, + AST::AttrVec outer_attribs, location_t locus) + : OperatorExpr (std::move (mappings), std::move (borrow_lvalue), + std::move (outer_attribs), locus), + mut (mut) +{} + +DereferenceExpr::DereferenceExpr (Analysis::NodeMapping mappings, + std::unique_ptr deref_lvalue, + AST::AttrVec outer_attribs, location_t locus) + : OperatorExpr (std::move (mappings), std::move (deref_lvalue), + std::move (outer_attribs), locus) +{} + +ErrorPropagationExpr::ErrorPropagationExpr ( + Analysis::NodeMapping mappings, std::unique_ptr potential_error_value, + AST::AttrVec outer_attribs, location_t locus) + : OperatorExpr (std::move (mappings), std::move (potential_error_value), + std::move (outer_attribs), locus) +{} + +NegationExpr::NegationExpr (Analysis::NodeMapping mappings, + std::unique_ptr negated_value, + ExprType expr_kind, AST::AttrVec outer_attribs, + location_t locus) + : OperatorExpr (std::move (mappings), std::move (negated_value), + std::move (outer_attribs), locus), + expr_type (expr_kind) +{} + +ArithmeticOrLogicalExpr::ArithmeticOrLogicalExpr ( + Analysis::NodeMapping mappings, std::unique_ptr left_value, + std::unique_ptr right_value, ExprType expr_kind, location_t locus) + : OperatorExpr (std::move (mappings), std::move (left_value), AST::AttrVec (), + locus), + expr_type (expr_kind), right_expr (std::move (right_value)) +{} + +ArithmeticOrLogicalExpr::ArithmeticOrLogicalExpr ( + ArithmeticOrLogicalExpr const &other) + : OperatorExpr (other), expr_type (other.expr_type), + right_expr (other.right_expr->clone_expr ()) +{} + +ArithmeticOrLogicalExpr & +ArithmeticOrLogicalExpr::operator= (ArithmeticOrLogicalExpr const &other) +{ + OperatorExpr::operator= (other); + // main_or_left_expr = other.main_or_left_expr->clone_expr(); + right_expr = other.right_expr->clone_expr (); + expr_type = other.expr_type; + + return *this; +} + +ComparisonExpr::ComparisonExpr (Analysis::NodeMapping mappings, + std::unique_ptr left_value, + std::unique_ptr right_value, + ExprType comparison_kind, location_t locus) + : OperatorExpr (std::move (mappings), std::move (left_value), AST::AttrVec (), + locus), + expr_type (comparison_kind), right_expr (std::move (right_value)) +{} + +ComparisonExpr::ComparisonExpr (ComparisonExpr const &other) + : OperatorExpr (other), expr_type (other.expr_type), + right_expr (other.right_expr->clone_expr ()) +{} + +ComparisonExpr & +ComparisonExpr::operator= (ComparisonExpr const &other) +{ + OperatorExpr::operator= (other); + // main_or_left_expr = other.main_or_left_expr->clone_expr(); + right_expr = other.right_expr->clone_expr (); + expr_type = other.expr_type; + // outer_attrs = other.outer_attrs; + + return *this; +} + +LazyBooleanExpr::LazyBooleanExpr (Analysis::NodeMapping mappings, + std::unique_ptr left_bool_expr, + std::unique_ptr right_bool_expr, + ExprType expr_kind, location_t locus) + : OperatorExpr (std::move (mappings), std::move (left_bool_expr), + AST::AttrVec (), locus), + expr_type (expr_kind), right_expr (std::move (right_bool_expr)) +{} + +LazyBooleanExpr::LazyBooleanExpr (LazyBooleanExpr const &other) + : OperatorExpr (other), expr_type (other.expr_type), + right_expr (other.right_expr->clone_expr ()) +{} + +LazyBooleanExpr & +LazyBooleanExpr::operator= (LazyBooleanExpr const &other) +{ + OperatorExpr::operator= (other); + // main_or_left_expr = other.main_or_left_expr->clone_expr(); + right_expr = other.right_expr->clone_expr (); + expr_type = other.expr_type; + + return *this; +} + +TypeCastExpr::TypeCastExpr (Analysis::NodeMapping mappings, + std::unique_ptr expr_to_cast, + std::unique_ptr type_to_cast_to, + location_t locus) + : OperatorExpr (std::move (mappings), std::move (expr_to_cast), + AST::AttrVec (), locus), + type_to_convert_to (std::move (type_to_cast_to)) +{} + +TypeCastExpr::TypeCastExpr (TypeCastExpr const &other) + : OperatorExpr (other), + type_to_convert_to (other.type_to_convert_to->clone_type ()) +{} + +TypeCastExpr & +TypeCastExpr::operator= (TypeCastExpr const &other) +{ + OperatorExpr::operator= (other); + // main_or_left_expr = other.main_or_left_expr->clone_expr(); + type_to_convert_to = other.type_to_convert_to->clone_type (); + + return *this; +} + +AssignmentExpr::AssignmentExpr (Analysis::NodeMapping mappings, + std::unique_ptr value_to_assign_to, + std::unique_ptr value_to_assign, + location_t locus) + : OperatorExpr (std::move (mappings), std::move (value_to_assign_to), + AST::AttrVec (), locus), + right_expr (std::move (value_to_assign)) +{} + +AssignmentExpr::AssignmentExpr (AssignmentExpr const &other) + : OperatorExpr (other), right_expr (other.right_expr->clone_expr ()) +{} + +AssignmentExpr & +AssignmentExpr::operator= (AssignmentExpr const &other) +{ + OperatorExpr::operator= (other); + // main_or_left_expr = other.main_or_left_expr->clone_expr(); + right_expr = other.right_expr->clone_expr (); + // outer_attrs = other.outer_attrs; + + return *this; +} + +CompoundAssignmentExpr::CompoundAssignmentExpr ( + Analysis::NodeMapping mappings, std::unique_ptr value_to_assign_to, + std::unique_ptr value_to_assign, ExprType expr_kind, location_t locus) + : OperatorExpr (std::move (mappings), std::move (value_to_assign_to), + AST::AttrVec (), locus), + expr_type (expr_kind), right_expr (std::move (value_to_assign)) +{} + +CompoundAssignmentExpr::CompoundAssignmentExpr ( + CompoundAssignmentExpr const &other) + : OperatorExpr (other), expr_type (other.expr_type), + right_expr (other.right_expr->clone_expr ()) +{} + +CompoundAssignmentExpr & +CompoundAssignmentExpr::operator= (CompoundAssignmentExpr const &other) +{ + OperatorExpr::operator= (other); + // main_or_left_expr = other.main_or_left_expr->clone_expr(); + right_expr = other.right_expr->clone_expr (); + expr_type = other.expr_type; + // outer_attrs = other.outer_attrs; + + return *this; +} + +GroupedExpr::GroupedExpr (Analysis::NodeMapping mappings, + std::unique_ptr parenthesised_expr, + AST::AttrVec inner_attribs, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + WithInnerAttrs (std::move (inner_attribs)), + expr_in_parens (std::move (parenthesised_expr)), locus (locus) +{} + +GroupedExpr::GroupedExpr (GroupedExpr const &other) + : ExprWithoutBlock (other), WithInnerAttrs (other.inner_attrs), + expr_in_parens (other.expr_in_parens->clone_expr ()), locus (other.locus) +{} + +GroupedExpr & +GroupedExpr::operator= (GroupedExpr const &other) +{ + ExprWithoutBlock::operator= (other); + inner_attrs = other.inner_attrs; + expr_in_parens = other.expr_in_parens->clone_expr (); + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +ArrayElemsValues::ArrayElemsValues (Analysis::NodeMapping mappings, + std::vector> elems) + : ArrayElems (mappings), values (std::move (elems)) +{} + +ArrayElemsValues::ArrayElemsValues (ArrayElemsValues const &other) + : ArrayElems (other) +{ + values.reserve (other.values.size ()); + for (const auto &e : other.values) + values.push_back (e->clone_expr ()); +} + +ArrayElemsValues & +ArrayElemsValues::operator= (ArrayElemsValues const &other) +{ + values.reserve (other.values.size ()); + for (const auto &e : other.values) + values.push_back (e->clone_expr ()); + + return *this; +} + +ArrayElemsCopied::ArrayElemsCopied (Analysis::NodeMapping mappings, + std::unique_ptr copied_elem, + std::unique_ptr copy_amount) + : ArrayElems (mappings), elem_to_copy (std::move (copied_elem)), + num_copies (std::move (copy_amount)) +{} + +ArrayElemsCopied::ArrayElemsCopied (ArrayElemsCopied const &other) + : ArrayElems (other), elem_to_copy (other.elem_to_copy->clone_expr ()), + num_copies (other.num_copies->clone_expr ()) +{} + +ArrayElemsCopied & +ArrayElemsCopied::operator= (ArrayElemsCopied const &other) +{ + elem_to_copy = other.elem_to_copy->clone_expr (); + num_copies = other.num_copies->clone_expr (); + + return *this; +} + +ArrayExpr::ArrayExpr (Analysis::NodeMapping mappings, + std::unique_ptr array_elems, + AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, + location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + WithInnerAttrs (std::move (inner_attribs)), + internal_elements (std::move (array_elems)), locus (locus) +{} + +ArrayExpr::ArrayExpr (ArrayExpr const &other) + : ExprWithoutBlock (other), WithInnerAttrs (other.inner_attrs), + locus (other.locus) +{ + if (other.has_array_elems ()) + internal_elements = other.internal_elements->clone_array_elems (); +} + +ArrayExpr & +ArrayExpr::operator= (ArrayExpr const &other) +{ + ExprWithoutBlock::operator= (other); + inner_attrs = other.inner_attrs; + if (other.has_array_elems ()) + internal_elements = other.internal_elements->clone_array_elems (); + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +ArrayIndexExpr::ArrayIndexExpr (Analysis::NodeMapping mappings, + std::unique_ptr array_expr, + std::unique_ptr array_index_expr, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + array_expr (std::move (array_expr)), + index_expr (std::move (array_index_expr)), locus (locus) +{} + +ArrayIndexExpr::ArrayIndexExpr (ArrayIndexExpr const &other) + : ExprWithoutBlock (other), array_expr (other.array_expr->clone_expr ()), + index_expr (other.index_expr->clone_expr ()), locus (other.locus) +{} + +ArrayIndexExpr & +ArrayIndexExpr::operator= (ArrayIndexExpr const &other) +{ + ExprWithoutBlock::operator= (other); + array_expr = other.array_expr->clone_expr (); + index_expr = other.index_expr->clone_expr (); + // outer_attrs = other.outer_attrs; + locus = other.locus; + + return *this; +} + +TupleExpr::TupleExpr (Analysis::NodeMapping mappings, + std::vector> tuple_elements, + AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, + location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + WithInnerAttrs (std::move (inner_attribs)), + tuple_elems (std::move (tuple_elements)), locus (locus) +{} + +TupleExpr::TupleExpr (TupleExpr const &other) + : ExprWithoutBlock (other), WithInnerAttrs (other.inner_attrs), + locus (other.locus) +{ + tuple_elems.reserve (other.tuple_elems.size ()); + for (const auto &e : other.tuple_elems) + tuple_elems.push_back (e->clone_expr ()); +} + +TupleExpr & +TupleExpr::operator= (TupleExpr const &other) +{ + ExprWithoutBlock::operator= (other); + inner_attrs = other.inner_attrs; + locus = other.locus; + + tuple_elems.reserve (other.tuple_elems.size ()); + for (const auto &e : other.tuple_elems) + tuple_elems.push_back (e->clone_expr ()); + + return *this; +} + +TupleIndexExpr::TupleIndexExpr (Analysis::NodeMapping mappings, + std::unique_ptr tuple_expr, + TupleIndex index, AST::AttrVec outer_attribs, + location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + tuple_expr (std::move (tuple_expr)), tuple_index (index), locus (locus) +{} + +TupleIndexExpr::TupleIndexExpr (TupleIndexExpr const &other) + : ExprWithoutBlock (other), tuple_expr (other.tuple_expr->clone_expr ()), + tuple_index (other.tuple_index), locus (other.locus) +{} + +TupleIndexExpr & +TupleIndexExpr::operator= (TupleIndexExpr const &other) +{ + ExprWithoutBlock::operator= (other); + tuple_expr = other.tuple_expr->clone_expr (); + tuple_index = other.tuple_index; + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +StructExpr::StructExpr (Analysis::NodeMapping mappings, + PathInExpression struct_path, + AST::AttrVec outer_attribs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + struct_name (std::move (struct_path)) +{} + +StructExprStruct::StructExprStruct (Analysis::NodeMapping mappings, + PathInExpression struct_path, + AST::AttrVec inner_attribs, + AST::AttrVec outer_attribs, + location_t locus) + : StructExpr (std::move (mappings), std::move (struct_path), + std::move (outer_attribs)), + WithInnerAttrs (std::move (inner_attribs)), locus (locus) +{} + +StructBase::StructBase (std::unique_ptr base_struct_ptr) + : base_struct (std::move (base_struct_ptr)) +{} + +StructBase::StructBase (StructBase const &other) +{ + /* HACK: gets around base_struct pointer being null (e.g. if no struct base + * exists) */ + if (other.base_struct != nullptr) + other.base_struct->clone_expr (); +} + +StructBase & +StructBase::operator= (StructBase const &other) +{ + base_struct = other.base_struct->clone_expr (); + + return *this; +} + +StructExprField::StructExprField (Analysis::NodeMapping mapping, + location_t locus) + : mappings (mapping), locus (locus) +{} + +StructExprFieldIdentifier::StructExprFieldIdentifier ( + Analysis::NodeMapping mapping, Identifier field_identifier, location_t locus) + : StructExprField (mapping, locus), field_name (std::move (field_identifier)) +{} + +StructExprFieldWithVal::StructExprFieldWithVal ( + Analysis::NodeMapping mapping, std::unique_ptr field_value, + location_t locus) + : StructExprField (mapping, locus), value (std::move (field_value)) +{} + +StructExprFieldWithVal::StructExprFieldWithVal ( + StructExprFieldWithVal const &other) + : StructExprField (other.mappings, other.locus), + value (other.value->clone_expr ()) +{} + +StructExprFieldWithVal & +StructExprFieldWithVal::operator= (StructExprFieldWithVal const &other) +{ + value = other.value->clone_expr (); + mappings = other.mappings; + locus = other.locus; + + return *this; +} + +StructExprFieldIdentifierValue::StructExprFieldIdentifierValue ( + Analysis::NodeMapping mapping, Identifier field_identifier, + std::unique_ptr field_value, location_t locus) + : StructExprFieldWithVal (mapping, std::move (field_value), locus), + field_name (std::move (field_identifier)) +{} + +StructExprFieldIndexValue::StructExprFieldIndexValue ( + Analysis::NodeMapping mapping, TupleIndex tuple_index, + std::unique_ptr field_value, location_t locus) + : StructExprFieldWithVal (mapping, std::move (field_value), locus), + index (tuple_index) +{} + +StructExprStructFields::StructExprStructFields ( + Analysis::NodeMapping mappings, PathInExpression struct_path, + std::vector> expr_fields, location_t locus, + tl::optional> base_struct, + AST::AttrVec inner_attribs = AST::AttrVec (), + AST::AttrVec outer_attribs = AST::AttrVec ()) + : StructExprStruct (std::move (mappings), std::move (struct_path), + std::move (inner_attribs), std::move (outer_attribs), + locus), + fields (std::move (expr_fields)), struct_base (std::move (base_struct)) +{} + +StructExprStructFields::StructExprStructFields ( + StructExprStructFields const &other) + : StructExprStruct (other), + struct_base ( + other.has_struct_base () ? tl::optional> ( + Rust::make_unique (*other.struct_base.value ())) + : tl::nullopt), + union_index (other.union_index) +{ + fields.reserve (other.fields.size ()); + for (const auto &e : other.fields) + fields.push_back (e->clone_struct_expr_field ()); +} + +StructExprStructFields & +StructExprStructFields::operator= (StructExprStructFields const &other) +{ + StructExprStruct::operator= (other); + struct_base = other.has_struct_base () + ? tl::optional> ( + Rust::make_unique (*other.struct_base.value ())) + : tl::nullopt; + union_index = other.union_index; + + fields.reserve (other.fields.size ()); + for (const auto &e : other.fields) + fields.push_back (e->clone_struct_expr_field ()); + + return *this; +} + +StructExprStructBase::StructExprStructBase (Analysis::NodeMapping mappings, + PathInExpression struct_path, + StructBase base_struct, + AST::AttrVec inner_attribs, + AST::AttrVec outer_attribs, + location_t locus) + : StructExprStruct (std::move (mappings), std::move (struct_path), + std::move (inner_attribs), std::move (outer_attribs), + locus), + struct_base (std::move (base_struct)) +{} + +CallExpr::CallExpr (Analysis::NodeMapping mappings, + std::unique_ptr function_expr, + std::vector> function_params, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + function (std::move (function_expr)), params (std::move (function_params)), + locus (locus) +{} + +CallExpr::CallExpr (CallExpr const &other) + : ExprWithoutBlock (other), function (other.function->clone_expr ()), + locus (other.locus) +/*, params(other.params),*/ { + params.reserve (other.params.size ()); + for (const auto &e : other.params) + params.push_back (e->clone_expr ()); +} + +CallExpr & +CallExpr::operator= (CallExpr const &other) +{ + ExprWithoutBlock::operator= (other); + function = other.function->clone_expr (); + locus = other.locus; + // params = other.params; + // outer_attrs = other.outer_attrs; + + params.reserve (other.params.size ()); + for (const auto &e : other.params) + params.push_back (e->clone_expr ()); + + return *this; +} + +MethodCallExpr::MethodCallExpr ( + Analysis::NodeMapping mappings, std::unique_ptr call_receiver, + PathExprSegment method_path, std::vector> method_params, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + receiver (std::move (call_receiver)), method_name (std::move (method_path)), + params (std::move (method_params)), locus (locus) +{} + +MethodCallExpr::MethodCallExpr (MethodCallExpr const &other) + : ExprWithoutBlock (other), receiver (other.receiver->clone_expr ()), + method_name (other.method_name), locus (other.locus) +/*, params(other.params),*/ { + params.reserve (other.params.size ()); + for (const auto &e : other.params) + params.push_back (e->clone_expr ()); +} + +MethodCallExpr & +MethodCallExpr::operator= (MethodCallExpr const &other) +{ + ExprWithoutBlock::operator= (other); + receiver = other.receiver->clone_expr (); + method_name = other.method_name; + locus = other.locus; + // params = other.params; + // outer_attrs = other.outer_attrs; + + params.reserve (other.params.size ()); + for (const auto &e : other.params) + params.push_back (e->clone_expr ()); + + return *this; +} + +FieldAccessExpr::FieldAccessExpr (Analysis::NodeMapping mappings, + std::unique_ptr field_access_receiver, + Identifier field_name, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + receiver (std::move (field_access_receiver)), + field (std::move (field_name)), locus (locus) +{} + +FieldAccessExpr::FieldAccessExpr (FieldAccessExpr const &other) + : ExprWithoutBlock (other), receiver (other.receiver->clone_expr ()), + field (other.field), locus (other.locus) +{} + +FieldAccessExpr & +FieldAccessExpr::operator= (FieldAccessExpr const &other) +{ + ExprWithoutBlock::operator= (other); + receiver = other.receiver->clone_expr (); + field = other.field; + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +ClosureParam::ClosureParam (std::unique_ptr param_pattern, + location_t locus, std::unique_ptr param_type, + std::vector outer_attrs) + : outer_attrs (std::move (outer_attrs)), pattern (std::move (param_pattern)), + type (std::move (param_type)), locus (locus) +{} + +ClosureParam::ClosureParam (ClosureParam const &other) + : pattern (other.pattern->clone_pattern ()) +{ + // guard to protect from null pointer dereference + if (other.pattern != nullptr) + pattern = other.pattern->clone_pattern (); + if (other.type != nullptr) + type = other.type->clone_type (); +} + +ClosureParam & +ClosureParam::operator= (ClosureParam const &other) +{ + outer_attrs = other.outer_attrs; + + // guard to protect from null pointer dereference + if (other.pattern != nullptr) + pattern = other.pattern->clone_pattern (); + else + pattern = nullptr; + if (other.type != nullptr) + type = other.type->clone_type (); + else + type = nullptr; + + return *this; +} + +ClosureExpr::ClosureExpr (Analysis::NodeMapping mappings, + std::vector closure_params, + std::unique_ptr closure_return_type, + std::unique_ptr closure_expr, bool has_move, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + has_move (has_move), params (std::move (closure_params)), locus (locus), + return_type (std::move (closure_return_type)), + expr (std::move (closure_expr)) +{} + +ClosureExpr::ClosureExpr (ClosureExpr const &other) + : ExprWithoutBlock (other.get_mappings (), other.get_outer_attrs ()) +{ + return_type + = other.has_return_type () ? other.return_type->clone_type () : nullptr; + expr = other.expr->clone_expr (); + params = other.params; + has_move = other.has_move; +} + +ClosureExpr & +ClosureExpr::operator= (ClosureExpr const &other) +{ + mappings = other.mappings; + return_type + = other.has_return_type () ? other.return_type->clone_type () : nullptr; + expr = other.expr->clone_expr (); + params = other.params; + has_move = other.has_move; + + return *this; +} + +BlockExpr::BlockExpr (Analysis::NodeMapping mappings, + std::vector> block_statements, + std::unique_ptr block_expr, bool tail_reachable, + AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, + LoopLabel label, location_t start_locus, + location_t end_locus) + : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), + WithInnerAttrs (std::move (inner_attribs)), + statements (std::move (block_statements)), expr (std::move (block_expr)), + tail_reachable (tail_reachable), label (std::move (label)), + start_locus (start_locus), end_locus (end_locus) +{} + +BlockExpr::BlockExpr (BlockExpr const &other) + : ExprWithBlock (other), /*statements(other.statements),*/ + WithInnerAttrs (other.inner_attrs), label (other.label), + start_locus (other.start_locus), end_locus (other.end_locus) +{ + // guard to protect from null pointer dereference + if (other.expr != nullptr) + expr = other.expr->clone_expr (); + + statements.reserve (other.statements.size ()); + for (const auto &e : other.statements) + statements.push_back (e->clone_stmt ()); +} + +BlockExpr & +BlockExpr::operator= (BlockExpr const &other) +{ + ExprWithBlock::operator= (other); + // statements = other.statements; + expr = other.expr->clone_expr (); + inner_attrs = other.inner_attrs; + start_locus = other.end_locus; + end_locus = other.end_locus; + // outer_attrs = other.outer_attrs; + + statements.reserve (other.statements.size ()); + for (const auto &e : other.statements) + statements.push_back (e->clone_stmt ()); + + return *this; +} + +ContinueExpr::ContinueExpr (Analysis::NodeMapping mappings, location_t locus, + Lifetime label, AST::AttrVec outer_attribs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + label (std::move (label)), locus (locus) +{} + +BreakExpr::BreakExpr (Analysis::NodeMapping mappings, location_t locus, + Lifetime break_label, std::unique_ptr expr_in_break, + AST::AttrVec outer_attribs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + label (std::move (break_label)), break_expr (std::move (expr_in_break)), + locus (locus) +{} + +BreakExpr::BreakExpr (BreakExpr const &other) + : ExprWithoutBlock (other), label (other.label), locus (other.locus) +{ + // guard to protect from null pointer dereference + if (other.break_expr != nullptr) + break_expr = other.break_expr->clone_expr (); +} + +BreakExpr & +BreakExpr::operator= (BreakExpr const &other) +{ + ExprWithoutBlock::operator= (other); + label = other.label; + break_expr = other.break_expr->clone_expr (); + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +RangeExpr::RangeExpr (Analysis::NodeMapping mappings, location_t locus) + : ExprWithoutBlock (std::move (mappings), AST::AttrVec ()), locus (locus) +{} + +RangeFromToExpr::RangeFromToExpr (Analysis::NodeMapping mappings, + std::unique_ptr range_from, + std::unique_ptr range_to, + location_t locus) + : RangeExpr (std::move (mappings), locus), from (std::move (range_from)), + to (std::move (range_to)) +{} + +RangeFromToExpr::RangeFromToExpr (RangeFromToExpr const &other) + : RangeExpr (other), from (other.from->clone_expr ()), + to (other.to->clone_expr ()) +{} + +RangeFromToExpr & +RangeFromToExpr::operator= (RangeFromToExpr const &other) +{ + RangeExpr::operator= (other); + from = other.from->clone_expr (); + to = other.to->clone_expr (); + + return *this; +} + +RangeFromExpr::RangeFromExpr (Analysis::NodeMapping mappings, + std::unique_ptr range_from, + location_t locus) + : RangeExpr (std::move (mappings), locus), from (std::move (range_from)) +{} + +RangeFromExpr::RangeFromExpr (RangeFromExpr const &other) + : RangeExpr (other), from (other.from->clone_expr ()) +{} + +RangeFromExpr & +RangeFromExpr::operator= (RangeFromExpr const &other) +{ + RangeExpr::operator= (other); + from = other.from->clone_expr (); + + return *this; +} + +RangeToExpr::RangeToExpr (Analysis::NodeMapping mappings, + std::unique_ptr range_to, location_t locus) + : RangeExpr (std::move (mappings), locus), to (std::move (range_to)) +{} + +RangeToExpr::RangeToExpr (RangeToExpr const &other) + : RangeExpr (other), to (other.to->clone_expr ()) +{} + +RangeToExpr & +RangeToExpr::operator= (RangeToExpr const &other) +{ + RangeExpr::operator= (other); + to = other.to->clone_expr (); + + return *this; +} + +RangeFullExpr::RangeFullExpr (Analysis::NodeMapping mappings, location_t locus) + : RangeExpr (std::move (mappings), locus) +{} + +RangeFromToInclExpr::RangeFromToInclExpr (Analysis::NodeMapping mappings, + std::unique_ptr range_from, + std::unique_ptr range_to, + location_t locus) + : RangeExpr (std::move (mappings), locus), from (std::move (range_from)), + to (std::move (range_to)) +{} + +RangeFromToInclExpr::RangeFromToInclExpr (RangeFromToInclExpr const &other) + : RangeExpr (other), from (other.from->clone_expr ()), + to (other.to->clone_expr ()) +{} + +RangeFromToInclExpr & +RangeFromToInclExpr::operator= (RangeFromToInclExpr const &other) +{ + RangeExpr::operator= (other); + from = other.from->clone_expr (); + to = other.to->clone_expr (); + + return *this; +} + +RangeToInclExpr::RangeToInclExpr (Analysis::NodeMapping mappings, + std::unique_ptr range_to, + location_t locus) + : RangeExpr (std::move (mappings), locus), to (std::move (range_to)) +{} + +RangeToInclExpr::RangeToInclExpr (RangeToInclExpr const &other) + : RangeExpr (other), to (other.to->clone_expr ()) +{} + +RangeToInclExpr & +RangeToInclExpr::operator= (RangeToInclExpr const &other) +{ + RangeExpr::operator= (other); + to = other.to->clone_expr (); + + return *this; +} + +ReturnExpr::ReturnExpr (Analysis::NodeMapping mappings, location_t locus, + std::unique_ptr returned_expr, + AST::AttrVec outer_attribs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + return_expr (std::move (returned_expr)), locus (locus) +{} + +ReturnExpr::ReturnExpr (ReturnExpr const &other) + : ExprWithoutBlock (other), locus (other.locus) +{ + // guard to protect from null pointer dereference + if (other.return_expr != nullptr) + return_expr = other.return_expr->clone_expr (); +} + +ReturnExpr & +ReturnExpr::operator= (ReturnExpr const &other) +{ + ExprWithoutBlock::operator= (other); + return_expr = other.return_expr->clone_expr (); + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +UnsafeBlockExpr::UnsafeBlockExpr (Analysis::NodeMapping mappings, + std::unique_ptr block_expr, + AST::AttrVec outer_attribs, location_t locus) + : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), + expr (std::move (block_expr)), locus (locus) +{} + +UnsafeBlockExpr::UnsafeBlockExpr (UnsafeBlockExpr const &other) + : ExprWithBlock (other), expr (other.expr->clone_block_expr ()), + locus (other.locus) +{} + +UnsafeBlockExpr & +UnsafeBlockExpr::operator= (UnsafeBlockExpr const &other) +{ + ExprWithBlock::operator= (other); + expr = other.expr->clone_block_expr (); + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +BaseLoopExpr::BaseLoopExpr (Analysis::NodeMapping mappings, + std::unique_ptr loop_block, + location_t locus, LoopLabel loop_label, + AST::AttrVec outer_attribs) + : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), + loop_label (std::move (loop_label)), loop_block (std::move (loop_block)), + locus (locus) +{} + +BaseLoopExpr::BaseLoopExpr (BaseLoopExpr const &other) + : ExprWithBlock (other), loop_label (other.loop_label), + loop_block (other.loop_block->clone_block_expr ()), locus (other.locus) +{} + +BaseLoopExpr & +BaseLoopExpr::operator= (BaseLoopExpr const &other) +{ + ExprWithBlock::operator= (other); + loop_block = other.loop_block->clone_block_expr (); + loop_label = other.loop_label; + locus = other.locus; + // outer_attrs = other.outer_attrs; + + return *this; +} + +LoopExpr::LoopExpr (Analysis::NodeMapping mappings, + std::unique_ptr loop_block, location_t locus, + LoopLabel loop_label, AST::AttrVec outer_attribs) + : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, + std::move (loop_label), std::move (outer_attribs)) +{} + +WhileLoopExpr::WhileLoopExpr (Analysis::NodeMapping mappings, + std::unique_ptr loop_condition, + std::unique_ptr loop_block, + location_t locus, LoopLabel loop_label, + AST::AttrVec outer_attribs) + : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, + std::move (loop_label), std::move (outer_attribs)), + condition (std::move (loop_condition)) +{} + +WhileLoopExpr::WhileLoopExpr (WhileLoopExpr const &other) + : BaseLoopExpr (other), condition (other.condition->clone_expr ()) +{} + +WhileLoopExpr & +WhileLoopExpr::operator= (WhileLoopExpr const &other) +{ + BaseLoopExpr::operator= (other); + condition = other.condition->clone_expr (); + // loop_block = other.loop_block->clone_block_expr(); + // loop_label = other.loop_label; + // outer_attrs = other.outer_attrs; + + return *this; +} + +WhileLetLoopExpr::WhileLetLoopExpr ( + Analysis::NodeMapping mappings, + std::vector> match_arm_patterns, + std::unique_ptr condition, std::unique_ptr loop_block, + location_t locus, LoopLabel loop_label, AST::AttrVec outer_attribs) + : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, + std::move (loop_label), std::move (outer_attribs)), + match_arm_patterns (std::move (match_arm_patterns)), + condition (std::move (condition)) +{} + +WhileLetLoopExpr::WhileLetLoopExpr (WhileLetLoopExpr const &other) + : BaseLoopExpr (other), + /*match_arm_patterns(other.match_arm_patterns),*/ condition ( + other.condition->clone_expr ()) +{ + match_arm_patterns.reserve (other.match_arm_patterns.size ()); + for (const auto &e : other.match_arm_patterns) + match_arm_patterns.push_back (e->clone_pattern ()); +} + +WhileLetLoopExpr & +WhileLetLoopExpr::operator= (WhileLetLoopExpr const &other) +{ + BaseLoopExpr::operator= (other); + // match_arm_patterns = other.match_arm_patterns; + condition = other.condition->clone_expr (); + // loop_block = other.loop_block->clone_block_expr(); + // loop_label = other.loop_label; + // outer_attrs = other.outer_attrs; + + match_arm_patterns.reserve (other.match_arm_patterns.size ()); + for (const auto &e : other.match_arm_patterns) + match_arm_patterns.push_back (e->clone_pattern ()); + + return *this; +} + +IfExpr::IfExpr (Analysis::NodeMapping mappings, std::unique_ptr condition, + std::unique_ptr if_block, location_t locus) + : ExprWithBlock (std::move (mappings), AST::AttrVec ()), + condition (std::move (condition)), if_block (std::move (if_block)), + locus (locus) +{} + +IfExpr::IfExpr (IfExpr const &other) + : ExprWithBlock (other), condition (other.condition->clone_expr ()), + if_block (other.if_block->clone_block_expr ()), locus (other.locus) +{} + +IfExpr & +IfExpr::operator= (IfExpr const &other) +{ + ExprWithBlock::operator= (other); + condition = other.condition->clone_expr (); + if_block = other.if_block->clone_block_expr (); + locus = other.locus; + + return *this; +} + +IfExprConseqElse::IfExprConseqElse (Analysis::NodeMapping mappings, + std::unique_ptr condition, + std::unique_ptr if_block, + std::unique_ptr else_block, + location_t locus) + : IfExpr (std::move (mappings), std::move (condition), std::move (if_block), + locus), + else_block (std::move (else_block)) +{} + +IfExprConseqElse::IfExprConseqElse (IfExprConseqElse const &other) + : IfExpr (other), else_block (other.else_block->clone_expr_with_block ()) +{} + +IfExprConseqElse & +IfExprConseqElse::operator= (IfExprConseqElse const &other) +{ + IfExpr::operator= (other); + // condition = other.condition->clone_expr(); + // if_block = other.if_block->clone_block_expr(); + else_block = other.else_block->clone_expr_with_block (); + + return *this; +} + +MatchArm::MatchArm (std::vector> match_arm_patterns, + location_t locus, std::unique_ptr guard_expr, + AST::AttrVec outer_attrs) + : outer_attrs (std::move (outer_attrs)), + match_arm_patterns (std::move (match_arm_patterns)), + guard_expr (std::move (guard_expr)), locus (locus) +{} + +MatchArm::MatchArm (MatchArm const &other) : outer_attrs (other.outer_attrs) +{ + // guard to protect from null pointer dereference + if (other.guard_expr != nullptr) + guard_expr = other.guard_expr->clone_expr (); + + match_arm_patterns.reserve (other.match_arm_patterns.size ()); + for (const auto &e : other.match_arm_patterns) + match_arm_patterns.push_back (e->clone_pattern ()); + + locus = other.locus; +} + +MatchArm & +MatchArm::operator= (MatchArm const &other) +{ + outer_attrs = other.outer_attrs; + + if (other.guard_expr != nullptr) + guard_expr = other.guard_expr->clone_expr (); + + match_arm_patterns.clear (); + match_arm_patterns.reserve (other.match_arm_patterns.size ()); + for (const auto &e : other.match_arm_patterns) + match_arm_patterns.push_back (e->clone_pattern ()); + + return *this; +} + +MatchCase::MatchCase (Analysis::NodeMapping mappings, MatchArm arm, + std::unique_ptr expr) + : mappings (mappings), arm (std::move (arm)), expr (std::move (expr)) +{} + +MatchCase::MatchCase (const MatchCase &other) + : mappings (other.mappings), arm (other.arm), expr (other.expr->clone_expr ()) +{} + +MatchCase & +MatchCase::operator= (const MatchCase &other) +{ + mappings = other.mappings; + arm = other.arm; + expr = other.expr->clone_expr (); + + return *this; +} + +MatchExpr::MatchExpr (Analysis::NodeMapping mappings, + std::unique_ptr branch_value, + std::vector match_arms, + AST::AttrVec inner_attrs, AST::AttrVec outer_attrs, + location_t locus) + : ExprWithBlock (std::move (mappings), std::move (outer_attrs)), + WithInnerAttrs (std::move (inner_attrs)), + branch_value (std::move (branch_value)), + match_arms (std::move (match_arms)), locus (locus) +{} + +MatchExpr::MatchExpr (MatchExpr const &other) + : ExprWithBlock (other), WithInnerAttrs (other.inner_attrs), + branch_value (other.branch_value->clone_expr ()), + match_arms (other.match_arms), locus (other.locus) +{ + /*match_arms.reserve (other.match_arms.size ()); + for (const auto &e : other.match_arms) + match_arms.push_back (e->clone_match_case ());*/ +} + +MatchExpr & +MatchExpr::operator= (MatchExpr const &other) +{ + ExprWithBlock::operator= (other); + branch_value = other.branch_value->clone_expr (); + inner_attrs = other.inner_attrs; + match_arms = other.match_arms; + // outer_attrs = other.outer_attrs; + locus = other.locus; + + /*match_arms.reserve (other.match_arms.size ()); + for (const auto &e : other.match_arms) + match_arms.push_back (e->clone_match_case ());*/ + + return *this; +} + +AwaitExpr::AwaitExpr (Analysis::NodeMapping mappings, + std::unique_ptr awaited_expr, + AST::AttrVec outer_attrs, location_t locus) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), + awaited_expr (std::move (awaited_expr)), locus (locus) +{} + +AwaitExpr::AwaitExpr (AwaitExpr const &other) + : ExprWithoutBlock (other), awaited_expr (other.awaited_expr->clone_expr ()), + locus (other.locus) +{} + +AwaitExpr & +AwaitExpr::operator= (AwaitExpr const &other) +{ + ExprWithoutBlock::operator= (other); + awaited_expr = other.awaited_expr->clone_expr (); + locus = other.locus; + + return *this; +} + +AsyncBlockExpr::AsyncBlockExpr (Analysis::NodeMapping mappings, + std::unique_ptr block_expr, + bool has_move, AST::AttrVec outer_attrs, + location_t locus) + : ExprWithBlock (std::move (mappings), std::move (outer_attrs)), + has_move (has_move), block_expr (std::move (block_expr)), locus (locus) +{} + +AsyncBlockExpr::AsyncBlockExpr (AsyncBlockExpr const &other) + : ExprWithBlock (other), has_move (other.has_move), + block_expr (other.block_expr->clone_block_expr ()), locus (other.locus) +{} + +AsyncBlockExpr & +AsyncBlockExpr::operator= (AsyncBlockExpr const &other) +{ + ExprWithBlock::operator= (other); + has_move = other.has_move; + block_expr = other.block_expr->clone_block_expr (); + locus = other.locus; + + return *this; +} + +OperatorExprMeta::OperatorExprMeta (HIR::CompoundAssignmentExpr &expr) + : node_mappings (expr.get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), + locus (expr.get_locus ()) +{} + +OperatorExprMeta::OperatorExprMeta (HIR::ArithmeticOrLogicalExpr &expr) + : node_mappings (expr.get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), + locus (expr.get_locus ()) +{} + +OperatorExprMeta::OperatorExprMeta (HIR::NegationExpr &expr) + : node_mappings (expr.get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), + locus (expr.get_locus ()) +{} + +OperatorExprMeta::OperatorExprMeta (HIR::DereferenceExpr &expr) + : node_mappings (expr.get_mappings ()), + lvalue_mappings (expr.get_expr ().get_mappings ()), + locus (expr.get_locus ()) +{} + +OperatorExprMeta::OperatorExprMeta (HIR::ArrayIndexExpr &expr) + : node_mappings (expr.get_mappings ()), + lvalue_mappings (expr.get_array_expr ().get_mappings ()), + locus (expr.get_locus ()) +{} + +AnonConst::AnonConst (NodeId id, std::unique_ptr expr) + : id (id), expr (std::move (expr)) +{ + rust_assert (this->expr != nullptr); +} + +AnonConst::AnonConst (const AnonConst &other) +{ + id = other.id; + expr = other.expr->clone_expr (); +} + +AnonConst +AnonConst::operator= (const AnonConst &other) +{ + id = other.id; + expr = other.expr->clone_expr (); + return *this; +} + +InlineAsmOperand::In::In ( + const tl::optional ®, + std::unique_ptr expr) + : reg (reg), expr (std::move (expr)) +{ + rust_assert (this->expr != nullptr); +} + +InlineAsmOperand::In::In (const struct In &other) +{ + reg = other.reg; + + expr = other.expr->clone_expr (); +} + +InlineAsmOperand::In +InlineAsmOperand::In::operator= (const struct In &other) +{ + reg = other.reg; + expr = other.expr->clone_expr (); + + return *this; +} + +InlineAsmOperand::Out::Out ( + tl::optional ®, bool late, + std::unique_ptr expr) + : reg (reg), late (late), expr (std::move (expr)) +{ + rust_assert (this->expr != nullptr); +} + +InlineAsmOperand::Out::Out (const struct Out &other) +{ + reg = other.reg; + late = other.late; + expr = other.expr->clone_expr (); +} + +InlineAsmOperand::Out +InlineAsmOperand::Out::operator= (const struct Out &other) +{ + reg = other.reg; + late = other.late; + expr = other.expr->clone_expr (); + return *this; +} + +InlineAsmOperand::InOut::InOut ( + tl::optional ®, bool late, + std::unique_ptr expr) + : reg (reg), late (late), expr (std::move (expr)) +{ + rust_assert (this->expr != nullptr); +} + +InlineAsmOperand::InOut::InOut (const struct InOut &other) +{ + reg = other.reg; + late = other.late; + expr = other.expr->clone_expr (); +} + +InlineAsmOperand::InOut +InlineAsmOperand::InOut::operator= (const struct InOut &other) +{ + reg = other.reg; + late = other.late; + expr = other.expr->clone_expr (); + + return *this; +} + +InlineAsmOperand::SplitInOut::SplitInOut ( + tl::optional ®, bool late, + std::unique_ptr in_expr, std::unique_ptr out_expr) + : reg (reg), late (late), in_expr (std::move (in_expr)), + out_expr (std::move (out_expr)) +{ + rust_assert (this->in_expr != nullptr); + rust_assert (this->out_expr != nullptr); +} + +InlineAsmOperand::SplitInOut::SplitInOut (const struct SplitInOut &other) +{ + reg = other.reg; + late = other.late; + in_expr = other.in_expr->clone_expr (); + out_expr = other.out_expr->clone_expr (); +} + +InlineAsmOperand::SplitInOut +InlineAsmOperand::SplitInOut::operator= (const struct SplitInOut &other) +{ + reg = other.reg; + late = other.late; + in_expr = other.in_expr->clone_expr (); + out_expr = other.out_expr->clone_expr (); + + return *this; +} + +InlineAsmOperand::Sym::Sym (std::unique_ptr expr) + : expr (std::move (expr)) +{ + rust_assert (this->expr != nullptr); +} + +InlineAsmOperand::Sym::Sym (const struct Sym &other) +{ + expr = std::unique_ptr (other.expr->clone_expr ()); +} + +InlineAsmOperand::Sym +InlineAsmOperand::Sym::operator= (const struct Sym &other) +{ + expr = std::unique_ptr (other.expr->clone_expr ()); + return *this; +} + +InlineAsmOperand::Label::Label (tl::optional label_name, + std::unique_ptr expr) + : expr (std::move (expr)) +{ + rust_assert (this->expr != nullptr); + if (label_name.has_value ()) + this->label_name = label_name.value (); +} + +InlineAsmOperand::Label::Label (const struct Label &other) +{ + expr = std::unique_ptr (other.expr->clone_expr ()); +} + +InlineAsmOperand::Label +InlineAsmOperand::Label::operator= (const struct Label &other) +{ + expr = std::unique_ptr (other.expr->clone_expr ()); + return *this; +} + +InlineAsm::InlineAsm (location_t locus, bool is_global_asm, + std::vector template_, + std::vector template_strs, + std::vector operands, + std::vector clobber_abi, + std::set options, + Analysis::NodeMapping mappings, + AST::AttrVec outer_attribs) + : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), + locus (locus), is_global_asm (is_global_asm), + template_ (std::move (template_)), + template_strs (std::move (template_strs)), operands (std::move (operands)), + clobber_abi (std::move (clobber_abi)), options (std::move (options)) +{} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index c0d10c2b97f1..e956108dc53f 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -19,12 +19,13 @@ #ifndef RUST_HIR_EXPR_H #define RUST_HIR_EXPR_H +#include "rust-hir-expr-abstract.h" +#include "rust-hir-literal.h" #include "rust-common.h" -#include "rust-ast-full-decls.h" -#include "rust-hir.h" -#include "rust-hir-path.h" -#include "rust-operators.h" +#include "rust-hir-bound.h" +#include "rust-hir-attrs.h" #include "rust-expr.h" + namespace Rust { namespace HIR { @@ -42,9 +43,7 @@ class LoopLabel /*: public Node*/ std::string as_string () const; LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label, - location_t locus) - : label (std::move (loop_label)), locus (locus), mappings (mapping) - {} + location_t locus); // Returns whether the LoopLabel is in an error state. bool is_error () const { return label.is_error (); } @@ -62,9 +61,7 @@ class ExprWithBlock : public Expr // TODO: should this mean that a BlockExpr should be a member variable? protected: ExprWithBlock (Analysis::NodeMapping mappings, - AST::AttrVec outer_attrs = AST::AttrVec ()) - : Expr (std::move (mappings), std::move (outer_attrs)) - {} + AST::AttrVec outer_attrs = AST::AttrVec ()); // pure virtual clone implementation virtual ExprWithBlock *clone_expr_with_block_impl () const = 0; @@ -106,16 +103,10 @@ class LiteralExpr : public ExprWithoutBlock LiteralExpr (Analysis::NodeMapping mappings, std::string value_as_string, Literal::LitType type, PrimitiveCoreType type_hint, - location_t locus, AST::AttrVec outer_attrs) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), - literal (std::move (value_as_string), type, type_hint), locus (locus) - {} + location_t locus, AST::AttrVec outer_attrs); LiteralExpr (Analysis::NodeMapping mappings, Literal literal, - location_t locus, AST::AttrVec outer_attrs) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), - literal (std::move (literal)), locus (locus) - {} + location_t locus, AST::AttrVec outer_attrs); // Unique pointer custom clone function std::unique_ptr clone_literal_expr () const @@ -180,27 +171,13 @@ class OperatorExpr : public ExprWithoutBlock // Constructor (only for initialisation of expr purposes) OperatorExpr (Analysis::NodeMapping mappings, std::unique_ptr main_or_left_expr, - AST::AttrVec outer_attribs, location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - locus (locus), main_or_left_expr (std::move (main_or_left_expr)) - {} + AST::AttrVec outer_attribs, location_t locus); // Copy constructor (only for initialisation of expr purposes) - OperatorExpr (OperatorExpr const &other) - : ExprWithoutBlock (other), locus (other.locus), - main_or_left_expr (other.main_or_left_expr->clone_expr ()) - {} + OperatorExpr (OperatorExpr const &other); // Overload assignment operator to deep copy expr - OperatorExpr &operator= (OperatorExpr const &other) - { - ExprWithoutBlock::operator= (other); - main_or_left_expr = other.main_or_left_expr->clone_expr (); - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + OperatorExpr &operator= (OperatorExpr const &other); // move constructors OperatorExpr (OperatorExpr &&other) = default; @@ -228,11 +205,7 @@ class BorrowExpr : public OperatorExpr BorrowExpr (Analysis::NodeMapping mappings, std::unique_ptr borrow_lvalue, Mutability mut, - AST::AttrVec outer_attribs, location_t locus) - : OperatorExpr (std::move (mappings), std::move (borrow_lvalue), - std::move (outer_attribs), locus), - mut (mut) - {} + AST::AttrVec outer_attribs, location_t locus); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; @@ -265,10 +238,7 @@ class DereferenceExpr : public OperatorExpr // Constructor calls OperatorExpr's protected constructor DereferenceExpr (Analysis::NodeMapping mappings, std::unique_ptr deref_lvalue, - AST::AttrVec outer_attribs, location_t locus) - : OperatorExpr (std::move (mappings), std::move (deref_lvalue), - std::move (outer_attribs), locus) - {} + AST::AttrVec outer_attribs, location_t locus); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; @@ -298,10 +268,7 @@ class ErrorPropagationExpr : public OperatorExpr // Constructor calls OperatorExpr's protected constructor ErrorPropagationExpr (Analysis::NodeMapping mappings, std::unique_ptr potential_error_value, - AST::AttrVec outer_attribs, location_t locus) - : OperatorExpr (std::move (mappings), std::move (potential_error_value), - std::move (outer_attribs), locus) - {} + AST::AttrVec outer_attribs, location_t locus); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; @@ -342,11 +309,7 @@ class NegationExpr : public OperatorExpr // Constructor calls OperatorExpr's protected constructor NegationExpr (Analysis::NodeMapping mappings, std::unique_ptr negated_value, ExprType expr_kind, - AST::AttrVec outer_attribs, location_t locus) - : OperatorExpr (std::move (mappings), std::move (negated_value), - std::move (outer_attribs), locus), - expr_type (expr_kind) - {} + AST::AttrVec outer_attribs, location_t locus); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; @@ -388,29 +351,14 @@ class ArithmeticOrLogicalExpr : public OperatorExpr ArithmeticOrLogicalExpr (Analysis::NodeMapping mappings, std::unique_ptr left_value, std::unique_ptr right_value, - ExprType expr_kind, location_t locus) - : OperatorExpr (std::move (mappings), std::move (left_value), - AST::AttrVec (), locus), - expr_type (expr_kind), right_expr (std::move (right_value)) - {} + ExprType expr_kind, location_t locus); // outer attributes not allowed // Copy constructor - probably required due to unique pointer - ArithmeticOrLogicalExpr (ArithmeticOrLogicalExpr const &other) - : OperatorExpr (other), expr_type (other.expr_type), - right_expr (other.right_expr->clone_expr ()) - {} + ArithmeticOrLogicalExpr (ArithmeticOrLogicalExpr const &other); // Overload assignment operator - ArithmeticOrLogicalExpr &operator= (ArithmeticOrLogicalExpr const &other) - { - OperatorExpr::operator= (other); - // main_or_left_expr = other.main_or_left_expr->clone_expr(); - right_expr = other.right_expr->clone_expr (); - expr_type = other.expr_type; - - return *this; - } + ArithmeticOrLogicalExpr &operator= (ArithmeticOrLogicalExpr const &other); // move constructors ArithmeticOrLogicalExpr (ArithmeticOrLogicalExpr &&other) = default; @@ -465,30 +413,14 @@ class ComparisonExpr : public OperatorExpr ComparisonExpr (Analysis::NodeMapping mappings, std::unique_ptr left_value, std::unique_ptr right_value, ExprType comparison_kind, - location_t locus) - : OperatorExpr (std::move (mappings), std::move (left_value), - AST::AttrVec (), locus), - expr_type (comparison_kind), right_expr (std::move (right_value)) - {} + location_t locus); // outer attributes not allowed // Copy constructor also calls OperatorExpr's protected constructor - ComparisonExpr (ComparisonExpr const &other) - : OperatorExpr (other), expr_type (other.expr_type), - right_expr (other.right_expr->clone_expr ()) - {} + ComparisonExpr (ComparisonExpr const &other); // Overload assignment operator to deep copy - ComparisonExpr &operator= (ComparisonExpr const &other) - { - OperatorExpr::operator= (other); - // main_or_left_expr = other.main_or_left_expr->clone_expr(); - right_expr = other.right_expr->clone_expr (); - expr_type = other.expr_type; - // outer_attrs = other.outer_attrs; - - return *this; - } + ComparisonExpr &operator= (ComparisonExpr const &other); // move constructors ComparisonExpr (ComparisonExpr &&other) = default; @@ -536,29 +468,14 @@ class LazyBooleanExpr : public OperatorExpr LazyBooleanExpr (Analysis::NodeMapping mappings, std::unique_ptr left_bool_expr, std::unique_ptr right_bool_expr, ExprType expr_kind, - location_t locus) - : OperatorExpr (std::move (mappings), std::move (left_bool_expr), - AST::AttrVec (), locus), - expr_type (expr_kind), right_expr (std::move (right_bool_expr)) - {} + location_t locus); // outer attributes not allowed // Copy constructor also calls OperatorExpr's protected constructor - LazyBooleanExpr (LazyBooleanExpr const &other) - : OperatorExpr (other), expr_type (other.expr_type), - right_expr (other.right_expr->clone_expr ()) - {} + LazyBooleanExpr (LazyBooleanExpr const &other); // Overload assignment operator to deep copy - LazyBooleanExpr &operator= (LazyBooleanExpr const &other) - { - OperatorExpr::operator= (other); - // main_or_left_expr = other.main_or_left_expr->clone_expr(); - right_expr = other.right_expr->clone_expr (); - expr_type = other.expr_type; - - return *this; - } + LazyBooleanExpr &operator= (LazyBooleanExpr const &other); // move constructors LazyBooleanExpr (LazyBooleanExpr &&other) = default; @@ -602,28 +519,14 @@ class TypeCastExpr : public OperatorExpr // Constructor requires calling protected constructor of OperatorExpr TypeCastExpr (Analysis::NodeMapping mappings, std::unique_ptr expr_to_cast, - std::unique_ptr type_to_cast_to, location_t locus) - : OperatorExpr (std::move (mappings), std::move (expr_to_cast), - AST::AttrVec (), locus), - type_to_convert_to (std::move (type_to_cast_to)) - {} + std::unique_ptr type_to_cast_to, location_t locus); // outer attributes not allowed // Copy constructor also requires calling protected constructor - TypeCastExpr (TypeCastExpr const &other) - : OperatorExpr (other), - type_to_convert_to (other.type_to_convert_to->clone_type ()) - {} + TypeCastExpr (TypeCastExpr const &other); // Overload assignment operator to deep copy - TypeCastExpr &operator= (TypeCastExpr const &other) - { - OperatorExpr::operator= (other); - // main_or_left_expr = other.main_or_left_expr->clone_expr(); - type_to_convert_to = other.type_to_convert_to->clone_type (); - - return *this; - } + TypeCastExpr &operator= (TypeCastExpr const &other); // move constructors as not supported in c++03 TypeCastExpr (TypeCastExpr &&other) = default; @@ -664,28 +567,14 @@ class AssignmentExpr : public OperatorExpr // Call OperatorExpr constructor to initialise left_expr AssignmentExpr (Analysis::NodeMapping mappings, std::unique_ptr value_to_assign_to, - std::unique_ptr value_to_assign, location_t locus) - : OperatorExpr (std::move (mappings), std::move (value_to_assign_to), - AST::AttrVec (), locus), - right_expr (std::move (value_to_assign)) - {} + std::unique_ptr value_to_assign, location_t locus); // outer attributes not allowed // Call OperatorExpr constructor in copy constructor, as well as clone - AssignmentExpr (AssignmentExpr const &other) - : OperatorExpr (other), right_expr (other.right_expr->clone_expr ()) - {} + AssignmentExpr (AssignmentExpr const &other); // Overload assignment operator to clone unique_ptr right_expr - AssignmentExpr &operator= (AssignmentExpr const &other) - { - OperatorExpr::operator= (other); - // main_or_left_expr = other.main_or_left_expr->clone_expr(); - right_expr = other.right_expr->clone_expr (); - // outer_attrs = other.outer_attrs; - - return *this; - } + AssignmentExpr &operator= (AssignmentExpr const &other); // move constructors AssignmentExpr (AssignmentExpr &&other) = default; @@ -735,30 +624,14 @@ class CompoundAssignmentExpr : public OperatorExpr CompoundAssignmentExpr (Analysis::NodeMapping mappings, std::unique_ptr value_to_assign_to, std::unique_ptr value_to_assign, - ExprType expr_kind, location_t locus) - : OperatorExpr (std::move (mappings), std::move (value_to_assign_to), - AST::AttrVec (), locus), - expr_type (expr_kind), right_expr (std::move (value_to_assign)) - {} + ExprType expr_kind, location_t locus); // outer attributes not allowed // Have clone in copy constructor - CompoundAssignmentExpr (CompoundAssignmentExpr const &other) - : OperatorExpr (other), expr_type (other.expr_type), - right_expr (other.right_expr->clone_expr ()) - {} + CompoundAssignmentExpr (CompoundAssignmentExpr const &other); // Overload assignment operator to clone - CompoundAssignmentExpr &operator= (CompoundAssignmentExpr const &other) - { - OperatorExpr::operator= (other); - // main_or_left_expr = other.main_or_left_expr->clone_expr(); - right_expr = other.right_expr->clone_expr (); - expr_type = other.expr_type; - // outer_attrs = other.outer_attrs; - - return *this; - } + CompoundAssignmentExpr &operator= (CompoundAssignmentExpr const &other); // move constructors CompoundAssignmentExpr (CompoundAssignmentExpr &&other) = default; @@ -798,29 +671,13 @@ class GroupedExpr : public ExprWithoutBlock, public WithInnerAttrs GroupedExpr (Analysis::NodeMapping mappings, std::unique_ptr parenthesised_expr, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - WithInnerAttrs (std::move (inner_attribs)), - expr_in_parens (std::move (parenthesised_expr)), locus (locus) - {} + location_t locus); // Copy constructor includes clone for expr_in_parens - GroupedExpr (GroupedExpr const &other) - : ExprWithoutBlock (other), WithInnerAttrs (other.inner_attrs), - expr_in_parens (other.expr_in_parens->clone_expr ()), locus (other.locus) - {} + GroupedExpr (GroupedExpr const &other); // Overloaded assignment operator to clone expr_in_parens - GroupedExpr &operator= (GroupedExpr const &other) - { - ExprWithoutBlock::operator= (other); - inner_attrs = other.inner_attrs; - expr_in_parens = other.expr_in_parens->clone_expr (); - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + GroupedExpr &operator= (GroupedExpr const &other); // move constructors GroupedExpr (GroupedExpr &&other) = default; @@ -893,33 +750,19 @@ class ArrayElems : public FullVisitable // Value array elements class ArrayElemsValues : public ArrayElems { - std::vector > values; + std::vector> values; // TODO: should this store location data? public: ArrayElemsValues (Analysis::NodeMapping mappings, - std::vector > elems) - : ArrayElems (mappings), values (std::move (elems)) - {} + std::vector> elems); // copy constructor with vector clone - ArrayElemsValues (ArrayElemsValues const &other) : ArrayElems (other) - { - values.reserve (other.values.size ()); - for (const auto &e : other.values) - values.push_back (e->clone_expr ()); - } + ArrayElemsValues (ArrayElemsValues const &other); // overloaded assignment operator with vector clone - ArrayElemsValues &operator= (ArrayElemsValues const &other) - { - values.reserve (other.values.size ()); - for (const auto &e : other.values) - values.push_back (e->clone_expr ()); - - return *this; - } + ArrayElemsValues &operator= (ArrayElemsValues const &other); // move constructors ArrayElemsValues (ArrayElemsValues &&other) = default; @@ -931,7 +774,7 @@ class ArrayElemsValues : public ArrayElems size_t get_num_elements () const { return values.size (); } - std::vector > &get_values () { return values; } + std::vector> &get_values () { return values; } ArrayElems::ArrayExprType get_array_expr_type () const override final { @@ -955,25 +798,13 @@ class ArrayElemsCopied : public ArrayElems // Constructor requires pointers for polymorphism ArrayElemsCopied (Analysis::NodeMapping mappings, std::unique_ptr copied_elem, - std::unique_ptr copy_amount) - : ArrayElems (mappings), elem_to_copy (std::move (copied_elem)), - num_copies (std::move (copy_amount)) - {} + std::unique_ptr copy_amount); // Copy constructor required due to unique_ptr - uses custom clone - ArrayElemsCopied (ArrayElemsCopied const &other) - : ArrayElems (other), elem_to_copy (other.elem_to_copy->clone_expr ()), - num_copies (other.num_copies->clone_expr ()) - {} + ArrayElemsCopied (ArrayElemsCopied const &other); // Overloaded assignment operator for deep copying - ArrayElemsCopied &operator= (ArrayElemsCopied const &other) - { - elem_to_copy = other.elem_to_copy->clone_expr (); - num_copies = other.num_copies->clone_expr (); - - return *this; - } + ArrayElemsCopied &operator= (ArrayElemsCopied const &other); // move constructors ArrayElemsCopied (ArrayElemsCopied &&other) = default; @@ -1016,33 +847,13 @@ class ArrayExpr : public ExprWithoutBlock, public WithInnerAttrs ArrayExpr (Analysis::NodeMapping mappings, std::unique_ptr array_elems, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - WithInnerAttrs (std::move (inner_attribs)), - internal_elements (std::move (array_elems)), locus (locus) - {} + location_t locus); // Copy constructor requires cloning ArrayElems for polymorphism to hold - ArrayExpr (ArrayExpr const &other) - : ExprWithoutBlock (other), WithInnerAttrs (other.inner_attrs), - locus (other.locus) - { - if (other.has_array_elems ()) - internal_elements = other.internal_elements->clone_array_elems (); - } + ArrayExpr (ArrayExpr const &other); // Overload assignment operator to clone internal_elements - ArrayExpr &operator= (ArrayExpr const &other) - { - ExprWithoutBlock::operator= (other); - inner_attrs = other.inner_attrs; - if (other.has_array_elems ()) - internal_elements = other.internal_elements->clone_array_elems (); - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + ArrayExpr &operator= (ArrayExpr const &other); // move constructors ArrayExpr (ArrayExpr &&other) = default; @@ -1086,29 +897,13 @@ class ArrayIndexExpr : public ExprWithoutBlock ArrayIndexExpr (Analysis::NodeMapping mappings, std::unique_ptr array_expr, std::unique_ptr array_index_expr, - AST::AttrVec outer_attribs, location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - array_expr (std::move (array_expr)), - index_expr (std::move (array_index_expr)), locus (locus) - {} + AST::AttrVec outer_attribs, location_t locus); // Copy constructor requires special cloning due to unique_ptr - ArrayIndexExpr (ArrayIndexExpr const &other) - : ExprWithoutBlock (other), array_expr (other.array_expr->clone_expr ()), - index_expr (other.index_expr->clone_expr ()), locus (other.locus) - {} + ArrayIndexExpr (ArrayIndexExpr const &other); // Overload assignment operator to clone unique_ptrs - ArrayIndexExpr &operator= (ArrayIndexExpr const &other) - { - ExprWithoutBlock::operator= (other); - array_expr = other.array_expr->clone_expr (); - index_expr = other.index_expr->clone_expr (); - // outer_attrs = other.outer_attrs; - locus = other.locus; - - return *this; - } + ArrayIndexExpr &operator= (ArrayIndexExpr const &other); // move constructors ArrayIndexExpr (ArrayIndexExpr &&other) = default; @@ -1146,7 +941,7 @@ class ArrayIndexExpr : public ExprWithoutBlock // HIR representation of a tuple class TupleExpr : public ExprWithoutBlock, public WithInnerAttrs { - std::vector > tuple_elems; + std::vector> tuple_elems; // replaces (inlined version of) TupleElements location_t locus; @@ -1155,37 +950,15 @@ class TupleExpr : public ExprWithoutBlock, public WithInnerAttrs std::string as_string () const override; TupleExpr (Analysis::NodeMapping mappings, - std::vector > tuple_elements, + std::vector> tuple_elements, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - WithInnerAttrs (std::move (inner_attribs)), - tuple_elems (std::move (tuple_elements)), locus (locus) - {} + location_t locus); // copy constructor with vector clone - TupleExpr (TupleExpr const &other) - : ExprWithoutBlock (other), WithInnerAttrs (other.inner_attrs), - locus (other.locus) - { - tuple_elems.reserve (other.tuple_elems.size ()); - for (const auto &e : other.tuple_elems) - tuple_elems.push_back (e->clone_expr ()); - } + TupleExpr (TupleExpr const &other); // overloaded assignment operator to vector clone - TupleExpr &operator= (TupleExpr const &other) - { - ExprWithoutBlock::operator= (other); - inner_attrs = other.inner_attrs; - locus = other.locus; - - tuple_elems.reserve (other.tuple_elems.size ()); - for (const auto &e : other.tuple_elems) - tuple_elems.push_back (e->clone_expr ()); - - return *this; - } + TupleExpr &operator= (TupleExpr const &other); // move constructors TupleExpr (TupleExpr &&other) = default; @@ -1199,14 +972,11 @@ class TupleExpr : public ExprWithoutBlock, public WithInnerAttrs void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - const std::vector > &get_tuple_elems () const - { - return tuple_elems; - } - std::vector > &get_tuple_elems () + const std::vector> &get_tuple_elems () const { return tuple_elems; } + std::vector> &get_tuple_elems () { return tuple_elems; } bool is_unit () const { return tuple_elems.size () == 0; } @@ -1241,28 +1011,13 @@ class TupleIndexExpr : public ExprWithoutBlock TupleIndexExpr (Analysis::NodeMapping mappings, std::unique_ptr tuple_expr, TupleIndex index, - AST::AttrVec outer_attribs, location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - tuple_expr (std::move (tuple_expr)), tuple_index (index), locus (locus) - {} + AST::AttrVec outer_attribs, location_t locus); // Copy constructor requires a clone for tuple_expr - TupleIndexExpr (TupleIndexExpr const &other) - : ExprWithoutBlock (other), tuple_expr (other.tuple_expr->clone_expr ()), - tuple_index (other.tuple_index), locus (other.locus) - {} + TupleIndexExpr (TupleIndexExpr const &other); // Overload assignment operator in order to clone - TupleIndexExpr &operator= (TupleIndexExpr const &other) - { - ExprWithoutBlock::operator= (other); - tuple_expr = other.tuple_expr->clone_expr (); - tuple_index = other.tuple_index; - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + TupleIndexExpr &operator= (TupleIndexExpr const &other); // move constructors TupleIndexExpr (TupleIndexExpr &&other) = default; @@ -1304,10 +1059,7 @@ class StructExpr : public ExprWithoutBlock // Protected constructor to allow initialising struct_name StructExpr (Analysis::NodeMapping mappings, PathInExpression struct_path, - AST::AttrVec outer_attribs) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - struct_name (std::move (struct_path)) - {} + AST::AttrVec outer_attribs); public: PathInExpression &get_struct_name () { return struct_name; } @@ -1331,11 +1083,7 @@ class StructExprStruct : public StructExpr, public WithInnerAttrs // Constructor has to call protected constructor of base class StructExprStruct (Analysis::NodeMapping mappings, PathInExpression struct_path, AST::AttrVec inner_attribs, - AST::AttrVec outer_attribs, location_t locus) - : StructExpr (std::move (mappings), std::move (struct_path), - std::move (outer_attribs)), - WithInnerAttrs (std::move (inner_attribs)), locus (locus) - {} + AST::AttrVec outer_attribs, location_t locus); location_t get_locus () const override final { return locus; } @@ -1362,33 +1110,21 @@ class StructExprStruct : public StructExpr, public WithInnerAttrs * struct */ struct StructBase { -public: +private: std::unique_ptr base_struct; +public: // TODO: should this store location data? - StructBase (std::unique_ptr base_struct_ptr) - : base_struct (std::move (base_struct_ptr)) - {} + StructBase (std::unique_ptr base_struct_ptr); // Copy constructor requires clone - StructBase (StructBase const &other) - { - /* HACK: gets around base_struct pointer being null (e.g. if no struct base - * exists) */ - if (other.base_struct != nullptr) - other.base_struct->clone_expr (); - } + StructBase (StructBase const &other); // Destructor ~StructBase () = default; // Overload assignment operator to clone base_struct - StructBase &operator= (StructBase const &other) - { - base_struct = other.base_struct->clone_expr (); - - return *this; - } + StructBase &operator= (StructBase const &other); // move constructors StructBase (StructBase &&other) = default; @@ -1402,7 +1138,7 @@ struct StructBase std::string as_string () const; - Expr *get_base () { return base_struct.get (); } + Expr &get_base () { return *base_struct; } }; /* Base HIR node for a single struct expression field (in struct instance @@ -1440,9 +1176,7 @@ class StructExprField : public FullVisitable // pure virtual clone implementation virtual StructExprField *clone_struct_expr_field_impl () const = 0; - StructExprField (Analysis::NodeMapping mapping, location_t locus) - : mappings (mapping), locus (locus) - {} + StructExprField (Analysis::NodeMapping mapping, location_t locus); Analysis::NodeMapping mappings; location_t locus; @@ -1457,10 +1191,7 @@ class StructExprFieldIdentifier : public StructExprField // TODO: should this store location data? public: StructExprFieldIdentifier (Analysis::NodeMapping mapping, - Identifier field_identifier, location_t locus) - : StructExprField (mapping, locus), - field_name (std::move (field_identifier)) - {} + Identifier field_identifier, location_t locus); std::string as_string () const override { return field_name.as_string (); } @@ -1491,25 +1222,13 @@ class StructExprFieldWithVal : public StructExprField protected: StructExprFieldWithVal (Analysis::NodeMapping mapping, - std::unique_ptr field_value, location_t locus) - : StructExprField (mapping, locus), value (std::move (field_value)) - {} + std::unique_ptr field_value, location_t locus); // Copy constructor requires clone - StructExprFieldWithVal (StructExprFieldWithVal const &other) - : StructExprField (other.mappings, other.locus), - value (other.value->clone_expr ()) - {} + StructExprFieldWithVal (StructExprFieldWithVal const &other); // Overload assignment operator to clone unique_ptr - StructExprFieldWithVal &operator= (StructExprFieldWithVal const &other) - { - value = other.value->clone_expr (); - mappings = other.mappings; - locus = other.locus; - - return *this; - } + StructExprFieldWithVal &operator= (StructExprFieldWithVal const &other); // move constructors StructExprFieldWithVal (StructExprFieldWithVal &&other) = default; @@ -1532,10 +1251,7 @@ class StructExprFieldIdentifierValue : public StructExprFieldWithVal StructExprFieldIdentifierValue (Analysis::NodeMapping mapping, Identifier field_identifier, std::unique_ptr field_value, - location_t locus) - : StructExprFieldWithVal (mapping, std::move (field_value), locus), - field_name (std::move (field_identifier)) - {} + location_t locus); std::string as_string () const override; @@ -1569,10 +1285,7 @@ class StructExprFieldIndexValue : public StructExprFieldWithVal StructExprFieldIndexValue (Analysis::NodeMapping mapping, TupleIndex tuple_index, std::unique_ptr field_value, - location_t locus) - : StructExprFieldWithVal (mapping, std::move (field_value), locus), - index (tuple_index) - {} + location_t locus); std::string as_string () const override; @@ -1598,58 +1311,31 @@ class StructExprFieldIndexValue : public StructExprFieldWithVal // HIR node of a struct creator with fields class StructExprStructFields : public StructExprStruct { -public: // std::vector fields; - std::vector > fields; - - // bool has_struct_base; - // FIXME make unique_ptr - StructBase *struct_base; + std::vector> fields; + tl::optional> struct_base; +public: // For unions there is just one field, the index // is set when type checking int union_index = -1; std::string as_string () const override; - bool has_struct_base () const { return struct_base != nullptr; } + bool has_struct_base () const { return struct_base.has_value (); } // Constructor for StructExprStructFields when no struct base is used StructExprStructFields ( Analysis::NodeMapping mappings, PathInExpression struct_path, - std::vector > expr_fields, - location_t locus, StructBase *base_struct, - AST::AttrVec inner_attribs = AST::AttrVec (), - AST::AttrVec outer_attribs = AST::AttrVec ()) - : StructExprStruct (std::move (mappings), std::move (struct_path), - std::move (inner_attribs), std::move (outer_attribs), - locus), - fields (std::move (expr_fields)), struct_base (base_struct) - {} + std::vector> expr_fields, location_t locus, + tl::optional> base_struct, + AST::AttrVec inner_attribs, AST::AttrVec outer_attribs); // copy constructor with vector clone - StructExprStructFields (StructExprStructFields const &other) - : StructExprStruct (other), struct_base (other.struct_base), - union_index (other.union_index) - { - fields.reserve (other.fields.size ()); - for (const auto &e : other.fields) - fields.push_back (e->clone_struct_expr_field ()); - } + StructExprStructFields (StructExprStructFields const &other); // overloaded assignment operator with vector clone - StructExprStructFields &operator= (StructExprStructFields const &other) - { - StructExprStruct::operator= (other); - struct_base = other.struct_base; - union_index = other.union_index; - - fields.reserve (other.fields.size ()); - for (const auto &e : other.fields) - fields.push_back (e->clone_struct_expr_field ()); - - return *this; - } + StructExprStructFields &operator= (StructExprStructFields const &other); // move constructors StructExprStructFields (StructExprStructFields &&other) = default; @@ -1658,20 +1344,20 @@ class StructExprStructFields : public StructExprStruct void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - std::vector > &get_fields () + std::vector> &get_fields () { return fields; }; - const std::vector > &get_fields () const + const std::vector> &get_fields () const { return fields; }; - StructBase *get_struct_base () { return struct_base; } + StructBase &get_struct_base () { return *struct_base.value (); } - void set_fields_as_owner ( - std::vector > new_fields) + void + set_fields_as_owner (std::vector> new_fields) { fields = std::move (new_fields); } @@ -1698,26 +1384,15 @@ class StructExprStructBase : public StructExprStruct StructBase struct_base; public: - std::string as_string () const override; - - /*inline StructBase get_struct_base() const { - return struct_base; - }*/ - StructExprStructBase (Analysis::NodeMapping mappings, PathInExpression struct_path, StructBase base_struct, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - location_t locus) - : StructExprStruct (std::move (mappings), std::move (struct_path), - std::move (inner_attribs), std::move (outer_attribs), - locus), - struct_base (std::move (base_struct)) - {} + location_t locus); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - StructBase *get_struct_base () { return &struct_base; } + StructBase &get_struct_base () { return struct_base; } protected: /* Use covariance to implement clone function as returning this object rather @@ -1739,45 +1414,21 @@ class StructExprStructBase : public StructExprStruct class CallExpr : public ExprWithoutBlock { std::unique_ptr function; - std::vector > params; + std::vector> params; location_t locus; public: std::string as_string () const override; CallExpr (Analysis::NodeMapping mappings, std::unique_ptr function_expr, - std::vector > function_params, - AST::AttrVec outer_attribs, location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - function (std::move (function_expr)), - params (std::move (function_params)), locus (locus) - {} + std::vector> function_params, + AST::AttrVec outer_attribs, location_t locus); // copy constructor requires clone - CallExpr (CallExpr const &other) - : ExprWithoutBlock (other), function (other.function->clone_expr ()), - locus (other.locus) - /*, params(other.params),*/ { - params.reserve (other.params.size ()); - for (const auto &e : other.params) - params.push_back (e->clone_expr ()); - } + CallExpr (CallExpr const &other); // Overload assignment operator to clone - CallExpr &operator= (CallExpr const &other) - { - ExprWithoutBlock::operator= (other); - function = other.function->clone_expr (); - locus = other.locus; - // params = other.params; - // outer_attrs = other.outer_attrs; - - params.reserve (other.params.size ()); - for (const auto &e : other.params) - params.push_back (e->clone_expr ()); - - return *this; - } + CallExpr &operator= (CallExpr const &other); // move constructors CallExpr (CallExpr &&other) = default; @@ -1796,9 +1447,9 @@ class CallExpr : public ExprWithoutBlock size_t num_params () const { return params.size (); } - std::vector > &get_arguments () { return params; } + std::vector> &get_arguments () { return params; } - const std::vector > &get_arguments () const + const std::vector> &get_arguments () const { return params; } @@ -1826,7 +1477,7 @@ class MethodCallExpr : public ExprWithoutBlock { std::unique_ptr receiver; PathExprSegment method_name; - std::vector > params; + std::vector> params; location_t locus; public: @@ -1835,40 +1486,14 @@ class MethodCallExpr : public ExprWithoutBlock MethodCallExpr (Analysis::NodeMapping mappings, std::unique_ptr call_receiver, PathExprSegment method_path, - std::vector > method_params, - AST::AttrVec outer_attribs, location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - receiver (std::move (call_receiver)), - method_name (std::move (method_path)), params (std::move (method_params)), - locus (locus) - {} + std::vector> method_params, + AST::AttrVec outer_attribs, location_t locus); // copy constructor required due to cloning - MethodCallExpr (MethodCallExpr const &other) - : ExprWithoutBlock (other), receiver (other.receiver->clone_expr ()), - method_name (other.method_name), locus (other.locus) - /*, params(other.params),*/ { - params.reserve (other.params.size ()); - for (const auto &e : other.params) - params.push_back (e->clone_expr ()); - } + MethodCallExpr (MethodCallExpr const &other); // Overload assignment operator to clone receiver object - MethodCallExpr &operator= (MethodCallExpr const &other) - { - ExprWithoutBlock::operator= (other); - receiver = other.receiver->clone_expr (); - method_name = other.method_name; - locus = other.locus; - // params = other.params; - // outer_attrs = other.outer_attrs; - - params.reserve (other.params.size ()); - for (const auto &e : other.params) - params.push_back (e->clone_expr ()); - - return *this; - } + MethodCallExpr &operator= (MethodCallExpr const &other); // move constructors MethodCallExpr (MethodCallExpr &&other) = default; @@ -1887,9 +1512,9 @@ class MethodCallExpr : public ExprWithoutBlock bool has_params () const { return !params.empty (); } size_t num_params () const { return params.size (); } - std::vector > &get_arguments () { return params; } + std::vector> &get_arguments () { return params; } - const std::vector > &get_arguments () const + const std::vector> &get_arguments () const { return params; } @@ -1930,29 +1555,13 @@ class FieldAccessExpr : public ExprWithoutBlock FieldAccessExpr (Analysis::NodeMapping mappings, std::unique_ptr field_access_receiver, Identifier field_name, AST::AttrVec outer_attribs, - location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - receiver (std::move (field_access_receiver)), - field (std::move (field_name)), locus (locus) - {} + location_t locus); // Copy constructor required due to unique_ptr cloning - FieldAccessExpr (FieldAccessExpr const &other) - : ExprWithoutBlock (other), receiver (other.receiver->clone_expr ()), - field (other.field), locus (other.locus) - {} + FieldAccessExpr (FieldAccessExpr const &other); // Overload assignment operator to clone unique_ptr - FieldAccessExpr &operator= (FieldAccessExpr const &other) - { - ExprWithoutBlock::operator= (other); - receiver = other.receiver->clone_expr (); - field = other.field; - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + FieldAccessExpr &operator= (FieldAccessExpr const &other); // move constructors FieldAccessExpr (FieldAccessExpr &&other) = default; @@ -2004,42 +1613,15 @@ struct ClosureParam // Constructor for closure parameter ClosureParam (std::unique_ptr param_pattern, location_t locus, std::unique_ptr param_type = nullptr, - std::vector outer_attrs = {}) - : outer_attrs (std::move (outer_attrs)), - pattern (std::move (param_pattern)), type (std::move (param_type)), - locus (locus) - {} + std::vector outer_attrs = {}); // Copy constructor required due to cloning as a result of unique_ptrs - ClosureParam (ClosureParam const &other) - : pattern (other.pattern->clone_pattern ()) - { - // guard to protect from null pointer dereference - if (other.pattern != nullptr) - pattern = other.pattern->clone_pattern (); - if (other.type != nullptr) - type = other.type->clone_type (); - } + ClosureParam (ClosureParam const &other); ~ClosureParam () = default; // Assignment operator must be overloaded to clone as well - ClosureParam &operator= (ClosureParam const &other) - { - outer_attrs = other.outer_attrs; - - // guard to protect from null pointer dereference - if (other.pattern != nullptr) - pattern = other.pattern->clone_pattern (); - else - pattern = nullptr; - if (other.type != nullptr) - type = other.type->clone_type (); - else - type = nullptr; - - return *this; - } + ClosureParam &operator= (ClosureParam const &other); // move constructors ClosureParam (ClosureParam &&other) = default; @@ -2075,36 +1657,13 @@ class ClosureExpr : public ExprWithoutBlock std::vector closure_params, std::unique_ptr closure_return_type, std::unique_ptr closure_expr, bool has_move, - AST::AttrVec outer_attribs, location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - has_move (has_move), params (std::move (closure_params)), locus (locus), - return_type (std::move (closure_return_type)), - expr (std::move (closure_expr)) - {} + AST::AttrVec outer_attribs, location_t locus); // Copy constructor requires cloning - ClosureExpr (ClosureExpr const &other) - : ExprWithoutBlock (other.get_mappings (), other.get_outer_attrs ()) - { - return_type - = other.has_return_type () ? other.return_type->clone_type () : nullptr; - expr = other.expr->clone_expr (); - params = other.params; - has_move = other.has_move; - } + ClosureExpr (ClosureExpr const &other); // Overload assignment operator to clone unique_ptrs - ClosureExpr &operator= (ClosureExpr const &other) - { - mappings = other.mappings; - return_type - = other.has_return_type () ? other.return_type->clone_type () : nullptr; - expr = other.expr->clone_expr (); - params = other.params; - has_move = other.has_move; - - return *this; - } + ClosureExpr &operator= (ClosureExpr const &other); // move constructors ClosureExpr (ClosureExpr &&other) = default; @@ -2153,7 +1712,7 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs { // FIXME this should be private + get/set public: - std::vector > statements; + std::vector> statements; std::unique_ptr expr; bool tail_reachable; LoopLabel label; @@ -2173,49 +1732,16 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs bool is_tail_reachable () const { return tail_reachable; } BlockExpr (Analysis::NodeMapping mappings, - std::vector > block_statements, + std::vector> block_statements, std::unique_ptr block_expr, bool tail_reachable, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - LoopLabel label, location_t start_locus, location_t end_locus) - : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), - WithInnerAttrs (std::move (inner_attribs)), - statements (std::move (block_statements)), expr (std::move (block_expr)), - tail_reachable (tail_reachable), label (std::move (label)), - start_locus (start_locus), end_locus (end_locus) - {} + LoopLabel label, location_t start_locus, location_t end_locus); // Copy constructor with clone - BlockExpr (BlockExpr const &other) - : ExprWithBlock (other), /*statements(other.statements),*/ - WithInnerAttrs (other.inner_attrs), label (other.label), - start_locus (other.start_locus), end_locus (other.end_locus) - { - // guard to protect from null pointer dereference - if (other.expr != nullptr) - expr = other.expr->clone_expr (); - - statements.reserve (other.statements.size ()); - for (const auto &e : other.statements) - statements.push_back (e->clone_stmt ()); - } + BlockExpr (BlockExpr const &other); // Overloaded assignment operator to clone pointer - BlockExpr &operator= (BlockExpr const &other) - { - ExprWithBlock::operator= (other); - // statements = other.statements; - expr = other.expr->clone_expr (); - inner_attrs = other.inner_attrs; - start_locus = other.end_locus; - end_locus = other.end_locus; - // outer_attrs = other.outer_attrs; - - statements.reserve (other.statements.size ()); - for (const auto &e : other.statements) - statements.push_back (e->clone_stmt ()); - - return *this; - } + BlockExpr &operator= (BlockExpr const &other); // move constructors BlockExpr (BlockExpr &&other) = default; @@ -2241,7 +1767,7 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs bool has_final_expr () { return expr != nullptr; } Expr &get_final_expr () { return *expr; } - std::vector > &get_statements () { return statements; } + std::vector> &get_statements () { return statements; } ExprType get_expression_type () const final override { @@ -2288,10 +1814,7 @@ class ContinueExpr : public ExprWithoutBlock // Constructor for a ContinueExpr with a label. ContinueExpr (Analysis::NodeMapping mappings, location_t locus, - Lifetime label, AST::AttrVec outer_attribs = AST::AttrVec ()) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - label (std::move (label)), locus (locus) - {} + Lifetime label, AST::AttrVec outer_attribs = AST::AttrVec ()); location_t get_locus () const override final { return locus; } @@ -2346,32 +1869,13 @@ class BreakExpr : public ExprWithoutBlock BreakExpr (Analysis::NodeMapping mappings, location_t locus, Lifetime break_label, std::unique_ptr expr_in_break = nullptr, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - label (std::move (break_label)), break_expr (std::move (expr_in_break)), - locus (locus) - {} + AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor defined to use clone for unique pointer - BreakExpr (BreakExpr const &other) - : ExprWithoutBlock (other), label (other.label), locus (other.locus) - { - // guard to protect from null pointer dereference - if (other.break_expr != nullptr) - break_expr = other.break_expr->clone_expr (); - } + BreakExpr (BreakExpr const &other); // Overload assignment operator to clone unique pointer - BreakExpr &operator= (BreakExpr const &other) - { - ExprWithoutBlock::operator= (other); - label = other.label; - break_expr = other.break_expr->clone_expr (); - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + BreakExpr &operator= (BreakExpr const &other); // move constructors BreakExpr (BreakExpr &&other) = default; @@ -2411,9 +1915,7 @@ class RangeExpr : public ExprWithoutBlock protected: // outer attributes not allowed before range expressions - RangeExpr (Analysis::NodeMapping mappings, location_t locus) - : ExprWithoutBlock (std::move (mappings), AST::AttrVec ()), locus (locus) - {} + RangeExpr (Analysis::NodeMapping mappings, location_t locus); public: location_t get_locus () const override final { return locus; } @@ -2436,26 +1938,13 @@ class RangeFromToExpr : public RangeExpr RangeFromToExpr (Analysis::NodeMapping mappings, std::unique_ptr range_from, - std::unique_ptr range_to, location_t locus) - : RangeExpr (std::move (mappings), locus), from (std::move (range_from)), - to (std::move (range_to)) - {} + std::unique_ptr range_to, location_t locus); // Copy constructor with cloning - RangeFromToExpr (RangeFromToExpr const &other) - : RangeExpr (other), from (other.from->clone_expr ()), - to (other.to->clone_expr ()) - {} + RangeFromToExpr (RangeFromToExpr const &other); // Overload assignment operator to clone unique pointers - RangeFromToExpr &operator= (RangeFromToExpr const &other) - { - RangeExpr::operator= (other); - from = other.from->clone_expr (); - to = other.to->clone_expr (); - - return *this; - } + RangeFromToExpr &operator= (RangeFromToExpr const &other); // move constructors RangeFromToExpr (RangeFromToExpr &&other) = default; @@ -2493,23 +1982,13 @@ class RangeFromExpr : public RangeExpr std::string as_string () const override; RangeFromExpr (Analysis::NodeMapping mappings, - std::unique_ptr range_from, location_t locus) - : RangeExpr (std::move (mappings), locus), from (std::move (range_from)) - {} + std::unique_ptr range_from, location_t locus); // Copy constructor with clone - RangeFromExpr (RangeFromExpr const &other) - : RangeExpr (other), from (other.from->clone_expr ()) - {} + RangeFromExpr (RangeFromExpr const &other); // Overload assignment operator to clone unique_ptr - RangeFromExpr &operator= (RangeFromExpr const &other) - { - RangeExpr::operator= (other); - from = other.from->clone_expr (); - - return *this; - } + RangeFromExpr &operator= (RangeFromExpr const &other); // move constructors RangeFromExpr (RangeFromExpr &&other) = default; @@ -2547,23 +2026,13 @@ class RangeToExpr : public RangeExpr // outer attributes not allowed RangeToExpr (Analysis::NodeMapping mappings, std::unique_ptr range_to, - location_t locus) - : RangeExpr (std::move (mappings), locus), to (std::move (range_to)) - {} + location_t locus); // Copy constructor with clone - RangeToExpr (RangeToExpr const &other) - : RangeExpr (other), to (other.to->clone_expr ()) - {} + RangeToExpr (RangeToExpr const &other); // Overload assignment operator to clone unique_ptr - RangeToExpr &operator= (RangeToExpr const &other) - { - RangeExpr::operator= (other); - to = other.to->clone_expr (); - - return *this; - } + RangeToExpr &operator= (RangeToExpr const &other); // move constructors RangeToExpr (RangeToExpr &&other) = default; @@ -2597,9 +2066,7 @@ class RangeFullExpr : public RangeExpr public: std::string as_string () const override; - RangeFullExpr (Analysis::NodeMapping mappings, location_t locus) - : RangeExpr (std::move (mappings), locus) - {} + RangeFullExpr (Analysis::NodeMapping mappings, location_t locus); // outer attributes not allowed void accept_vis (HIRFullVisitor &vis) override; @@ -2633,27 +2100,14 @@ class RangeFromToInclExpr : public RangeExpr RangeFromToInclExpr (Analysis::NodeMapping mappings, std::unique_ptr range_from, - std::unique_ptr range_to, location_t locus) - : RangeExpr (std::move (mappings), locus), from (std::move (range_from)), - to (std::move (range_to)) - {} + std::unique_ptr range_to, location_t locus); // outer attributes not allowed // Copy constructor with clone - RangeFromToInclExpr (RangeFromToInclExpr const &other) - : RangeExpr (other), from (other.from->clone_expr ()), - to (other.to->clone_expr ()) - {} + RangeFromToInclExpr (RangeFromToInclExpr const &other); // Overload assignment operator to use clone - RangeFromToInclExpr &operator= (RangeFromToInclExpr const &other) - { - RangeExpr::operator= (other); - from = other.from->clone_expr (); - to = other.to->clone_expr (); - - return *this; - } + RangeFromToInclExpr &operator= (RangeFromToInclExpr const &other); // move constructors RangeFromToInclExpr (RangeFromToInclExpr &&other) = default; @@ -2691,24 +2145,14 @@ class RangeToInclExpr : public RangeExpr std::string as_string () const override; RangeToInclExpr (Analysis::NodeMapping mappings, - std::unique_ptr range_to, location_t locus) - : RangeExpr (std::move (mappings), locus), to (std::move (range_to)) - {} + std::unique_ptr range_to, location_t locus); // outer attributes not allowed // Copy constructor with clone - RangeToInclExpr (RangeToInclExpr const &other) - : RangeExpr (other), to (other.to->clone_expr ()) - {} + RangeToInclExpr (RangeToInclExpr const &other); // Overload assignment operator to clone pointer - RangeToInclExpr &operator= (RangeToInclExpr const &other) - { - RangeExpr::operator= (other); - to = other.to->clone_expr (); - - return *this; - } + RangeToInclExpr &operator= (RangeToInclExpr const &other); // move constructors RangeToInclExpr (RangeToInclExpr &&other) = default; @@ -2752,30 +2196,13 @@ class ReturnExpr : public ExprWithoutBlock // Constructor for ReturnExpr. ReturnExpr (Analysis::NodeMapping mappings, location_t locus, std::unique_ptr returned_expr = nullptr, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - return_expr (std::move (returned_expr)), locus (locus) - {} + AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor with clone - ReturnExpr (ReturnExpr const &other) - : ExprWithoutBlock (other), locus (other.locus) - { - // guard to protect from null pointer dereference - if (other.return_expr != nullptr) - return_expr = other.return_expr->clone_expr (); - } + ReturnExpr (ReturnExpr const &other); // Overloaded assignment operator to clone return_expr pointer - ReturnExpr &operator= (ReturnExpr const &other) - { - ExprWithoutBlock::operator= (other); - return_expr = other.return_expr->clone_expr (); - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + ReturnExpr &operator= (ReturnExpr const &other); // move constructors ReturnExpr (ReturnExpr &&other) = default; @@ -2822,27 +2249,13 @@ class UnsafeBlockExpr : public ExprWithBlock UnsafeBlockExpr (Analysis::NodeMapping mappings, std::unique_ptr block_expr, - AST::AttrVec outer_attribs, location_t locus) - : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), - expr (std::move (block_expr)), locus (locus) - {} + AST::AttrVec outer_attribs, location_t locus); // Copy constructor with clone - UnsafeBlockExpr (UnsafeBlockExpr const &other) - : ExprWithBlock (other), expr (other.expr->clone_block_expr ()), - locus (other.locus) - {} + UnsafeBlockExpr (UnsafeBlockExpr const &other); // Overloaded assignment operator to clone - UnsafeBlockExpr &operator= (UnsafeBlockExpr const &other) - { - ExprWithBlock::operator= (other); - expr = other.expr->clone_block_expr (); - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + UnsafeBlockExpr &operator= (UnsafeBlockExpr const &other); // move constructors UnsafeBlockExpr (UnsafeBlockExpr &&other) = default; @@ -2891,29 +2304,13 @@ class BaseLoopExpr : public ExprWithBlock BaseLoopExpr (Analysis::NodeMapping mappings, std::unique_ptr loop_block, location_t locus, LoopLabel loop_label, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), - loop_label (std::move (loop_label)), loop_block (std::move (loop_block)), - locus (locus) - {} + AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor for BaseLoopExpr with clone - BaseLoopExpr (BaseLoopExpr const &other) - : ExprWithBlock (other), loop_label (other.loop_label), - loop_block (other.loop_block->clone_block_expr ()), locus (other.locus) - {} + BaseLoopExpr (BaseLoopExpr const &other); // Overloaded assignment operator to clone - BaseLoopExpr &operator= (BaseLoopExpr const &other) - { - ExprWithBlock::operator= (other); - loop_block = other.loop_block->clone_block_expr (); - loop_label = other.loop_label; - locus = other.locus; - // outer_attrs = other.outer_attrs; - - return *this; - } + BaseLoopExpr &operator= (BaseLoopExpr const &other); // move constructors BaseLoopExpr (BaseLoopExpr &&other) = default; @@ -2943,10 +2340,7 @@ class LoopExpr : public BaseLoopExpr // Constructor for LoopExpr LoopExpr (Analysis::NodeMapping mappings, std::unique_ptr loop_block, location_t locus, - LoopLabel loop_label, AST::AttrVec outer_attribs = AST::AttrVec ()) - : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, - std::move (loop_label), std::move (outer_attribs)) - {} + LoopLabel loop_label, AST::AttrVec outer_attribs = AST::AttrVec ()); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; @@ -2977,28 +2371,13 @@ class WhileLoopExpr : public BaseLoopExpr std::unique_ptr loop_condition, std::unique_ptr loop_block, location_t locus, LoopLabel loop_label, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, - std::move (loop_label), std::move (outer_attribs)), - condition (std::move (loop_condition)) - {} + AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor with clone - WhileLoopExpr (WhileLoopExpr const &other) - : BaseLoopExpr (other), condition (other.condition->clone_expr ()) - {} + WhileLoopExpr (WhileLoopExpr const &other); // Overloaded assignment operator to clone - WhileLoopExpr &operator= (WhileLoopExpr const &other) - { - BaseLoopExpr::operator= (other); - condition = other.condition->clone_expr (); - // loop_block = other.loop_block->clone_block_expr(); - // loop_label = other.loop_label; - // outer_attrs = other.outer_attrs; - - return *this; - } + WhileLoopExpr &operator= (WhileLoopExpr const &other); // move constructors WhileLoopExpr (WhileLoopExpr &&other) = default; @@ -3029,7 +2408,7 @@ class WhileLoopExpr : public BaseLoopExpr class WhileLetLoopExpr : public BaseLoopExpr { // MatchArmPatterns patterns; - std::vector > match_arm_patterns; // inlined + std::vector> match_arm_patterns; // inlined std::unique_ptr condition; public: @@ -3037,44 +2416,17 @@ class WhileLetLoopExpr : public BaseLoopExpr // Constructor with a loop label WhileLetLoopExpr (Analysis::NodeMapping mappings, - std::vector > match_arm_patterns, + std::vector> match_arm_patterns, std::unique_ptr condition, std::unique_ptr loop_block, location_t locus, LoopLabel loop_label, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, - std::move (loop_label), std::move (outer_attribs)), - match_arm_patterns (std::move (match_arm_patterns)), - condition (std::move (condition)) - {} + AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor with clone - WhileLetLoopExpr (WhileLetLoopExpr const &other) - : BaseLoopExpr (other), - /*match_arm_patterns(other.match_arm_patterns),*/ condition ( - other.condition->clone_expr ()) - { - match_arm_patterns.reserve (other.match_arm_patterns.size ()); - for (const auto &e : other.match_arm_patterns) - match_arm_patterns.push_back (e->clone_pattern ()); - } + WhileLetLoopExpr (WhileLetLoopExpr const &other); // Overloaded assignment operator to clone pointers - WhileLetLoopExpr &operator= (WhileLetLoopExpr const &other) - { - BaseLoopExpr::operator= (other); - // match_arm_patterns = other.match_arm_patterns; - condition = other.condition->clone_expr (); - // loop_block = other.loop_block->clone_block_expr(); - // loop_label = other.loop_label; - // outer_attrs = other.outer_attrs; - - match_arm_patterns.reserve (other.match_arm_patterns.size ()); - for (const auto &e : other.match_arm_patterns) - match_arm_patterns.push_back (e->clone_pattern ()); - - return *this; - } + WhileLetLoopExpr &operator= (WhileLetLoopExpr const &other); // move constructors WhileLetLoopExpr (WhileLetLoopExpr &&other) = default; @@ -3084,7 +2436,7 @@ class WhileLetLoopExpr : public BaseLoopExpr void accept_vis (HIRExpressionVisitor &vis) override; Expr &get_cond () { return *condition; } - std::vector > &get_patterns () + std::vector> &get_patterns () { return match_arm_patterns; } @@ -3117,29 +2469,14 @@ class IfExpr : public ExprWithBlock std::string as_string () const override; IfExpr (Analysis::NodeMapping mappings, std::unique_ptr condition, - std::unique_ptr if_block, location_t locus) - : ExprWithBlock (std::move (mappings), AST::AttrVec ()), - condition (std::move (condition)), if_block (std::move (if_block)), - locus (locus) - {} + std::unique_ptr if_block, location_t locus); // outer attributes are never allowed on IfExprs // Copy constructor with clone - IfExpr (IfExpr const &other) - : ExprWithBlock (other), condition (other.condition->clone_expr ()), - if_block (other.if_block->clone_block_expr ()), locus (other.locus) - {} + IfExpr (IfExpr const &other); // Overloaded assignment operator to clone expressions - IfExpr &operator= (IfExpr const &other) - { - ExprWithBlock::operator= (other); - condition = other.condition->clone_expr (); - if_block = other.if_block->clone_block_expr (); - locus = other.locus; - - return *this; - } + IfExpr &operator= (IfExpr const &other); // move constructors IfExpr (IfExpr &&other) = default; @@ -3195,28 +2532,15 @@ class IfExprConseqElse : public IfExpr IfExprConseqElse (Analysis::NodeMapping mappings, std::unique_ptr condition, std::unique_ptr if_block, - std::unique_ptr else_block, location_t locus) - : IfExpr (std::move (mappings), std::move (condition), std::move (if_block), - locus), - else_block (std::move (else_block)) - {} + std::unique_ptr else_block, + location_t locus); // again, outer attributes not allowed // Copy constructor with clone - IfExprConseqElse (IfExprConseqElse const &other) - : IfExpr (other), else_block (other.else_block->clone_expr_with_block ()) - {} + IfExprConseqElse (IfExprConseqElse const &other); // Overloaded assignment operator with cloning - IfExprConseqElse &operator= (IfExprConseqElse const &other) - { - IfExpr::operator= (other); - // condition = other.condition->clone_expr(); - // if_block = other.if_block->clone_block_expr(); - else_block = other.else_block->clone_expr_with_block (); - - return *this; - } + IfExprConseqElse &operator= (IfExprConseqElse const &other); // move constructors IfExprConseqElse (IfExprConseqElse &&other) = default; @@ -3257,7 +2581,7 @@ struct MatchArm { private: AST::AttrVec outer_attrs; - std::vector > match_arm_patterns; + std::vector> match_arm_patterns; std::unique_ptr guard_expr; location_t locus; @@ -3266,45 +2590,17 @@ struct MatchArm bool has_match_arm_guard () const { return guard_expr != nullptr; } // Constructor for match arm with a guard expression - MatchArm (std::vector > match_arm_patterns, + MatchArm (std::vector> match_arm_patterns, location_t locus, std::unique_ptr guard_expr = nullptr, - AST::AttrVec outer_attrs = AST::AttrVec ()) - : outer_attrs (std::move (outer_attrs)), - match_arm_patterns (std::move (match_arm_patterns)), - guard_expr (std::move (guard_expr)), locus (locus) - {} + AST::AttrVec outer_attrs = AST::AttrVec ()); // Copy constructor with clone - MatchArm (MatchArm const &other) : outer_attrs (other.outer_attrs) - { - // guard to protect from null pointer dereference - if (other.guard_expr != nullptr) - guard_expr = other.guard_expr->clone_expr (); - - match_arm_patterns.reserve (other.match_arm_patterns.size ()); - for (const auto &e : other.match_arm_patterns) - match_arm_patterns.push_back (e->clone_pattern ()); - - locus = other.locus; - } + MatchArm (MatchArm const &other); ~MatchArm () = default; // Overload assignment operator to clone - MatchArm &operator= (MatchArm const &other) - { - outer_attrs = other.outer_attrs; - - if (other.guard_expr != nullptr) - guard_expr = other.guard_expr->clone_expr (); - - match_arm_patterns.clear (); - match_arm_patterns.reserve (other.match_arm_patterns.size ()); - for (const auto &e : other.match_arm_patterns) - match_arm_patterns.push_back (e->clone_pattern ()); - - return *this; - } + MatchArm &operator= (MatchArm const &other); // move constructors MatchArm (MatchArm &&other) = default; @@ -3317,12 +2613,12 @@ struct MatchArm static MatchArm create_error () { location_t locus = UNDEF_LOCATION; - return MatchArm (std::vector > (), locus); + return MatchArm (std::vector> (), locus); } std::string as_string () const; - std::vector > &get_patterns () + std::vector> &get_patterns () { return match_arm_patterns; } @@ -3343,23 +2639,11 @@ struct MatchCase public: MatchCase (Analysis::NodeMapping mappings, MatchArm arm, - std::unique_ptr expr) - : mappings (mappings), arm (std::move (arm)), expr (std::move (expr)) - {} + std::unique_ptr expr); - MatchCase (const MatchCase &other) - : mappings (other.mappings), arm (other.arm), - expr (other.expr->clone_expr ()) - {} - - MatchCase &operator= (const MatchCase &other) - { - mappings = other.mappings; - arm = other.arm; - expr = other.expr->clone_expr (); + MatchCase (const MatchCase &other); - return *this; - } + MatchCase &operator= (const MatchCase &other); MatchCase (MatchCase &&other) = default; MatchCase &operator= (MatchCase &&other) = default; @@ -3388,40 +2672,13 @@ class MatchExpr : public ExprWithBlock, public WithInnerAttrs MatchExpr (Analysis::NodeMapping mappings, std::unique_ptr branch_value, std::vector match_arms, AST::AttrVec inner_attrs, - AST::AttrVec outer_attrs, location_t locus) - : ExprWithBlock (std::move (mappings), std::move (outer_attrs)), - WithInnerAttrs (std::move (inner_attrs)), - branch_value (std::move (branch_value)), - match_arms (std::move (match_arms)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Copy constructor requires clone due to unique_ptr - MatchExpr (MatchExpr const &other) - : ExprWithBlock (other), WithInnerAttrs (other.inner_attrs), - branch_value (other.branch_value->clone_expr ()), - match_arms (other.match_arms), locus (other.locus) - { - /*match_arms.reserve (other.match_arms.size ()); - for (const auto &e : other.match_arms) - match_arms.push_back (e->clone_match_case ());*/ - } + MatchExpr (MatchExpr const &other); // Overloaded assignment operator to clone due to unique_ptr - MatchExpr &operator= (MatchExpr const &other) - { - ExprWithBlock::operator= (other); - branch_value = other.branch_value->clone_expr (); - inner_attrs = other.inner_attrs; - match_arms = other.match_arms; - // outer_attrs = other.outer_attrs; - locus = other.locus; - - /*match_arms.reserve (other.match_arms.size ()); - for (const auto &e : other.match_arms) - match_arms.push_back (e->clone_match_case ());*/ - - return *this; - } + MatchExpr &operator= (MatchExpr const &other); // move constructors MatchExpr (MatchExpr &&other) = default; @@ -3464,26 +2721,13 @@ class AwaitExpr : public ExprWithoutBlock public: // TODO: ensure outer attributes are actually allowed AwaitExpr (Analysis::NodeMapping mappings, std::unique_ptr awaited_expr, - AST::AttrVec outer_attrs, location_t locus) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attrs)), - awaited_expr (std::move (awaited_expr)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // copy constructor with clone - AwaitExpr (AwaitExpr const &other) - : ExprWithoutBlock (other), - awaited_expr (other.awaited_expr->clone_expr ()), locus (other.locus) - {} + AwaitExpr (AwaitExpr const &other); // overloaded assignment operator with clone - AwaitExpr &operator= (AwaitExpr const &other) - { - ExprWithoutBlock::operator= (other); - awaited_expr = other.awaited_expr->clone_expr (); - locus = other.locus; - - return *this; - } + AwaitExpr &operator= (AwaitExpr const &other); // move constructors AwaitExpr (AwaitExpr &&other) = default; @@ -3522,27 +2766,13 @@ class AsyncBlockExpr : public ExprWithBlock public: AsyncBlockExpr (Analysis::NodeMapping mappings, std::unique_ptr block_expr, bool has_move, - AST::AttrVec outer_attrs, location_t locus) - : ExprWithBlock (std::move (mappings), std::move (outer_attrs)), - has_move (has_move), block_expr (std::move (block_expr)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // copy constructor with clone - AsyncBlockExpr (AsyncBlockExpr const &other) - : ExprWithBlock (other), has_move (other.has_move), - block_expr (other.block_expr->clone_block_expr ()), locus (other.locus) - {} + AsyncBlockExpr (AsyncBlockExpr const &other); // overloaded assignment operator to clone - AsyncBlockExpr &operator= (AsyncBlockExpr const &other) - { - ExprWithBlock::operator= (other); - has_move = other.has_move; - block_expr = other.block_expr->clone_block_expr (); - locus = other.locus; - - return *this; - } + AsyncBlockExpr &operator= (AsyncBlockExpr const &other); // move constructors AsyncBlockExpr (AsyncBlockExpr &&other) = default; @@ -3576,35 +2806,15 @@ class AsyncBlockExpr : public ExprWithBlock class OperatorExprMeta { public: - OperatorExprMeta (HIR::CompoundAssignmentExpr &expr) - : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ().get_mappings ()), - locus (expr.get_locus ()) - {} + OperatorExprMeta (HIR::CompoundAssignmentExpr &expr); - OperatorExprMeta (HIR::ArithmeticOrLogicalExpr &expr) - : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ().get_mappings ()), - locus (expr.get_locus ()) - {} + OperatorExprMeta (HIR::ArithmeticOrLogicalExpr &expr); - OperatorExprMeta (HIR::NegationExpr &expr) - : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ().get_mappings ()), - locus (expr.get_locus ()) - {} + OperatorExprMeta (HIR::NegationExpr &expr); - OperatorExprMeta (HIR::DereferenceExpr &expr) - : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_expr ().get_mappings ()), - locus (expr.get_locus ()) - {} + OperatorExprMeta (HIR::DereferenceExpr &expr); - OperatorExprMeta (HIR::ArrayIndexExpr &expr) - : node_mappings (expr.get_mappings ()), - lvalue_mappings (expr.get_array_expr ().get_mappings ()), - locus (expr.get_locus ()) - {} + OperatorExprMeta (HIR::ArrayIndexExpr &expr); const Analysis::NodeMapping &get_mappings () const { return node_mappings; } @@ -3679,23 +2889,12 @@ struct AnonConst { NodeId id; std::unique_ptr expr; - AnonConst (NodeId id, std::unique_ptr expr) - : id (id), expr (std::move (expr)) - { - rust_assert (this->expr != nullptr); - } - AnonConst (const AnonConst &other) - { - id = other.id; - expr = other.expr->clone_expr (); - } - AnonConst operator= (const AnonConst &other) - { - id = other.id; - expr = other.expr->clone_expr (); - return *this; - } + AnonConst (NodeId id, std::unique_ptr expr); + + AnonConst (const AnonConst &other); + + AnonConst operator= (const AnonConst &other); }; class InlineAsmOperand @@ -3707,26 +2906,11 @@ class InlineAsmOperand std::unique_ptr expr; In (const tl::optional ®, - std::unique_ptr expr) - : reg (reg), expr (std::move (expr)) - { - rust_assert (this->expr != nullptr); - } - - In (const struct In &other) - { - reg = other.reg; - - expr = other.expr->clone_expr (); - } - - In operator= (const struct In &other) - { - reg = other.reg; - expr = other.expr->clone_expr (); - - return *this; - } + std::unique_ptr expr); + + In (const struct In &other); + + In operator= (const struct In &other); }; struct Out @@ -3736,26 +2920,11 @@ class InlineAsmOperand std::unique_ptr expr; // can be null Out (tl::optional ®, bool late, - std::unique_ptr expr) - : reg (reg), late (late), expr (std::move (expr)) - { - rust_assert (this->expr != nullptr); - } - - Out (const struct Out &other) - { - reg = other.reg; - late = other.late; - expr = other.expr->clone_expr (); - } - - Out operator= (const struct Out &other) - { - reg = other.reg; - late = other.late; - expr = other.expr->clone_expr (); - return *this; - } + std::unique_ptr expr); + + Out (const struct Out &other); + + Out operator= (const struct Out &other); }; struct InOut @@ -3765,27 +2934,11 @@ class InlineAsmOperand std::unique_ptr expr; // this can't be null InOut (tl::optional ®, bool late, - std::unique_ptr expr) - : reg (reg), late (late), expr (std::move (expr)) - { - rust_assert (this->expr != nullptr); - } - - InOut (const struct InOut &other) - { - reg = other.reg; - late = other.late; - expr = other.expr->clone_expr (); - } - - InOut operator= (const struct InOut &other) - { - reg = other.reg; - late = other.late; - expr = other.expr->clone_expr (); - - return *this; - } + std::unique_ptr expr); + + InOut (const struct InOut &other); + + InOut operator= (const struct InOut &other); }; struct SplitInOut @@ -3797,31 +2950,11 @@ class InlineAsmOperand SplitInOut (tl::optional ®, bool late, std::unique_ptr in_expr, - std::unique_ptr out_expr) - : reg (reg), late (late), in_expr (std::move (in_expr)), - out_expr (std::move (out_expr)) - { - rust_assert (this->in_expr != nullptr); - rust_assert (this->out_expr != nullptr); - } - - SplitInOut (const struct SplitInOut &other) - { - reg = other.reg; - late = other.late; - in_expr = other.in_expr->clone_expr (); - out_expr = other.out_expr->clone_expr (); - } - - SplitInOut operator= (const struct SplitInOut &other) - { - reg = other.reg; - late = other.late; - in_expr = other.in_expr->clone_expr (); - out_expr = other.out_expr->clone_expr (); - - return *this; - } + std::unique_ptr out_expr); + + SplitInOut (const struct SplitInOut &other); + + SplitInOut operator= (const struct SplitInOut &other); }; struct Const @@ -3833,20 +2966,11 @@ class InlineAsmOperand { std::unique_ptr expr; - Sym (std::unique_ptr expr) : expr (std::move (expr)) - { - rust_assert (this->expr != nullptr); - } - Sym (const struct Sym &other) - { - expr = std::unique_ptr (other.expr->clone_expr ()); - } - - Sym operator= (const struct Sym &other) - { - expr = std::unique_ptr (other.expr->clone_expr ()); - return *this; - } + Sym (std::unique_ptr expr); + + Sym (const struct Sym &other); + + Sym operator= (const struct Sym &other); }; struct Label @@ -3854,23 +2978,11 @@ class InlineAsmOperand std::string label_name; std::unique_ptr expr; - Label (tl::optional label_name, std::unique_ptr expr) - : expr (std::move (expr)) - { - rust_assert (this->expr != nullptr); - if (label_name.has_value ()) - this->label_name = label_name.value (); - } - Label (const struct Label &other) - { - expr = std::unique_ptr (other.expr->clone_expr ()); - } - - Label operator= (const struct Label &other) - { - expr = std::unique_ptr (other.expr->clone_expr ()); - return *this; - } + Label (tl::optional label_name, std::unique_ptr expr); + + Label (const struct Label &other); + + Label operator= (const struct Label &other); }; private: @@ -3988,6 +3100,7 @@ class InlineAsm : public ExprWithoutBlock // INFO: An inline asm is asm!, which is the opposite of a global_asm() return !this->is_global_asm; } + InlineAsm (location_t locus, bool is_global_asm, std::vector template_, std::vector template_strs, @@ -3995,15 +3108,7 @@ class InlineAsm : public ExprWithoutBlock std::vector clobber_abi, std::set options, Analysis::NodeMapping mappings, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), - locus (locus), is_global_asm (is_global_asm), - template_ (std::move (template_)), - template_strs (std::move (template_strs)), - operands (std::move (operands)), clobber_abi (std::move (clobber_abi)), - options (std::move (options)) - - {} + AST::AttrVec outer_attribs = AST::AttrVec ()); }; } // namespace HIR diff --git a/gcc/rust/hir/tree/rust-hir-generic-param.cc b/gcc/rust/hir/tree/rust-hir-generic-param.cc new file mode 100644 index 000000000000..e5afa8ee08fc --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-generic-param.cc @@ -0,0 +1,95 @@ +// Copyright (C) 2020-2024 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 +// . + +#include "rust-hir-generic-param.h" + +namespace Rust { +namespace HIR { + +GenericParam::GenericParam (Analysis::NodeMapping mapping, + enum GenericKind kind) + : mappings (mapping), kind (kind) +{} + +LifetimeParam::LifetimeParam (Analysis::NodeMapping mappings, Lifetime lifetime, + location_t locus, + std::vector lifetime_bounds, + AST::AttrVec outer_attrs) + : GenericParam (mappings, GenericKind::LIFETIME), + lifetime (std::move (lifetime)), + lifetime_bounds (std::move (lifetime_bounds)), + outer_attrs (std::move (outer_attrs)), locus (locus) +{} + +LifetimeParam::LifetimeParam (LifetimeParam const &other) + : GenericParam (other.mappings, GenericKind::LIFETIME), + lifetime (other.lifetime), lifetime_bounds (other.lifetime_bounds), + outer_attrs (other.outer_attrs), locus (other.locus) +{} + +LifetimeParam & +LifetimeParam::operator= (LifetimeParam const &other) +{ + lifetime = other.lifetime; + lifetime_bounds = other.lifetime_bounds; + outer_attrs = other.outer_attrs; + locus = other.locus; + mappings = other.mappings; + + return *this; +} + +ConstGenericParam::ConstGenericParam (std::string name, + std::unique_ptr type, + std::unique_ptr default_expression, + Analysis::NodeMapping mapping, + location_t locus) + : GenericParam (mapping, GenericKind::CONST), name (std::move (name)), + type (std::move (type)), + default_expression (std::move (default_expression)), locus (locus) +{} + +ConstGenericParam::ConstGenericParam (const ConstGenericParam &other) + : GenericParam (other) +{ + name = other.name; + locus = other.locus; + + if (other.type) + type = other.type->clone_type (); + if (other.default_expression) + default_expression = other.default_expression->clone_expr (); +} + +std::string +ConstGenericParam::as_string () const +{ + auto result = "ConstGenericParam: " + name + " : " + type->as_string (); + + if (default_expression) + result += " = " + default_expression->as_string (); + + return result; +} + +void +ConstGenericParam::accept_vis (HIRFullVisitor &) +{} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-generic-param.h b/gcc/rust/hir/tree/rust-hir-generic-param.h new file mode 100644 index 000000000000..73b93d48a49f --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-generic-param.h @@ -0,0 +1,186 @@ + +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_GENERIC_PARAM_H +#define RUST_HIR_GENERIC_PARAM_H + +#include "rust-hir-visitable.h" +#include "rust-hir-bound.h" + +namespace Rust { +namespace HIR { + +/* Base generic parameter in HIR. Abstract - can be represented by a Lifetime or + * Type param */ +class GenericParam : public FullVisitable +{ +public: + using FullVisitable::accept_vis; + + virtual ~GenericParam () {} + + enum class GenericKind + { + TYPE, + LIFETIME, + CONST, + }; + + virtual AST::AttrVec &get_outer_attrs () = 0; + virtual bool has_outer_attribute () const = 0; + + // Unique pointer custom clone function + std::unique_ptr clone_generic_param () const + { + return std::unique_ptr (clone_generic_param_impl ()); + } + + virtual std::string as_string () const = 0; + + virtual location_t get_locus () const = 0; + + Analysis::NodeMapping get_mappings () const { return mappings; } + + enum GenericKind get_kind () const { return kind; } + +protected: + // Clone function implementation as pure virtual method + virtual GenericParam *clone_generic_param_impl () const = 0; + + Analysis::NodeMapping mappings; + + enum GenericKind kind; + + GenericParam (Analysis::NodeMapping mapping, + enum GenericKind kind = GenericKind::TYPE); +}; + +// A lifetime generic parameter (as opposed to a type generic parameter) +class LifetimeParam : public GenericParam +{ + Lifetime lifetime; + + // bool has_lifetime_bounds; + // LifetimeBounds lifetime_bounds; + std::vector lifetime_bounds; // inlined LifetimeBounds + + AST::AttrVec outer_attrs; + + location_t locus; + +public: + Lifetime get_lifetime () { return lifetime; } + + // Returns whether the lifetime param has any lifetime bounds. + bool has_lifetime_bounds () const { return !lifetime_bounds.empty (); } + + std::vector &get_lifetime_bounds () { return lifetime_bounds; } + + // Returns whether the lifetime param has an outer attribute. + bool has_outer_attribute () const override { return outer_attrs.size () > 1; } + + AST::AttrVec &get_outer_attrs () override { return outer_attrs; } + + // Returns whether the lifetime param is in an error state. + bool is_error () const { return lifetime.is_error (); } + + // Constructor + LifetimeParam (Analysis::NodeMapping mappings, Lifetime lifetime, + location_t locus = UNDEF_LOCATION, + std::vector lifetime_bounds + = std::vector (), + AST::AttrVec outer_attrs = std::vector ()); + + // TODO: remove copy and assignment operator definitions - not required + + // Copy constructor with clone + LifetimeParam (LifetimeParam const &other); + + // Overloaded assignment operator to clone attribute + LifetimeParam &operator= (LifetimeParam const &other); + + // move constructors + LifetimeParam (LifetimeParam &&other) = default; + LifetimeParam &operator= (LifetimeParam &&other) = default; + + std::string as_string () const override; + + void accept_vis (HIRFullVisitor &vis) override; + + location_t get_locus () const override final { return locus; } + +protected: + /* Use covariance to implement clone function as returning this object rather + * than base */ + LifetimeParam *clone_generic_param_impl () const override + { + return new LifetimeParam (*this); + } +}; + +class ConstGenericParam : public GenericParam +{ +public: + ConstGenericParam (std::string name, std::unique_ptr type, + std::unique_ptr default_expression, + Analysis::NodeMapping mapping, location_t locus); + + ConstGenericParam (const ConstGenericParam &other); + + bool has_outer_attribute () const override { return false; } + + AST::AttrVec &get_outer_attrs () override { return outer_attrs; } + + std::string as_string () const override final; + + void accept_vis (HIRFullVisitor &vis) override final; + + location_t get_locus () const override final { return locus; }; + + bool has_default_expression () { return default_expression != nullptr; } + + std::string get_name () { return name; } + Type &get_type () { return *type; } + Expr &get_default_expression () { return *default_expression; } + +protected: + /* Use covariance to implement clone function as returning this object rather + * than base */ + ConstGenericParam *clone_generic_param_impl () const override + { + return new ConstGenericParam (*this); + } + +private: + std::string name; + std::unique_ptr type; + + /* const params have no outer attrs, should be empty */ + AST::AttrVec outer_attrs = std::vector (); + + /* Optional - can be a null pointer if there is no default expression */ + std::unique_ptr default_expression; + + location_t locus; +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-item.cc b/gcc/rust/hir/tree/rust-hir-item.cc new file mode 100644 index 000000000000..f81f1eae1343 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-item.cc @@ -0,0 +1,1017 @@ +// Copyright (C) 2020-2024 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 +// . + +#include "rust-hir-item.h" + +namespace Rust { +namespace HIR { + +TypeParam::TypeParam ( + Analysis::NodeMapping mappings, Identifier type_representation, + location_t locus, + std::vector> type_param_bounds, + std::unique_ptr, AST::AttrVec outer_attrs) + : GenericParam (mappings), outer_attrs (std::move (outer_attrs)), + type_representation (std::move (type_representation)), + type_param_bounds (std::move (type_param_bounds)), type (std::move (type)), + locus (locus) +{} + +TypeParam::TypeParam (TypeParam const &other) + : GenericParam (other.mappings), outer_attrs (other.outer_attrs), + type_representation (other.type_representation), locus (other.locus) +{ + // guard to prevent null pointer dereference + if (other.type != nullptr) + type = other.type->clone_type (); + + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); +} + +TypeParam & +TypeParam::operator= (TypeParam const &other) +{ + type_representation = other.type_representation; + outer_attrs = other.outer_attrs; + locus = other.locus; + mappings = other.mappings; + + // guard to prevent null pointer dereference + if (other.type != nullptr) + type = other.type->clone_type (); + else + type = nullptr; + + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); + + return *this; +} + +Analysis::NodeMapping +TypeParam::get_type_mappings () const +{ + rust_assert (type != nullptr); + return type->get_mappings (); +} + +std::vector> & +TypeParam::get_type_param_bounds () +{ + return type_param_bounds; +} + +TypeBoundWhereClauseItem::TypeBoundWhereClauseItem ( + Analysis::NodeMapping mappings, std::vector for_lifetimes, + std::unique_ptr bound_type, + std::vector> type_param_bounds, + location_t locus) + : for_lifetimes (std::move (for_lifetimes)), + bound_type (std::move (bound_type)), + type_param_bounds (std::move (type_param_bounds)), + mappings (std::move (mappings)), locus (locus) +{} + +TypeBoundWhereClauseItem::TypeBoundWhereClauseItem ( + TypeBoundWhereClauseItem const &other) + : for_lifetimes (other.for_lifetimes), + bound_type (other.bound_type->clone_type ()), mappings (other.mappings) +{ + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); +} + +TypeBoundWhereClauseItem & +TypeBoundWhereClauseItem::operator= (TypeBoundWhereClauseItem const &other) +{ + mappings = other.mappings; + for_lifetimes = other.for_lifetimes; + bound_type = other.bound_type->clone_type (); + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); + + return *this; +} + +std::vector> & +TypeBoundWhereClauseItem::get_type_param_bounds () +{ + return type_param_bounds; +} + +SelfParam::SelfParam (Analysis::NodeMapping mappings, + ImplicitSelfKind self_kind, Lifetime lifetime, Type *type) + : self_kind (self_kind), lifetime (std::move (lifetime)), type (type), + mappings (mappings) +{} + +SelfParam::SelfParam (Analysis::NodeMapping mappings, + std::unique_ptr type, bool is_mut, location_t locus) + : self_kind (is_mut ? ImplicitSelfKind::MUT : ImplicitSelfKind::IMM), + lifetime ( + Lifetime (mappings, AST::Lifetime::LifetimeType::NAMED, "", locus)), + type (std::move (type)), locus (locus), mappings (mappings) +{} + +SelfParam::SelfParam (Analysis::NodeMapping mappings, Lifetime lifetime, + bool is_mut, location_t locus) + : self_kind (is_mut ? ImplicitSelfKind::MUT_REF : ImplicitSelfKind::IMM_REF), + lifetime (std::move (lifetime)), locus (locus), mappings (mappings) +{} + +SelfParam::SelfParam (SelfParam const &other) + : self_kind (other.self_kind), lifetime (other.lifetime), locus (other.locus), + mappings (other.mappings) +{ + if (other.type != nullptr) + type = other.type->clone_type (); +} + +SelfParam & +SelfParam::operator= (SelfParam const &other) +{ + if (other.type != nullptr) + type = other.type->clone_type (); + + self_kind = other.self_kind; + lifetime = other.lifetime; + locus = other.locus; + mappings = other.mappings; + + return *this; +} + +Mutability +SelfParam::get_mut () const +{ + return (self_kind == ImplicitSelfKind::MUT + || self_kind == ImplicitSelfKind::MUT_REF) + ? Mutability::Mut + : Mutability::Imm; +} + +bool +SelfParam::is_mut () const +{ + return self_kind == ImplicitSelfKind::MUT + || self_kind == ImplicitSelfKind::MUT_REF; +} + +bool +SelfParam::is_ref () const +{ + return self_kind == ImplicitSelfKind::IMM_REF + || self_kind == ImplicitSelfKind::MUT_REF; +} + +FunctionParam::FunctionParam (Analysis::NodeMapping mappings, + std::unique_ptr param_name, + std::unique_ptr param_type, + location_t locus) + : param_name (std::move (param_name)), type (std::move (param_type)), + locus (locus), mappings (mappings) +{} + +FunctionParam::FunctionParam (FunctionParam const &other) + : param_name (other.param_name->clone_pattern ()), + type (other.type->clone_type ()), locus (other.locus), + mappings (other.mappings) +{} + +FunctionParam & +FunctionParam::operator= (FunctionParam const &other) +{ + param_name = other.param_name->clone_pattern (); + type = other.type->clone_type (); + locus = other.locus; + mappings = other.mappings; + + return *this; +} + +VisItem & +VisItem::operator= (VisItem const &other) +{ + Item::operator= (other); + visibility = other.visibility; + // outer_attrs = other.outer_attrs; + + return *this; +} + +VisItem::VisItem (VisItem const &other) + : Item (other), visibility (other.visibility) +{} + +Module::Module (Analysis::NodeMapping mappings, Identifier module_name, + location_t locus, std::vector> items, + Visibility visibility, AST::AttrVec inner_attrs, + AST::AttrVec outer_attrs) + : VisItem (std::move (mappings), std::move (visibility), + std::move (outer_attrs)), + WithInnerAttrs (std::move (inner_attrs)), module_name (module_name), + locus (locus), items (std::move (items)) +{} + +Module::Module (Module const &other) + : VisItem (other), WithInnerAttrs (other.inner_attrs), module_name ("") +{ + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_item ()); +} + +Module & +Module::operator= (Module const &other) +{ + VisItem::operator= (other); + inner_attrs = other.inner_attrs; + + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_item ()); + + return *this; +} + +Function::Function (Analysis::NodeMapping mappings, Identifier function_name, + FunctionQualifiers qualifiers, + std::vector> generic_params, + std::vector function_params, + std::unique_ptr return_type, WhereClause where_clause, + std::unique_ptr function_body, Visibility vis, + AST::AttrVec outer_attrs, SelfParam self, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + qualifiers (std::move (qualifiers)), + function_name (std::move (function_name)), + generic_params (std::move (generic_params)), + function_params (std::move (function_params)), + return_type (std::move (return_type)), + where_clause (std::move (where_clause)), + function_body (std::move (function_body)), self (std::move (self)), + locus (locus) +{} + +Function::Function (Function const &other) + : VisItem (other), qualifiers (other.qualifiers), + function_name (other.function_name), + function_params (other.function_params), where_clause (other.where_clause), + function_body (other.function_body->clone_block_expr ()), self (other.self), + locus (other.locus) +{ + // guard to prevent null dereference (always required) + if (other.return_type != nullptr) + return_type = other.return_type->clone_type (); + else + return_type = nullptr; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); +} + +Function & +Function::operator= (Function const &other) +{ + VisItem::operator= (other); + function_name = other.function_name; + qualifiers = other.qualifiers; + function_params = other.function_params; + + // guard to prevent null dereference (always required) + if (other.return_type != nullptr) + return_type = other.return_type->clone_type (); + else + return_type = nullptr; + + where_clause = other.where_clause; + function_body = other.function_body->clone_block_expr (); + locus = other.locus; + self = other.self; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + return *this; +} + +TypeAlias::TypeAlias (Analysis::NodeMapping mappings, Identifier new_type_name, + std::vector> generic_params, + WhereClause where_clause, + std::unique_ptr existing_type, Visibility vis, + AST::AttrVec outer_attrs, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + new_type_name (std::move (new_type_name)), + generic_params (std::move (generic_params)), + where_clause (std::move (where_clause)), + existing_type (std::move (existing_type)), locus (locus) +{} + +TypeAlias::TypeAlias (TypeAlias const &other) + : VisItem (other), new_type_name (other.new_type_name), + where_clause (other.where_clause), + existing_type (other.existing_type->clone_type ()), locus (other.locus) +{ + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); +} + +TypeAlias & +TypeAlias::operator= (TypeAlias const &other) +{ + VisItem::operator= (other); + new_type_name = other.new_type_name; + where_clause = other.where_clause; + existing_type = other.existing_type->clone_type (); + locus = other.locus; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + return *this; +} + +StructField::StructField (Analysis::NodeMapping mappings, Identifier field_name, + std::unique_ptr field_type, Visibility vis, + location_t locus, AST::AttrVec outer_attrs) + : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), + field_name (std::move (field_name)), field_type (std::move (field_type)), + mappings (mappings), locus (locus) +{} + +StructField::StructField (StructField const &other) + : outer_attrs (other.outer_attrs), visibility (other.visibility), + field_name (other.field_name), field_type (other.field_type->clone_type ()), + mappings (other.mappings) +{} + +StructField & +StructField::operator= (StructField const &other) +{ + field_name = other.field_name; + field_type = other.field_type->clone_type (); + visibility = other.visibility; + outer_attrs = other.outer_attrs; + mappings = other.mappings; + + return *this; +} + +TupleField::TupleField (Analysis::NodeMapping mapping, + std::unique_ptr field_type, Visibility vis, + location_t locus, AST::AttrVec outer_attrs) + : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), + field_type (std::move (field_type)), locus (locus), mappings (mapping) +{} + +TupleField::TupleField (TupleField const &other) + : outer_attrs (other.outer_attrs), visibility (other.visibility), + field_type (other.field_type->clone_type ()), locus (other.locus), + mappings (other.mappings) +{} + +TupleField & +TupleField::operator= (TupleField const &other) +{ + field_type = other.field_type->clone_type (); + visibility = other.visibility; + outer_attrs = other.outer_attrs; + locus = other.locus; + mappings = other.mappings; + + return *this; +} + +TupleStruct::TupleStruct ( + Analysis::NodeMapping mappings, std::vector fields, + Identifier struct_name, + std::vector> generic_params, + WhereClause where_clause, Visibility vis, AST::AttrVec outer_attrs, + location_t locus) + : Struct (std::move (mappings), std::move (struct_name), + std::move (generic_params), std::move (where_clause), + std::move (vis), locus, std::move (outer_attrs)), + fields (std::move (fields)) +{} + +EnumItem::EnumItem (Analysis::NodeMapping mappings, Identifier variant_name, + AST::AttrVec outer_attrs, location_t locus) + : Item (std::move (mappings), std::move (outer_attrs)), + variant_name (std::move (variant_name)), locus (locus) +{} + +EnumItemTuple::EnumItemTuple (Analysis::NodeMapping mappings, + Identifier variant_name, + std::vector tuple_fields, + AST::AttrVec outer_attrs, location_t locus) + : EnumItem (std::move (mappings), std::move (variant_name), + std::move (outer_attrs), locus), + tuple_fields (std::move (tuple_fields)) +{} + +EnumItemStruct::EnumItemStruct (Analysis::NodeMapping mappings, + Identifier variant_name, + std::vector struct_fields, + AST::AttrVec outer_attrs, location_t locus) + : EnumItem (std::move (mappings), std::move (variant_name), + std::move (outer_attrs), locus), + struct_fields (std::move (struct_fields)) +{} + +EnumItemDiscriminant::EnumItemDiscriminant (Analysis::NodeMapping mappings, + Identifier variant_name, + std::unique_ptr expr, + AST::AttrVec outer_attrs, + location_t locus) + : EnumItem (std::move (mappings), std::move (variant_name), + std::move (outer_attrs), locus), + expression (std::move (expr)) +{} + +EnumItemDiscriminant::EnumItemDiscriminant (EnumItemDiscriminant const &other) + : EnumItem (other), expression (other.expression->clone_expr ()) +{} + +EnumItemDiscriminant & +EnumItemDiscriminant::operator= (EnumItemDiscriminant const &other) +{ + EnumItem::operator= (other); + expression = other.expression->clone_expr (); + // variant_name = other.variant_name; + // outer_attrs = other.outer_attrs; + + return *this; +} + +Enum::Enum (Analysis::NodeMapping mappings, Identifier enum_name, + Visibility vis, + std::vector> generic_params, + WhereClause where_clause, + std::vector> items, + AST::AttrVec outer_attrs, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + enum_name (std::move (enum_name)), + generic_params (std::move (generic_params)), + where_clause (std::move (where_clause)), items (std::move (items)), + locus (locus) +{} + +Enum::Enum (Enum const &other) + : VisItem (other), enum_name (other.enum_name), + where_clause (other.where_clause), locus (other.locus) +{ + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_enum_item ()); +} + +Enum & +Enum::operator= (Enum const &other) +{ + VisItem::operator= (other); + enum_name = other.enum_name; + where_clause = other.where_clause; + locus = other.locus; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_enum_item ()); + + return *this; +} + +Union::Union (Analysis::NodeMapping mappings, Identifier union_name, + Visibility vis, + std::vector> generic_params, + WhereClause where_clause, std::vector variants, + AST::AttrVec outer_attrs, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + union_name (std::move (union_name)), + generic_params (std::move (generic_params)), + where_clause (std::move (where_clause)), variants (std::move (variants)), + locus (locus) +{} + +Union::Union (Union const &other) + : VisItem (other), union_name (other.union_name), + where_clause (other.where_clause), variants (other.variants), + locus (other.locus) +{ + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); +} + +Union & +Union::operator= (Union const &other) +{ + VisItem::operator= (other); + union_name = other.union_name; + where_clause = other.where_clause; + variants = other.variants; + locus = other.locus; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + return *this; +} + +ConstantItem::ConstantItem (Analysis::NodeMapping mappings, Identifier ident, + Visibility vis, std::unique_ptr type, + std::unique_ptr const_expr, + AST::AttrVec outer_attrs, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + identifier (std::move (ident)), type (std::move (type)), + const_expr (std::move (const_expr)), locus (locus) +{} + +ConstantItem::ConstantItem (ConstantItem const &other) + : VisItem (other), identifier (other.identifier), + type (other.type->clone_type ()), + const_expr (other.const_expr->clone_expr ()), locus (other.locus) +{} + +ConstantItem & +ConstantItem::operator= (ConstantItem const &other) +{ + VisItem::operator= (other); + identifier = other.identifier; + type = other.type->clone_type (); + const_expr = other.const_expr->clone_expr (); + locus = other.locus; + + return *this; +} + +StaticItem::StaticItem (Analysis::NodeMapping mappings, Identifier name, + Mutability mut, std::unique_ptr type, + std::unique_ptr expr, Visibility vis, + AST::AttrVec outer_attrs, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + mut (mut), name (std::move (name)), type (std::move (type)), + expr (std::move (expr)), locus (locus) +{} + +StaticItem::StaticItem (StaticItem const &other) + : VisItem (other), mut (other.mut), name (other.name), + type (other.type->clone_type ()), expr (other.expr->clone_expr ()), + locus (other.locus) +{} + +StaticItem & +StaticItem::operator= (StaticItem const &other) +{ + VisItem::operator= (other); + name = other.name; + mut = other.mut; + type = other.type->clone_type (); + expr = other.expr->clone_expr (); + locus = other.locus; + + return *this; +} + +TraitFunctionDecl::TraitFunctionDecl ( + Identifier function_name, FunctionQualifiers qualifiers, + std::vector> generic_params, SelfParam self, + std::vector function_params, std::unique_ptr return_type, + WhereClause where_clause) + : qualifiers (std::move (qualifiers)), + function_name (std::move (function_name)), + generic_params (std::move (generic_params)), + function_params (std::move (function_params)), + return_type (std::move (return_type)), + where_clause (std::move (where_clause)), self (std::move (self)) +{} + +TraitFunctionDecl::TraitFunctionDecl (TraitFunctionDecl const &other) + : qualifiers (other.qualifiers), function_name (other.function_name), + function_params (other.function_params), + return_type (other.return_type->clone_type ()), + where_clause (other.where_clause), self (other.self) +{ + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); +} + +TraitFunctionDecl & +TraitFunctionDecl::operator= (TraitFunctionDecl const &other) +{ + function_name = other.function_name; + qualifiers = other.qualifiers; + function_params = other.function_params; + return_type = other.return_type->clone_type (); + where_clause = other.where_clause; + self = other.self; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + return *this; +} + +TraitItemFunc::TraitItemFunc (Analysis::NodeMapping mappings, + TraitFunctionDecl decl, + std::unique_ptr block_expr, + AST::AttrVec outer_attrs, location_t locus) + : TraitItem (mappings), outer_attrs (std::move (outer_attrs)), + decl (std::move (decl)), block_expr (std::move (block_expr)), locus (locus) +{} + +TraitItemFunc::TraitItemFunc (TraitItemFunc const &other) + : TraitItem (other.mappings), outer_attrs (other.outer_attrs), + decl (other.decl), locus (other.locus) +{ + if (other.block_expr != nullptr) + block_expr = other.block_expr->clone_block_expr (); +} + +TraitItemFunc & +TraitItemFunc::operator= (TraitItemFunc const &other) +{ + TraitItem::operator= (other); + outer_attrs = other.outer_attrs; + decl = other.decl; + locus = other.locus; + mappings = other.mappings; + if (other.block_expr != nullptr) + block_expr = other.block_expr->clone_block_expr (); + + return *this; +} + +TraitItemConst::TraitItemConst (Analysis::NodeMapping mappings, Identifier name, + std::unique_ptr type, + std::unique_ptr expr, + AST::AttrVec outer_attrs, location_t locus) + : TraitItem (mappings), outer_attrs (std::move (outer_attrs)), + name (std::move (name)), type (std::move (type)), expr (std::move (expr)), + locus (locus) +{} + +TraitItemConst::TraitItemConst (TraitItemConst const &other) + : TraitItem (other.mappings), outer_attrs (other.outer_attrs), + name (other.name), type (other.type->clone_type ()), + expr (other.expr->clone_expr ()), locus (other.locus) +{} + +TraitItemConst & +TraitItemConst::operator= (TraitItemConst const &other) +{ + TraitItem::operator= (other); + outer_attrs = other.outer_attrs; + name = other.name; + type = other.type->clone_type (); + expr = other.expr->clone_expr (); + locus = other.locus; + mappings = other.mappings; + + return *this; +} + +TraitItemType::TraitItemType ( + Analysis::NodeMapping mappings, Identifier name, + std::vector> type_param_bounds, + AST::AttrVec outer_attrs, location_t locus) + : TraitItem (mappings), outer_attrs (std::move (outer_attrs)), + name (std::move (name)), type_param_bounds (std::move (type_param_bounds)), + locus (locus) +{} + +TraitItemType::TraitItemType (TraitItemType const &other) + : TraitItem (other.mappings), outer_attrs (other.outer_attrs), + name (other.name), locus (other.locus) +{ + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); +} + +TraitItemType & +TraitItemType::operator= (TraitItemType const &other) +{ + TraitItem::operator= (other); + outer_attrs = other.outer_attrs; + name = other.name; + locus = other.locus; + mappings = other.mappings; + + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); + + return *this; +} + +Trait::Trait (Analysis::NodeMapping mappings, Identifier name, + Unsafety unsafety, + std::vector> generic_params, + std::vector> type_param_bounds, + WhereClause where_clause, + std::vector> trait_items, + Visibility vis, AST::AttrVec outer_attrs, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + unsafety (unsafety), name (std::move (name)), + generic_params (std::move (generic_params)), + type_param_bounds (std::move (type_param_bounds)), + where_clause (std::move (where_clause)), + trait_items (std::move (trait_items)), locus (locus) +{} + +Trait::Trait (Trait const &other) + : VisItem (other), unsafety (other.unsafety), name (other.name), + where_clause (other.where_clause), locus (other.locus) +{ + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); + + trait_items.reserve (other.trait_items.size ()); + for (const auto &e : other.trait_items) + trait_items.push_back (e->clone_trait_item ()); +} + +Trait & +Trait::operator= (Trait const &other) +{ + VisItem::operator= (other); + name = other.name; + unsafety = other.unsafety; + where_clause = other.where_clause; + locus = other.locus; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); + + trait_items.reserve (other.trait_items.size ()); + for (const auto &e : other.trait_items) + trait_items.push_back (e->clone_trait_item ()); + + return *this; +} + +ImplBlock::ImplBlock (Analysis::NodeMapping mappings, + std::vector> impl_items, + std::vector> generic_params, + std::unique_ptr impl_type, + std::unique_ptr trait_ref, + WhereClause where_clause, BoundPolarity polarity, + Visibility vis, AST::AttrVec inner_attrs, + AST::AttrVec outer_attrs, location_t locus, bool unsafe) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + WithInnerAttrs (std::move (inner_attrs)), + generic_params (std::move (generic_params)), + impl_type (std::move (impl_type)), trait_ref (std::move (trait_ref)), + where_clause (std::move (where_clause)), polarity (polarity), locus (locus), + impl_items (std::move (impl_items)), unsafe (unsafe) +{} + +ImplBlock::ImplBlock (ImplBlock const &other) + : VisItem (other), WithInnerAttrs (other.inner_attrs), + impl_type (other.impl_type->clone_type ()), + where_clause (other.where_clause), polarity (other.polarity), + locus (other.locus), unsafe (other.unsafe) +{ + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + impl_items.reserve (other.impl_items.size ()); + for (const auto &e : other.impl_items) + impl_items.push_back (e->clone_inherent_impl_item ()); +} + +ImplBlock & +ImplBlock::operator= (ImplBlock const &other) +{ + VisItem::operator= (other); + impl_type = other.impl_type->clone_type (); + where_clause = other.where_clause; + polarity = other.polarity; + inner_attrs = other.inner_attrs; + locus = other.locus; + unsafe = other.unsafe; + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + impl_items.reserve (other.impl_items.size ()); + for (const auto &e : other.impl_items) + impl_items.push_back (e->clone_inherent_impl_item ()); + + return *this; +} + +ExternalItem::ExternalItem (Analysis::NodeMapping mappings, + Identifier item_name, Visibility vis, + AST::AttrVec outer_attrs, location_t locus) + : mappings (mappings), outer_attrs (std::move (outer_attrs)), + visibility (std::move (vis)), item_name (std::move (item_name)), + locus (locus) +{} + +ExternalItem::ExternalItem (ExternalItem const &other) + : mappings (other.mappings), outer_attrs (other.outer_attrs), + visibility (other.visibility), item_name (other.item_name), + locus (other.locus) +{} + +ExternalItem & +ExternalItem::operator= (ExternalItem const &other) +{ + mappings = other.mappings; + item_name = other.item_name; + visibility = other.visibility; + outer_attrs = other.outer_attrs; + locus = other.locus; + + return *this; +} + +ExternalStaticItem::ExternalStaticItem (Analysis::NodeMapping mappings, + Identifier item_name, + std::unique_ptr item_type, + Mutability mut, Visibility vis, + AST::AttrVec outer_attrs, + location_t locus) + : ExternalItem (std::move (mappings), std::move (item_name), std::move (vis), + std::move (outer_attrs), locus), + mut (mut), item_type (std::move (item_type)) +{} + +ExternalStaticItem::ExternalStaticItem (ExternalStaticItem const &other) + : ExternalItem (other), mut (other.mut), + item_type (other.item_type->clone_type ()) +{} + +ExternalStaticItem & +ExternalStaticItem::operator= (ExternalStaticItem const &other) +{ + ExternalItem::operator= (other); + item_type = other.item_type->clone_type (); + mut = other.mut; + + return *this; +} + +NamedFunctionParam::NamedFunctionParam (Analysis::NodeMapping mappings, + Identifier name, + std::unique_ptr param_type) + : name (std::move (name)), param_type (std::move (param_type)), + mappings (std::move (mappings)) +{} + +NamedFunctionParam::NamedFunctionParam (NamedFunctionParam const &other) + : name (other.name), param_type (other.param_type->clone_type ()), + mappings (other.mappings) +{} + +NamedFunctionParam & +NamedFunctionParam::operator= (NamedFunctionParam const &other) +{ + mappings = other.mappings; + name = other.name; + param_type = other.param_type->clone_type (); + // has_name = other.has_name; + + return *this; +} + +ExternalFunctionItem::ExternalFunctionItem ( + Analysis::NodeMapping mappings, Identifier item_name, + std::vector> generic_params, + std::unique_ptr return_type, WhereClause where_clause, + std::vector function_params, bool has_variadics, + Visibility vis, AST::AttrVec outer_attrs, location_t locus) + : ExternalItem (std::move (mappings), std::move (item_name), std::move (vis), + std::move (outer_attrs), locus), + generic_params (std::move (generic_params)), + return_type (std::move (return_type)), + where_clause (std::move (where_clause)), + function_params (std::move (function_params)), has_variadics (has_variadics) +{} + +ExternalFunctionItem::ExternalFunctionItem (ExternalFunctionItem const &other) + : ExternalItem (other), where_clause (other.where_clause), + function_params (other.function_params), has_variadics (other.has_variadics) +{ + if (other.return_type) + return_type = other.return_type->clone_type (); + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); +} + +ExternalFunctionItem & +ExternalFunctionItem::operator= (ExternalFunctionItem const &other) +{ + ExternalItem::operator= (other); + + where_clause = other.where_clause; + function_params = other.function_params; + has_variadics = other.has_variadics; + + if (other.return_type) + return_type = other.return_type->clone_type (); + + generic_params.reserve (other.generic_params.size ()); + for (const auto &e : other.generic_params) + generic_params.push_back (e->clone_generic_param ()); + + return *this; +} + +ExternalTypeItem::ExternalTypeItem (Analysis::NodeMapping mappings, + Identifier item_name, Visibility vis, + location_t locus) + : ExternalItem (std::move (mappings), std::move (item_name), + Visibility (std::move (vis)), + /* FIXME: Is that correct? */ + {}, locus) +{} + +ExternalTypeItem::ExternalTypeItem (ExternalTypeItem const &other) + : ExternalItem (other) +{} + +ExternBlock::ExternBlock ( + Analysis::NodeMapping mappings, ABI abi, + std::vector> extern_items, Visibility vis, + AST::AttrVec inner_attrs, AST::AttrVec outer_attrs, location_t locus) + : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), + WithInnerAttrs (std::move (inner_attrs)), abi (abi), + extern_items (std::move (extern_items)), locus (locus) +{} + +ExternBlock::ExternBlock (ExternBlock const &other) + : VisItem (other), WithInnerAttrs (other.inner_attrs), abi (other.abi), + locus (other.locus) +{ + extern_items.reserve (other.extern_items.size ()); + for (const auto &e : other.extern_items) + extern_items.push_back (e->clone_external_item ()); +} + +ExternBlock & +ExternBlock::operator= (ExternBlock const &other) +{ + VisItem::operator= (other); + abi = other.abi; + inner_attrs = other.inner_attrs; + locus = other.locus; + + extern_items.reserve (other.extern_items.size ()); + for (const auto &e : other.extern_items) + extern_items.push_back (e->clone_external_item ()); + + return *this; +} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 92e8fc3712e1..37c5313869ce 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -20,17 +20,74 @@ #define RUST_HIR_ITEM_H #include "rust-abi.h" -#include "rust-ast-full-decls.h" +#include "rust-hir-stmt.h" #include "rust-common.h" -#include "rust-hir-expr.h" -#include "rust-hir.h" -#include "rust-hir-path.h" +#include "rust-hir-visibility.h" +#include "rust-hir-generic-param.h" namespace Rust { namespace HIR { -// forward decls -class BlockExpr; -class TypePath; + +// Rust "item" HIR node (declaration of top-level/module-level allowed stuff) +class Item : public Stmt, public WithOuterAttrs +{ + // TODO: should outer attrs be defined here or in each derived class? +public: + enum class ItemKind + { + Static, + Constant, + TypeAlias, + Function, + UseDeclaration, + ExternBlock, + ExternCrate, + Struct, + Union, + Enum, + EnumItem, // FIXME: ARTHUR: Do we need that? + Trait, + Impl, + Module, + }; + + static std::string item_kind_string (ItemKind kind); + + virtual ItemKind get_item_kind () const = 0; + + // Unique pointer custom clone function + std::unique_ptr clone_item () const + { + return std::unique_ptr (clone_item_impl ()); + } + + BaseKind get_hir_kind () override { return Node::BaseKind::ITEM; } + + std::string as_string () const override; + + /* Adds crate names to the vector passed by reference, if it can + * (polymorphism). */ + virtual void + add_crate_name (std::vector &names ATTRIBUTE_UNUSED) const + {} + + bool is_item () const override final { return true; } + +protected: + // Constructor + Item (Analysis::NodeMapping mappings, + AST::AttrVec outer_attribs = AST::AttrVec ()) + : Stmt (std::move (mappings)), WithOuterAttrs (std::move (outer_attribs)) + {} + + // Clone function implementation as pure virtual method + virtual Item *clone_item_impl () const = 0; + + /* Save having to specify two clone methods in derived classes by making + * statement clone return item clone. Hopefully won't affect performance too + * much. */ + Item *clone_stmt_impl () const override { return clone_item_impl (); } +}; // A type generic parameter (as opposed to a lifetime generic parameter) class TypeParam : public GenericParam @@ -65,47 +122,13 @@ class TypeParam : public GenericParam std::vector> type_param_bounds = std::vector> (), std::unique_ptr type = nullptr, - AST::AttrVec outer_attrs = std::vector ()) - : GenericParam (mappings), outer_attrs (std::move (outer_attrs)), - type_representation (std::move (type_representation)), - type_param_bounds (std::move (type_param_bounds)), - type (std::move (type)), locus (locus) - {} + AST::AttrVec outer_attrs = std::vector ()); // Copy constructor uses clone - TypeParam (TypeParam const &other) - : GenericParam (other.mappings), outer_attrs (other.outer_attrs), - type_representation (other.type_representation), locus (other.locus) - { - // guard to prevent null pointer dereference - if (other.type != nullptr) - type = other.type->clone_type (); - - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - } + TypeParam (TypeParam const &other); // Overloaded assignment operator to clone - TypeParam &operator= (TypeParam const &other) - { - type_representation = other.type_representation; - outer_attrs = other.outer_attrs; - locus = other.locus; - mappings = other.mappings; - - // guard to prevent null pointer dereference - if (other.type != nullptr) - type = other.type->clone_type (); - else - type = nullptr; - - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - - return *this; - } + TypeParam &operator= (TypeParam const &other); // move constructors TypeParam (TypeParam &&other) = default; TypeParam &operator= (TypeParam &&other) = default; @@ -120,16 +143,9 @@ class TypeParam : public GenericParam Type &get_type () { return *type; } - Analysis::NodeMapping get_type_mappings () const - { - rust_assert (type != nullptr); - return type->get_mappings (); - } + Analysis::NodeMapping get_type_mappings () const; - std::vector> &get_type_param_bounds () - { - return type_param_bounds; - } + std::vector> &get_type_param_bounds (); protected: // Clone function implementation as (not pure) virtual method @@ -234,35 +250,13 @@ class TypeBoundWhereClauseItem : public WhereClauseItem Analysis::NodeMapping mappings, std::vector for_lifetimes, std::unique_ptr bound_type, std::vector> type_param_bounds, - location_t locus) - : for_lifetimes (std::move (for_lifetimes)), - bound_type (std::move (bound_type)), - type_param_bounds (std::move (type_param_bounds)), - mappings (std::move (mappings)), locus (locus) - {} + location_t locus); // Copy constructor requires clone - TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other) - : for_lifetimes (other.for_lifetimes), - bound_type (other.bound_type->clone_type ()), mappings (other.mappings) - { - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - } + TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other); // Overload assignment operator to clone - TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other) - { - mappings = other.mappings; - for_lifetimes = other.for_lifetimes; - bound_type = other.bound_type->clone_type (); - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - - return *this; - } + TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other); // move constructors TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default; @@ -279,10 +273,7 @@ class TypeBoundWhereClauseItem : public WhereClauseItem Type &get_bound_type () { return *bound_type; } - std::vector> &get_type_param_bounds () - { - return type_param_bounds; - } + std::vector> &get_type_param_bounds (); Analysis::NodeMapping get_mappings () const override final { @@ -379,51 +370,22 @@ struct SelfParam Analysis::NodeMapping mappings; SelfParam (Analysis::NodeMapping mappings, ImplicitSelfKind self_kind, - Lifetime lifetime, Type *type) - : self_kind (self_kind), lifetime (std::move (lifetime)), type (type), - mappings (mappings) - {} + Lifetime lifetime, Type *type); public: // Type-based self parameter (not ref, no lifetime) SelfParam (Analysis::NodeMapping mappings, std::unique_ptr type, - bool is_mut, location_t locus) - : self_kind (is_mut ? ImplicitSelfKind::MUT : ImplicitSelfKind::IMM), - lifetime ( - Lifetime (mappings, AST::Lifetime::LifetimeType::NAMED, "", locus)), - type (std::move (type)), locus (locus), mappings (mappings) - {} + bool is_mut, location_t locus); // Lifetime-based self parameter (is ref, no type) SelfParam (Analysis::NodeMapping mappings, Lifetime lifetime, bool is_mut, - location_t locus) - : self_kind (is_mut ? ImplicitSelfKind::MUT_REF - : ImplicitSelfKind::IMM_REF), - lifetime (std::move (lifetime)), locus (locus), mappings (mappings) - {} + location_t locus); // Copy constructor requires clone - SelfParam (SelfParam const &other) - : self_kind (other.self_kind), lifetime (other.lifetime), - locus (other.locus), mappings (other.mappings) - { - if (other.type != nullptr) - type = other.type->clone_type (); - } + SelfParam (SelfParam const &other); // Overload assignment operator to use clone - SelfParam &operator= (SelfParam const &other) - { - if (other.type != nullptr) - type = other.type->clone_type (); - - self_kind = other.self_kind; - lifetime = other.lifetime; - locus = other.locus; - mappings = other.mappings; - - return *this; - } + SelfParam &operator= (SelfParam const &other); // move constructors SelfParam (SelfParam &&other) = default; @@ -456,25 +418,11 @@ struct SelfParam Analysis::NodeMapping get_mappings () { return mappings; } - Mutability get_mut () const - { - return (self_kind == ImplicitSelfKind::MUT - || self_kind == ImplicitSelfKind::MUT_REF) - ? Mutability::Mut - : Mutability::Imm; - } + Mutability get_mut () const; - bool is_mut () const - { - return self_kind == ImplicitSelfKind::MUT - || self_kind == ImplicitSelfKind::MUT_REF; - } + bool is_mut () const; - bool is_ref () const - { - return self_kind == ImplicitSelfKind::IMM_REF - || self_kind == ImplicitSelfKind::MUT_REF; - } + bool is_ref () const; }; // Qualifiers for function, i.e. const, unsafe, extern etc. @@ -516,28 +464,13 @@ struct FunctionParam public: FunctionParam (Analysis::NodeMapping mappings, std::unique_ptr param_name, - std::unique_ptr param_type, location_t locus) - : param_name (std::move (param_name)), type (std::move (param_type)), - locus (locus), mappings (mappings) - {} + std::unique_ptr param_type, location_t locus); // Copy constructor uses clone - FunctionParam (FunctionParam const &other) - : param_name (other.param_name->clone_pattern ()), - type (other.type->clone_type ()), locus (other.locus), - mappings (other.mappings) - {} + FunctionParam (FunctionParam const &other); // Overload assignment operator to use clone - FunctionParam &operator= (FunctionParam const &other) - { - param_name = other.param_name->clone_pattern (); - type = other.type->clone_type (); - locus = other.locus; - mappings = other.mappings; - - return *this; - } + FunctionParam &operator= (FunctionParam const &other); // move constructors FunctionParam (FunctionParam &&other) = default; @@ -554,58 +487,6 @@ struct FunctionParam const Analysis::NodeMapping &get_mappings () const { return mappings; } }; -// Visibility of an item -struct Visibility -{ -public: - enum VisType - { - PRIVATE, - PUBLIC, - RESTRICTED, - ERROR, - }; - -private: - VisType vis_type; - HIR::SimplePath path; - location_t locus; - - // should this store location info? - -public: - Visibility (VisType vis_type, - HIR::SimplePath path = HIR::SimplePath::create_empty (), - location_t locus = UNDEF_LOCATION) - : vis_type (vis_type), path (std::move (path)), locus (locus) - {} - - // Returns whether visibility is in an error state. - bool is_error () const { return vis_type == ERROR; } - - // Does the current visibility refer to a simple `pub ` entirely public - bool is_public () const { return vis_type == PUBLIC; } - - // Is the current visibility public restricted to a certain path - bool is_restricted () const { return vis_type == RESTRICTED; } - - // Creates an error visibility. - static Visibility create_error () - { - return Visibility (ERROR, HIR::SimplePath::create_empty ()); - } - - VisType get_vis_type () const { return vis_type; } - - const HIR::SimplePath &get_path () const - { - rust_assert (!is_error ()); - return path; - } - - std::string as_string () const; -}; - // Item that supports visibility - abstract base class class VisItem : public Item { @@ -620,18 +501,10 @@ class VisItem : public Item {} // Visibility copy constructor - VisItem (VisItem const &other) : Item (other), visibility (other.visibility) - {} + VisItem (VisItem const &other); // Overload assignment operator to clone - VisItem &operator= (VisItem const &other) - { - Item::operator= (other); - visibility = other.visibility; - // outer_attrs = other.outer_attrs; - - return *this; - } + VisItem &operator= (VisItem const &other); // move constructors VisItem (VisItem &&other) = default; @@ -673,34 +546,13 @@ class Module : public VisItem, public WithInnerAttrs location_t locus, std::vector> items, Visibility visibility = Visibility::create_error (), AST::AttrVec inner_attrs = AST::AttrVec (), - AST::AttrVec outer_attrs = AST::AttrVec ()) - : VisItem (std::move (mappings), std::move (visibility), - std::move (outer_attrs)), - WithInnerAttrs (std::move (inner_attrs)), module_name (module_name), - locus (locus), items (std::move (items)) - {} + AST::AttrVec outer_attrs = AST::AttrVec ()); // Copy constructor with vector clone - Module (Module const &other) - : VisItem (other), WithInnerAttrs (other.inner_attrs), module_name ("") - { - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_item ()); - } + Module (Module const &other); // Overloaded assignment operator with vector clone - Module &operator= (Module const &other) - { - VisItem::operator= (other); - inner_attrs = other.inner_attrs; - - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_item ()); - - return *this; - } + Module &operator= (Module const &other); // move constructors Module (Module &&other) = default; @@ -1120,63 +972,13 @@ class Function : public VisItem, public ImplItem std::vector function_params, std::unique_ptr return_type, WhereClause where_clause, std::unique_ptr function_body, Visibility vis, - AST::AttrVec outer_attrs, SelfParam self, location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - qualifiers (std::move (qualifiers)), - function_name (std::move (function_name)), - generic_params (std::move (generic_params)), - function_params (std::move (function_params)), - return_type (std::move (return_type)), - where_clause (std::move (where_clause)), - function_body (std::move (function_body)), self (std::move (self)), - locus (locus) - {} + AST::AttrVec outer_attrs, SelfParam self, location_t locus); // Copy constructor with clone - Function (Function const &other) - : VisItem (other), qualifiers (other.qualifiers), - function_name (other.function_name), - function_params (other.function_params), - where_clause (other.where_clause), - function_body (other.function_body->clone_block_expr ()), - self (other.self), locus (other.locus) - { - // guard to prevent null dereference (always required) - if (other.return_type != nullptr) - return_type = other.return_type->clone_type (); - else - return_type = nullptr; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - } + Function (Function const &other); // Overloaded assignment operator to clone - Function &operator= (Function const &other) - { - VisItem::operator= (other); - function_name = other.function_name; - qualifiers = other.qualifiers; - function_params = other.function_params; - - // guard to prevent null dereference (always required) - if (other.return_type != nullptr) - return_type = other.return_type->clone_type (); - else - return_type = nullptr; - - where_clause = other.where_clause; - function_body = other.function_body->clone_block_expr (); - locus = other.locus; - self = other.self; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - return *this; - } + Function &operator= (Function const &other); // move constructors Function (Function &&other) = default; @@ -1280,40 +1082,13 @@ class TypeAlias : public VisItem, public ImplItem TypeAlias (Analysis::NodeMapping mappings, Identifier new_type_name, std::vector> generic_params, WhereClause where_clause, std::unique_ptr existing_type, - Visibility vis, AST::AttrVec outer_attrs, location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - new_type_name (std::move (new_type_name)), - generic_params (std::move (generic_params)), - where_clause (std::move (where_clause)), - existing_type (std::move (existing_type)), locus (locus) - {} + Visibility vis, AST::AttrVec outer_attrs, location_t locus); // Copy constructor - TypeAlias (TypeAlias const &other) - : VisItem (other), new_type_name (other.new_type_name), - where_clause (other.where_clause), - existing_type (other.existing_type->clone_type ()), locus (other.locus) - { - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - } + TypeAlias (TypeAlias const &other); // Overloaded assignment operator to clone - TypeAlias &operator= (TypeAlias const &other) - { - VisItem::operator= (other); - new_type_name = other.new_type_name; - where_clause = other.where_clause; - existing_type = other.existing_type->clone_type (); - locus = other.locus; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - return *this; - } + TypeAlias &operator= (TypeAlias const &other); // move constructors TypeAlias (TypeAlias &&other) = default; @@ -1468,32 +1243,15 @@ class StructField StructField (Analysis::NodeMapping mappings, Identifier field_name, std::unique_ptr field_type, Visibility vis, - location_t locus, AST::AttrVec outer_attrs = AST::AttrVec ()) - : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), - field_name (std::move (field_name)), field_type (std::move (field_type)), - mappings (mappings), locus (locus) - {} + location_t locus, AST::AttrVec outer_attrs = AST::AttrVec ()); // Copy constructor - StructField (StructField const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - field_name (other.field_name), - field_type (other.field_type->clone_type ()), mappings (other.mappings) - {} + StructField (StructField const &other); ~StructField () = default; // Overloaded assignment operator to clone - StructField &operator= (StructField const &other) - { - field_name = other.field_name; - field_type = other.field_type->clone_type (); - visibility = other.visibility; - outer_attrs = other.outer_attrs; - mappings = other.mappings; - - return *this; - } + StructField &operator= (StructField const &other); // move constructors StructField (StructField &&other) = default; @@ -1598,31 +1356,15 @@ class TupleField // Complete constructor TupleField (Analysis::NodeMapping mapping, std::unique_ptr field_type, Visibility vis, location_t locus, - AST::AttrVec outer_attrs = AST::AttrVec ()) - : outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)), - field_type (std::move (field_type)), locus (locus), mappings (mapping) - {} + AST::AttrVec outer_attrs = AST::AttrVec ()); // Copy constructor with clone - TupleField (TupleField const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - field_type (other.field_type->clone_type ()), locus (other.locus), - mappings (other.mappings) - {} + TupleField (TupleField const &other); ~TupleField () = default; // Overloaded assignment operator to clone - TupleField &operator= (TupleField const &other) - { - field_type = other.field_type->clone_type (); - visibility = other.visibility; - outer_attrs = other.outer_attrs; - locus = other.locus; - mappings = other.mappings; - - return *this; - } + TupleField &operator= (TupleField const &other); // move constructors TupleField (TupleField &&other) = default; @@ -1656,12 +1398,7 @@ class TupleStruct : public Struct Identifier struct_name, std::vector> generic_params, WhereClause where_clause, Visibility vis, - AST::AttrVec outer_attrs, location_t locus) - : Struct (std::move (mappings), std::move (struct_name), - std::move (generic_params), std::move (where_clause), - std::move (vis), locus, std::move (outer_attrs)), - fields (std::move (fields)) - {} + AST::AttrVec outer_attrs, location_t locus); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRStmtVisitor &vis) override; @@ -1706,10 +1443,7 @@ class EnumItem : public Item }; EnumItem (Analysis::NodeMapping mappings, Identifier variant_name, - AST::AttrVec outer_attrs, location_t locus) - : Item (std::move (mappings), std::move (outer_attrs)), - variant_name (std::move (variant_name)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Unique pointer custom clone function std::unique_ptr clone_enum_item () const @@ -1752,11 +1486,7 @@ class EnumItemTuple : public EnumItem EnumItemTuple (Analysis::NodeMapping mappings, Identifier variant_name, std::vector tuple_fields, AST::AttrVec outer_attrs, - location_t locus) - : EnumItem (std::move (mappings), std::move (variant_name), - std::move (outer_attrs), locus), - tuple_fields (std::move (tuple_fields)) - {} + location_t locus); std::string as_string () const override; @@ -1790,11 +1520,7 @@ class EnumItemStruct : public EnumItem EnumItemStruct (Analysis::NodeMapping mappings, Identifier variant_name, std::vector struct_fields, - AST::AttrVec outer_attrs, location_t locus) - : EnumItem (std::move (mappings), std::move (variant_name), - std::move (outer_attrs), locus), - struct_fields (std::move (struct_fields)) - {} + AST::AttrVec outer_attrs, location_t locus); std::string as_string () const override; @@ -1819,27 +1545,13 @@ class EnumItemDiscriminant : public EnumItem public: EnumItemDiscriminant (Analysis::NodeMapping mappings, Identifier variant_name, std::unique_ptr expr, AST::AttrVec outer_attrs, - location_t locus) - : EnumItem (std::move (mappings), std::move (variant_name), - std::move (outer_attrs), locus), - expression (std::move (expr)) - {} + location_t locus); // Copy constructor with clone - EnumItemDiscriminant (EnumItemDiscriminant const &other) - : EnumItem (other), expression (other.expression->clone_expr ()) - {} + EnumItemDiscriminant (EnumItemDiscriminant const &other); // Overloaded assignment operator to clone - EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other) - { - EnumItem::operator= (other); - expression = other.expression->clone_expr (); - // variant_name = other.variant_name; - // outer_attrs = other.outer_attrs; - - return *this; - } + EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other); // move constructors EnumItemDiscriminant (EnumItemDiscriminant &&other) = default; @@ -1903,48 +1615,15 @@ class Enum : public VisItem Enum (Analysis::NodeMapping mappings, Identifier enum_name, Visibility vis, std::vector> generic_params, WhereClause where_clause, std::vector> items, - AST::AttrVec outer_attrs, location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - enum_name (std::move (enum_name)), - generic_params (std::move (generic_params)), - where_clause (std::move (where_clause)), items (std::move (items)), - locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // TODO: constructor with less arguments // Copy constructor with vector clone - Enum (Enum const &other) - : VisItem (other), enum_name (other.enum_name), - where_clause (other.where_clause), locus (other.locus) - { - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_enum_item ()); - } + Enum (Enum const &other); // Overloaded assignment operator with vector clone - Enum &operator= (Enum const &other) - { - VisItem::operator= (other); - enum_name = other.enum_name; - where_clause = other.where_clause; - locus = other.locus; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_enum_item ()); - - return *this; - } + Enum &operator= (Enum const &other); // Move constructors Enum (Enum &&other) = default; @@ -2012,40 +1691,13 @@ class Union : public VisItem Union (Analysis::NodeMapping mappings, Identifier union_name, Visibility vis, std::vector> generic_params, WhereClause where_clause, std::vector variants, - AST::AttrVec outer_attrs, location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - union_name (std::move (union_name)), - generic_params (std::move (generic_params)), - where_clause (std::move (where_clause)), variants (std::move (variants)), - locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // copy constructor with vector clone - Union (Union const &other) - : VisItem (other), union_name (other.union_name), - where_clause (other.where_clause), variants (other.variants), - locus (other.locus) - { - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - } + Union (Union const &other); // overloaded assignment operator with vector clone - Union &operator= (Union const &other) - { - VisItem::operator= (other); - union_name = other.union_name; - where_clause = other.where_clause; - variants = other.variants; - locus = other.locus; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - return *this; - } + Union &operator= (Union const &other); // move constructors Union (Union &&other) = default; @@ -2089,29 +1741,12 @@ class ConstantItem : public VisItem, public ImplItem ConstantItem (Analysis::NodeMapping mappings, Identifier ident, Visibility vis, std::unique_ptr type, std::unique_ptr const_expr, AST::AttrVec outer_attrs, - location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - identifier (std::move (ident)), type (std::move (type)), - const_expr (std::move (const_expr)), locus (locus) - {} + location_t locus); - ConstantItem (ConstantItem const &other) - : VisItem (other), identifier (other.identifier), - type (other.type->clone_type ()), - const_expr (other.const_expr->clone_expr ()), locus (other.locus) - {} + ConstantItem (ConstantItem const &other); // Overload assignment operator to clone - ConstantItem &operator= (ConstantItem const &other) - { - VisItem::operator= (other); - identifier = other.identifier; - type = other.type->clone_type (); - const_expr = other.const_expr->clone_expr (); - locus = other.locus; - - return *this; - } + ConstantItem &operator= (ConstantItem const &other); // move constructors ConstantItem (ConstantItem &&other) = default; @@ -2185,31 +1820,13 @@ class StaticItem : public VisItem StaticItem (Analysis::NodeMapping mappings, Identifier name, Mutability mut, std::unique_ptr type, std::unique_ptr expr, - Visibility vis, AST::AttrVec outer_attrs, location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - mut (mut), name (std::move (name)), type (std::move (type)), - expr (std::move (expr)), locus (locus) - {} + Visibility vis, AST::AttrVec outer_attrs, location_t locus); // Copy constructor with clone - StaticItem (StaticItem const &other) - : VisItem (other), mut (other.mut), name (other.name), - type (other.type->clone_type ()), expr (other.expr->clone_expr ()), - locus (other.locus) - {} + StaticItem (StaticItem const &other); // Overloaded assignment operator to clone - StaticItem &operator= (StaticItem const &other) - { - VisItem::operator= (other); - name = other.name; - mut = other.mut; - type = other.type->clone_type (); - expr = other.expr->clone_expr (); - locus = other.locus; - - return *this; - } + StaticItem &operator= (StaticItem const &other); // move constructors StaticItem (StaticItem &&other) = default; @@ -2258,45 +1875,15 @@ class TraitFunctionDecl std::vector> generic_params, SelfParam self, std::vector function_params, std::unique_ptr return_type, - WhereClause where_clause) - : qualifiers (std::move (qualifiers)), - function_name (std::move (function_name)), - generic_params (std::move (generic_params)), - function_params (std::move (function_params)), - return_type (std::move (return_type)), - where_clause (std::move (where_clause)), self (std::move (self)) - {} + WhereClause where_clause); // Copy constructor with clone - TraitFunctionDecl (TraitFunctionDecl const &other) - : qualifiers (other.qualifiers), function_name (other.function_name), - function_params (other.function_params), - return_type (other.return_type->clone_type ()), - where_clause (other.where_clause), self (other.self) - { - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - } + TraitFunctionDecl (TraitFunctionDecl const &other); ~TraitFunctionDecl () = default; // Overloaded assignment operator with clone - TraitFunctionDecl &operator= (TraitFunctionDecl const &other) - { - function_name = other.function_name; - qualifiers = other.qualifiers; - function_params = other.function_params; - return_type = other.return_type->clone_type (); - where_clause = other.where_clause; - self = other.self; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - return *this; - } + TraitFunctionDecl &operator= (TraitFunctionDecl const &other); // move constructors TraitFunctionDecl (TraitFunctionDecl &&other) = default; @@ -2350,34 +1937,13 @@ class TraitItemFunc : public TraitItem TraitItemFunc (Analysis::NodeMapping mappings, TraitFunctionDecl decl, std::unique_ptr block_expr, - AST::AttrVec outer_attrs, location_t locus) - : TraitItem (mappings), outer_attrs (std::move (outer_attrs)), - decl (std::move (decl)), block_expr (std::move (block_expr)), - locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Copy constructor with clone - TraitItemFunc (TraitItemFunc const &other) - : TraitItem (other.mappings), outer_attrs (other.outer_attrs), - decl (other.decl), locus (other.locus) - { - if (other.block_expr != nullptr) - block_expr = other.block_expr->clone_block_expr (); - } + TraitItemFunc (TraitItemFunc const &other); // Overloaded assignment operator to clone - TraitItemFunc &operator= (TraitItemFunc const &other) - { - TraitItem::operator= (other); - outer_attrs = other.outer_attrs; - decl = other.decl; - locus = other.locus; - mappings = other.mappings; - if (other.block_expr != nullptr) - block_expr = other.block_expr->clone_block_expr (); - - return *this; - } + TraitItemFunc &operator= (TraitItemFunc const &other); // move constructors TraitItemFunc (TraitItemFunc &&other) = default; @@ -2439,32 +2005,13 @@ class TraitItemConst : public TraitItem TraitItemConst (Analysis::NodeMapping mappings, Identifier name, std::unique_ptr type, std::unique_ptr expr, - AST::AttrVec outer_attrs, location_t locus) - : TraitItem (mappings), outer_attrs (std::move (outer_attrs)), - name (std::move (name)), type (std::move (type)), expr (std::move (expr)), - locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Copy constructor with clones - TraitItemConst (TraitItemConst const &other) - : TraitItem (other.mappings), outer_attrs (other.outer_attrs), - name (other.name), type (other.type->clone_type ()), - expr (other.expr->clone_expr ()), locus (other.locus) - {} + TraitItemConst (TraitItemConst const &other); // Overloaded assignment operator to clone - TraitItemConst &operator= (TraitItemConst const &other) - { - TraitItem::operator= (other); - outer_attrs = other.outer_attrs; - name = other.name; - type = other.type->clone_type (); - expr = other.expr->clone_expr (); - locus = other.locus; - mappings = other.mappings; - - return *this; - } + TraitItemConst &operator= (TraitItemConst const &other); // move constructors TraitItemConst (TraitItemConst &&other) = default; @@ -2527,37 +2074,13 @@ class TraitItemType : public TraitItem TraitItemType (Analysis::NodeMapping mappings, Identifier name, std::vector> type_param_bounds, - AST::AttrVec outer_attrs, location_t locus) - : TraitItem (mappings), outer_attrs (std::move (outer_attrs)), - name (std::move (name)), - type_param_bounds (std::move (type_param_bounds)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Copy constructor with vector clone - TraitItemType (TraitItemType const &other) - : TraitItem (other.mappings), outer_attrs (other.outer_attrs), - name (other.name), locus (other.locus) - { - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - } + TraitItemType (TraitItemType const &other); // Overloaded assignment operator with vector clone - TraitItemType &operator= (TraitItemType const &other) - { - TraitItem::operator= (other); - outer_attrs = other.outer_attrs; - name = other.name; - locus = other.locus; - mappings = other.mappings; - - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - - return *this; - } + TraitItemType &operator= (TraitItemType const &other); // default move constructors TraitItemType (TraitItemType &&other) = default; @@ -2645,56 +2168,13 @@ class Trait : public VisItem std::vector> type_param_bounds, WhereClause where_clause, std::vector> trait_items, Visibility vis, - AST::AttrVec outer_attrs, location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - unsafety (unsafety), name (std::move (name)), - generic_params (std::move (generic_params)), - type_param_bounds (std::move (type_param_bounds)), - where_clause (std::move (where_clause)), - trait_items (std::move (trait_items)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Copy constructor with vector clone - Trait (Trait const &other) - : VisItem (other), unsafety (other.unsafety), name (other.name), - where_clause (other.where_clause), locus (other.locus) - { - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - - trait_items.reserve (other.trait_items.size ()); - for (const auto &e : other.trait_items) - trait_items.push_back (e->clone_trait_item ()); - } + Trait (Trait const &other); // Overloaded assignment operator with vector clone - Trait &operator= (Trait const &other) - { - VisItem::operator= (other); - name = other.name; - unsafety = other.unsafety; - where_clause = other.where_clause; - locus = other.locus; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - - trait_items.reserve (other.trait_items.size ()); - for (const auto &e : other.trait_items) - trait_items.push_back (e->clone_trait_item ()); - - return *this; - } + Trait &operator= (Trait const &other); // default move constructors Trait (Trait &&other) = default; @@ -2753,50 +2233,11 @@ class ImplBlock : public VisItem, public WithInnerAttrs std::unique_ptr impl_type, std::unique_ptr trait_ref, WhereClause where_clause, BoundPolarity polarity, Visibility vis, AST::AttrVec inner_attrs, - AST::AttrVec outer_attrs, location_t locus, bool unsafe = false) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - WithInnerAttrs (std::move (inner_attrs)), - generic_params (std::move (generic_params)), - impl_type (std::move (impl_type)), trait_ref (std::move (trait_ref)), - where_clause (std::move (where_clause)), polarity (polarity), - locus (locus), impl_items (std::move (impl_items)), unsafe (unsafe) - {} - - ImplBlock (ImplBlock const &other) - : VisItem (other), WithInnerAttrs (other.inner_attrs), - impl_type (other.impl_type->clone_type ()), - where_clause (other.where_clause), polarity (other.polarity), - locus (other.locus), unsafe (other.unsafe) - { - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - impl_items.reserve (other.impl_items.size ()); - for (const auto &e : other.impl_items) - impl_items.push_back (e->clone_inherent_impl_item ()); - } - - ImplBlock &operator= (ImplBlock const &other) - { - VisItem::operator= (other); - impl_type = other.impl_type->clone_type (); - where_clause = other.where_clause; - polarity = other.polarity; - inner_attrs = other.inner_attrs; - locus = other.locus; - unsafe = other.unsafe; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); + AST::AttrVec outer_attrs, location_t locus, bool unsafe = false); - impl_items.reserve (other.impl_items.size ()); - for (const auto &e : other.impl_items) - impl_items.push_back (e->clone_inherent_impl_item ()); + ImplBlock (ImplBlock const &other); - return *this; - } + ImplBlock &operator= (ImplBlock const &other); ImplBlock (ImplBlock &&other) = default; ImplBlock &operator= (ImplBlock &&other) = default; @@ -2905,30 +2346,13 @@ class ExternalItem : public Node protected: ExternalItem (Analysis::NodeMapping mappings, Identifier item_name, - Visibility vis, AST::AttrVec outer_attrs, location_t locus) - : mappings (mappings), outer_attrs (std::move (outer_attrs)), - visibility (std::move (vis)), item_name (std::move (item_name)), - locus (locus) - {} + Visibility vis, AST::AttrVec outer_attrs, location_t locus); // Copy constructor - ExternalItem (ExternalItem const &other) - : mappings (other.mappings), outer_attrs (other.outer_attrs), - visibility (other.visibility), item_name (other.item_name), - locus (other.locus) - {} + ExternalItem (ExternalItem const &other); // Overloaded assignment operator to clone - ExternalItem &operator= (ExternalItem const &other) - { - mappings = other.mappings; - item_name = other.item_name; - visibility = other.visibility; - outer_attrs = other.outer_attrs; - locus = other.locus; - - return *this; - } + ExternalItem &operator= (ExternalItem const &other); // move constructors ExternalItem (ExternalItem &&other) = default; @@ -2948,27 +2372,13 @@ class ExternalStaticItem : public ExternalItem ExternalStaticItem (Analysis::NodeMapping mappings, Identifier item_name, std::unique_ptr item_type, Mutability mut, Visibility vis, AST::AttrVec outer_attrs, - location_t locus) - : ExternalItem (std::move (mappings), std::move (item_name), - std::move (vis), std::move (outer_attrs), locus), - mut (mut), item_type (std::move (item_type)) - {} + location_t locus); // Copy constructor - ExternalStaticItem (ExternalStaticItem const &other) - : ExternalItem (other), mut (other.mut), - item_type (other.item_type->clone_type ()) - {} + ExternalStaticItem (ExternalStaticItem const &other); // Overloaded assignment operator to clone - ExternalStaticItem &operator= (ExternalStaticItem const &other) - { - ExternalItem::operator= (other); - item_type = other.item_type->clone_type (); - mut = other.mut; - - return *this; - } + ExternalStaticItem &operator= (ExternalStaticItem const &other); // move constructors ExternalStaticItem (ExternalStaticItem &&other) = default; @@ -3008,29 +2418,15 @@ struct NamedFunctionParam bool has_name () const { return name.as_string () != "_"; } NamedFunctionParam (Analysis::NodeMapping mappings, Identifier name, - std::unique_ptr param_type) - : name (std::move (name)), param_type (std::move (param_type)), - mappings (std::move (mappings)) - {} + std::unique_ptr param_type); // Copy constructor - NamedFunctionParam (NamedFunctionParam const &other) - : name (other.name), param_type (other.param_type->clone_type ()), - mappings (other.mappings) - {} + NamedFunctionParam (NamedFunctionParam const &other); ~NamedFunctionParam () = default; // Overloaded assignment operator to clone - NamedFunctionParam &operator= (NamedFunctionParam const &other) - { - mappings = other.mappings; - name = other.name; - param_type = other.param_type->clone_type (); - // has_name = other.has_name; - - return *this; - } + NamedFunctionParam &operator= (NamedFunctionParam const &other); // move constructors NamedFunctionParam (NamedFunctionParam &&other) = default; @@ -3082,48 +2478,13 @@ class ExternalFunctionItem : public ExternalItem std::vector> generic_params, std::unique_ptr return_type, WhereClause where_clause, std::vector function_params, bool has_variadics, - Visibility vis, AST::AttrVec outer_attrs, location_t locus) - : ExternalItem (std::move (mappings), std::move (item_name), - std::move (vis), std::move (outer_attrs), locus), - generic_params (std::move (generic_params)), - return_type (std::move (return_type)), - where_clause (std::move (where_clause)), - function_params (std::move (function_params)), - has_variadics (has_variadics) - {} + Visibility vis, AST::AttrVec outer_attrs, location_t locus); // Copy constructor with clone - ExternalFunctionItem (ExternalFunctionItem const &other) - : ExternalItem (other), where_clause (other.where_clause), - function_params (other.function_params), - has_variadics (other.has_variadics) - { - if (other.return_type) - return_type = other.return_type->clone_type (); - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - } + ExternalFunctionItem (ExternalFunctionItem const &other); // Overloaded assignment operator with clone - ExternalFunctionItem &operator= (ExternalFunctionItem const &other) - { - ExternalItem::operator= (other); - - where_clause = other.where_clause; - function_params = other.function_params; - has_variadics = other.has_variadics; - - if (other.return_type) - return_type = other.return_type->clone_type (); - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - return *this; - } + ExternalFunctionItem &operator= (ExternalFunctionItem const &other); // move constructors ExternalFunctionItem (ExternalFunctionItem &&other) = default; @@ -3163,14 +2524,9 @@ class ExternalTypeItem : public ExternalItem { public: ExternalTypeItem (Analysis::NodeMapping mappings, Identifier item_name, - Visibility vis, location_t locus) - : ExternalItem (std::move (mappings), std::move (item_name), - Visibility (std::move (vis)), - /* FIXME: Is that correct? */ - {}, locus) - {} + Visibility vis, location_t locus); - ExternalTypeItem (ExternalTypeItem const &other) : ExternalItem (other) {} + ExternalTypeItem (ExternalTypeItem const &other); ExternalTypeItem (ExternalTypeItem &&other) = default; ExternalTypeItem &operator= (ExternalTypeItem &&other) = default; @@ -3210,36 +2566,13 @@ class ExternBlock : public VisItem, public WithInnerAttrs ExternBlock (Analysis::NodeMapping mappings, ABI abi, std::vector> extern_items, Visibility vis, AST::AttrVec inner_attrs, - AST::AttrVec outer_attrs, location_t locus) - : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), - WithInnerAttrs (std::move (inner_attrs)), abi (abi), - extern_items (std::move (extern_items)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Copy constructor with vector clone - ExternBlock (ExternBlock const &other) - : VisItem (other), WithInnerAttrs (other.inner_attrs), abi (other.abi), - locus (other.locus) - { - extern_items.reserve (other.extern_items.size ()); - for (const auto &e : other.extern_items) - extern_items.push_back (e->clone_external_item ()); - } + ExternBlock (ExternBlock const &other); // Overloaded assignment operator with vector clone - ExternBlock &operator= (ExternBlock const &other) - { - VisItem::operator= (other); - abi = other.abi; - inner_attrs = other.inner_attrs; - locus = other.locus; - - extern_items.reserve (other.extern_items.size ()); - for (const auto &e : other.extern_items) - extern_items.push_back (e->clone_external_item ()); - - return *this; - } + ExternBlock &operator= (ExternBlock const &other); // move constructors ExternBlock (ExternBlock &&other) = default; diff --git a/gcc/rust/hir/tree/rust-hir-literal.h b/gcc/rust/hir/tree/rust-hir-literal.h new file mode 100644 index 000000000000..9a97e71b719f --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-literal.h @@ -0,0 +1,78 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_LITERAL_H +#define RUST_HIR_LITERAL_H + +#include "rust-token.h" + +namespace Rust { +namespace HIR { +// A literal - value with a type. Used in LiteralExpr and LiteralPattern. +struct Literal +{ +public: + enum LitType + { + CHAR, + STRING, + BYTE, + BYTE_STRING, + INT, + FLOAT, + BOOL + }; + +private: + std::string value_as_string; + LitType type; + PrimitiveCoreType type_hint; + +public: + std::string as_string () const { return value_as_string; } + + LitType get_lit_type () const { return type; } + + PrimitiveCoreType get_type_hint () const { return type_hint; } + + Literal (std::string value_as_string, LitType type, + PrimitiveCoreType type_hint) + : value_as_string (std::move (value_as_string)), type (type), + type_hint (type_hint) + {} + + static Literal create_error () + { + return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN); + } + + void set_lit_type (LitType lt) { type = lt; } + + // Returns whether literal is in an invalid state. + bool is_error () const { return value_as_string == ""; } + + bool is_equal (Literal &other) + { + return value_as_string == other.value_as_string && type == other.type + && type_hint == other.type_hint; + } +}; +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-node.h b/gcc/rust/hir/tree/rust-hir-node.h new file mode 100644 index 000000000000..4010c23db6e2 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-node.h @@ -0,0 +1,63 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_NODE_H +#define RUST_HIR_NODE_H + +namespace Rust { + +namespace HIR { + +class Node +{ +public: + // Kind for downcasting various HIR nodes to other base classes when visiting + // them + enum BaseKind + { + /* class ExternalItem */ + EXTERNAL, + /* class TraitItem */ + TRAIT_ITEM, + /* class VisItem */ + VIS_ITEM, + /* class Item */ + ITEM, + /* class ImplItem */ + IMPL, + /* class Type */ + TYPE, + /* class Stmt */ + STMT, + /* class Expr */ + EXPR, + /* class Pattern */ + PATTERN, + }; + + /** + * Get the kind of HIR node we are dealing with. This is useful for + * downcasting to more precise types when necessary, i.e going from an `Item*` + * to a `VisItem*` + */ + virtual BaseKind get_hir_kind () = 0; +}; + +} // namespace HIR +} // namespace Rust +#endif diff --git a/gcc/rust/hir/tree/rust-hir-path.cc b/gcc/rust/hir/tree/rust-hir-path.cc new file mode 100644 index 000000000000..c8d3079a85e3 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-path.cc @@ -0,0 +1,383 @@ +// Copyright (C) 2020-2024 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 +// . + +#include "rust-hir-path.h" +#include "rust-hir-bound.h" + +namespace Rust { +namespace HIR { + +GenericArgsBinding::GenericArgsBinding (Identifier ident, + std::unique_ptr type_ptr, + location_t locus) + : identifier (std::move (ident)), type (std::move (type_ptr)), locus (locus) +{} + +GenericArgsBinding::GenericArgsBinding (GenericArgsBinding const &other) + : identifier (other.identifier), type (other.type->clone_type ()), + locus (other.locus) +{} + +GenericArgsBinding & +GenericArgsBinding::operator= (GenericArgsBinding const &other) +{ + identifier = other.identifier; + type = other.type->clone_type (); + locus = other.locus; + return *this; +} + +ConstGenericArg::ConstGenericArg (std::unique_ptr expression, + location_t locus) + : expression (std::move (expression)), locus (locus) +{} + +ConstGenericArg::ConstGenericArg (const ConstGenericArg &other) + : locus (other.locus) +{ + expression = other.expression->clone_expr (); +} + +ConstGenericArg +ConstGenericArg::operator= (const ConstGenericArg &other) +{ + expression = other.expression->clone_expr (); + locus = other.locus; + + return *this; +} + +GenericArgs & +GenericArgs::operator= (GenericArgs const &other) +{ + lifetime_args = other.lifetime_args; + binding_args = other.binding_args; + const_args = other.const_args; + locus = other.locus; + + type_args.clear (); + type_args.reserve (other.type_args.size ()); + for (const auto &e : other.type_args) + type_args.push_back (e->clone_type ()); + + return *this; +} + +GenericArgs::GenericArgs (std::vector lifetime_args, + std::vector > type_args, + std::vector binding_args, + std::vector const_args, + location_t locus) + : lifetime_args (std::move (lifetime_args)), + type_args (std::move (type_args)), binding_args (std::move (binding_args)), + const_args (std::move (const_args)), locus (locus) +{} + +GenericArgs::GenericArgs (GenericArgs const &other) + : lifetime_args (other.lifetime_args), binding_args (other.binding_args), + const_args (other.const_args), locus (other.locus) +{ + type_args.clear (); + type_args.reserve (other.type_args.size ()); + + for (const auto &e : other.type_args) + type_args.push_back (e->clone_type ()); +} + +bool +GenericArgs::is_empty () const +{ + return lifetime_args.size () == 0 && type_args.size () == 0 + && binding_args.size () == 0; +} + +PathExprSegment::PathExprSegment (Analysis::NodeMapping mappings, + PathIdentSegment segment_name, + location_t locus, GenericArgs generic_args) + : mappings (std::move (mappings)), segment_name (std::move (segment_name)), + generic_args (std::move (generic_args)), locus (locus) +{} + +PathExprSegment::PathExprSegment (PathExprSegment const &other) + : mappings (other.mappings), segment_name (other.segment_name), + generic_args (other.generic_args), locus (other.locus) +{} + +PathExprSegment & +PathExprSegment::operator= (PathExprSegment const &other) +{ + mappings = other.mappings; + segment_name = other.segment_name; + generic_args = other.generic_args; + locus = other.locus; + + return *this; +} + +void +PathPattern::iterate_path_segments (std::function cb) +{ + for (auto it = segments.begin (); it != segments.end (); it++) + { + if (!cb (*it)) + return; + } +} + +PathInExpression::PathInExpression (Analysis::NodeMapping mappings, + std::vector path_segments, + location_t locus, + bool has_opening_scope_resolution, + std::vector outer_attrs) + : PathPattern (std::move (path_segments)), + PathExpr (std::move (mappings), std::move (outer_attrs)), + has_opening_scope_resolution (has_opening_scope_resolution), locus (locus) +{} + +bool +PathInExpression::is_self () const + +{ + if (!is_single_segment ()) + return false; + + return get_final_segment ().get_segment ().as_string ().compare ("self") == 0; +} + +TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings, + PathIdentSegment ident_segment, + bool has_separating_scope_resolution, + location_t locus) + : mappings (std::move (mappings)), ident_segment (std::move (ident_segment)), + locus (locus), + has_separating_scope_resolution (has_separating_scope_resolution), + type (SegmentType::REG) +{} + +TypePathSegment::TypePathSegment (Analysis::NodeMapping mappings, + std::string segment_name, + bool has_separating_scope_resolution, + location_t locus) + : mappings (std::move (mappings)), + ident_segment (PathIdentSegment (std::move (segment_name))), locus (locus), + has_separating_scope_resolution (has_separating_scope_resolution), + type (SegmentType::REG) +{} + +TypePathSegmentGeneric::TypePathSegmentGeneric ( + Analysis::NodeMapping mappings, PathIdentSegment ident_segment, + bool has_separating_scope_resolution, GenericArgs generic_args, + location_t locus) + : TypePathSegment (std::move (mappings), std::move (ident_segment), + has_separating_scope_resolution, locus), + generic_args (std::move (generic_args)) +{} + +TypePathSegmentGeneric::TypePathSegmentGeneric ( + Analysis::NodeMapping mappings, std::string segment_name, + bool has_separating_scope_resolution, std::vector lifetime_args, + std::vector > type_args, + std::vector binding_args, + std::vector const_args, location_t locus) + : TypePathSegment (std::move (mappings), std::move (segment_name), + has_separating_scope_resolution, locus), + generic_args (GenericArgs (std::move (lifetime_args), std::move (type_args), + std::move (binding_args), std::move (const_args), + locus)) +{} + +TypePathFunction::TypePathFunction (std::vector > inputs, + std::unique_ptr type) + : inputs (std::move (inputs)), return_type (std::move (type)) +{} + +TypePathFunction::TypePathFunction (TypePathFunction const &other) +{ + return_type = other.has_return_type () + ? other.get_return_type ().clone_type () + : nullptr; + + inputs.reserve (other.inputs.size ()); + for (const auto &e : other.inputs) + inputs.push_back (e->clone_type ()); +} + +TypePathFunction & +TypePathFunction::operator= (TypePathFunction const &other) +{ + return_type = other.has_return_type () + ? other.get_return_type ().clone_type () + : nullptr; + + inputs.reserve (other.inputs.size ()); + for (const auto &e : other.inputs) + inputs.push_back (e->clone_type ()); + + return *this; +} + +TypePathSegmentFunction::TypePathSegmentFunction ( + Analysis::NodeMapping mappings, PathIdentSegment ident_segment, + bool has_separating_scope_resolution, TypePathFunction function_path, + location_t locus) + : TypePathSegment (std::move (mappings), std::move (ident_segment), + has_separating_scope_resolution, locus), + function_path (std::move (function_path)) +{} + +TypePathSegmentFunction::TypePathSegmentFunction ( + Analysis::NodeMapping mappings, std::string segment_name, + bool has_separating_scope_resolution, TypePathFunction function_path, + location_t locus) + : TypePathSegment (std::move (mappings), std::move (segment_name), + has_separating_scope_resolution, locus), + function_path (std::move (function_path)) +{} + +TypePath::TypePath (Analysis::NodeMapping mappings, + std::vector > segments, + location_t locus, bool has_opening_scope_resolution) + : TypeNoBounds (mappings, locus), + has_opening_scope_resolution (has_opening_scope_resolution), + segments (std::move (segments)) +{} + +TypePath::TypePath (TypePath const &other) + : TypeNoBounds (other.mappings, other.locus), + has_opening_scope_resolution (other.has_opening_scope_resolution) +{ + segments.reserve (other.segments.size ()); + for (const auto &e : other.segments) + segments.push_back (e->clone_type_path_segment ()); +} + +TypePath & +TypePath::operator= (TypePath const &other) +{ + has_opening_scope_resolution = other.has_opening_scope_resolution; + locus = other.locus; + mappings = other.mappings; + + segments.reserve (other.segments.size ()); + for (const auto &e : other.segments) + segments.push_back (e->clone_type_path_segment ()); + + return *this; +} + +QualifiedPathType::QualifiedPathType (Analysis::NodeMapping mappings, + std::unique_ptr type, + std::unique_ptr trait, + location_t locus) + : type (std::move (type)), trait (std::move (trait)), locus (locus), + mappings (mappings) +{} + +QualifiedPathType::QualifiedPathType (QualifiedPathType const &other) + : type (other.type->clone_type ()), + trait (other.has_as_clause () + ? std::unique_ptr (new HIR::TypePath (*other.trait)) + : nullptr), + locus (other.locus), mappings (other.mappings) +{} + +QualifiedPathType & +QualifiedPathType::operator= (QualifiedPathType const &other) +{ + type = other.type->clone_type (); + locus = other.locus; + mappings = other.mappings; + trait = other.has_as_clause () + ? std::unique_ptr (new HIR::TypePath (*other.trait)) + : nullptr; + + return *this; +} + +bool +QualifiedPathType::trait_has_generic_args () const +{ + rust_assert (has_as_clause ()); + bool is_generic_seg = trait->get_final_segment ().get_type () + == TypePathSegment::SegmentType::GENERIC; + if (!is_generic_seg) + return false; + + auto &seg + = static_cast (trait->get_final_segment ()); + return seg.has_generic_args (); +} + +GenericArgs & +QualifiedPathType::get_trait_generic_args () +{ + rust_assert (trait_has_generic_args ()); + auto &seg + = static_cast (trait->get_final_segment ()); + return seg.get_generic_args (); +} + +QualifiedPathInExpression::QualifiedPathInExpression ( + Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, + std::vector path_segments, location_t locus, + std::vector outer_attrs) + : PathPattern (std::move (path_segments)), + PathExpr (std::move (mappings), std::move (outer_attrs)), + path_type (std::move (qual_path_type)), locus (locus) +{} + +QualifiedPathInType::QualifiedPathInType ( + Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, + std::unique_ptr associated_segment, + std::vector > path_segments, + location_t locus) + : TypeNoBounds (mappings, locus), path_type (std::move (qual_path_type)), + associated_segment (std::move (associated_segment)), + segments (std::move (path_segments)) +{} + +QualifiedPathInType::QualifiedPathInType (QualifiedPathInType const &other) + : TypeNoBounds (other.mappings, other.locus), path_type (other.path_type) +{ + auto seg = other.associated_segment->clone_type_path_segment_impl (); + associated_segment = std::unique_ptr (seg); + + segments.reserve (other.segments.size ()); + for (const auto &e : other.segments) + segments.push_back (e->clone_type_path_segment ()); +} + +QualifiedPathInType & +QualifiedPathInType::operator= (QualifiedPathInType const &other) +{ + auto seg = other.associated_segment->clone_type_path_segment_impl (); + associated_segment = std::unique_ptr (seg); + + path_type = other.path_type; + locus = other.locus; + mappings = other.mappings; + + segments.reserve (other.segments.size ()); + for (const auto &e : other.segments) + segments.push_back (e->clone_type_path_segment ()); + + return *this; +} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index b561c30c49dc..4839ca6442d3 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -19,7 +19,10 @@ #ifndef RUST_HIR_PATH_H #define RUST_HIR_PATH_H -#include "rust-hir.h" +#include "rust-hir-simple-path.h" +#include "rust-hir-type-no-bounds.h" +#include "rust-hir-pattern-abstract.h" +#include "rust-hir-expr-abstract.h" namespace Rust { namespace HIR { @@ -76,27 +79,16 @@ class GenericArgsBinding // Pointer type for type in constructor to enable polymorphism GenericArgsBinding (Identifier ident, std::unique_ptr type_ptr, - location_t locus = UNDEF_LOCATION) - : identifier (std::move (ident)), type (std::move (type_ptr)), locus (locus) - {} + location_t locus = UNDEF_LOCATION); // Copy constructor has to deep copy the type as it is a unique pointer - GenericArgsBinding (GenericArgsBinding const &other) - : identifier (other.identifier), type (other.type->clone_type ()), - locus (other.locus) - {} + GenericArgsBinding (GenericArgsBinding const &other); // default destructor ~GenericArgsBinding () = default; // Overload assignment operator to deep copy the pointed-to type - GenericArgsBinding &operator= (GenericArgsBinding const &other) - { - identifier = other.identifier; - type = other.type->clone_type (); - locus = other.locus; - return *this; - } + GenericArgsBinding &operator= (GenericArgsBinding const &other); // move constructors GenericArgsBinding (GenericArgsBinding &&other) = default; @@ -119,22 +111,11 @@ class ConstGenericArg // at name-resolution, hence no need for ambiguities here public: - ConstGenericArg (std::unique_ptr expression, location_t locus) - : expression (std::move (expression)), locus (locus) - {} - - ConstGenericArg (const ConstGenericArg &other) : locus (other.locus) - { - expression = other.expression->clone_expr (); - } + ConstGenericArg (std::unique_ptr expression, location_t locus); - ConstGenericArg operator= (const ConstGenericArg &other) - { - expression = other.expression->clone_expr (); - locus = other.locus; + ConstGenericArg (const ConstGenericArg &other); - return *this; - } + ConstGenericArg operator= (const ConstGenericArg &other); std::unique_ptr &get_expression () { return expression; } @@ -162,42 +143,15 @@ class GenericArgs GenericArgs (std::vector lifetime_args, std::vector > type_args, std::vector binding_args, - std::vector const_args, location_t locus) - : lifetime_args (std::move (lifetime_args)), - type_args (std::move (type_args)), - binding_args (std::move (binding_args)), - const_args (std::move (const_args)), locus (locus) - {} + std::vector const_args, location_t locus); // copy constructor with vector clone - GenericArgs (GenericArgs const &other) - : lifetime_args (other.lifetime_args), binding_args (other.binding_args), - const_args (other.const_args), locus (other.locus) - { - type_args.clear (); - type_args.reserve (other.type_args.size ()); - - for (const auto &e : other.type_args) - type_args.push_back (e->clone_type ()); - } + GenericArgs (GenericArgs const &other); ~GenericArgs () = default; // overloaded assignment operator to vector clone - GenericArgs &operator= (GenericArgs const &other) - { - lifetime_args = other.lifetime_args; - binding_args = other.binding_args; - const_args = other.const_args; - locus = other.locus; - - type_args.clear (); - type_args.reserve (other.type_args.size ()); - for (const auto &e : other.type_args) - type_args.push_back (e->clone_type ()); - - return *this; - } + GenericArgs &operator= (GenericArgs const &other); // move constructors GenericArgs (GenericArgs &&other) = default; @@ -209,11 +163,7 @@ class GenericArgs return GenericArgs ({}, {}, {}, {}, locus); } - bool is_empty () const - { - return lifetime_args.size () == 0 && type_args.size () == 0 - && binding_args.size () == 0; - } + bool is_empty () const; std::string as_string () const; @@ -245,25 +195,11 @@ class PathExprSegment public: PathExprSegment (Analysis::NodeMapping mappings, PathIdentSegment segment_name, location_t locus, - GenericArgs generic_args) - : mappings (std::move (mappings)), segment_name (std::move (segment_name)), - generic_args (std::move (generic_args)), locus (locus) - {} + GenericArgs generic_args); - PathExprSegment (PathExprSegment const &other) - : mappings (other.mappings), segment_name (other.segment_name), - generic_args (other.generic_args), locus (other.locus) - {} - - PathExprSegment &operator= (PathExprSegment const &other) - { - mappings = other.mappings; - segment_name = other.segment_name; - generic_args = other.generic_args; - locus = other.locus; + PathExprSegment (PathExprSegment const &other); - return *this; - } + PathExprSegment &operator= (PathExprSegment const &other); // move constructors PathExprSegment (PathExprSegment &&other) = default; @@ -308,14 +244,7 @@ class PathPattern : public Pattern std::string as_string () const override; - void iterate_path_segments (std::function cb) - { - for (auto it = segments.begin (); it != segments.end (); it++) - { - if (!cb (*it)) - return; - } - } + void iterate_path_segments (std::function cb); size_t get_num_segments () const { return segments.size (); } @@ -349,11 +278,7 @@ class PathInExpression : public PathPattern, public PathExpr location_t locus = UNDEF_LOCATION, bool has_opening_scope_resolution = false, std::vector outer_attrs - = std::vector ()) - : PathPattern (std::move (path_segments)), - PathExpr (std::move (mappings), std::move (outer_attrs)), - has_opening_scope_resolution (has_opening_scope_resolution), locus (locus) - {} + = std::vector ()); // Creates an error state path in expression. static PathInExpression create_error () @@ -385,14 +310,7 @@ class PathInExpression : public PathPattern, public PathExpr bool opening_scope_resolution () { return has_opening_scope_resolution; } - bool is_self () const - { - if (!is_single_segment ()) - return false; - - return get_final_segment ().get_segment ().as_string ().compare ("self") - == 0; - } + bool is_self () const; const Analysis::NodeMapping &get_mappings () const override final { @@ -456,21 +374,10 @@ class TypePathSegment TypePathSegment (Analysis::NodeMapping mappings, PathIdentSegment ident_segment, - bool has_separating_scope_resolution, location_t locus) - : mappings (std::move (mappings)), - ident_segment (std::move (ident_segment)), locus (locus), - has_separating_scope_resolution (has_separating_scope_resolution), - type (SegmentType::REG) - {} + bool has_separating_scope_resolution, location_t locus); TypePathSegment (Analysis::NodeMapping mappings, std::string segment_name, - bool has_separating_scope_resolution, location_t locus) - : mappings (std::move (mappings)), - ident_segment (PathIdentSegment (std::move (segment_name))), - locus (locus), - has_separating_scope_resolution (has_separating_scope_resolution), - type (SegmentType::REG) - {} + bool has_separating_scope_resolution, location_t locus); virtual std::string as_string () const { return ident_segment.as_string (); } @@ -511,11 +418,7 @@ class TypePathSegmentGeneric : public TypePathSegment TypePathSegmentGeneric (Analysis::NodeMapping mappings, PathIdentSegment ident_segment, bool has_separating_scope_resolution, - GenericArgs generic_args, location_t locus) - : TypePathSegment (std::move (mappings), std::move (ident_segment), - has_separating_scope_resolution, locus), - generic_args (std::move (generic_args)) - {} + GenericArgs generic_args, location_t locus); // Constructor from segment name and all args TypePathSegmentGeneric (Analysis::NodeMapping mappings, @@ -525,13 +428,7 @@ class TypePathSegmentGeneric : public TypePathSegment std::vector > type_args, std::vector binding_args, std::vector const_args, - location_t locus) - : TypePathSegment (std::move (mappings), std::move (segment_name), - has_separating_scope_resolution, locus), - generic_args ( - GenericArgs (std::move (lifetime_args), std::move (type_args), - std::move (binding_args), std::move (const_args), locus)) - {} + location_t locus); std::string as_string () const override; @@ -566,37 +463,15 @@ class TypePathFunction // Constructor TypePathFunction (std::vector > inputs, - std::unique_ptr type) - : inputs (std::move (inputs)), return_type (std::move (type)) - {} + std::unique_ptr type); // Copy constructor with clone - TypePathFunction (TypePathFunction const &other) - { - return_type = other.has_return_type () - ? other.get_return_type ().clone_type () - : nullptr; - - inputs.reserve (other.inputs.size ()); - for (const auto &e : other.inputs) - inputs.push_back (e->clone_type ()); - } + TypePathFunction (TypePathFunction const &other); ~TypePathFunction () = default; // Overloaded assignment operator to clone type - TypePathFunction &operator= (TypePathFunction const &other) - { - return_type = other.has_return_type () - ? other.get_return_type ().clone_type () - : nullptr; - - inputs.reserve (other.inputs.size ()); - for (const auto &e : other.inputs) - inputs.push_back (e->clone_type ()); - - return *this; - } + TypePathFunction &operator= (TypePathFunction const &other); // move constructors TypePathFunction (TypePathFunction &&other) = default; @@ -624,21 +499,13 @@ class TypePathSegmentFunction : public TypePathSegment TypePathSegmentFunction (Analysis::NodeMapping mappings, PathIdentSegment ident_segment, bool has_separating_scope_resolution, - TypePathFunction function_path, location_t locus) - : TypePathSegment (std::move (mappings), std::move (ident_segment), - has_separating_scope_resolution, locus), - function_path (std::move (function_path)) - {} + TypePathFunction function_path, location_t locus); // Constructor with segment name and TypePathFn TypePathSegmentFunction (Analysis::NodeMapping mappings, std::string segment_name, bool has_separating_scope_resolution, - TypePathFunction function_path, location_t locus) - : TypePathSegment (std::move (mappings), std::move (segment_name), - has_separating_scope_resolution, locus), - function_path (std::move (function_path)) - {} + TypePathFunction function_path, location_t locus); std::string as_string () const override; @@ -698,35 +565,13 @@ class TypePath : public TypeNoBounds // Constructor TypePath (Analysis::NodeMapping mappings, std::vector > segments, - location_t locus, bool has_opening_scope_resolution = false) - : TypeNoBounds (mappings, locus), - has_opening_scope_resolution (has_opening_scope_resolution), - segments (std::move (segments)) - {} + location_t locus, bool has_opening_scope_resolution = false); // Copy constructor with vector clone - TypePath (TypePath const &other) - : TypeNoBounds (other.mappings, other.locus), - has_opening_scope_resolution (other.has_opening_scope_resolution) - { - segments.reserve (other.segments.size ()); - for (const auto &e : other.segments) - segments.push_back (e->clone_type_path_segment ()); - } + TypePath (TypePath const &other); // Overloaded assignment operator with clone - TypePath &operator= (TypePath const &other) - { - has_opening_scope_resolution = other.has_opening_scope_resolution; - locus = other.locus; - mappings = other.mappings; - - segments.reserve (other.segments.size ()); - for (const auto &e : other.segments) - segments.push_back (e->clone_type_path_segment ()); - - return *this; - } + TypePath &operator= (TypePath const &other); // move constructors TypePath (TypePath &&other) = default; @@ -739,7 +584,7 @@ class TypePath : public TypeNoBounds AST::SimplePath as_simple_path () const; // Creates a trait bound with a clone of this type path as its only element. - TraitBound *to_trait_bound (bool in_parens) const override; + std::unique_ptr to_trait_bound (bool in_parens) const override; void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRTypeVisitor &vis) override; @@ -751,10 +596,7 @@ class TypePath : public TypeNoBounds return segments; } - std::unique_ptr &get_final_segment () - { - return segments.back (); - } + TypePathSegment &get_final_segment () { return *segments.back (); } }; class QualifiedPathType @@ -767,36 +609,16 @@ class QualifiedPathType public: // Constructor QualifiedPathType (Analysis::NodeMapping mappings, std::unique_ptr type, - std::unique_ptr trait, location_t locus) - : type (std::move (type)), trait (std::move (trait)), locus (locus), - mappings (mappings) - {} + std::unique_ptr trait, location_t locus); // Copy constructor uses custom deep copy for Type to preserve polymorphism - QualifiedPathType (QualifiedPathType const &other) - : type (other.type->clone_type ()), - trait (other.has_as_clause () ? std::unique_ptr ( - new HIR::TypePath (*other.trait)) - : nullptr), - locus (other.locus), mappings (other.mappings) - {} + QualifiedPathType (QualifiedPathType const &other); // default destructor ~QualifiedPathType () = default; // overload assignment operator to use custom clone method - QualifiedPathType &operator= (QualifiedPathType const &other) - { - type = other.type->clone_type (); - locus = other.locus; - mappings = other.mappings; - trait - = other.has_as_clause () - ? std::unique_ptr (new HIR::TypePath (*other.trait)) - : nullptr; - - return *this; - } + QualifiedPathType &operator= (QualifiedPathType const &other); // move constructor QualifiedPathType (QualifiedPathType &&other) = default; @@ -815,26 +637,9 @@ class QualifiedPathType TypePath &get_trait () { return *trait; } - bool trait_has_generic_args () const - { - rust_assert (has_as_clause ()); - bool is_generic_seg = trait->get_final_segment ()->get_type () - == TypePathSegment::SegmentType::GENERIC; - if (!is_generic_seg) - return false; - - TypePathSegmentGeneric *seg = static_cast ( - trait->get_final_segment ().get ()); - return seg->has_generic_args (); - } + bool trait_has_generic_args () const; - GenericArgs &get_trait_generic_args () - { - rust_assert (trait_has_generic_args ()); - TypePathSegmentGeneric *seg = static_cast ( - trait->get_final_segment ().get ()); - return seg->get_generic_args (); - } + GenericArgs &get_trait_generic_args (); }; /* HIR node representing a qualified path-in-expression pattern (path that @@ -852,11 +657,7 @@ class QualifiedPathInExpression : public PathPattern, public PathExpr std::vector path_segments, location_t locus = UNDEF_LOCATION, std::vector outer_attrs - = std::vector ()) - : PathPattern (std::move (path_segments)), - PathExpr (std::move (mappings), std::move (outer_attrs)), - path_type (std::move (qual_path_type)), locus (locus) - {} + = std::vector ()); location_t get_locus () const override final { return locus; } @@ -917,40 +718,13 @@ class QualifiedPathInType : public TypeNoBounds Analysis::NodeMapping mappings, QualifiedPathType qual_path_type, std::unique_ptr associated_segment, std::vector > path_segments, - location_t locus = UNDEF_LOCATION) - : TypeNoBounds (mappings, locus), path_type (std::move (qual_path_type)), - associated_segment (std::move (associated_segment)), - segments (std::move (path_segments)) - {} + location_t locus = UNDEF_LOCATION); // Copy constructor with vector clone - QualifiedPathInType (QualifiedPathInType const &other) - : TypeNoBounds (other.mappings, other.locus), path_type (other.path_type) - { - auto seg = other.associated_segment->clone_type_path_segment_impl (); - associated_segment = std::unique_ptr (seg); - - segments.reserve (other.segments.size ()); - for (const auto &e : other.segments) - segments.push_back (e->clone_type_path_segment ()); - } + QualifiedPathInType (QualifiedPathInType const &other); // Overloaded assignment operator with vector clone - QualifiedPathInType &operator= (QualifiedPathInType const &other) - { - auto seg = other.associated_segment->clone_type_path_segment_impl (); - associated_segment = std::unique_ptr (seg); - - path_type = other.path_type; - locus = other.locus; - mappings = other.mappings; - - segments.reserve (other.segments.size ()); - for (const auto &e : other.segments) - segments.push_back (e->clone_type_path_segment ()); - - return *this; - } + QualifiedPathInType &operator= (QualifiedPathInType const &other); // move constructors QualifiedPathInType (QualifiedPathInType &&other) = default; @@ -971,40 +745,6 @@ class QualifiedPathInType : public TypeNoBounds } }; -class SimplePathSegment -{ - Analysis::NodeMapping mappings; - -public: - SimplePathSegment (Analysis::NodeMapping mappings) : mappings (mappings) {} - - const Analysis::NodeMapping &get_mappings () const { return mappings; } -}; - -class SimplePath -{ - std::vector segments; - Analysis::NodeMapping mappings; - location_t locus; - -public: - SimplePath (std::vector segments, - Analysis::NodeMapping mappings, location_t locus) - : segments (std::move (segments)), mappings (mappings), locus (locus) - {} - - static HIR::SimplePath create_empty () - { - return HIR::SimplePath ({}, Analysis::NodeMapping::get_error (), - UNDEF_LOCATION); - } - - bool is_error () const { return segments.empty (); } - - const Analysis::NodeMapping &get_mappings () const { return mappings; } - location_t get_locus () const { return locus; } -}; - } // namespace HIR } // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-pattern-abstract.h b/gcc/rust/hir/tree/rust-hir-pattern-abstract.h new file mode 100644 index 000000000000..b156a8072396 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-pattern-abstract.h @@ -0,0 +1,82 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_PATTERN_ABSTRACT_H +#define RUST_HIR_PATTERN_ABSTRACT_H + +#include "rust-hir-visitable.h" +#include "rust-hir-visitor.h" +#include "rust-hir-node.h" +#include "rust-system.h" + +namespace Rust { +namespace HIR { + +// Pattern base HIR node +class Pattern : public Node, virtual public FullVisitable +{ +public: + using FullVisitable::accept_vis; + + enum PatternType + { + PATH, + LITERAL, + IDENTIFIER, + WILDCARD, + RANGE, + REFERENCE, + STRUCT, + TUPLE_STRUCT, + TUPLE, + GROUPED, + SLICE, + ALT + }; + + BaseKind get_hir_kind () override final { return PATTERN; } + + // Unique pointer custom clone function + std::unique_ptr clone_pattern () const + { + return std::unique_ptr (clone_pattern_impl ()); + } + + // possible virtual methods: is_refutable() + + virtual ~Pattern () {} + + virtual std::string as_string () const = 0; + + virtual void accept_vis (HIRPatternVisitor &vis) = 0; + + virtual const Analysis::NodeMapping &get_mappings () const = 0; + + virtual location_t get_locus () const = 0; + + virtual PatternType get_pattern_type () const = 0; + +protected: + // Clone pattern implementation as pure virtual method + virtual Pattern *clone_pattern_impl () const = 0; +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-pattern.h b/gcc/rust/hir/tree/rust-hir-pattern.h index bedc6de51f69..1843e87d86ee 100644 --- a/gcc/rust/hir/tree/rust-hir-pattern.h +++ b/gcc/rust/hir/tree/rust-hir-pattern.h @@ -19,12 +19,13 @@ #ifndef RUST_HIR_PATTERN_H #define RUST_HIR_PATTERN_H +#include "rust-hir-pattern-abstract.h" #include "rust-common.h" -#include "rust-hir.h" +#include "rust-hir-literal.h" +#include "rust-hir-path.h" namespace Rust { namespace HIR { - // Literal pattern HIR node (comparing to a literal) class LiteralPattern : public Pattern { diff --git a/gcc/rust/hir/tree/rust-hir-simple-path.h b/gcc/rust/hir/tree/rust-hir-simple-path.h new file mode 100644 index 000000000000..7f832ff08506 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-simple-path.h @@ -0,0 +1,64 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_SIMPLE_PATH_H +#define RUST_HIR_SIMPLE_PATH_H + +#include "rust-hir-map.h" + +namespace Rust { +namespace HIR { + +class SimplePathSegment +{ + Analysis::NodeMapping mappings; + +public: + SimplePathSegment (Analysis::NodeMapping mappings) : mappings (mappings) {} + + const Analysis::NodeMapping &get_mappings () const { return mappings; } +}; + +class SimplePath +{ + std::vector segments; + Analysis::NodeMapping mappings; + location_t locus; + +public: + SimplePath (std::vector segments, + Analysis::NodeMapping mappings, location_t locus) + : segments (std::move (segments)), mappings (mappings), locus (locus) + {} + + static HIR::SimplePath create_empty () + { + return HIR::SimplePath ({}, Analysis::NodeMapping::get_error (), + UNDEF_LOCATION); + } + + bool is_error () const { return segments.empty (); } + + const Analysis::NodeMapping &get_mappings () const { return mappings; } + location_t get_locus () const { return locus; } +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-stmt.cc b/gcc/rust/hir/tree/rust-hir-stmt.cc new file mode 100644 index 000000000000..764ea118cebe --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-stmt.cc @@ -0,0 +1,98 @@ +// Copyright (C) 2020-2024 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 +// . + +#include "rust-hir-stmt.h" + +namespace Rust { +namespace HIR { + +LetStmt::LetStmt (Analysis::NodeMapping mappings, + std::unique_ptr variables_pattern, + std::unique_ptr init_expr, std::unique_ptr type, + AST::AttrVec outer_attrs, location_t locus) + : Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)), + variables_pattern (std::move (variables_pattern)), type (std::move (type)), + init_expr (std::move (init_expr)), locus (locus) +{} + +LetStmt::LetStmt (LetStmt const &other) + : Stmt (other.mappings), outer_attrs (other.outer_attrs), locus (other.locus) +{ + // guard to prevent null dereference (only required if error state) + if (other.variables_pattern != nullptr) + variables_pattern = other.variables_pattern->clone_pattern (); + + // guard to prevent null dereference (always required) + if (other.init_expr != nullptr) + init_expr = other.init_expr->clone_expr (); + if (other.type != nullptr) + type = other.type->clone_type (); +} + +LetStmt & +LetStmt::operator= (LetStmt const &other) +{ + outer_attrs = other.outer_attrs; + locus = other.locus; + + // guard to prevent null dereference (only required if error state) + if (other.variables_pattern != nullptr) + variables_pattern = other.variables_pattern->clone_pattern (); + else + variables_pattern = nullptr; + + // guard to prevent null dereference (always required) + if (other.init_expr != nullptr) + init_expr = other.init_expr->clone_expr (); + else + init_expr = nullptr; + if (other.type != nullptr) + type = other.type->clone_type (); + else + type = nullptr; + + return *this; +} + +ExprStmt::ExprStmt (Analysis::NodeMapping mappings, std::unique_ptr expr, + location_t locus, bool must_be_unit) + : Stmt (std::move (mappings)), expr (std::move (expr)), locus (locus), + must_be_unit (must_be_unit) +{} + +ExprStmt::ExprStmt (Analysis::NodeMapping mappings, std::unique_ptr expr, + location_t locus) + : ExprStmt (std::move (mappings), std::move (expr), locus, false) +{} + +ExprStmt::ExprStmt (ExprStmt const &other) + : Stmt (other), expr (other.expr->clone_expr ()), locus (other.locus) +{} + +ExprStmt & +ExprStmt::operator= (ExprStmt const &other) +{ + Stmt::operator= (other); + expr = other.expr->clone_expr (); + locus = other.locus; + + return *this; +} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-stmt.h b/gcc/rust/hir/tree/rust-hir-stmt.h index f63717732020..3e5adc165fba 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.h +++ b/gcc/rust/hir/tree/rust-hir-stmt.h @@ -25,6 +25,44 @@ namespace Rust { namespace HIR { +/* Base statement abstract class. Note that most "statements" are not allowed in + * top-level module scope - only a subclass of statements called "items" are. */ +class Stmt : public Node, public FullVisitable +{ +public: + using FullVisitable::accept_vis; + + // Unique pointer custom clone function + std::unique_ptr clone_stmt () const + { + return std::unique_ptr (clone_stmt_impl ()); + } + + BaseKind get_hir_kind () override { return STMT; } + + virtual ~Stmt () {} + + virtual std::string as_string () const = 0; + + virtual void accept_vis (HIRStmtVisitor &vis) = 0; + + virtual location_t get_locus () const = 0; + + virtual bool is_unit_check_needed () const { return false; } + + const Analysis::NodeMapping &get_mappings () const { return mappings; } + + virtual bool is_item () const = 0; + +protected: + Stmt (Analysis::NodeMapping mappings) : mappings (std::move (mappings)) {} + + // Clone function implementation as pure virtual method + virtual Stmt *clone_stmt_impl () const = 0; + + Analysis::NodeMapping mappings; +}; + // Just a semi-colon, which apparently is a statement. class EmptyStmt : public Stmt { @@ -82,52 +120,13 @@ class LetStmt : public Stmt LetStmt (Analysis::NodeMapping mappings, std::unique_ptr variables_pattern, std::unique_ptr init_expr, std::unique_ptr type, - AST::AttrVec outer_attrs, location_t locus) - : Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)), - variables_pattern (std::move (variables_pattern)), - type (std::move (type)), init_expr (std::move (init_expr)), locus (locus) - {} + AST::AttrVec outer_attrs, location_t locus); // Copy constructor with clone - LetStmt (LetStmt const &other) - : Stmt (other.mappings), outer_attrs (other.outer_attrs), - locus (other.locus) - { - // guard to prevent null dereference (only required if error state) - if (other.variables_pattern != nullptr) - variables_pattern = other.variables_pattern->clone_pattern (); - - // guard to prevent null dereference (always required) - if (other.init_expr != nullptr) - init_expr = other.init_expr->clone_expr (); - if (other.type != nullptr) - type = other.type->clone_type (); - } + LetStmt (LetStmt const &other); // Overloaded assignment operator to clone - LetStmt &operator= (LetStmt const &other) - { - outer_attrs = other.outer_attrs; - locus = other.locus; - - // guard to prevent null dereference (only required if error state) - if (other.variables_pattern != nullptr) - variables_pattern = other.variables_pattern->clone_pattern (); - else - variables_pattern = nullptr; - - // guard to prevent null dereference (always required) - if (other.init_expr != nullptr) - init_expr = other.init_expr->clone_expr (); - else - init_expr = nullptr; - if (other.type != nullptr) - type = other.type->clone_type (); - else - type = nullptr; - - return *this; - } + LetStmt &operator= (LetStmt const &other); // move constructors LetStmt (LetStmt &&other) = default; @@ -167,15 +166,10 @@ class ExprStmt : public Stmt public: ExprStmt (Analysis::NodeMapping mappings, std::unique_ptr expr, - location_t locus, bool must_be_unit) - : Stmt (std::move (mappings)), expr (std::move (expr)), locus (locus), - must_be_unit (must_be_unit) - {} + location_t locus, bool must_be_unit); ExprStmt (Analysis::NodeMapping mappings, std::unique_ptr expr, - location_t locus) - : ExprStmt (std::move (mappings), std::move (expr), locus, false) - {} + location_t locus); std::string as_string () const override; @@ -189,19 +183,10 @@ class ExprStmt : public Stmt Expr &get_expr () { return *expr; } // Copy constructor with clone - ExprStmt (ExprStmt const &other) - : Stmt (other), expr (other.expr->clone_expr ()), locus (other.locus) - {} + ExprStmt (ExprStmt const &other); // Overloaded assignment operator to clone - ExprStmt &operator= (ExprStmt const &other) - { - Stmt::operator= (other); - expr = other.expr->clone_expr (); - locus = other.locus; - - return *this; - } + ExprStmt &operator= (ExprStmt const &other); // move constructors ExprStmt (ExprStmt &&other) = default; diff --git a/gcc/rust/hir/tree/rust-hir-trait-bound.h b/gcc/rust/hir/tree/rust-hir-trait-bound.h new file mode 100644 index 000000000000..d20fa794f583 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-trait-bound.h @@ -0,0 +1,87 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_TRAIT_BOUND_H +#define RUST_HIR_TRAIT_BOUND_H + +#include "rust-hir-bound-abstract.h" +#include "rust-hir-path.h" +#include "rust-hir-generic-param.h" + +namespace Rust { +namespace HIR { + +// A trait bound +class TraitBound : public TypeParamBound +{ + bool in_parens; + BoundPolarity polarity; + std::vector for_lifetimes; + TypePath type_path; + location_t locus; + + Analysis::NodeMapping mappings; + +public: + // Returns whether trait bound has "for" lifetimes + bool has_for_lifetimes () const { return !for_lifetimes.empty (); } + + TraitBound (Analysis::NodeMapping mapping, TypePath type_path, + location_t locus, bool in_parens = false, + BoundPolarity polarity = BoundPolarity::RegularBound, + std::vector for_lifetimes + = std::vector ()) + : in_parens (in_parens), polarity (polarity), + for_lifetimes (std::move (for_lifetimes)), + type_path (std::move (type_path)), locus (locus), mappings (mapping) + {} + + std::string as_string () const override; + + location_t get_locus () const override final { return locus; } + + void accept_vis (HIRFullVisitor &vis) override; + + Analysis::NodeMapping get_mappings () const override final + { + return mappings; + } + + std::vector &get_for_lifetimes () { return for_lifetimes; } + bool get_in_parens () { return in_parens; } + BoundPolarity get_polarity () { return polarity; } + + BoundType get_bound_type () const final override { return TRAITBOUND; } + + TypePath &get_path () { return type_path; } + + const TypePath &get_path () const { return type_path; } + +protected: + /* Use covariance to implement clone function as returning this object rather + * than base */ + TraitBound *clone_type_param_bound_impl () const override + { + return new TraitBound (*this); + } +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-type-abstract.cc b/gcc/rust/hir/tree/rust-hir-type-abstract.cc new file mode 100644 index 000000000000..901c603c3839 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-type-abstract.cc @@ -0,0 +1,32 @@ +// Copyright (C) 2020-2024 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 +// . + +#include "rust-hir-type-abstract.h" +#include "rust-hir-trait-bound.h" + +namespace Rust { +namespace HIR { + +std::unique_ptr +Type::to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const +{ + return std::unique_ptr (nullptr); +} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-type-abstract.h b/gcc/rust/hir/tree/rust-hir-type-abstract.h new file mode 100644 index 000000000000..6142d88b9723 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-type-abstract.h @@ -0,0 +1,80 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_TYPE_ABSTRACT_H +#define RUST_HIR_TYPE_ABSTRACT_H + +#include "rust-hir-node.h" +#include "rust-hir-visitable.h" +#include "rust-system.h" +#include "rust-hir-map.h" + +namespace Rust { +namespace HIR { + +class TraitBound; + +// Base class for types as represented in HIR - abstract +class Type : public Node, public FullVisitable +{ +public: + using FullVisitable::accept_vis; + // Unique pointer custom clone function + std::unique_ptr clone_type () const + { + return std::unique_ptr (clone_type_impl ()); + } + + // virtual destructor + virtual ~Type () {} + + BaseKind get_hir_kind () override final { return TYPE; } + + virtual std::string as_string () const = 0; + + /* HACK: convert to trait bound. Virtual method overriden by classes that + * enable this. */ + virtual std::unique_ptr + to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const; + /* as pointer, shouldn't require definition beforehand, only forward + * declaration. */ + + virtual void accept_vis (HIRTypeVisitor &vis) = 0; + + virtual const Analysis::NodeMapping &get_mappings () const + { + return mappings; + } + virtual location_t get_locus () const { return locus; } + +protected: + Type (Analysis::NodeMapping mappings, location_t locus) + : mappings (mappings), locus (locus) + {} + + // Clone function implementation as pure virtual method + virtual Type *clone_type_impl () const = 0; + + Analysis::NodeMapping mappings; + location_t locus; +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-type-no-bounds.h b/gcc/rust/hir/tree/rust-hir-type-no-bounds.h new file mode 100644 index 000000000000..b86ff30fd851 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-type-no-bounds.h @@ -0,0 +1,58 @@ + +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_TYPE_NO_BOUNDS_H +#define RUST_HIR_TYPE_NO_BOUNDS_H + +#include "rust-hir-type-abstract.h" + +namespace Rust { +namespace HIR { + +// A type without parentheses? - abstract +class TypeNoBounds : public Type +{ +public: + // Unique pointer custom clone function + std::unique_ptr clone_type_no_bounds () const + { + return std::unique_ptr (clone_type_no_bounds_impl ()); + } + +protected: + TypeNoBounds (Analysis::NodeMapping mappings, location_t locus) + : Type (mappings, locus) + {} + + // Clone function implementation as pure virtual method + virtual TypeNoBounds *clone_type_no_bounds_impl () const = 0; + + /* Save having to specify two clone methods in derived classes by making type + * clone return typenobounds clone. Hopefully won't affect performance too + * much. */ + TypeNoBounds *clone_type_impl () const override + { + return clone_type_no_bounds_impl (); + } +}; + +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-type.cc b/gcc/rust/hir/tree/rust-hir-type.cc new file mode 100644 index 000000000000..689d86b7372b --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-type.cc @@ -0,0 +1,289 @@ + +// Copyright (C) 2020-2024 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 +// . + +#include "rust-hir-type.h" + +namespace Rust { +namespace HIR { + +ImplTraitType::ImplTraitType ( + Analysis::NodeMapping mappings, + std::vector> type_param_bounds, + location_t locus) + : Type (mappings, locus), type_param_bounds (std::move (type_param_bounds)) +{} + +ImplTraitType::ImplTraitType (ImplTraitType const &other) + : Type (other.mappings, other.locus) +{ + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); +} + +ImplTraitType & +ImplTraitType::operator= (ImplTraitType const &other) +{ + locus = other.locus; + mappings = other.mappings; + + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); + + return *this; +} + +TraitObjectType::TraitObjectType ( + Analysis::NodeMapping mappings, + std::vector> type_param_bounds, + location_t locus, bool is_dyn_dispatch) + : Type (mappings, locus), has_dyn (is_dyn_dispatch), + type_param_bounds (std::move (type_param_bounds)) +{} + +TraitObjectType::TraitObjectType (TraitObjectType const &other) + : Type (other.mappings, other.locus), has_dyn (other.has_dyn) +{ + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); +} + +TraitObjectType & +TraitObjectType::operator= (TraitObjectType const &other) +{ + mappings = other.mappings; + has_dyn = other.has_dyn; + locus = other.locus; + type_param_bounds.reserve (other.type_param_bounds.size ()); + for (const auto &e : other.type_param_bounds) + type_param_bounds.push_back (e->clone_type_param_bound ()); + + return *this; +} + +ParenthesisedType::ParenthesisedType (Analysis::NodeMapping mappings, + std::unique_ptr type_inside_parens, + location_t locus) + : TypeNoBounds (mappings, locus), + type_in_parens (std::move (type_inside_parens)) +{} + +ParenthesisedType::ParenthesisedType (ParenthesisedType const &other) + : TypeNoBounds (other.mappings, other.locus), + type_in_parens (other.type_in_parens->clone_type ()) +{} + +ParenthesisedType & +ParenthesisedType::operator= (ParenthesisedType const &other) +{ + mappings = other.mappings; + type_in_parens = other.type_in_parens->clone_type (); + locus = other.locus; + return *this; +} + +std::unique_ptr +ParenthesisedType::to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const +{ + /* NOTE: obviously it is unknown whether the internal type is a trait bound + * due to polymorphism, so just let the internal type handle it. As + * parenthesised type, it must be in parentheses. */ + return type_in_parens->to_trait_bound (true); +} + +TupleType::TupleType (Analysis::NodeMapping mappings, + std::vector> elems, + location_t locus) + : TypeNoBounds (mappings, locus), elems (std::move (elems)) +{} + +TupleType::TupleType (TupleType const &other) + : TypeNoBounds (other.mappings, other.locus) +{ + mappings = other.mappings; + elems.reserve (other.elems.size ()); + for (const auto &e : other.elems) + elems.push_back (e->clone_type ()); +} + +TupleType & +TupleType::operator= (TupleType const &other) +{ + locus = other.locus; + + elems.reserve (other.elems.size ()); + for (const auto &e : other.elems) + elems.push_back (e->clone_type ()); + + return *this; +} + +NeverType::NeverType (Analysis::NodeMapping mappings, location_t locus) + : TypeNoBounds (mappings, locus) +{} + +RawPointerType::RawPointerType (Analysis::NodeMapping mappings, Mutability mut, + std::unique_ptr type, location_t locus) + : TypeNoBounds (mappings, locus), mut (mut), type (std::move (type)) +{} + +RawPointerType::RawPointerType (RawPointerType const &other) + : TypeNoBounds (other.mappings, other.locus), mut (other.mut), + type (other.type->clone_type ()) +{} + +RawPointerType & +RawPointerType::operator= (RawPointerType const &other) +{ + mappings = other.mappings; + mut = other.mut; + type = other.type->clone_type (); + locus = other.locus; + return *this; +} + +ReferenceType::ReferenceType (Analysis::NodeMapping mappings, Mutability mut, + std::unique_ptr type_no_bounds, + location_t locus, Lifetime lifetime) + : TypeNoBounds (mappings, locus), lifetime (std::move (lifetime)), mut (mut), + type (std::move (type_no_bounds)) +{} + +ReferenceType::ReferenceType (ReferenceType const &other) + : TypeNoBounds (other.mappings, other.locus), lifetime (other.lifetime), + mut (other.mut), type (other.type->clone_type ()) +{} + +ReferenceType & +ReferenceType::operator= (ReferenceType const &other) +{ + mappings = other.mappings; + lifetime = other.lifetime; + mut = other.mut; + type = other.type->clone_type (); + locus = other.locus; + + return *this; +} + +ArrayType::ArrayType (Analysis::NodeMapping mappings, + std::unique_ptr type, + std::unique_ptr array_size, location_t locus) + : TypeNoBounds (mappings, locus), elem_type (std::move (type)), + size (std::move (array_size)) +{} + +ArrayType::ArrayType (ArrayType const &other) + : TypeNoBounds (other.mappings, other.locus), + elem_type (other.elem_type->clone_type ()), size (other.size->clone_expr ()) +{} + +ArrayType & +ArrayType::operator= (ArrayType const &other) +{ + mappings = other.mappings; + elem_type = other.elem_type->clone_type (); + size = other.size->clone_expr (); + locus = other.locus; + return *this; +} + +SliceType::SliceType (Analysis::NodeMapping mappings, + std::unique_ptr type, location_t locus) + : TypeNoBounds (mappings, locus), elem_type (std::move (type)) +{} + +SliceType::SliceType (SliceType const &other) + : TypeNoBounds (other.mappings, other.locus), + elem_type (other.elem_type->clone_type ()) +{} + +SliceType & +SliceType::operator= (SliceType const &other) +{ + mappings = other.mappings; + elem_type = other.elem_type->clone_type (); + locus = other.locus; + + return *this; +} + +InferredType::InferredType (Analysis::NodeMapping mappings, location_t locus) + : TypeNoBounds (mappings, locus) +{} + +MaybeNamedParam::MaybeNamedParam (Identifier name, ParamKind param_kind, + std::unique_ptr param_type, + location_t locus) + : param_type (std::move (param_type)), param_kind (param_kind), + name (std::move (name)), locus (locus) +{} + +MaybeNamedParam::MaybeNamedParam (MaybeNamedParam const &other) + : param_type (other.param_type->clone_type ()), param_kind (other.param_kind), + name (other.name), locus (other.locus) +{} + +MaybeNamedParam & +MaybeNamedParam::operator= (MaybeNamedParam const &other) +{ + name = other.name; + param_kind = other.param_kind; + param_type = other.param_type->clone_type (); + locus = other.locus; + + return *this; +} + +BareFunctionType::BareFunctionType ( + Analysis::NodeMapping mappings, std::vector lifetime_params, + FunctionQualifiers qualifiers, std::vector named_params, + bool is_variadic, std::unique_ptr type, location_t locus) + : TypeNoBounds (mappings, locus), for_lifetimes (std::move (lifetime_params)), + function_qualifiers (std::move (qualifiers)), + params (std::move (named_params)), is_variadic (is_variadic), + return_type (std::move (type)) +{} + +BareFunctionType::BareFunctionType (BareFunctionType const &other) + : TypeNoBounds (other.mappings, other.locus), + for_lifetimes (other.for_lifetimes), + function_qualifiers (other.function_qualifiers), params (other.params), + is_variadic (other.is_variadic), + return_type (other.return_type->clone_type ()) +{} + +BareFunctionType & +BareFunctionType::operator= (BareFunctionType const &other) +{ + mappings = other.mappings; + for_lifetimes = other.for_lifetimes; + function_qualifiers = other.function_qualifiers; + params = other.params; + is_variadic = other.is_variadic; + return_type = other.return_type->clone_type (); + locus = other.locus; + + return *this; +} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir-type.h b/gcc/rust/hir/tree/rust-hir-type.h index a8ac4a1b076e..bc665eb7052e 100644 --- a/gcc/rust/hir/tree/rust-hir-type.h +++ b/gcc/rust/hir/tree/rust-hir-type.h @@ -19,73 +19,13 @@ #ifndef RUST_HIR_TYPE_H #define RUST_HIR_TYPE_H +#include "rust-hir-type-abstract.h" #include "rust-common.h" -#include "rust-hir.h" -#include "rust-hir-path.h" +#include "rust-hir-trait-bound.h" +#include "rust-hir-item.h" namespace Rust { namespace HIR { -// definitions moved to rust-ast.h -class TypeParamBound; -class Lifetime; - -// A trait bound -class TraitBound : public TypeParamBound -{ - bool in_parens; - BoundPolarity polarity; - std::vector for_lifetimes; - TypePath type_path; - location_t locus; - - Analysis::NodeMapping mappings; - -public: - // Returns whether trait bound has "for" lifetimes - bool has_for_lifetimes () const { return !for_lifetimes.empty (); } - - TraitBound (Analysis::NodeMapping mapping, TypePath type_path, - location_t locus, bool in_parens = false, - BoundPolarity polarity = BoundPolarity::RegularBound, - std::vector for_lifetimes - = std::vector ()) - : in_parens (in_parens), polarity (polarity), - for_lifetimes (std::move (for_lifetimes)), - type_path (std::move (type_path)), locus (locus), mappings (mapping) - {} - - std::string as_string () const override; - - location_t get_locus () const override final { return locus; } - - void accept_vis (HIRFullVisitor &vis) override; - - Analysis::NodeMapping get_mappings () const override final - { - return mappings; - } - - std::vector &get_for_lifetimes () { return for_lifetimes; } - bool get_in_parens () { return in_parens; } - BoundPolarity get_polarity () { return polarity; } - - BoundType get_bound_type () const final override { return TRAITBOUND; } - - TypePath &get_path () { return type_path; } - - const TypePath &get_path () const { return type_path; } - -protected: - /* Use covariance to implement clone function as returning this object rather - * than base */ - TraitBound *clone_type_param_bound_impl () const override - { - return new TraitBound (*this); - } -}; - -// definition moved to rust-ast.h -class TypeNoBounds; // An impl trait? Poor reference material here. class ImplTraitType : public Type @@ -105,31 +45,13 @@ class ImplTraitType : public Type public: ImplTraitType (Analysis::NodeMapping mappings, std::vector> type_param_bounds, - location_t locus) - : Type (mappings, locus), type_param_bounds (std::move (type_param_bounds)) - {} + location_t locus); // copy constructor with vector clone - ImplTraitType (ImplTraitType const &other) - : Type (other.mappings, other.locus) - { - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - } + ImplTraitType (ImplTraitType const &other); // overloaded assignment operator to clone - ImplTraitType &operator= (ImplTraitType const &other) - { - locus = other.locus; - mappings = other.mappings; - - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - - return *this; - } + ImplTraitType &operator= (ImplTraitType const &other); // move constructors ImplTraitType (ImplTraitType &&other) = default; @@ -162,32 +84,13 @@ class TraitObjectType : public Type TraitObjectType ( Analysis::NodeMapping mappings, std::vector> type_param_bounds, - location_t locus, bool is_dyn_dispatch) - : Type (mappings, locus), has_dyn (is_dyn_dispatch), - type_param_bounds (std::move (type_param_bounds)) - {} + location_t locus, bool is_dyn_dispatch); // copy constructor with vector clone - TraitObjectType (TraitObjectType const &other) - : Type (other.mappings, other.locus), has_dyn (other.has_dyn) - { - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - } + TraitObjectType (TraitObjectType const &other); // overloaded assignment operator to clone - TraitObjectType &operator= (TraitObjectType const &other) - { - mappings = other.mappings; - has_dyn = other.has_dyn; - locus = other.locus; - type_param_bounds.reserve (other.type_param_bounds.size ()); - for (const auto &e : other.type_param_bounds) - type_param_bounds.push_back (e->clone_type_param_bound ()); - - return *this; - } + TraitObjectType &operator= (TraitObjectType const &other); // move constructors TraitObjectType (TraitObjectType &&other) = default; @@ -233,26 +136,15 @@ class ParenthesisedType : public TypeNoBounds public: // Constructor uses Type pointer for polymorphism ParenthesisedType (Analysis::NodeMapping mappings, - std::unique_ptr type_inside_parens, location_t locus) - : TypeNoBounds (mappings, locus), - type_in_parens (std::move (type_inside_parens)) - {} + std::unique_ptr type_inside_parens, + location_t locus); /* Copy constructor uses custom deep copy method for type to preserve * polymorphism */ - ParenthesisedType (ParenthesisedType const &other) - : TypeNoBounds (other.mappings, other.locus), - type_in_parens (other.type_in_parens->clone_type ()) - {} + ParenthesisedType (ParenthesisedType const &other); // overload assignment operator to use custom clone method - ParenthesisedType &operator= (ParenthesisedType const &other) - { - mappings = other.mappings; - type_in_parens = other.type_in_parens->clone_type (); - locus = other.locus; - return *this; - } + ParenthesisedType &operator= (ParenthesisedType const &other); // default move semantics ParenthesisedType (ParenthesisedType &&other) = default; @@ -264,13 +156,9 @@ class ParenthesisedType : public TypeNoBounds } // Creates a trait bound (clone of this one's trait bound) - HACK - TraitBound *to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const override - { - /* NOTE: obviously it is unknown whether the internal type is a trait bound - * due to polymorphism, so just let the internal type handle it. As - * parenthesised type, it must be in parentheses. */ - return type_in_parens->to_trait_bound (true); - } + std::unique_ptr + to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const override; + Type &get_type_in_parens () { return *type_in_parens; } void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRTypeVisitor &vis) override; @@ -308,8 +196,6 @@ class ImplTraitTypeOneBound : public TypeNoBounds void accept_vis (HIRTypeVisitor &vis) override; }; -class TypePath; // definition moved to "rust-path.h" - /* A type consisting of the "product" of others (the tuple's elements) in a * specific order */ class TupleType : public TypeNoBounds @@ -321,31 +207,13 @@ class TupleType : public TypeNoBounds bool is_unit_type () const { return elems.empty (); } TupleType (Analysis::NodeMapping mappings, - std::vector> elems, location_t locus) - : TypeNoBounds (mappings, locus), elems (std::move (elems)) - {} + std::vector> elems, location_t locus); // copy constructor with vector clone - TupleType (TupleType const &other) - : TypeNoBounds (other.mappings, other.locus) - { - mappings = other.mappings; - elems.reserve (other.elems.size ()); - for (const auto &e : other.elems) - elems.push_back (e->clone_type ()); - } + TupleType (TupleType const &other); // overloaded assignment operator to clone - TupleType &operator= (TupleType const &other) - { - locus = other.locus; - - elems.reserve (other.elems.size ()); - for (const auto &e : other.elems) - elems.push_back (e->clone_type ()); - - return *this; - } + TupleType &operator= (TupleType const &other); // move constructors TupleType (TupleType &&other) = default; @@ -390,9 +258,7 @@ class NeverType : public TypeNoBounds } public: - NeverType (Analysis::NodeMapping mappings, location_t locus) - : TypeNoBounds (mappings, locus) - {} + NeverType (Analysis::NodeMapping mappings, location_t locus); std::string as_string () const override { return "! (never type)"; } @@ -410,25 +276,13 @@ class RawPointerType : public TypeNoBounds public: // Constructor requires pointer for polymorphism reasons RawPointerType (Analysis::NodeMapping mappings, Mutability mut, - std::unique_ptr type, location_t locus) - : TypeNoBounds (mappings, locus), mut (mut), type (std::move (type)) - {} + std::unique_ptr type, location_t locus); // Copy constructor calls custom polymorphic clone function - RawPointerType (RawPointerType const &other) - : TypeNoBounds (other.mappings, other.locus), mut (other.mut), - type (other.type->clone_type ()) - {} + RawPointerType (RawPointerType const &other); // overload assignment operator to use custom clone method - RawPointerType &operator= (RawPointerType const &other) - { - mappings = other.mappings; - mut = other.mut; - type = other.type->clone_type (); - locus = other.locus; - return *this; - } + RawPointerType &operator= (RawPointerType const &other); // default move semantics RawPointerType (RawPointerType &&other) = default; @@ -484,28 +338,13 @@ class ReferenceType : public TypeNoBounds // Constructor ReferenceType (Analysis::NodeMapping mappings, Mutability mut, std::unique_ptr type_no_bounds, location_t locus, - Lifetime lifetime) - : TypeNoBounds (mappings, locus), lifetime (std::move (lifetime)), - mut (mut), type (std::move (type_no_bounds)) - {} + Lifetime lifetime); // Copy constructor with custom clone method - ReferenceType (ReferenceType const &other) - : TypeNoBounds (other.mappings, other.locus), lifetime (other.lifetime), - mut (other.mut), type (other.type->clone_type ()) - {} + ReferenceType (ReferenceType const &other); // Operator overload assignment operator to custom clone the unique pointer - ReferenceType &operator= (ReferenceType const &other) - { - mappings = other.mappings; - lifetime = other.lifetime; - mut = other.mut; - type = other.type->clone_type (); - locus = other.locus; - - return *this; - } + ReferenceType &operator= (ReferenceType const &other); // move constructors ReferenceType (ReferenceType &&other) = default; @@ -547,27 +386,13 @@ class ArrayType : public TypeNoBounds public: // Constructor requires pointers for polymorphism ArrayType (Analysis::NodeMapping mappings, std::unique_ptr type, - std::unique_ptr array_size, location_t locus) - : TypeNoBounds (mappings, locus), elem_type (std::move (type)), - size (std::move (array_size)) - {} + std::unique_ptr array_size, location_t locus); // Copy constructor requires deep copies of both unique pointers - ArrayType (ArrayType const &other) - : TypeNoBounds (other.mappings, other.locus), - elem_type (other.elem_type->clone_type ()), - size (other.size->clone_expr ()) - {} + ArrayType (ArrayType const &other); // Overload assignment operator to deep copy pointers - ArrayType &operator= (ArrayType const &other) - { - mappings = other.mappings; - elem_type = other.elem_type->clone_type (); - size = other.size->clone_expr (); - locus = other.locus; - return *this; - } + ArrayType &operator= (ArrayType const &other); // move constructors ArrayType (ArrayType &&other) = default; @@ -604,25 +429,13 @@ class SliceType : public TypeNoBounds public: // Constructor requires pointer for polymorphism SliceType (Analysis::NodeMapping mappings, std::unique_ptr type, - location_t locus) - : TypeNoBounds (mappings, locus), elem_type (std::move (type)) - {} + location_t locus); // Copy constructor requires deep copy of Type smart pointer - SliceType (SliceType const &other) - : TypeNoBounds (other.mappings, other.locus), - elem_type (other.elem_type->clone_type ()) - {} + SliceType (SliceType const &other); // Overload assignment operator to deep copy - SliceType &operator= (SliceType const &other) - { - mappings = other.mappings; - elem_type = other.elem_type->clone_type (); - locus = other.locus; - - return *this; - } + SliceType &operator= (SliceType const &other); // move constructors SliceType (SliceType &&other) = default; @@ -669,9 +482,7 @@ class InferredType : public TypeNoBounds } public: - InferredType (Analysis::NodeMapping mappings, location_t locus) - : TypeNoBounds (mappings, locus) - {} + InferredType (Analysis::NodeMapping mappings, location_t locus); std::string as_string () const override; @@ -679,8 +490,6 @@ class InferredType : public TypeNoBounds void accept_vis (HIRTypeVisitor &vis) override; }; -class QualifiedPathInType; // definition moved to "rust-path.h" - // A possibly named param used in a BaseFunctionType struct MaybeNamedParam { @@ -702,29 +511,15 @@ struct MaybeNamedParam public: MaybeNamedParam (Identifier name, ParamKind param_kind, - std::unique_ptr param_type, location_t locus) - : param_type (std::move (param_type)), param_kind (param_kind), - name (std::move (name)), locus (locus) - {} + std::unique_ptr param_type, location_t locus); // Copy constructor with clone - MaybeNamedParam (MaybeNamedParam const &other) - : param_type (other.param_type->clone_type ()), - param_kind (other.param_kind), name (other.name), locus (other.locus) - {} + MaybeNamedParam (MaybeNamedParam const &other); ~MaybeNamedParam () = default; // Overloaded assignment operator with clone - MaybeNamedParam &operator= (MaybeNamedParam const &other) - { - name = other.name; - param_kind = other.param_kind; - param_type = other.param_type->clone_type (); - locus = other.locus; - - return *this; - } + MaybeNamedParam &operator= (MaybeNamedParam const &other); // move constructors MaybeNamedParam (MaybeNamedParam &&other) = default; @@ -777,36 +572,13 @@ class BareFunctionType : public TypeNoBounds std::vector lifetime_params, FunctionQualifiers qualifiers, std::vector named_params, bool is_variadic, - std::unique_ptr type, location_t locus) - : TypeNoBounds (mappings, locus), - for_lifetimes (std::move (lifetime_params)), - function_qualifiers (std::move (qualifiers)), - params (std::move (named_params)), is_variadic (is_variadic), - return_type (std::move (type)) - {} + std::unique_ptr type, location_t locus); // Copy constructor with clone - BareFunctionType (BareFunctionType const &other) - : TypeNoBounds (other.mappings, other.locus), - for_lifetimes (other.for_lifetimes), - function_qualifiers (other.function_qualifiers), params (other.params), - is_variadic (other.is_variadic), - return_type (other.return_type->clone_type ()) - {} + BareFunctionType (BareFunctionType const &other); // Overload assignment operator to deep copy - BareFunctionType &operator= (BareFunctionType const &other) - { - mappings = other.mappings; - for_lifetimes = other.for_lifetimes; - function_qualifiers = other.function_qualifiers; - params = other.params; - is_variadic = other.is_variadic; - return_type = other.return_type->clone_type (); - locus = other.locus; - - return *this; - } + BareFunctionType &operator= (BareFunctionType const &other); // move constructors BareFunctionType (BareFunctionType &&other) = default; diff --git a/gcc/rust/hir/tree/rust-hir-visibility.h b/gcc/rust/hir/tree/rust-hir-visibility.h new file mode 100644 index 000000000000..a750d88f284d --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-visibility.h @@ -0,0 +1,80 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_VISIBILITY_H +#define RUST_HIR_VISIBILITY_H + +#include "rust-hir-simple-path.h" + +namespace Rust { +namespace HIR { +// Visibility of an item +struct Visibility +{ +public: + enum VisType + { + PRIVATE, + PUBLIC, + RESTRICTED, + ERROR, + }; + +private: + VisType vis_type; + HIR::SimplePath path; + location_t locus; + + // should this store location info? + +public: + Visibility (VisType vis_type, + HIR::SimplePath path = HIR::SimplePath::create_empty (), + location_t locus = UNDEF_LOCATION) + : vis_type (vis_type), path (std::move (path)), locus (locus) + {} + + // Returns whether visibility is in an error state. + bool is_error () const { return vis_type == ERROR; } + + // Does the current visibility refer to a simple `pub ` entirely public + bool is_public () const { return vis_type == PUBLIC; } + + // Is the current visibility public restricted to a certain path + bool is_restricted () const { return vis_type == RESTRICTED; } + + // Creates an error visibility. + static Visibility create_error () + { + return Visibility (ERROR, HIR::SimplePath::create_empty ()); + } + + VisType get_vis_type () const { return vis_type; } + + const HIR::SimplePath &get_path () const + { + rust_assert (!is_error ()); + return path; + } + + std::string as_string () const; +}; +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-visitable.h b/gcc/rust/hir/tree/rust-hir-visitable.h new file mode 100644 index 000000000000..9c05cbf81240 --- /dev/null +++ b/gcc/rust/hir/tree/rust-hir-visitable.h @@ -0,0 +1,41 @@ +// Copyright (C) 2020-2024 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 +// . + +#ifndef RUST_HIR_VISITABLE_H +#define RUST_HIR_VISITABLE_H + +namespace Rust { +namespace HIR { + +class HIRFullVisitor; +class HIRTraitItemVisitor; +class HIRImplVisitor; +class HIRStmtVisitor; +class HIRExpressionVisitor; +class HIRTypeVisitor; +class HIRPatternVisitor; + +class FullVisitable +{ +public: + virtual void accept_vis (HIRFullVisitor &vis) = 0; +}; +} // namespace HIR +} // namespace Rust + +#endif diff --git a/gcc/rust/hir/tree/rust-hir-visitor.h b/gcc/rust/hir/tree/rust-hir-visitor.h index d30901684b4c..6efc15dded6e 100644 --- a/gcc/rust/hir/tree/rust-hir-visitor.h +++ b/gcc/rust/hir/tree/rust-hir-visitor.h @@ -19,7 +19,6 @@ #ifndef RUST_HIR_VISITOR_H #define RUST_HIR_VISITOR_H -#include "rust-hir-expr.h" #include "rust-hir-full-decls.h" namespace Rust { diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index b00e147549e3..f7140ad93cfe 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -70,6 +70,33 @@ get_string_in_delims (std::string str_input, AST::DelimType delim_type) rust_unreachable (); } +Crate::Crate (std::vector> items, + AST::AttrVec inner_attrs, Analysis::NodeMapping mappings) + : WithInnerAttrs (std::move (inner_attrs)), items (std::move (items)), + mappings (mappings) +{} + +Crate::Crate (Crate const &other) + : WithInnerAttrs (other.inner_attrs), mappings (other.mappings) +{ + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_item ()); +} + +Crate & +Crate::operator= (Crate const &other) +{ + inner_attrs = other.inner_attrs; + mappings = other.mappings; + + items.reserve (other.items.size ()); + for (const auto &e : other.items) + items.push_back (e->clone_item ()); + + return *this; +} + std::string Crate::as_string () const { @@ -2677,14 +2704,14 @@ Expr::as_string () const } // hopefully definition here will prevent circular dependency issue -TraitBound * +std::unique_ptr TypePath::to_trait_bound (bool in_parens) const { // create clone FIXME is this required? or is copy constructor automatically // called? TypePath copy (*this); - return new TraitBound (mappings, std::move (copy), copy.get_locus (), - in_parens); + return Rust::make_unique (mappings, std::move (copy), + copy.get_locus (), in_parens); } std::string @@ -3012,7 +3039,7 @@ StructExprStructFields::as_string () const } else { - str += struct_base->as_string (); + str += (*struct_base)->as_string (); } return str; @@ -3994,6 +4021,12 @@ StructExprStructBase::accept_vis (HIRFullVisitor &vis) vis.visit (*this); } +void +StructExprStructBase::accept_vis (HIRExpressionVisitor &vis) +{ + vis.visit (*this); +} + void CallExpr::accept_vis (HIRFullVisitor &vis) { @@ -5152,20 +5185,5 @@ StaticItem::accept_vis (HIRVisItemVisitor &vis) vis.visit (*this); } -std::string -ConstGenericParam::as_string () const -{ - auto result = "ConstGenericParam: " + name + " : " + type->as_string (); - - if (default_expression) - result += " = " + default_expression->as_string (); - - return result; -} - -void -ConstGenericParam::accept_vis (HIRFullVisitor &) -{} - } // namespace HIR } // namespace Rust diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 05d855c0a7db..af63570c8ba8 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -19,778 +19,24 @@ #ifndef RUST_HIR_BASE_H #define RUST_HIR_BASE_H -#include "rust-ast.h" #include "rust-system.h" +#include "rust-ast.h" +#include "rust-hir-visitable.h" +#include "rust-hir-attrs.h" + #include "rust-token.h" + #include "rust-location.h" + #include "rust-hir-map.h" #include "rust-diagnostics.h" +#include "rust-hir-bound.h" namespace Rust { + typedef int TupleIndex; namespace HIR { -// foward decl: ast visitor -class HIRFullVisitor; -class HIRStmtVisitor; -class HIRTraitItemVisitor; -class HIRExternalItemVisitor; -class HIRVisItemVisitor; -class HIRExpressionVisitor; -class HIRPatternVisitor; -class HIRImplVisitor; -class HIRTypeVisitor; - -class WithOuterAttrs -{ -protected: - AST::AttrVec outer_attrs; - -public: - AST::AttrVec &get_outer_attrs () { return outer_attrs; } - const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } - - WithOuterAttrs (AST::AttrVec outer_attrs) - : outer_attrs (std::move (outer_attrs)){}; -}; - -class WithInnerAttrs -{ -protected: - AST::AttrVec inner_attrs; - -public: - AST::AttrVec get_inner_attrs () const { return inner_attrs; } - WithInnerAttrs (AST::AttrVec inner_attrs) - : inner_attrs (std::move (inner_attrs)){}; -}; - -class FullVisitable -{ -public: - virtual void accept_vis (HIRFullVisitor &vis) = 0; -}; - -// forward decl for use in token tree method -class Token; - -class Node -{ -public: - // Kind for downcasting various HIR nodes to other base classes when visiting - // them - enum BaseKind - { - /* class ExternalItem */ - EXTERNAL, - /* class TraitItem */ - TRAIT_ITEM, - /* class VisItem */ - VIS_ITEM, - /* class Item */ - ITEM, - /* class ImplItem */ - IMPL, - /* class Type */ - TYPE, - /* class Stmt */ - STMT, - /* class Expr */ - EXPR, - /* class Pattern */ - PATTERN, - }; - - /** - * Get the kind of HIR node we are dealing with. This is useful for - * downcasting to more precise types when necessary, i.e going from an `Item*` - * to a `VisItem*` - */ - virtual BaseKind get_hir_kind () = 0; -}; - -// A literal - value with a type. Used in LiteralExpr and LiteralPattern. -struct Literal -{ -public: - enum LitType - { - CHAR, - STRING, - BYTE, - BYTE_STRING, - INT, - FLOAT, - BOOL - }; - -private: - std::string value_as_string; - LitType type; - PrimitiveCoreType type_hint; - -public: - std::string as_string () const { return value_as_string; } - - LitType get_lit_type () const { return type; } - - PrimitiveCoreType get_type_hint () const { return type_hint; } - - Literal (std::string value_as_string, LitType type, - PrimitiveCoreType type_hint) - : value_as_string (std::move (value_as_string)), type (type), - type_hint (type_hint) - {} - - static Literal create_error () - { - return Literal ("", CHAR, PrimitiveCoreType::CORETYPE_UNKNOWN); - } - - void set_lit_type (LitType lt) { type = lt; } - - // Returns whether literal is in an invalid state. - bool is_error () const { return value_as_string == ""; } - - bool is_equal (Literal &other) - { - return value_as_string == other.value_as_string && type == other.type - && type_hint == other.type_hint; - } -}; - -/* Base statement abstract class. Note that most "statements" are not allowed in - * top-level module scope - only a subclass of statements called "items" are. */ -class Stmt : public Node, public FullVisitable -{ -public: - using FullVisitable::accept_vis; - - // Unique pointer custom clone function - std::unique_ptr clone_stmt () const - { - return std::unique_ptr (clone_stmt_impl ()); - } - - BaseKind get_hir_kind () override { return STMT; } - - virtual ~Stmt () {} - - virtual std::string as_string () const = 0; - - virtual void accept_vis (HIRStmtVisitor &vis) = 0; - - virtual location_t get_locus () const = 0; - - virtual bool is_unit_check_needed () const { return false; } - - const Analysis::NodeMapping &get_mappings () const { return mappings; } - - virtual bool is_item () const = 0; - -protected: - Stmt (Analysis::NodeMapping mappings) : mappings (std::move (mappings)) {} - - // Clone function implementation as pure virtual method - virtual Stmt *clone_stmt_impl () const = 0; - - Analysis::NodeMapping mappings; -}; - -// Rust "item" HIR node (declaration of top-level/module-level allowed stuff) -class Item : public Stmt, public WithOuterAttrs -{ - // TODO: should outer attrs be defined here or in each derived class? -public: - enum class ItemKind - { - Static, - Constant, - TypeAlias, - Function, - UseDeclaration, - ExternBlock, - ExternCrate, - Struct, - Union, - Enum, - EnumItem, // FIXME: ARTHUR: Do we need that? - Trait, - Impl, - Module, - }; - - static std::string item_kind_string (ItemKind kind); - - virtual ItemKind get_item_kind () const = 0; - - // Unique pointer custom clone function - std::unique_ptr clone_item () const - { - return std::unique_ptr (clone_item_impl ()); - } - - BaseKind get_hir_kind () override { return ITEM; } - - std::string as_string () const override; - - /* Adds crate names to the vector passed by reference, if it can - * (polymorphism). */ - virtual void - add_crate_name (std::vector &names ATTRIBUTE_UNUSED) const - {} - - bool is_item () const override final { return true; } - -protected: - // Constructor - Item (Analysis::NodeMapping mappings, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : Stmt (std::move (mappings)), WithOuterAttrs (std::move (outer_attribs)) - {} - - // Clone function implementation as pure virtual method - virtual Item *clone_item_impl () const = 0; - - /* Save having to specify two clone methods in derived classes by making - * statement clone return item clone. Hopefully won't affect performance too - * much. */ - Item *clone_stmt_impl () const override { return clone_item_impl (); } -}; - -// forward decl of ExprWithoutBlock -class ExprWithoutBlock; - -// Base expression HIR node - abstract -class Expr : public Node, virtual public FullVisitable -{ -public: - using FullVisitable::accept_vis; - -protected: - AST::AttrVec outer_attrs; - Analysis::NodeMapping mappings; - -public: - enum BlockType - { - WITH_BLOCK, - WITHOUT_BLOCK, - }; - - enum ExprType - { - Lit, - Operator, - Grouped, - Array, - ArrayIndex, - Tuple, - TupleIdx, - Struct, - Call, - MethodCall, - FieldAccess, - Closure, - Block, - Continue, - Break, - Range, - Return, - UnsafeBlock, - BaseLoop, - If, - Match, - Await, - AsyncBlock, - Path, - InlineAsm, - }; - - BaseKind get_hir_kind () override final { return EXPR; } - - const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } - - // Unique pointer custom clone function - std::unique_ptr clone_expr () const - { - return std::unique_ptr (clone_expr_impl ()); - } - - // TODO: make pure virtual if move out outer attributes to derived classes - virtual std::string as_string () const; - - virtual ~Expr () {} - - virtual location_t get_locus () const = 0; - - const Analysis::NodeMapping &get_mappings () const { return mappings; } - - // Clone function implementation as pure virtual method - virtual Expr *clone_expr_impl () const = 0; - - virtual BlockType get_block_expr_type () const = 0; - - virtual ExprType get_expression_type () const = 0; - - virtual void accept_vis (HIRExpressionVisitor &vis) = 0; - -protected: - // Constructor - Expr (Analysis::NodeMapping mappings, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : outer_attrs (std::move (outer_attribs)), mappings (std::move (mappings)) - {} - - // TODO: think of less hacky way to implement this kind of thing - // Sets outer attributes. - void set_outer_attrs (AST::AttrVec outer_attrs_to_set) - { - outer_attrs = std::move (outer_attrs_to_set); - } -}; - -// HIR node for an expression without an accompanying block - abstract -class ExprWithoutBlock : public Expr -{ -protected: - // Constructor - ExprWithoutBlock (Analysis::NodeMapping mappings, - AST::AttrVec outer_attribs = AST::AttrVec ()) - : Expr (std::move (mappings), std::move (outer_attribs)) - {} - - // pure virtual clone implementation - virtual ExprWithoutBlock *clone_expr_without_block_impl () const = 0; - - /* Save having to specify two clone methods in derived classes by making expr - * clone return exprwithoutblock clone. Hopefully won't affect performance too - * much. */ - ExprWithoutBlock *clone_expr_impl () const override - { - return clone_expr_without_block_impl (); - } - -public: - // Unique pointer custom clone function - std::unique_ptr clone_expr_without_block () const - { - return std::unique_ptr (clone_expr_without_block_impl ()); - } - - BlockType get_block_expr_type () const final override - { - return BlockType::WITHOUT_BLOCK; - }; -}; - -// Pattern base HIR node -class Pattern : public Node, virtual public FullVisitable -{ -public: - using FullVisitable::accept_vis; - - enum PatternType - { - PATH, - LITERAL, - IDENTIFIER, - WILDCARD, - RANGE, - REFERENCE, - STRUCT, - TUPLE_STRUCT, - TUPLE, - GROUPED, - SLICE, - ALT - }; - - BaseKind get_hir_kind () override final { return PATTERN; } - - // Unique pointer custom clone function - std::unique_ptr clone_pattern () const - { - return std::unique_ptr (clone_pattern_impl ()); - } - - // possible virtual methods: is_refutable() - - virtual ~Pattern () {} - - virtual std::string as_string () const = 0; - - virtual void accept_vis (HIRPatternVisitor &vis) = 0; - - virtual const Analysis::NodeMapping &get_mappings () const = 0; - - virtual location_t get_locus () const = 0; - - virtual PatternType get_pattern_type () const = 0; - -protected: - // Clone pattern implementation as pure virtual method - virtual Pattern *clone_pattern_impl () const = 0; -}; - -// forward decl for Type -class TraitBound; - -// Base class for types as represented in HIR - abstract -class Type : public Node, public FullVisitable -{ -public: - using FullVisitable::accept_vis; - // Unique pointer custom clone function - std::unique_ptr clone_type () const - { - return std::unique_ptr (clone_type_impl ()); - } - - // virtual destructor - virtual ~Type () {} - - BaseKind get_hir_kind () override final { return TYPE; } - - virtual std::string as_string () const = 0; - - /* HACK: convert to trait bound. Virtual method overriden by classes that - * enable this. */ - virtual TraitBound *to_trait_bound (bool in_parens ATTRIBUTE_UNUSED) const - { - return nullptr; - } - /* as pointer, shouldn't require definition beforehand, only forward - * declaration. */ - - virtual void accept_vis (HIRTypeVisitor &vis) = 0; - - virtual const Analysis::NodeMapping &get_mappings () const - { - return mappings; - } - virtual location_t get_locus () const { return locus; } - -protected: - Type (Analysis::NodeMapping mappings, location_t locus) - : mappings (mappings), locus (locus) - {} - - // Clone function implementation as pure virtual method - virtual Type *clone_type_impl () const = 0; - - Analysis::NodeMapping mappings; - location_t locus; -}; - -// A type without parentheses? - abstract -class TypeNoBounds : public Type -{ -public: - // Unique pointer custom clone function - std::unique_ptr clone_type_no_bounds () const - { - return std::unique_ptr (clone_type_no_bounds_impl ()); - } - -protected: - TypeNoBounds (Analysis::NodeMapping mappings, location_t locus) - : Type (mappings, locus) - {} - - // Clone function implementation as pure virtual method - virtual TypeNoBounds *clone_type_no_bounds_impl () const = 0; - - /* Save having to specify two clone methods in derived classes by making type - * clone return typenobounds clone. Hopefully won't affect performance too - * much. */ - TypeNoBounds *clone_type_impl () const override - { - return clone_type_no_bounds_impl (); - } -}; - -/* Abstract base class representing a type param bound - Lifetime and TraitBound - * extends it */ -class TypeParamBound : public FullVisitable -{ -public: - using FullVisitable::accept_vis; - enum BoundType - { - LIFETIME, - TRAITBOUND - }; - - virtual ~TypeParamBound () {} - - // Unique pointer custom clone function - std::unique_ptr clone_type_param_bound () const - { - return std::unique_ptr (clone_type_param_bound_impl ()); - } - - virtual std::string as_string () const = 0; - - virtual Analysis::NodeMapping get_mappings () const = 0; - - virtual location_t get_locus () const = 0; - - virtual BoundType get_bound_type () const = 0; - -protected: - // Clone function implementation as pure virtual method - virtual TypeParamBound *clone_type_param_bound_impl () const = 0; -}; - -// Represents a lifetime (and is also a kind of type param bound) -class Lifetime : public TypeParamBound -{ -private: - AST::Lifetime::LifetimeType lifetime_type; - std::string lifetime_name; - location_t locus; - Analysis::NodeMapping mappings; - -public: - // Constructor - Lifetime (Analysis::NodeMapping mapping, AST::Lifetime::LifetimeType type, - std::string name, location_t locus) - : lifetime_type (type), lifetime_name (std::move (name)), locus (locus), - mappings (mapping) - {} - - // Returns true if the lifetime is in an error state. - bool is_error () const - { - return lifetime_type == AST::Lifetime::LifetimeType::NAMED - && lifetime_name.empty (); - } - - static Lifetime error () - { - return Lifetime (Analysis::NodeMapping::get_error (), - AST::Lifetime::LifetimeType::NAMED, "", UNDEF_LOCATION); - } - - std::string as_string () const override; - - void accept_vis (HIRFullVisitor &vis) override; - - WARN_UNUSED_RESULT const std::string &get_name () const - { - return lifetime_name; - } - - AST::Lifetime::LifetimeType get_lifetime_type () const - { - return lifetime_type; - } - - location_t get_locus () const override final { return locus; } - - Analysis::NodeMapping get_mappings () const override final - { - return mappings; - } - - BoundType get_bound_type () const final override { return LIFETIME; } - -protected: - /* Use covariance to implement clone function as returning this object rather - * than base */ - Lifetime *clone_type_param_bound_impl () const override - { - return new Lifetime (*this); - } -}; - -/* Base generic parameter in HIR. Abstract - can be represented by a Lifetime or - * Type param */ -class GenericParam : public FullVisitable -{ -public: - using FullVisitable::accept_vis; - - virtual ~GenericParam () {} - - enum class GenericKind - { - TYPE, - LIFETIME, - CONST, - }; - - virtual AST::AttrVec &get_outer_attrs () = 0; - virtual bool has_outer_attribute () const = 0; - - // Unique pointer custom clone function - std::unique_ptr clone_generic_param () const - { - return std::unique_ptr (clone_generic_param_impl ()); - } - - virtual std::string as_string () const = 0; - - virtual location_t get_locus () const = 0; - - Analysis::NodeMapping get_mappings () const { return mappings; } - - enum GenericKind get_kind () const { return kind; } - -protected: - // Clone function implementation as pure virtual method - virtual GenericParam *clone_generic_param_impl () const = 0; - - Analysis::NodeMapping mappings; - - enum GenericKind kind; - - GenericParam (Analysis::NodeMapping mapping, - enum GenericKind kind = GenericKind::TYPE) - : mappings (mapping), kind (kind) - {} -}; - -// A lifetime generic parameter (as opposed to a type generic parameter) -class LifetimeParam : public GenericParam -{ - Lifetime lifetime; - - // bool has_lifetime_bounds; - // LifetimeBounds lifetime_bounds; - std::vector lifetime_bounds; // inlined LifetimeBounds - - AST::AttrVec outer_attrs; - - location_t locus; - -public: - Lifetime get_lifetime () { return lifetime; } - - // Returns whether the lifetime param has any lifetime bounds. - bool has_lifetime_bounds () const { return !lifetime_bounds.empty (); } - - std::vector &get_lifetime_bounds () { return lifetime_bounds; } - - // Returns whether the lifetime param has an outer attribute. - bool has_outer_attribute () const override { return outer_attrs.size () > 1; } - - AST::AttrVec &get_outer_attrs () override { return outer_attrs; } - - // Returns whether the lifetime param is in an error state. - bool is_error () const { return lifetime.is_error (); } - - // Constructor - LifetimeParam (Analysis::NodeMapping mappings, Lifetime lifetime, - location_t locus = UNDEF_LOCATION, - std::vector lifetime_bounds - = std::vector (), - AST::AttrVec outer_attrs = std::vector ()) - : GenericParam (mappings, GenericKind::LIFETIME), - lifetime (std::move (lifetime)), - lifetime_bounds (std::move (lifetime_bounds)), - outer_attrs (std::move (outer_attrs)), locus (locus) - {} - - // TODO: remove copy and assignment operator definitions - not required - - // Copy constructor with clone - LifetimeParam (LifetimeParam const &other) - : GenericParam (other.mappings, GenericKind::LIFETIME), - lifetime (other.lifetime), lifetime_bounds (other.lifetime_bounds), - outer_attrs (other.outer_attrs), locus (other.locus) - {} - - // Overloaded assignment operator to clone attribute - LifetimeParam &operator= (LifetimeParam const &other) - { - lifetime = other.lifetime; - lifetime_bounds = other.lifetime_bounds; - outer_attrs = other.outer_attrs; - locus = other.locus; - mappings = other.mappings; - - return *this; - } - - // move constructors - LifetimeParam (LifetimeParam &&other) = default; - LifetimeParam &operator= (LifetimeParam &&other) = default; - - std::string as_string () const override; - - void accept_vis (HIRFullVisitor &vis) override; - - location_t get_locus () const override final { return locus; } - -protected: - /* Use covariance to implement clone function as returning this object rather - * than base */ - LifetimeParam *clone_generic_param_impl () const override - { - return new LifetimeParam (*this); - } -}; - -class ConstGenericParam : public GenericParam -{ -public: - ConstGenericParam (std::string name, std::unique_ptr type, - std::unique_ptr default_expression, - Analysis::NodeMapping mapping, location_t locus) - : GenericParam (mapping, GenericKind::CONST), name (std::move (name)), - type (std::move (type)), - default_expression (std::move (default_expression)), locus (locus) - {} - - ConstGenericParam (const ConstGenericParam &other) : GenericParam (other) - { - name = other.name; - locus = other.locus; - - if (other.type) - type = other.type->clone_type (); - if (other.default_expression) - default_expression = other.default_expression->clone_expr (); - } - - bool has_outer_attribute () const override { return false; } - - AST::AttrVec &get_outer_attrs () override { return outer_attrs; } - - std::string as_string () const override final; - - void accept_vis (HIRFullVisitor &vis) override final; - - location_t get_locus () const override final { return locus; }; - - bool has_default_expression () { return default_expression != nullptr; } - - std::string get_name () { return name; } - Type &get_type () { return *type; } - Expr &get_default_expression () { return *default_expression; } - -protected: - /* Use covariance to implement clone function as returning this object rather - * than base */ - ConstGenericParam *clone_generic_param_impl () const override - { - return new ConstGenericParam (*this); - } - -private: - std::string name; - std::unique_ptr type; - - /* const params have no outer attrs, should be empty */ - AST::AttrVec outer_attrs = std::vector (); - - /* Optional - can be a null pointer if there is no default expression */ - std::unique_ptr default_expression; - - location_t locus; -}; // Item used in trait declarations - abstract base class class TraitItem : public Node, public FullVisitable @@ -891,34 +137,15 @@ class Crate : public WithInnerAttrs public: // Constructor Crate (std::vector> items, AST::AttrVec inner_attrs, - Analysis::NodeMapping mappings) - : WithInnerAttrs (std::move (inner_attrs)), items (std::move (items)), - mappings (mappings) - {} + Analysis::NodeMapping mappings); // Copy constructor with vector clone - Crate (Crate const &other) - : WithInnerAttrs (other.inner_attrs), mappings (other.mappings) - { - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_item ()); - } + Crate (Crate const &other); ~Crate () = default; // Overloaded assignment operator with vector clone - Crate &operator= (Crate const &other) - { - inner_attrs = other.inner_attrs; - mappings = other.mappings; - - items.reserve (other.items.size ()); - for (const auto &e : other.items) - items.push_back (e->clone_item ()); - - return *this; - } + Crate &operator= (Crate const &other); // Move constructors Crate (Crate &&other) = default; @@ -931,27 +158,6 @@ class Crate : public WithInnerAttrs std::vector> &get_items () { return items; } }; -// Base path expression HIR node - abstract -class PathExpr : public ExprWithoutBlock -{ -protected: - PathExpr (Analysis::NodeMapping mappings, AST::AttrVec outer_attribs) - : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)) - {} - -public: - /* Replaces the outer attributes of this path expression with the given outer - * attributes. */ - void replace_outer_attrs (AST::AttrVec outer_attrs) - { - set_outer_attrs (std::move (outer_attrs)); - } - - ExprType get_expression_type () const final override - { - return ExprType::Path; - } -}; } // namespace HIR } // namespace Rust diff --git a/gcc/rust/typecheck/rust-hir-type-check-struct.cc b/gcc/rust/typecheck/rust-hir-type-check-struct.cc index 47881021b699..1931f08ff4d2 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-struct.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-struct.cc @@ -56,17 +56,18 @@ TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr) if (struct_expr.has_struct_base ()) { TyTy::BaseType *base_resolved - = TypeCheckExpr::Resolve (*struct_expr.struct_base->base_struct); + = TypeCheckExpr::Resolve (struct_expr.get_struct_base ().get_base ()); TyTy::BaseType *base_unify = unify_site ( - struct_expr.struct_base->base_struct->get_mappings ().get_hirid (), + struct_expr.get_struct_base ().get_base ().get_mappings ().get_hirid (), TyTy::TyWithLocation (struct_path_resolved), TyTy::TyWithLocation (base_resolved), - struct_expr.struct_base->base_struct->get_locus ()); + struct_expr.get_struct_base ().get_base ().get_locus ()); if (base_unify->get_kind () != struct_path_ty->get_kind ()) { - rust_fatal_error (struct_expr.struct_base->base_struct->get_locus (), - "incompatible types for base struct reference"); + rust_fatal_error ( + struct_expr.get_struct_base ().get_base ().get_locus (), + "incompatible types for base struct reference"); return; } @@ -190,26 +191,29 @@ TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr) for (auto &missing : missing_fields) { HIR::Expr *receiver - = struct_expr.struct_base->base_struct->clone_expr_impl (); + = struct_expr.get_struct_base ().get_base ().clone_expr_impl (); HIR::StructExprField *implicit_field = nullptr; AST::AttrVec outer_attribs; auto crate_num = mappings.get_current_crate (); - Analysis::NodeMapping mapping ( - crate_num, - struct_expr.struct_base->base_struct->get_mappings () - .get_nodeid (), - mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); + Analysis::NodeMapping mapping (crate_num, + struct_expr.get_struct_base () + .get_base () + .get_mappings () + .get_nodeid (), + mappings.get_next_hir_id ( + crate_num), + UNKNOWN_LOCAL_DEFID); HIR::Expr *field_value = new HIR::FieldAccessExpr ( mapping, std::unique_ptr (receiver), missing, std::move (outer_attribs), - struct_expr.struct_base->base_struct->get_locus ()); + struct_expr.get_struct_base ().get_base ().get_locus ()); implicit_field = new HIR::StructExprFieldIdentifierValue ( mapping, missing, std::unique_ptr (field_value), - struct_expr.struct_base->base_struct->get_locus ()); + struct_expr.get_struct_base ().get_base ().get_locus ()); size_t field_index; bool ok = variant->lookup_field (missing, nullptr, &field_index); diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index b3c19efadee9..a5f415a33d4c 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // . +#include "rust-hir-full-decls.h" #include "rust-hir-type-bounds.h" #include "rust-hir-trait-resolve.h" #include "rust-substitution-mapper.h" @@ -203,21 +204,21 @@ TypeCheckBase::get_predicate_from_bound ( = HIR::GenericArgs::create_empty (type_path.get_locus ()); auto &final_seg = type_path.get_final_segment (); - switch (final_seg->get_type ()) + switch (final_seg.get_type ()) { case HIR::TypePathSegment::SegmentType::GENERIC: { - auto final_generic_seg - = static_cast (final_seg.get ()); - if (final_generic_seg->has_generic_args ()) + auto &final_generic_seg + = static_cast (final_seg); + if (final_generic_seg.has_generic_args ()) { - args = final_generic_seg->get_generic_args (); + args = final_generic_seg.get_generic_args (); } } break; case HIR::TypePathSegment::SegmentType::FUNCTION: { auto &final_function_seg - = static_cast (*final_seg); + = static_cast (final_seg); auto &fn = final_function_seg.get_function_path (); // we need to make implicit generic args which must be an implicit @@ -225,7 +226,7 @@ TypeCheckBase::get_predicate_from_bound ( auto crate_num = mappings.get_current_crate (); HirId implicit_args_id = mappings.get_next_hir_id (); Analysis::NodeMapping mapping (crate_num, - final_seg->get_mappings ().get_nodeid (), + final_seg.get_mappings ().get_nodeid (), implicit_args_id, UNKNOWN_LOCAL_DEFID); std::vector> params_copy; @@ -234,12 +235,10 @@ TypeCheckBase::get_predicate_from_bound ( params_copy.push_back (p->clone_type ()); } - HIR::TupleType *implicit_tuple - = new HIR::TupleType (mapping, std::move (params_copy), - final_seg->get_locus ()); - std::vector> inputs; - inputs.push_back (std::unique_ptr (implicit_tuple)); + inputs.push_back ( + Rust::make_unique (mapping, std::move (params_copy), + final_seg.get_locus ())); // resolve the fn_once_output type which assumes there must be an output // set @@ -249,7 +248,7 @@ TypeCheckBase::get_predicate_from_bound ( HIR::TraitItem *trait_item = mappings .lookup_trait_item_lang_item (LangItem::Kind::FN_ONCE_OUTPUT, - final_seg->get_locus ()) + final_seg.get_locus ()) .value (); std::vector bindings; @@ -263,7 +262,7 @@ TypeCheckBase::get_predicate_from_bound ( args = HIR::GenericArgs ({} /* lifetimes */, std::move (inputs) /* type_args*/, std::move (bindings) /* binding_args*/, - {} /* const_args */, final_seg->get_locus ()); + {} /* const_args */, final_seg.get_locus ()); } break; From 3e3bcdd5d2785ab6d284e5c6d6f6c1d8871b3ac5 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Mon, 28 Oct 2024 18:08:52 +0100 Subject: [PATCH 04/11] Fixes some tests appearing with a moved variant A variant being moved lead to a null being created and a segfault later down the line. gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::visit): Call getter instead of size function. * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): Only check privacy if the type is present. * hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Use an optional. * hir/tree/rust-hir-generic-param.h: Assert type before getting it. * hir/tree/rust-hir-item.h: Assert pointers before dereference, fix has_type condition. * hir/tree/rust-hir-path.h: Add more assertions. * hir/tree/rust-hir-stmt.cc: Change constructor with optionals. * hir/tree/rust-hir-stmt.h: Use optionals over smart pointers to emphasize these fields might be missing. * hir/tree/rust-hir.cc (LetStmt::as_string): Use getters. * typecheck/rust-hir-type-check-expr.cc: Clone structures to prevent parent's fields from being nulled by the move operation. * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Use optionals. * typecheck/rust-tyty.cc: Likewise. * typecheck/rust-tyty.h: Likewise. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/backend/rust-compile-expr.cc | 2 +- .../errors/privacy/rust-privacy-reporter.cc | 6 +- gcc/rust/hir/rust-ast-lower-stmt.cc | 20 +++--- gcc/rust/hir/tree/rust-hir-generic-param.h | 6 +- gcc/rust/hir/tree/rust-hir-item.h | 69 +++++++++++++++---- gcc/rust/hir/tree/rust-hir-path.h | 18 ++++- gcc/rust/hir/tree/rust-hir-stmt.cc | 26 ++++--- gcc/rust/hir/tree/rust-hir-stmt.h | 40 ++++++++--- gcc/rust/hir/tree/rust-hir.cc | 4 +- .../typecheck/rust-hir-type-check-expr.cc | 5 +- .../typecheck/rust-hir-type-check-item.cc | 7 +- gcc/rust/typecheck/rust-tyty.cc | 40 ++++++++--- gcc/rust/typecheck/rust-tyty.h | 12 +++- 13 files changed, 189 insertions(+), 66 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 35a6f7044090..c4ae79dbde27 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -1200,7 +1200,7 @@ CompileExpr::visit (HIR::CallExpr &expr) // this assumes all fields are in order from type resolution and if a // base struct was specified those fields are filed via accessors std::vector arguments; - for (size_t i = 0; i < expr.get_arguments ().size (); i++) + for (size_t i = 0; i < expr.num_params (); i++) { auto &argument = expr.get_arguments ().at (i); auto rvalue = CompileExpr::Compile (*argument, ctx); diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 39212bf97f51..0c3c9f64bca1 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -741,9 +741,11 @@ PrivacyReporter::visit (HIR::EmptyStmt &) void PrivacyReporter::visit (HIR::LetStmt &stmt) { - check_type_privacy (stmt.get_type ()); + if (stmt.has_type ()) + check_type_privacy (stmt.get_type ()); - stmt.get_init_expr ().accept_vis (*this); + if (stmt.has_init_expr ()) + stmt.get_init_expr ().accept_vis (*this); } void diff --git a/gcc/rust/hir/rust-ast-lower-stmt.cc b/gcc/rust/hir/rust-ast-lower-stmt.cc index 8f8a1a9a4d57..07283941328f 100644 --- a/gcc/rust/hir/rust-ast-lower-stmt.cc +++ b/gcc/rust/hir/rust-ast-lower-stmt.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // . +#include "optional.h" #include "rust-ast-lower-item.h" #include "rust-ast-lower-stmt.h" #include "rust-ast-lower-type.h" @@ -68,12 +69,16 @@ ASTLoweringStmt::visit (AST::LetStmt &stmt) { HIR::Pattern *variables = ASTLoweringPattern::translate (stmt.get_pattern (), true); - HIR::Type *type = stmt.has_type () - ? ASTLoweringType::translate (stmt.get_type ()) - : nullptr; - HIR::Expr *init_expression - = stmt.has_init_expr () ? ASTLoweringExpr::translate (stmt.get_init_expr ()) - : nullptr; + + auto type + = stmt.has_type () ? tl::optional> ( + std::unique_ptr (ASTLoweringType::translate (stmt.get_type ()))) + : tl::nullopt; + auto init_expression + = stmt.has_init_expr () + ? tl::optional> (std::unique_ptr ( + ASTLoweringExpr::translate (stmt.get_init_expr ()))) + : tl::nullopt; auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (), @@ -81,8 +86,7 @@ ASTLoweringStmt::visit (AST::LetStmt &stmt) UNKNOWN_LOCAL_DEFID); translated = new HIR::LetStmt (mapping, std::unique_ptr (variables), - std::unique_ptr (init_expression), - std::unique_ptr (type), + std::move (init_expression), std::move (type), stmt.get_outer_attrs (), stmt.get_locus ()); } diff --git a/gcc/rust/hir/tree/rust-hir-generic-param.h b/gcc/rust/hir/tree/rust-hir-generic-param.h index 73b93d48a49f..a1c59bf0d6bf 100644 --- a/gcc/rust/hir/tree/rust-hir-generic-param.h +++ b/gcc/rust/hir/tree/rust-hir-generic-param.h @@ -156,7 +156,11 @@ class ConstGenericParam : public GenericParam bool has_default_expression () { return default_expression != nullptr; } std::string get_name () { return name; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Expr &get_default_expression () { return *default_expression; } protected: diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 37c5313869ce..567432ecb325 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -24,6 +24,7 @@ #include "rust-common.h" #include "rust-hir-visibility.h" #include "rust-hir-generic-param.h" +#include "rust-system.h" namespace Rust { namespace HIR { @@ -141,7 +142,11 @@ class TypeParam : public GenericParam Identifier get_type_representation () const { return type_representation; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Analysis::NodeMapping get_type_mappings () const; @@ -414,7 +419,11 @@ struct SelfParam ImplicitSelfKind get_self_kind () const { return self_kind; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Analysis::NodeMapping get_mappings () { return mappings; } @@ -482,7 +491,11 @@ struct FunctionParam Pattern &get_param_name () { return *param_name; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } const Analysis::NodeMapping &get_mappings () const { return mappings; } }; @@ -1112,7 +1125,11 @@ class TypeAlias : public VisItem, public ImplItem WhereClause &get_where_clause () { return where_clause; } - Type &get_type_aliased () { return *existing_type; } + Type &get_type_aliased () + { + rust_assert (existing_type); + return *existing_type; + } Identifier get_new_type_name () const { return new_type_name; } @@ -1766,7 +1783,11 @@ class ConstantItem : public VisItem, public ImplItem void accept_vis (HIRImplVisitor &vis) override; void accept_vis (HIRVisItemVisitor &vis) override; - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Expr &get_expr () { return *const_expr; } @@ -1844,9 +1865,17 @@ class StaticItem : public VisItem bool is_mut () const { return mut == Mutability::Mut; } - Expr &get_expr () { return *expr; } + Expr &get_expr () + { + rust_assert (expr); + return *expr; + } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } ItemKind get_item_kind () const override { return ItemKind::Static; } @@ -2028,9 +2057,17 @@ class TraitItemConst : public TraitItem bool has_expr () const { return expr != nullptr; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } - Expr &get_expr () { return *expr; } + Expr &get_expr () + { + rust_assert (expr); + return *expr; + } const std::string trait_identifier () const override final { @@ -2274,9 +2311,13 @@ class ImplBlock : public VisItem, public WithInnerAttrs location_t get_locus () const override final { return locus; } - Type &get_type () { return *impl_type; }; + Type &get_type () + { + rust_assert (impl_type); + return *impl_type; + }; - bool has_type () { return impl_type == nullptr; } + bool has_type () { return impl_type != nullptr; } std::vector> &get_generic_params () { @@ -2436,7 +2477,11 @@ struct NamedFunctionParam Identifier get_param_name () const { return name; } - Type &get_type () { return *param_type; } + Type &get_type () + { + rust_assert (param_type); + return *param_type; + } Analysis::NodeMapping get_mappings () const { return mappings; } }; diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index 4839ca6442d3..df5fd0c4e469 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -99,8 +99,16 @@ class GenericArgsBinding Identifier &get_identifier () { return identifier; } const Identifier &get_identifier () const { return identifier; } - Type &get_type () { return *type; } - const Type &get_type () const { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } + const Type &get_type () const + { + rust_assert (type); + return *type; + } location_t get_locus () const { return locus; } }; @@ -633,7 +641,11 @@ class QualifiedPathType Analysis::NodeMapping get_mappings () const { return mappings; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } TypePath &get_trait () { return *trait; } diff --git a/gcc/rust/hir/tree/rust-hir-stmt.cc b/gcc/rust/hir/tree/rust-hir-stmt.cc index 764ea118cebe..025f67e2c9b1 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.cc +++ b/gcc/rust/hir/tree/rust-hir-stmt.cc @@ -17,13 +17,16 @@ // . #include "rust-hir-stmt.h" +#include "optional.h" +#include "rust-system.h" namespace Rust { namespace HIR { LetStmt::LetStmt (Analysis::NodeMapping mappings, std::unique_ptr variables_pattern, - std::unique_ptr init_expr, std::unique_ptr type, + tl::optional> init_expr, + tl::optional> type, AST::AttrVec outer_attrs, location_t locus) : Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)), variables_pattern (std::move (variables_pattern)), type (std::move (type)), @@ -38,10 +41,13 @@ LetStmt::LetStmt (LetStmt const &other) variables_pattern = other.variables_pattern->clone_pattern (); // guard to prevent null dereference (always required) - if (other.init_expr != nullptr) - init_expr = other.init_expr->clone_expr (); - if (other.type != nullptr) - type = other.type->clone_type (); + if (other.has_init_expr ()) + init_expr = other.get_init_expr ().clone_expr (); + + if (other.has_type ()) + type = other.get_type ().clone_type (); + else + type = tl::nullopt; } LetStmt & @@ -57,14 +63,14 @@ LetStmt::operator= (LetStmt const &other) variables_pattern = nullptr; // guard to prevent null dereference (always required) - if (other.init_expr != nullptr) - init_expr = other.init_expr->clone_expr (); + if (other.has_init_expr ()) + init_expr = other.get_init_expr ().clone_expr (); else init_expr = nullptr; - if (other.type != nullptr) - type = other.type->clone_type (); + if (other.has_type ()) + type = other.get_type ().clone_type (); else - type = nullptr; + type = tl::nullopt; return *this; } diff --git a/gcc/rust/hir/tree/rust-hir-stmt.h b/gcc/rust/hir/tree/rust-hir-stmt.h index 3e5adc165fba..1e17f047d3e2 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.h +++ b/gcc/rust/hir/tree/rust-hir-stmt.h @@ -22,6 +22,7 @@ #include "rust-hir.h" #include "rust-hir-path.h" #include "rust-hir-expr.h" +#include "rust-system.h" namespace Rust { namespace HIR { @@ -97,11 +98,9 @@ class LetStmt : public Stmt std::unique_ptr variables_pattern; - // bool has_type; - std::unique_ptr type; + tl::optional> type; - // bool has_init_expr; - std::unique_ptr init_expr; + tl::optional> init_expr; location_t locus; @@ -110,17 +109,18 @@ class LetStmt : public Stmt bool has_outer_attrs () const { return !outer_attrs.empty (); } // Returns whether let statement has a given return type. - bool has_type () const { return type != nullptr; } + bool has_type () const { return type.has_value (); } // Returns whether let statement has an initialisation expression. - bool has_init_expr () const { return init_expr != nullptr; } + bool has_init_expr () const { return init_expr.has_value (); } std::string as_string () const override; LetStmt (Analysis::NodeMapping mappings, std::unique_ptr variables_pattern, - std::unique_ptr init_expr, std::unique_ptr type, - AST::AttrVec outer_attrs, location_t locus); + tl::optional> init_expr, + tl::optional> type, AST::AttrVec outer_attrs, + location_t locus); // Copy constructor with clone LetStmt (LetStmt const &other); @@ -143,9 +143,29 @@ class LetStmt : public Stmt } std::vector &get_outer_attrs () { return outer_attrs; } - HIR::Type &get_type () { return *type; } + HIR::Type &get_type () + { + rust_assert (*type); + return *type.value (); + } + + const HIR::Type &get_type () const + { + rust_assert (*type); + return *type.value (); + } - HIR::Expr &get_init_expr () { return *init_expr; } + HIR::Expr &get_init_expr () + { + rust_assert (*init_expr); + return *init_expr.value (); + } + + const HIR::Expr &get_init_expr () const + { + rust_assert (*init_expr); + return *init_expr.value (); + } HIR::Pattern &get_pattern () { return *variables_pattern; } diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index f7140ad93cfe..764619115d70 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -2669,12 +2669,12 @@ LetStmt::as_string () const if (has_type ()) { - str += " : " + type->as_string (); + str += " : " + get_type ().as_string (); } if (has_init_expr ()) { - str += " = " + init_expr->as_string (); + str += " = " + get_init_expr ().as_string (); } return str; diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 8bb5058d3fc1..d4a60c67ccec 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -203,14 +203,13 @@ TypeCheckExpr::visit (HIR::CallExpr &expr) ok = adt->lookup_variant_by_id (variant_id, &lookup_variant); rust_assert (ok); - variant = std::move (*lookup_variant); + variant = std::move (*lookup_variant->clone ()); } else { rust_assert (adt->number_of_variants () == 1); - variant = std::move (*adt->get_variants ().at (0)); + variant = std::move (*adt->get_variants ().at (0)->clone ()); } - infered = TyTy::TypeCheckCallExpr::go (function_tyty, expr, variant, context); return; diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index 12784d2650c5..17c331e9a21a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -17,6 +17,7 @@ // . #include "rust-hir-type-check-item.h" +#include "optional.h" #include "rust-canonical-path.h" #include "rust-diagnostics.h" #include "rust-hir-type-check-enumitem.h" @@ -222,7 +223,7 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl) new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (), struct_decl.get_mappings ().get_defid (), struct_decl.get_identifier ().as_string (), ident, - TyTy::VariantDef::VariantType::TUPLE, nullptr, + TyTy::VariantDef::VariantType::TUPLE, tl::nullopt, std::move (fields))); // Process #[repr(X)] attribute, if any @@ -304,7 +305,7 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl) new TyTy::VariantDef (struct_decl.get_mappings ().get_hirid (), struct_decl.get_mappings ().get_defid (), struct_decl.get_identifier ().as_string (), ident, - TyTy::VariantDef::VariantType::STRUCT, nullptr, + TyTy::VariantDef::VariantType::STRUCT, tl::nullopt, std::move (fields))); // Process #[repr(X)] attribute, if any @@ -437,7 +438,7 @@ TypeCheckItem::visit (HIR::Union &union_decl) new TyTy::VariantDef (union_decl.get_mappings ().get_hirid (), union_decl.get_mappings ().get_defid (), union_decl.get_identifier ().as_string (), ident, - TyTy::VariantDef::VariantType::STRUCT, nullptr, + TyTy::VariantDef::VariantType::STRUCT, tl::nullopt, std::move (fields))); auto *type diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index ec910b2e6740..26c061258cd2 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -18,6 +18,7 @@ #include "rust-tyty.h" +#include "optional.h" #include "rust-tyty-visitor.h" #include "rust-hir-map.h" #include "rust-location.h" @@ -1343,7 +1344,7 @@ VariantDef::variant_type_string (VariantType type) VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, - std::unique_ptr &&discriminant) + tl::optional> &&discriminant) : id (id), defid (defid), identifier (identifier), ident (ident), discriminant (std::move (discriminant)) @@ -1354,7 +1355,7 @@ VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, VariantType type, - std::unique_ptr &&discriminant, + tl::optional> &&discriminant, std::vector fields) : id (id), defid (defid), identifier (identifier), ident (ident), type (type), discriminant (std::move (discriminant)), fields (fields) @@ -1369,7 +1370,7 @@ VariantDef::get_error_node () static VariantDef node = VariantDef (UNKNOWN_HIRID, UNKNOWN_DEFID, "", {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION}, - nullptr); + tl::nullopt); return node; } @@ -1461,15 +1462,28 @@ VariantDef::lookup_field (const std::string &lookup, HIR::Expr & VariantDef::get_discriminant () { - rust_assert (discriminant != nullptr); - return *discriminant; + return *discriminant.value (); +} + +const HIR::Expr & +VariantDef::get_discriminant () const +{ + return *discriminant.value (); +} + +bool +VariantDef::has_discriminant () const +{ + return discriminant.has_value (); } std::string VariantDef::as_string () const { if (type == VariantType::NUM) - return identifier + " = " + discriminant->as_string (); + return identifier + + (has_discriminant () ? " = " + get_discriminant ().as_string () + : ""); std::string buffer; for (size_t i = 0; i < fields.size (); ++i) @@ -1516,8 +1530,13 @@ VariantDef::clone () const for (auto &f : fields) cloned_fields.push_back ((StructFieldType *) f->clone ()); + auto &&discriminant_opt = has_discriminant () + ? tl::optional> ( + get_discriminant ().clone_expr ()) + : tl::nullopt; + return new VariantDef (id, defid, identifier, ident, type, - discriminant->clone_expr (), cloned_fields); + std::move (discriminant_opt), cloned_fields); } VariantDef * @@ -1527,8 +1546,13 @@ VariantDef::monomorphized_clone () const for (auto &f : fields) cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ()); + auto discriminant_opt = has_discriminant () + ? tl::optional> ( + get_discriminant ().clone_expr ()) + : tl::nullopt; + return new VariantDef (id, defid, identifier, ident, type, - discriminant->clone_expr (), cloned_fields); + std::move (discriminant_opt), cloned_fields); } const RustIdent & diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 2a8786169821..c896d1e03b75 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -570,10 +570,11 @@ class VariantDef static std::string variant_type_string (VariantType type); VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, - std::unique_ptr &&discriminant); + tl::optional> &&discriminant); VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, - VariantType type, std::unique_ptr &&discriminant, + VariantType type, + tl::optional> &&discriminant, std::vector fields); static VariantDef &get_error_node (); @@ -596,7 +597,10 @@ class VariantDef bool lookup_field (const std::string &lookup, StructFieldType **field_lookup, size_t *index) const; + bool has_discriminant () const; + HIR::Expr &get_discriminant (); + const HIR::Expr &get_discriminant () const; std::string as_string () const; @@ -614,8 +618,10 @@ class VariantDef std::string identifier; RustIdent ident; VariantType type; + // can either be a structure or a discriminant value - std::unique_ptr discriminant; + tl::optional> discriminant; + std::vector fields; }; From c0007ec0aad1dae14fc57d45682980d0bca9e76c Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Fri, 8 Nov 2024 12:01:15 +0100 Subject: [PATCH 05/11] Fix Generic type retrieval gcc/rust/ChangeLog: * hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit): Forward an optional to the constructor. * hir/tree/rust-hir-item.cc (TypeParam::TypeParam): Use an optional in the constructor. (TypeParam::operator=): Ensure the TypeParam has a type properly. (TypeParam::get_type_mappings): Likewise. * hir/tree/rust-hir-item.h: Wrap the type smart pointer into an optional. * hir/tree/rust-hir.cc (TypeParam::as_string): Unwrap optional type correctly. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/hir/rust-ast-lower-type.cc | 10 +++++----- gcc/rust/hir/tree/rust-hir-item.cc | 19 +++++++++++-------- gcc/rust/hir/tree/rust-hir-item.h | 14 ++++++++------ gcc/rust/hir/tree/rust-hir.cc | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/gcc/rust/hir/rust-ast-lower-type.cc b/gcc/rust/hir/rust-ast-lower-type.cc index cc7cb937f7cc..c1bfe213aa17 100644 --- a/gcc/rust/hir/rust-ast-lower-type.cc +++ b/gcc/rust/hir/rust-ast-lower-type.cc @@ -17,6 +17,7 @@ // . #include "rust-ast-lower-type.h" +#include "optional.h" #include "rust-attribute-values.h" namespace Rust { @@ -502,9 +503,9 @@ ASTLowerGenericParam::visit (AST::TypeParam ¶m) } } - HIR::Type *type = param.has_type () - ? ASTLoweringType::translate (param.get_type ()) - : nullptr; + auto type = param.has_type () ? tl::optional (std::unique_ptr ( + ASTLoweringType::translate (param.get_type ()))) + : tl::nullopt; auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, param.get_node_id (), @@ -514,8 +515,7 @@ ASTLowerGenericParam::visit (AST::TypeParam ¶m) translated = new HIR::TypeParam (mapping, param.get_type_representation (), param.get_locus (), std::move (type_param_bounds), - std::unique_ptr (type), - param.get_outer_attrs ()); + std::move (type), param.get_outer_attrs ()); } HIR::TypeParamBound * diff --git a/gcc/rust/hir/tree/rust-hir-item.cc b/gcc/rust/hir/tree/rust-hir-item.cc index f81f1eae1343..cff06d35269d 100644 --- a/gcc/rust/hir/tree/rust-hir-item.cc +++ b/gcc/rust/hir/tree/rust-hir-item.cc @@ -17,6 +17,7 @@ // . #include "rust-hir-item.h" +#include "optional.h" namespace Rust { namespace HIR { @@ -25,7 +26,7 @@ TypeParam::TypeParam ( Analysis::NodeMapping mappings, Identifier type_representation, location_t locus, std::vector> type_param_bounds, - std::unique_ptr, AST::AttrVec outer_attrs) + tl::optional> type, AST::AttrVec outer_attrs) : GenericParam (mappings), outer_attrs (std::move (outer_attrs)), type_representation (std::move (type_representation)), type_param_bounds (std::move (type_param_bounds)), type (std::move (type)), @@ -37,8 +38,10 @@ TypeParam::TypeParam (TypeParam const &other) type_representation (other.type_representation), locus (other.locus) { // guard to prevent null pointer dereference - if (other.type != nullptr) - type = other.type->clone_type (); + if (other.has_type ()) + type = {other.type.value ()->clone_type ()}; + else + type = tl::nullopt; type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) @@ -54,10 +57,10 @@ TypeParam::operator= (TypeParam const &other) mappings = other.mappings; // guard to prevent null pointer dereference - if (other.type != nullptr) - type = other.type->clone_type (); + if (other.has_type ()) + type = {other.type.value ()->clone_type ()}; else - type = nullptr; + type = tl::nullopt; type_param_bounds.reserve (other.type_param_bounds.size ()); for (const auto &e : other.type_param_bounds) @@ -69,8 +72,8 @@ TypeParam::operator= (TypeParam const &other) Analysis::NodeMapping TypeParam::get_type_mappings () const { - rust_assert (type != nullptr); - return type->get_mappings (); + rust_assert (type.has_value ()); + return type.value ()->get_mappings (); } std::vector> & diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 567432ecb325..4f8821855513 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -19,6 +19,7 @@ #ifndef RUST_HIR_ITEM_H #define RUST_HIR_ITEM_H +#include "optional.h" #include "rust-abi.h" #include "rust-hir-stmt.h" #include "rust-common.h" @@ -102,14 +103,13 @@ class TypeParam : public GenericParam std::vector> type_param_bounds; // inlined form - // bool has_type; - std::unique_ptr type; + tl::optional> type; location_t locus; public: // Returns whether the type of the type param has been specified. - bool has_type () const { return type != nullptr; } + bool has_type () const { return type.has_value (); } // Returns whether the type param has type param bounds. bool has_type_param_bounds () const { return !type_param_bounds.empty (); } @@ -122,7 +122,7 @@ class TypeParam : public GenericParam location_t locus = UNDEF_LOCATION, std::vector> type_param_bounds = std::vector> (), - std::unique_ptr type = nullptr, + tl::optional> type = tl::nullopt, AST::AttrVec outer_attrs = std::vector ()); // Copy constructor uses clone @@ -130,8 +130,10 @@ class TypeParam : public GenericParam // Overloaded assignment operator to clone TypeParam &operator= (TypeParam const &other); + // move constructors TypeParam (TypeParam &&other) = default; + TypeParam &operator= (TypeParam &&other) = default; std::string as_string () const override; @@ -144,8 +146,8 @@ class TypeParam : public GenericParam Type &get_type () { - rust_assert (type); - return *type; + rust_assert (*type); + return *type.value (); } Analysis::NodeMapping get_type_mappings () const; diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index 764619115d70..509a897ce39c 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -2178,7 +2178,7 @@ TypeParam::as_string () const } else { - str += type->as_string (); + str += type.value ()->as_string (); } return str; From 1415528c8496d7da6fff5fc739cdfb7682388091 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Tue, 19 Nov 2024 15:37:56 +0100 Subject: [PATCH 06/11] FnParam cloning now keeps projections FnParam type where monomorphized during cloning. gcc/rust/ChangeLog: * typecheck/rust-tyty.h: Reverse monomorphization during cloning and make a new function to explicitly monomorphize. * typecheck/rust-tyty.cc: Use monomorphization when required. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/typecheck/rust-tyty.cc | 2 +- gcc/rust/typecheck/rust-tyty.h | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 26c061258cd2..467bf1c21753 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -597,7 +597,7 @@ BaseType::monomorphized_clone () const { std::vector cloned_params; for (auto &p : fn->get_params ()) - cloned_params.push_back (p.clone ()); + cloned_params.push_back (p.monomorphized_clone ()); BaseType *retty = fn->get_return_type ()->monomorphized_clone (); return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (), diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index c896d1e03b75..167d425840e6 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -791,6 +791,11 @@ class FnParam void set_type (BaseType *new_type) { type = new_type; } FnParam clone () const + { + return FnParam (pattern->clone_pattern (), type->clone ()); + } + + FnParam monomorphized_clone () const { return FnParam (pattern->clone_pattern (), type->monomorphized_clone ()); } From 26fdda1505bd50233f27b4386274fa829a0ae753 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Tue, 19 Nov 2024 17:24:59 +0100 Subject: [PATCH 07/11] Refactor optional initializers Refactor some optional initializer in the lowering stage to make them more readable. gcc/rust/ChangeLog: * hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Change the ternary expression with a more readable if. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/hir/rust-ast-lower-stmt.cc | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/gcc/rust/hir/rust-ast-lower-stmt.cc b/gcc/rust/hir/rust-ast-lower-stmt.cc index 07283941328f..29da916cefc4 100644 --- a/gcc/rust/hir/rust-ast-lower-stmt.cc +++ b/gcc/rust/hir/rust-ast-lower-stmt.cc @@ -70,15 +70,17 @@ ASTLoweringStmt::visit (AST::LetStmt &stmt) HIR::Pattern *variables = ASTLoweringPattern::translate (stmt.get_pattern (), true); - auto type - = stmt.has_type () ? tl::optional> ( - std::unique_ptr (ASTLoweringType::translate (stmt.get_type ()))) - : tl::nullopt; - auto init_expression - = stmt.has_init_expr () - ? tl::optional> (std::unique_ptr ( - ASTLoweringExpr::translate (stmt.get_init_expr ()))) - : tl::nullopt; + tl::optional> type = tl::nullopt; + + if (stmt.has_type ()) + type + = std::unique_ptr (ASTLoweringType::translate (stmt.get_type ())); + + tl::optional> init_expression = tl::nullopt; + + if (stmt.has_init_expr ()) + init_expression = std::unique_ptr ( + ASTLoweringExpr::translate (stmt.get_init_expr ())); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (), From 52ab325d7bbe979639c2b612de453ba6adf73d15 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 20 Nov 2024 01:28:04 +0100 Subject: [PATCH 08/11] Fix FnParam pattern location ternary logic Condition was inverted, we should retrieve the locus only if we have a pattern. gcc/rust/ChangeLog: * typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Do not get a reference if the pattern does not exist. (TypeCheckMethodCallExpr::check): Likewise. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/typecheck/rust-tyty-call.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index da1c383b2ca2..dbb0379ebbfc 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -152,12 +152,11 @@ TypeCheckCallExpr::visit (FnType &type) if (i < type.num_params ()) { auto &fnparam = type.param_at (i); - auto &fn_param_pattern = fnparam.get_pattern (); BaseType *param_ty = fnparam.get_type (); location_t param_locus = fnparam.has_pattern () - ? mappings.lookup_location (param_ty->get_ref ()) - : fn_param_pattern.get_locus (); + ? fnparam.get_pattern ().get_locus () + : mappings.lookup_location (param_ty->get_ref ()); HirId coercion_side_id = argument->get_mappings ().get_hirid (); auto resolved_argument_type @@ -375,12 +374,11 @@ TypeCheckMethodCallExpr::check (FnType &type) location_t arg_locus = argument.get_locus (); auto &fnparam = type.param_at (i); - HIR::Pattern &fn_param_pattern = fnparam.get_pattern (); BaseType *param_ty = fnparam.get_type (); location_t param_locus = fnparam.has_pattern () - ? mappings.lookup_location (param_ty->get_ref ()) - : fn_param_pattern.get_locus (); + ? fnparam.get_pattern ().get_locus () + : mappings.lookup_location (param_ty->get_ref ()); auto argument_expr_tyty = argument.get_argument_type (); HirId coercion_side_id = argument.get_mappings ().get_hirid (); From 9614958e6e20a40af9698c2305b7c5ed378f44a8 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 20 Nov 2024 01:44:28 +0100 Subject: [PATCH 09/11] Add optional template arguments to please GCC4.8 Clang on macos as well as GCC 4.8 complains when those templates are missing. gcc/rust/ChangeLog: * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Add template to tl::optional. * hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeResolveGenericParam::visit): Likewise. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/hir/rust-ast-lower-expr.cc | 5 +++-- gcc/rust/hir/rust-ast-lower-type.cc | 8 +++++--- gcc/rust/typecheck/rust-hir-type-check-type.cc | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc b/gcc/rust/hir/rust-ast-lower-expr.cc index fb24a72efa70..a8c07303bd1c 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.cc +++ b/gcc/rust/hir/rust-ast-lower-expr.cc @@ -517,8 +517,9 @@ ASTLoweringExpr::visit (AST::StructExprStructFields &struct_expr) { HIR::Expr *translated_base = ASTLoweringExpr::translate ( struct_expr.get_struct_base ().get_base_struct ()); - base = tl::optional (Rust::make_unique ( - std::unique_ptr (translated_base))); + base = tl::optional> ( + Rust::make_unique ( + std::unique_ptr (translated_base))); } auto const &in_fields = struct_expr.get_fields (); diff --git a/gcc/rust/hir/rust-ast-lower-type.cc b/gcc/rust/hir/rust-ast-lower-type.cc index c1bfe213aa17..553c9c9612da 100644 --- a/gcc/rust/hir/rust-ast-lower-type.cc +++ b/gcc/rust/hir/rust-ast-lower-type.cc @@ -503,9 +503,11 @@ ASTLowerGenericParam::visit (AST::TypeParam ¶m) } } - auto type = param.has_type () ? tl::optional (std::unique_ptr ( - ASTLoweringType::translate (param.get_type ()))) - : tl::nullopt; + tl::optional> type = tl::nullopt; + if (param.has_type ()) + type + = tl::optional> (std::unique_ptr ( + ASTLoweringType::translate (param.get_type ()))); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, param.get_node_id (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 099a08910e8d..e0b70976a617 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -829,7 +829,9 @@ TypeResolveGenericParam::visit (HIR::TypeParam ¶m) HIR::TraitBound &b = static_cast (*bound); TyTy::TypeBoundPredicate predicate = get_predicate_from_bound ( - b.get_path (), tl::optional (std::ref (*implicit_self_bound)), + b.get_path (), + tl::optional> ( + std::ref (*implicit_self_bound)), b.get_polarity ()); if (!predicate.is_error ()) { From bd257b0262b31f938577a487d38db84f6c700acd Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 20 Nov 2024 13:19:15 +0100 Subject: [PATCH 10/11] Use default constructor for default arguments GCC 4.8 complains about the initializer list. gcc/rust/ChangeLog: * typecheck/rust-tyty.h: Change initializer list to default constructor call. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/typecheck/rust-tyty.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 167d425840e6..de806a2c9bee 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -657,7 +657,7 @@ class ADTType : public BaseType, public SubstitutionRef std::vector subst_refs, SubstitutionArgumentMappings generic_arguments = SubstitutionArgumentMappings::error (), - RegionConstraints region_constraints = {}, + RegionConstraints region_constraints = RegionConstraints{}, std::set refs = std::set ()) : BaseType (ref, ref, TypeKind::ADT, ident, refs), SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), @@ -670,7 +670,7 @@ class ADTType : public BaseType, public SubstitutionRef std::vector subst_refs, SubstitutionArgumentMappings generic_arguments = SubstitutionArgumentMappings::error (), - RegionConstraints region_constraints = {}, + RegionConstraints region_constraints = RegionConstraints{}, std::set refs = std::set ()) : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs), SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), @@ -683,7 +683,7 @@ class ADTType : public BaseType, public SubstitutionRef std::vector subst_refs, ReprOptions repr, SubstitutionArgumentMappings generic_arguments = SubstitutionArgumentMappings::error (), - RegionConstraints region_constraints = {}, + RegionConstraints region_constraints = RegionConstraints{}, std::set refs = std::set ()) : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs), SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), From b41ffb23360c42941644f259a3583d590836e533 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 20 Nov 2024 13:35:22 +0100 Subject: [PATCH 11/11] Use a reference wrapper to please GCC 4.8 gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::visit): Change call. (CompileExpr::resolve_operator_overload): Update function arguments. * backend/rust-compile-expr.h: Change the function's prototype to use a reference wrapper instead of a reference within the optional. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/backend/rust-compile-expr.cc | 13 +++++++------ gcc/rust/backend/rust-compile-expr.h | 8 ++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index c4ae79dbde27..615354fc9612 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -31,6 +31,7 @@ #include "convert.h" #include "print-tree.h" #include "rust-system.h" +#include namespace Rust { namespace Compile { @@ -152,8 +153,9 @@ CompileExpr::visit (HIR::ArithmeticOrLogicalExpr &expr) { auto lang_item_type = LangItem::OperatorToLangItem (expr.get_expr_type ()); - translated = resolve_operator_overload (lang_item_type, expr, lhs, rhs, - expr.get_lhs (), expr.get_rhs ()); + translated = resolve_operator_overload ( + lang_item_type, expr, lhs, rhs, expr.get_lhs (), + tl::optional> (expr.get_rhs ())); return; } @@ -1476,10 +1478,9 @@ CompileExpr::get_receiver_from_dyn (const TyTy::DynamicObjectType *dyn, } tree -CompileExpr::resolve_operator_overload (LangItem::Kind lang_item_type, - HIR::OperatorExprMeta expr, tree lhs, - tree rhs, HIR::Expr &lhs_expr, - tl::optional rhs_expr) +CompileExpr::resolve_operator_overload ( + LangItem::Kind lang_item_type, HIR::OperatorExprMeta expr, tree lhs, tree rhs, + HIR::Expr &lhs_expr, tl::optional> rhs_expr) { TyTy::FnType *fntype; bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload ( diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 872b510090b6..9e7af42c64f2 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -96,10 +96,10 @@ class CompileExpr : private HIRCompileBase, protected HIR::HIRExpressionVisitor TyTy::BaseType *receiver, TyTy::FnType *fntype, tree receiver_ref, location_t expr_locus); - tree resolve_operator_overload (LangItem::Kind lang_item_type, - HIR::OperatorExprMeta expr, tree lhs, - tree rhs, HIR::Expr &lhs_expr, - tl::optional rhs_expr); + tree resolve_operator_overload ( + LangItem::Kind lang_item_type, HIR::OperatorExprMeta expr, tree lhs, + tree rhs, HIR::Expr &lhs_expr, + tl::optional> rhs_expr); tree compile_bool_literal (const HIR::LiteralExpr &expr, const TyTy::BaseType *tyty);