From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 36C8638582B2 for ; Wed, 7 Feb 2024 12:44:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 36C8638582B2 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 36C8638582B2 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707309868; cv=none; b=fguFDVzA6WBX7Mv/DEDEY5a9jBw4l9qzHjbBSRvtus0aWRvkFFuJQEK4wkG+vfVDYmTiNuZdDM2JxbJr4+0qeFRrMefYEfzW/7BE4SfBTLmvpN77LCU6QZiSt0UEkncxmEYq25t2IzAXn/e0YIRJSsrpcbJEkcKlf4UzEvAfbc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707309868; c=relaxed/simple; bh=aBmEKKFjKZ1RNs5fYjPTpVtFXtRT6KZVcQVne8S8eYw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rt6XQBbKf7/7HerZsKzJYFS7xmkJBn1YyRFeCNWq/wxwtCKfWRS2YkTVeE2L9vSQAMWD9Ov49htBDpVGmtAuyfEA7T8xOkAdf3DdXhiiFjrl53mlX0OtpxaEr2/weiO2TkFJxDkl/BpMDGYgwrIGuUmb26uIZ6ka6hn039PSvZQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-5114cd44f6aso765244e87.1 for ; Wed, 07 Feb 2024 04:44:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1707309854; x=1707914654; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=Chvm7TPbYMmlsfG1XuyENv3MxBxmaG66Xo9Z2aFB3FM=; b=RO1gu6/ixNW7a/xQbgry2pwmmKWIBFg4Wqqr6GliJ5Ios8ZuxQKJkgcZsJ4mC0Tr/o 1boag1oSEXnE+XJ2iN+1dZDXziqzlrBvtUhjgpLGY2c1MCmZz0B8Pt151lllH08O/q52 9xho6Q9QnbSFbePvg+fHyB9l9gd99k2nyFykisyxOQnQHVWQCi5KNx3Ycgbq3T1u43LH 98aZKD2jLYeV302ztJKokTOApKo23jajqosExMEjZh3ZsnaY7MsmYUgPn3Ku4f+U+p1V lCBBDHWx9kVM/dkJm+kW/V4TZIZGaQ6dJZ4I2I6NwQUntiPHsDAeL6StWiIBFFRhFvSO Bf8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707309854; x=1707914654; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Chvm7TPbYMmlsfG1XuyENv3MxBxmaG66Xo9Z2aFB3FM=; b=X8C1KRiG8lbK2pr+aQUE3AboU65QdVGOYOM4yR29mmNVMkUQR2W2Mhrx1I8rRnhTI8 oiqSmG9kTepzsR6JmMSKosPoaV9ht+JvEJor1TAUDohB/QUNCaatR9j6UhGY0sE4dZ02 dGKRHysRD8xLQRE2us7B4tNhpll24TsLQs6SSSTwWbBvV3yXmQ6uLYqxx2xfh8otRek3 dMbHYpJ6f4efKa0eceUiuShI4iuEERFG90UdDOI57Bc+erFhmo6e1cgNKHE6ayu1L3Wh hohzcKLG8cwnhL6WbJjr/C3qlSZXFl1NALDRh4xl6BRLXThwMro0MM5GUoJMdXmb/ip5 EViQ== X-Gm-Message-State: AOJu0YwBbXwRrQw7bs8lFk0JQGw9NtF90w+wMaaZv9j4sw0DXtyuqlym iILwYPX+8zCNt3cABfXx4s8Expl7j7098EkOuK+HowfEKk8+6IaHeqOB/vI3Jw== X-Google-Smtp-Source: AGHT+IFFLo0No1A8PcbTE7DqdRue6+QGF8uAcB5P6NCzzHLXEp0HnNU7/eEcpu2UNuHW35WBa2xYRw== X-Received: by 2002:ac2:4947:0:b0:511:1b89:1a30 with SMTP id o7-20020ac24947000000b005111b891a30mr3519309lfi.63.1707309854476; Wed, 07 Feb 2024 04:44:14 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXf9kugIfvRA8w3BNaT6UA5yzxXcVQgal0EClnerOP5tQNfXBbeeANqxAzeARzPoILXIyPxWgD7kRsTfvSqtwigxKnwHXEsPf/eB1uxA/xoZXqfs7i3Qhyh Received: from platypus.sou.embecosm-corp.com ([212.69.42.53]) by smtp.gmail.com with ESMTPSA id x19-20020a05600c2a5300b0040fbdd6f69bsm5044188wme.33.2024.02.07.04.44.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 04:44:13 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Nobel Singh , Nobel Singh Subject: [COMMITTED 16/25] gccrs: Move the Implementation of implitem lowering into its own file. Date: Wed, 7 Feb 2024 12:44:02 +0100 Message-ID: <20240207114419.1100894-17-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240207114419.1100894-2-arthur.cohen@embecosm.com> References: <20240207114419.1100894-2-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Nobel Singh This patch moves the implementation of the implitem lowering from rust-ast-lower-implitem.h into the rust-ast-lower-implitem.cc file. gcc/rust/ChangeLog: * Make-lang.in: Add rust-ast-lower-implitem.cc to list of objects. * hir/rust-ast-lower-implitem.h (RUST_AST_LOWER_IMPLITEM_H): Remove implementation. * hir/rust-ast-lower-implitem.cc: Copy implementation from header. Signed-off-by: Nobel Singh --- gcc/rust/Make-lang.in | 1 + gcc/rust/hir/rust-ast-lower-implitem.cc | 428 ++++++++++++++++++++++++ gcc/rust/hir/rust-ast-lower-implitem.h | 406 +--------------------- 3 files changed, 439 insertions(+), 396 deletions(-) create mode 100644 gcc/rust/hir/rust-ast-lower-implitem.cc diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index bdaef41c419..4d646018792 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -111,6 +111,7 @@ GRS_OBJS = \ rust/rust-ast-lower-base.o \ rust/rust-ast-lower-pattern.o \ rust/rust-ast-lower-item.o \ + rust/rust-ast-lower-implitem.o \ rust/rust-ast-lower-expr.o \ rust/rust-ast-lower-type.o \ rust/rust-ast-lower-stmt.o \ diff --git a/gcc/rust/hir/rust-ast-lower-implitem.cc b/gcc/rust/hir/rust-ast-lower-implitem.cc new file mode 100644 index 00000000000..98db1dccd7c --- /dev/null +++ b/gcc/rust/hir/rust-ast-lower-implitem.cc @@ -0,0 +1,428 @@ +// Copyright (C) 2020-2023 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-ast-lower-implitem.h" +#include "rust-ast-lower.h" +#include "rust-ast-lower-type.h" +#include "rust-ast-lower-expr.h" +#include "rust-ast-lower-pattern.h" +#include "rust-ast-lower-block.h" +#include "rust-item.h" + +namespace Rust { +namespace HIR { + +HIR::ImplItem * +ASTLowerImplItem::translate (AST::AssociatedItem *item, HirId parent_impl_id) +{ + ASTLowerImplItem resolver; + item->accept_vis (resolver); + + if (resolver.translated != nullptr) + { + rust_assert (resolver.item_cast != nullptr); + + auto id = resolver.translated->get_impl_mappings ().get_hirid (); + auto defid = resolver.translated->get_impl_mappings ().get_defid (); + auto locus = resolver.translated->get_locus (); + + resolver.handle_outer_attributes (*resolver.item_cast); + resolver.mappings->insert_hir_implitem (parent_impl_id, + resolver.translated); + resolver.mappings->insert_location (id, locus); + resolver.mappings->insert_defid_mapping (defid, resolver.item_cast); + } + + return resolver.translated; +} + +void +ASTLowerImplItem::visit (AST::TypeAlias &alias) +{ + std::vector > where_clause_items; + HIR::WhereClause where_clause (std::move (where_clause_items)); + HIR::Visibility vis = translate_visibility (alias.get_visibility ()); + + std::vector > generic_params; + if (alias.has_generics ()) + generic_params = lower_generic_params (alias.get_generic_params ()); + + HIR::Type *existing_type + = ASTLoweringType::translate (alias.get_type_aliased ().get ()); + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, alias.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + + auto type_alias + = new HIR::TypeAlias (mapping, alias.get_new_type_name (), + std::move (generic_params), std::move (where_clause), + std::unique_ptr (existing_type), + std::move (vis), alias.get_outer_attrs (), + alias.get_locus ()); + + translated = type_alias; + item_cast = type_alias; +} + +void +ASTLowerImplItem::visit (AST::ConstantItem &constant) +{ + HIR::Visibility vis = translate_visibility (constant.get_visibility ()); + + HIR::Type *type + = ASTLoweringType::translate (constant.get_type ().get (), true); + HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ().get ()); + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, constant.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + + auto translated_constant + = new HIR::ConstantItem (mapping, constant.get_identifier (), vis, + std::unique_ptr (type), + std::unique_ptr (expr), + constant.get_outer_attrs (), + constant.get_locus ()); + + translated = translated_constant; + item_cast = translated_constant; +} + +void +ASTLowerImplItem::visit (AST::Function &function) +{ + // ignore for now and leave empty + std::vector > where_clause_items; + for (auto &item : function.get_where_clause ().get_items ()) + { + HIR::WhereClauseItem *i + = ASTLowerWhereClauseItem::translate (*item.get ()); + where_clause_items.push_back (std::unique_ptr (i)); + } + + HIR::WhereClause where_clause (std::move (where_clause_items)); + HIR::FunctionQualifiers qualifiers + = lower_qualifiers (function.get_qualifiers ()); + HIR::Visibility vis = translate_visibility (function.get_visibility ()); + + // need + std::vector > generic_params; + if (function.has_generics ()) + { + generic_params = lower_generic_params (function.get_generic_params ()); + } + Identifier function_name = function.get_function_name (); + location_t locus = function.get_locus (); + + HIR::SelfParam self_param = HIR::SelfParam::error (); + if (function.has_self_param ()) + self_param = lower_self (function.get_self_param ()); + + std::unique_ptr return_type + = function.has_return_type () ? std::unique_ptr ( + ASTLoweringType::translate (function.get_return_type ().get ())) + : nullptr; + + std::vector function_params; + for (auto &p : function.get_function_params ()) + { + if (p->is_self () || p->is_variadic ()) + continue; + auto param = static_cast (p.get ()); + + auto translated_pattern = std::unique_ptr ( + ASTLoweringPattern::translate (param->get_pattern ().get ())); + auto translated_type = std::unique_ptr ( + ASTLoweringType::translate (param->get_type ().get ())); + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, param->get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + auto hir_param + = HIR::FunctionParam (mapping, std::move (translated_pattern), + std::move (translated_type), param->get_locus ()); + function_params.push_back (std::move (hir_param)); + } + + bool terminated = false; + std::unique_ptr function_body + = std::unique_ptr ( + ASTLoweringBlock::translate (function.get_definition ()->get (), + &terminated)); + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, function.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + + mappings->insert_location (function_body->get_mappings ().get_hirid (), + function.get_locus ()); + + auto fn + = new HIR::Function (mapping, std::move (function_name), + std::move (qualifiers), std::move (generic_params), + std::move (function_params), std::move (return_type), + std::move (where_clause), std::move (function_body), + std::move (vis), function.get_outer_attrs (), + std::move (self_param), locus); + + if (!fn->get_self_param ().is_error ()) + { + // insert mappings for self + mappings->insert_hir_self_param (&fn->get_self_param ()); + mappings->insert_location ( + fn->get_self_param ().get_mappings ().get_hirid (), + fn->get_self_param ().get_locus ()); + } + + // add the mappings for the function params at the end + for (auto ¶m : fn->get_function_params ()) + { + mappings->insert_hir_param (¶m); + mappings->insert_location (mapping.get_hirid (), param.get_locus ()); + } + + translated = fn; + item_cast = fn; +} + +HIR::TraitItem * +ASTLowerTraitItem::translate (AST::AssociatedItem *item) +{ + ASTLowerTraitItem resolver; + item->accept_vis (resolver); + + if (resolver.translated != nullptr) + { + auto id = resolver.translated->get_mappings ().get_hirid (); + auto defid = resolver.translated->get_mappings ().get_defid (); + auto locus = resolver.translated->get_trait_locus (); + + resolver.handle_outer_attributes (*resolver.translated); + resolver.mappings->insert_hir_trait_item (resolver.translated); + resolver.mappings->insert_location (id, locus); + resolver.mappings->insert_defid_mapping (defid, resolver.translated); + } + + return resolver.translated; +} + +void +ASTLowerTraitItem::visit (AST::TraitItemFunc &func) +{ + AST::TraitFunctionDecl &ref = func.get_trait_function_decl (); + std::vector > where_clause_items; + HIR::WhereClause where_clause (std::move (where_clause_items)); + HIR::FunctionQualifiers qualifiers + = lower_qualifiers (func.get_trait_function_decl ().get_qualifiers ()); + + std::vector > generic_params; + if (ref.has_generics ()) + { + generic_params = lower_generic_params (ref.get_generic_params ()); + } + + std::unique_ptr return_type + = ref.has_return_type () ? std::unique_ptr ( + ASTLoweringType::translate (ref.get_return_type ().get ())) + : nullptr; + + std::vector function_params; + for (auto &p : ref.get_function_params ()) + { + if (p->is_variadic () || p->is_self ()) + continue; + + auto param = static_cast (p.get ()); + + auto translated_pattern = std::unique_ptr ( + ASTLoweringPattern::translate (param->get_pattern ().get ())); + auto translated_type = std::unique_ptr ( + ASTLoweringType::translate (param->get_type ().get ())); + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, param->get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + auto hir_param + = HIR::FunctionParam (mapping, std::move (translated_pattern), + std::move (translated_type), param->get_locus ()); + function_params.push_back (std::move (hir_param)); + } + + HIR::TraitFunctionDecl decl (ref.get_identifier (), std::move (qualifiers), + std::move (generic_params), + HIR::SelfParam::error (), + std::move (function_params), + std::move (return_type), + std::move (where_clause)); + bool terminated = false; + std::unique_ptr block_expr + = func.has_definition () ? std::unique_ptr ( + ASTLoweringBlock::translate (func.get_definition ().get (), + &terminated)) + : nullptr; + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, func.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + + HIR::TraitItemFunc *trait_item + = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr), + func.get_outer_attrs (), func.get_locus ()); + translated = trait_item; + + // add the mappings for the function params at the end + for (auto ¶m : trait_item->get_decl ().get_function_params ()) + { + mappings->insert_hir_param (¶m); + mappings->insert_location (mapping.get_hirid (), param.get_locus ()); + } +} + +void +ASTLowerTraitItem::visit (AST::TraitItemMethod &method) +{ + AST::TraitMethodDecl &ref = method.get_trait_method_decl (); + + std::vector > where_clause_items; + HIR::WhereClause where_clause (std::move (where_clause_items)); + HIR::FunctionQualifiers qualifiers + = lower_qualifiers (method.get_trait_method_decl ().get_qualifiers ()); + + std::vector > generic_params; + if (ref.has_generics ()) + { + generic_params = lower_generic_params (ref.get_generic_params ()); + } + + std::unique_ptr return_type + = ref.has_return_type () ? std::unique_ptr ( + ASTLoweringType::translate (ref.get_return_type ().get ())) + : nullptr; + + HIR::SelfParam self_param = lower_self (ref.get_self_param ()); + + std::vector function_params; + for (auto &p : ref.get_function_params ()) + { + if (p->is_variadic () || p->is_self ()) + continue; + + auto param = static_cast (p.get ()); + + auto translated_pattern = std::unique_ptr ( + ASTLoweringPattern::translate (param->get_pattern ().get ())); + auto translated_type = std::unique_ptr ( + ASTLoweringType::translate (param->get_type ().get ())); + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, param->get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + auto hir_param + = HIR::FunctionParam (mapping, std::move (translated_pattern), + std::move (translated_type), param->get_locus ()); + function_params.push_back (hir_param); + } + + HIR::TraitFunctionDecl decl (ref.get_identifier (), std::move (qualifiers), + std::move (generic_params), + std::move (self_param), + std::move (function_params), + std::move (return_type), + std::move (where_clause)); + + bool terminated = false; + std::unique_ptr block_expr + = method.has_definition () ? std::unique_ptr ( + ASTLoweringBlock::translate (method.get_definition ().get (), + &terminated)) + : nullptr; + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, method.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + + HIR::TraitItemFunc *trait_item + = new HIR::TraitItemFunc (mapping, std::move (decl), std::move (block_expr), + method.get_outer_attrs (), method.get_locus ()); + translated = trait_item; + + // insert mappings for self + mappings->insert_hir_self_param (&self_param); + mappings->insert_location (self_param.get_mappings ().get_hirid (), + self_param.get_locus ()); + + // add the mappings for the function params at the end + for (auto ¶m : trait_item->get_decl ().get_function_params ()) + { + mappings->insert_hir_param (¶m); + mappings->insert_location (mapping.get_hirid (), param.get_locus ()); + } +} + +void +ASTLowerTraitItem::visit (AST::TraitItemConst &constant) +{ + HIR::Type *type = ASTLoweringType::translate (constant.get_type ().get ()); + HIR::Expr *expr = constant.has_expression () + ? ASTLoweringExpr::translate (constant.get_expr ().get ()) + : nullptr; + + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, constant.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + + HIR::TraitItemConst *trait_item + = new HIR::TraitItemConst (mapping, constant.get_identifier (), + std::unique_ptr (type), + std::unique_ptr (expr), + constant.get_outer_attrs (), + constant.get_locus ()); + translated = trait_item; +} + +void +ASTLowerTraitItem::visit (AST::TraitItemType &type) +{ + std::vector > type_param_bounds; + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, type.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + + HIR::TraitItemType *trait_item + = new HIR::TraitItemType (mapping, type.get_identifier (), + std::move (type_param_bounds), + type.get_outer_attrs (), type.get_locus ()); + translated = trait_item; +} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h index 81dae14cdca..3a266b41ed4 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.h +++ b/gcc/rust/hir/rust-ast-lower-implitem.h @@ -19,11 +19,7 @@ #ifndef RUST_AST_LOWER_IMPLITEM_H #define RUST_AST_LOWER_IMPLITEM_H -#include "rust-ast-lower-type.h" -#include "rust-ast-lower-expr.h" -#include "rust-ast-lower-pattern.h" -#include "rust-ast-lower-block.h" -#include "rust-item.h" +#include "rust-ast-lower-base.h" namespace Rust { namespace HIR { @@ -34,179 +30,10 @@ class ASTLowerImplItem : public ASTLoweringBase public: static HIR::ImplItem *translate (AST::AssociatedItem *item, - HirId parent_impl_id) - { - ASTLowerImplItem resolver; - item->accept_vis (resolver); - - if (resolver.translated != nullptr) - { - rust_assert (resolver.item_cast != nullptr); - - auto id = resolver.translated->get_impl_mappings ().get_hirid (); - auto defid = resolver.translated->get_impl_mappings ().get_defid (); - auto locus = resolver.translated->get_locus (); - - resolver.handle_outer_attributes (*resolver.item_cast); - resolver.mappings->insert_hir_implitem (parent_impl_id, - resolver.translated); - resolver.mappings->insert_location (id, locus); - resolver.mappings->insert_defid_mapping (defid, resolver.item_cast); - } - - return resolver.translated; - } - - void visit (AST::TypeAlias &alias) override - { - std::vector > where_clause_items; - HIR::WhereClause where_clause (std::move (where_clause_items)); - HIR::Visibility vis = translate_visibility (alias.get_visibility ()); - - std::vector > generic_params; - if (alias.has_generics ()) - generic_params = lower_generic_params (alias.get_generic_params ()); - - HIR::Type *existing_type - = ASTLoweringType::translate (alias.get_type_aliased ().get ()); - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, alias.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - - auto type_alias = new HIR::TypeAlias ( - mapping, alias.get_new_type_name (), std::move (generic_params), - std::move (where_clause), std::unique_ptr (existing_type), - std::move (vis), alias.get_outer_attrs (), alias.get_locus ()); - - translated = type_alias; - item_cast = type_alias; - } - - void visit (AST::ConstantItem &constant) override - { - HIR::Visibility vis = translate_visibility (constant.get_visibility ()); - - HIR::Type *type - = ASTLoweringType::translate (constant.get_type ().get (), true); - HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ().get ()); - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, constant.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - - auto translated_constant - = new HIR::ConstantItem (mapping, constant.get_identifier (), vis, - std::unique_ptr (type), - std::unique_ptr (expr), - constant.get_outer_attrs (), - constant.get_locus ()); - translated = translated_constant; - item_cast = translated_constant; - } - - void visit (AST::Function &function) override - { - // ignore for now and leave empty - std::vector > where_clause_items; - for (auto &item : function.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back ( - std::unique_ptr (i)); - } - - HIR::WhereClause where_clause (std::move (where_clause_items)); - HIR::FunctionQualifiers qualifiers - = lower_qualifiers (function.get_qualifiers ()); - HIR::Visibility vis = translate_visibility (function.get_visibility ()); - - // need - std::vector > generic_params; - if (function.has_generics ()) - { - generic_params = lower_generic_params (function.get_generic_params ()); - } - Identifier function_name = function.get_function_name (); - location_t locus = function.get_locus (); - - HIR::SelfParam self_param = HIR::SelfParam::error (); - if (function.has_self_param ()) - self_param = lower_self (function.get_self_param ()); - - std::unique_ptr return_type - = function.has_return_type () ? std::unique_ptr ( - ASTLoweringType::translate (function.get_return_type ().get ())) - : nullptr; - - std::vector function_params; - for (auto &p : function.get_function_params ()) - { - if (p->is_self () || p->is_variadic ()) - continue; - auto param = static_cast (p.get ()); - - auto translated_pattern = std::unique_ptr ( - ASTLoweringPattern::translate (param->get_pattern ().get ())); - auto translated_type = std::unique_ptr ( - ASTLoweringType::translate (param->get_type ().get ())); - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, param->get_node_id (), - mappings->get_next_hir_id (crate_num), - UNKNOWN_LOCAL_DEFID); - - auto hir_param - = HIR::FunctionParam (mapping, std::move (translated_pattern), - std::move (translated_type), - param->get_locus ()); - function_params.push_back (std::move (hir_param)); - } - - bool terminated = false; - std::unique_ptr function_body - = std::unique_ptr ( - ASTLoweringBlock::translate (function.get_definition ()->get (), - &terminated)); - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, function.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - - mappings->insert_location (function_body->get_mappings ().get_hirid (), - function.get_locus ()); - - auto fn - = new HIR::Function (mapping, std::move (function_name), - std::move (qualifiers), std::move (generic_params), - std::move (function_params), std::move (return_type), - std::move (where_clause), std::move (function_body), - std::move (vis), function.get_outer_attrs (), - std::move (self_param), locus); - - if (!fn->get_self_param ().is_error ()) - { - // insert mappings for self - mappings->insert_hir_self_param (&fn->get_self_param ()); - mappings->insert_location ( - fn->get_self_param ().get_mappings ().get_hirid (), - fn->get_self_param ().get_locus ()); - } - - // add the mappings for the function params at the end - for (auto ¶m : fn->get_function_params ()) - { - mappings->insert_hir_param (¶m); - mappings->insert_location (mapping.get_hirid (), param.get_locus ()); - } - - translated = fn; - item_cast = fn; - } + HirId parent_impl_id); + void visit (AST::TypeAlias &alias) override; + void visit (AST::ConstantItem &constant) override; + void visit (AST::Function &function) override; private: ASTLowerImplItem () : translated (nullptr), item_cast (nullptr) {} @@ -220,224 +47,11 @@ class ASTLowerTraitItem : public ASTLoweringBase using Rust::HIR::ASTLoweringBase::visit; public: - static HIR::TraitItem *translate (AST::AssociatedItem *item) - { - ASTLowerTraitItem resolver; - item->accept_vis (resolver); - - if (resolver.translated != nullptr) - { - auto id = resolver.translated->get_mappings ().get_hirid (); - auto defid = resolver.translated->get_mappings ().get_defid (); - auto locus = resolver.translated->get_trait_locus (); - - resolver.handle_outer_attributes (*resolver.translated); - resolver.mappings->insert_hir_trait_item (resolver.translated); - resolver.mappings->insert_location (id, locus); - resolver.mappings->insert_defid_mapping (defid, resolver.translated); - } - - return resolver.translated; - } - - void visit (AST::TraitItemFunc &func) override - { - AST::TraitFunctionDecl &ref = func.get_trait_function_decl (); - - std::vector > where_clause_items; - HIR::WhereClause where_clause (std::move (where_clause_items)); - HIR::FunctionQualifiers qualifiers - = lower_qualifiers (func.get_trait_function_decl ().get_qualifiers ()); - - std::vector > generic_params; - if (ref.has_generics ()) - { - generic_params = lower_generic_params (ref.get_generic_params ()); - } - - std::unique_ptr return_type - = ref.has_return_type () ? std::unique_ptr ( - ASTLoweringType::translate (ref.get_return_type ().get ())) - : nullptr; - - std::vector function_params; - for (auto &p : ref.get_function_params ()) - { - if (p->is_variadic () || p->is_self ()) - continue; - - auto param = static_cast (p.get ()); - - auto translated_pattern = std::unique_ptr ( - ASTLoweringPattern::translate (param->get_pattern ().get ())); - auto translated_type = std::unique_ptr ( - ASTLoweringType::translate (param->get_type ().get ())); - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, param->get_node_id (), - mappings->get_next_hir_id (crate_num), - UNKNOWN_LOCAL_DEFID); - - auto hir_param - = HIR::FunctionParam (mapping, std::move (translated_pattern), - std::move (translated_type), - param->get_locus ()); - function_params.push_back (std::move (hir_param)); - } - - HIR::TraitFunctionDecl decl (ref.get_identifier (), std::move (qualifiers), - std::move (generic_params), - HIR::SelfParam::error (), - std::move (function_params), - std::move (return_type), - std::move (where_clause)); - bool terminated = false; - std::unique_ptr block_expr - = func.has_definition () ? std::unique_ptr ( - ASTLoweringBlock::translate (func.get_definition ().get (), - &terminated)) - : nullptr; - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, func.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - - HIR::TraitItemFunc *trait_item - = new HIR::TraitItemFunc (mapping, std::move (decl), - std::move (block_expr), func.get_outer_attrs (), - func.get_locus ()); - translated = trait_item; - - // add the mappings for the function params at the end - for (auto ¶m : trait_item->get_decl ().get_function_params ()) - { - mappings->insert_hir_param (¶m); - mappings->insert_location (mapping.get_hirid (), param.get_locus ()); - } - } - - void visit (AST::TraitItemMethod &method) override - { - AST::TraitMethodDecl &ref = method.get_trait_method_decl (); - - std::vector > where_clause_items; - HIR::WhereClause where_clause (std::move (where_clause_items)); - HIR::FunctionQualifiers qualifiers - = lower_qualifiers (method.get_trait_method_decl ().get_qualifiers ()); - - std::vector > generic_params; - if (ref.has_generics ()) - { - generic_params = lower_generic_params (ref.get_generic_params ()); - } - - std::unique_ptr return_type - = ref.has_return_type () ? std::unique_ptr ( - ASTLoweringType::translate (ref.get_return_type ().get ())) - : nullptr; - - HIR::SelfParam self_param = lower_self (ref.get_self_param ()); - - std::vector function_params; - for (auto &p : ref.get_function_params ()) - { - if (p->is_variadic () || p->is_self ()) - continue; - - auto param = static_cast (p.get ()); - - auto translated_pattern = std::unique_ptr ( - ASTLoweringPattern::translate (param->get_pattern ().get ())); - auto translated_type = std::unique_ptr ( - ASTLoweringType::translate (param->get_type ().get ())); - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, param->get_node_id (), - mappings->get_next_hir_id (crate_num), - UNKNOWN_LOCAL_DEFID); - - auto hir_param - = HIR::FunctionParam (mapping, std::move (translated_pattern), - std::move (translated_type), - param->get_locus ()); - function_params.push_back (hir_param); - } - - HIR::TraitFunctionDecl decl (ref.get_identifier (), std::move (qualifiers), - std::move (generic_params), - std::move (self_param), - std::move (function_params), - std::move (return_type), - std::move (where_clause)); - bool terminated = false; - std::unique_ptr block_expr - = method.has_definition () ? std::unique_ptr ( - ASTLoweringBlock::translate (method.get_definition ().get (), - &terminated)) - : nullptr; - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, method.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - - HIR::TraitItemFunc *trait_item - = new HIR::TraitItemFunc (mapping, std::move (decl), - std::move (block_expr), - method.get_outer_attrs (), method.get_locus ()); - translated = trait_item; - - // insert mappings for self - mappings->insert_hir_self_param (&self_param); - mappings->insert_location (self_param.get_mappings ().get_hirid (), - self_param.get_locus ()); - - // add the mappings for the function params at the end - for (auto ¶m : trait_item->get_decl ().get_function_params ()) - { - mappings->insert_hir_param (¶m); - mappings->insert_location (mapping.get_hirid (), param.get_locus ()); - } - } - - void visit (AST::TraitItemConst &constant) override - { - HIR::Type *type = ASTLoweringType::translate (constant.get_type ().get ()); - HIR::Expr *expr - = constant.has_expression () - ? ASTLoweringExpr::translate (constant.get_expr ().get ()) - : nullptr; - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, constant.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - - HIR::TraitItemConst *trait_item - = new HIR::TraitItemConst (mapping, constant.get_identifier (), - std::unique_ptr (type), - std::unique_ptr (expr), - constant.get_outer_attrs (), - constant.get_locus ()); - translated = trait_item; - } - - void visit (AST::TraitItemType &type) override - { - std::vector > type_param_bounds; - - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, type.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - - HIR::TraitItemType *trait_item - = new HIR::TraitItemType (mapping, type.get_identifier (), - std::move (type_param_bounds), - type.get_outer_attrs (), type.get_locus ()); - translated = trait_item; - } + static HIR::TraitItem *translate (AST::AssociatedItem *item); + void visit (AST::TraitItemFunc &func) override; + void visit (AST::TraitItemMethod &method) override; + void visit (AST::TraitItemConst &constant) override; + void visit (AST::TraitItemType &type) override; private: ASTLowerTraitItem () : translated (nullptr) {} -- 2.42.1