From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id A9BF938582A1; Tue, 30 Jan 2024 11:59:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A9BF938582A1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1706615996; bh=qWyQ93bxp+Yh6KwjW6xNlLlXueijOe6I3R/8X0GbRM0=; h=From:To:Subject:Date:From; b=taOrgEGfQ1f7pKLMSseNC322BVOCnQbiGRaeghH/HYljp0mzfaBxxlGbyTEJVGDFJ SqNDAZ50qyLI+oZ3pgUZ1ITqgLQcWLnSG5lWoDjnZvxIjQScpk1GHri0Ssrl8aGAQS y2H7oay3D+HvkiwnuXPABZQLBdzILnuGZPLIPxuA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8561] gccrs: late: Start setting up builtin types X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/trunk X-Git-Oldrev: cb3a12f3ee26de1f51d9c900d4056fc4bcf6974f X-Git-Newrev: df6eadda5ef181525fc013e7ded9ca0761929c2e Message-Id: <20240130115956.A9BF938582A1@sourceware.org> Date: Tue, 30 Jan 2024 11:59:56 +0000 (GMT) List-Id: https://gcc.gnu.org/g:df6eadda5ef181525fc013e7ded9ca0761929c2e commit r14-8561-gdf6eadda5ef181525fc013e7ded9ca0761929c2e Author: Arthur Cohen Date: Tue Aug 22 16:58:28 2023 +0200 gccrs: late: Start setting up builtin types gcc/rust/ChangeLog: * resolve/rust-late-name-resolver-2.0.cc (Late::setup_builtin_types): New function. (Late::go): Setup builtin types. * resolve/rust-late-name-resolver-2.0.h: * resolve/rust-name-resolution-context.cc (NameResolutionContext::map_usage): New function. * resolve/rust-name-resolution-context.h: Likewise. Diff: --- gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 42 ++++++++++++++++++++++++ gcc/rust/resolve/rust-late-name-resolver-2.0.h | 2 ++ gcc/rust/resolve/rust-name-resolution-context.cc | 9 +++++ gcc/rust/resolve/rust-name-resolution-context.h | 8 ++++- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc index 352d59b09205..3236886f37d4 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc @@ -16,18 +16,60 @@ // along with GCC; see the file COPYING3. If not see // . +#include "optional.h" #include "rust-ast-full.h" #include "rust-late-name-resolver-2.0.h" #include "rust-default-resolver.h" +#include "rust-tyty.h" +#include "rust-hir-type-check.h" namespace Rust { namespace Resolver2_0 { Late::Late (NameResolutionContext &ctx) : DefaultResolver (ctx) {} +void +Late::setup_builtin_types () +{ + auto next_id = [this] () { return ctx.mappings.get_next_hir_id (); }; + + static const std::pair builtins[] = { + {"u8", new TyTy::UintType (next_id (), TyTy::UintType::U8)}, + {"u16", new TyTy::UintType (next_id (), TyTy::UintType::U16)}, + {"u32", new TyTy::UintType (next_id (), TyTy::UintType::U32)}, + {"u64", new TyTy::UintType (next_id (), TyTy::UintType::U64)}, + {"u128", new TyTy::UintType (next_id (), TyTy::UintType::U128)}, + {"i8", new TyTy::IntType (next_id (), TyTy::IntType::I8)}, + {"i16", new TyTy::IntType (next_id (), TyTy::IntType::I16)}, + {"i32", new TyTy::IntType (next_id (), TyTy::IntType::I32)}, + {"i64", new TyTy::IntType (next_id (), TyTy::IntType::I64)}, + {"i128", new TyTy::IntType (next_id (), TyTy::IntType::I128)}, + {"f32", new TyTy::FloatType (next_id (), TyTy::FloatType::F32)}, + {"f64", new TyTy::FloatType (next_id (), TyTy::FloatType::F64)}, + {"usize", new TyTy::USizeType (next_id ())}, + {"isize", new TyTy::ISizeType (next_id ())}, + // missing char, str, never, () + // does name resolution play a part for this? or is it all at typechecking? + // yeah it seems to be name resolution as well, which makes sense + }; + + for (const auto &builtin : builtins) + { + // we should be able to use `insert_at_root` or `insert` here, since we're + // at the root :) hopefully! + auto ok + = ctx.types.insert (builtin.first, builtin.second->get_ref () + /* FIXME: Invalid! This returns an *HirId* */); + + rust_assert (ok); + } +} + void Late::go (AST::Crate &crate) { + setup_builtin_types (); + for (auto &item : crate.items) item->accept_vis (*this); } diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h b/gcc/rust/resolve/rust-late-name-resolver-2.0.h index 12540c0d2200..f54bbf2eea4f 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h @@ -47,6 +47,8 @@ public: void visit (AST::PathInExpression &) override; private: + /* Setup Rust's builtin types (u8, i32, !...) in the resolver */ + void setup_builtin_types (); }; // TODO: Add missing mappings and data structures diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc b/gcc/rust/resolve/rust-name-resolution-context.cc index 8bb7a9a15c10..f71ef91505bd 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.cc +++ b/gcc/rust/resolve/rust-name-resolution-context.cc @@ -43,6 +43,15 @@ NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns) } } +void +NameResolutionContext::map_usage (NodeId usage, NodeId definition) +{ + auto inserted = resolved_nodes.emplace (usage, definition).second; + + // is that valid? + rust_assert (inserted); +} + void NameResolutionContext::scoped (Rib rib, NodeId id, std::function lambda, diff --git a/gcc/rust/resolve/rust-name-resolution-context.h b/gcc/rust/resolve/rust-name-resolution-context.h index d63ee33378b8..7a1924581abd 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.h +++ b/gcc/rust/resolve/rust-name-resolution-context.h @@ -19,7 +19,6 @@ #ifndef RUST_NAME_RESOLVER_2_0_H #define RUST_NAME_RESOLVER_2_0_H -#include "optional.h" #include "rust-forever-stack.h" #include "rust-hir-map.h" @@ -179,6 +178,13 @@ public: ForeverStack labels; Analysis::Mappings &mappings; + + // TODO: Rename + void map_usage (NodeId usage, NodeId definition); + +private: + /* Map of "usage" nodes which have been resolved to a "definition" node */ + std::map resolved_nodes; }; } // namespace Resolver2_0