|
| 1 | +// Copyright (C) 2025 Free Software Foundation, Inc. |
| 2 | + |
| 3 | +// This file is part of GCC. |
| 4 | + |
| 5 | +// GCC is free software; you can redistribute it and/or modify it under |
| 6 | +// the terms of the GNU General Public License as published by the Free |
| 7 | +// Software Foundation; either version 3, or (at your option) any later |
| 8 | +// version. |
| 9 | + |
| 10 | +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +// for more details. |
| 14 | + |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with GCC; see the file COPYING3. If not see |
| 17 | +// <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +#include "rust-resolve-builtins.h" |
| 20 | +#include "rust-name-resolution-context.h" |
| 21 | +#include "rust-tyty.h" |
| 22 | +#include "rust-hir-type-check.h" |
| 23 | + |
| 24 | +namespace Rust { |
| 25 | +namespace Resolver2_0 { |
| 26 | +namespace Builtins { |
| 27 | + |
| 28 | +class TypeBuilder |
| 29 | +{ |
| 30 | +public: |
| 31 | + constexpr TypeBuilder (const char *name, TyTy::BaseType *(*type_gen) (HirId)) |
| 32 | + : name (name), type_gen (type_gen) |
| 33 | + {} |
| 34 | + |
| 35 | + std::string create_name () const { return name; } |
| 36 | + |
| 37 | + TyTy::BaseType *create_type (HirId hir_id) const { return type_gen (hir_id); } |
| 38 | + |
| 39 | +private: |
| 40 | + const char *name; |
| 41 | + TyTy::BaseType *(*type_gen) (HirId); |
| 42 | +}; |
| 43 | + |
| 44 | +#define GEN_DEF(name, expr) \ |
| 45 | + TypeBuilder ((name), [] (HirId hir_id) -> TyTy::BaseType * { return (expr); }) |
| 46 | + |
| 47 | +static constexpr TypeBuilder builtins[] |
| 48 | + = {GEN_DEF ("bool", new TyTy::BoolType (hir_id)), |
| 49 | + GEN_DEF ("u8", new TyTy::UintType (hir_id, TyTy::UintType::U8)), |
| 50 | + GEN_DEF ("u16", new TyTy::UintType (hir_id, TyTy::UintType::U16)), |
| 51 | + GEN_DEF ("u32", new TyTy::UintType (hir_id, TyTy::UintType::U32)), |
| 52 | + GEN_DEF ("u64", new TyTy::UintType (hir_id, TyTy::UintType::U64)), |
| 53 | + GEN_DEF ("u128", new TyTy::UintType (hir_id, TyTy::UintType::U128)), |
| 54 | + GEN_DEF ("i8", new TyTy::IntType (hir_id, TyTy::IntType::I8)), |
| 55 | + GEN_DEF ("i16", new TyTy::IntType (hir_id, TyTy::IntType::I16)), |
| 56 | + GEN_DEF ("i32", new TyTy::IntType (hir_id, TyTy::IntType::I32)), |
| 57 | + GEN_DEF ("i64", new TyTy::IntType (hir_id, TyTy::IntType::I64)), |
| 58 | + GEN_DEF ("i128", new TyTy::IntType (hir_id, TyTy::IntType::I128)), |
| 59 | + GEN_DEF ("f32", new TyTy::FloatType (hir_id, TyTy::FloatType::F32)), |
| 60 | + GEN_DEF ("f64", new TyTy::FloatType (hir_id, TyTy::FloatType::F64)), |
| 61 | + GEN_DEF ("usize", new TyTy::USizeType (hir_id)), |
| 62 | + GEN_DEF ("isize", new TyTy::ISizeType (hir_id)), |
| 63 | + GEN_DEF ("char", new TyTy::CharType (hir_id)), |
| 64 | + GEN_DEF ("str", new TyTy::StrType (hir_id)), |
| 65 | + GEN_DEF ("!", new TyTy::NeverType (hir_id))}; |
| 66 | + |
| 67 | +static NodeId builtin_node_ids[sizeof (builtins) / sizeof (TypeBuilder)]; |
| 68 | + |
| 69 | +void |
| 70 | +setup_lang_prelude (NameResolutionContext &ctx) |
| 71 | +{ |
| 72 | + auto &mappings = Analysis::Mappings::get (); |
| 73 | + |
| 74 | + // insert into prelude rib |
| 75 | + ctx.scoped (Rib::Kind::Prelude, 0, [&mappings, &ctx] (void) -> void { |
| 76 | + for (size_t i = 0; i < sizeof (builtins) / sizeof (TypeBuilder); i++) |
| 77 | + { |
| 78 | + NodeId node_id = mappings.get_next_node_id (); |
| 79 | + rust_assert (ctx.types.insert (builtins[i].create_name (), node_id)); |
| 80 | + builtin_node_ids[i] = node_id; |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +void |
| 86 | +setup_type_ctx () |
| 87 | +{ |
| 88 | + auto &mappings = Analysis::Mappings::get (); |
| 89 | + auto &ty_ctx = *Resolver::TypeCheckContext::get (); |
| 90 | + |
| 91 | + for (size_t i = 0; i < sizeof (builtins) / sizeof (TypeBuilder); i++) |
| 92 | + { |
| 93 | + NodeId node_id = builtin_node_ids[i]; |
| 94 | + HirId hir_id = mappings.get_next_hir_id (); |
| 95 | + mappings.insert_node_to_hir (node_id, hir_id); |
| 96 | + ty_ctx.insert_builtin (hir_id, node_id, builtins[i].create_type (hir_id)); |
| 97 | + } |
| 98 | + |
| 99 | + // handle unit type separately |
| 100 | + auto *unit_type = TyTy::TupleType::get_unit_type (); |
| 101 | + ty_ctx.insert_builtin (unit_type->get_ref (), mappings.get_next_node_id (), |
| 102 | + unit_type); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace Builtins |
| 106 | +} // namespace Resolver2_0 |
| 107 | +} // namespace Rust |
0 commit comments