Line data Source code
1 : // Copyright (C) 2025-2026 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 : // Use X-macros
29 :
30 : #define TYPE_UINT(n, enum_ident) TYPE1 (n, UintType, UintType::enum_ident)
31 : #define TYPE_INT(n, enum_ident) TYPE1 (n, IntType, IntType::enum_ident)
32 :
33 : #define BUILTIN_TYPES \
34 : TYPE0 ("bool", BoolType) \
35 : TYPE_UINT ("u8", U8) \
36 : TYPE_UINT ("u16", U16) \
37 : TYPE_UINT ("u32", U32) \
38 : TYPE_UINT ("u64", U64) \
39 : TYPE_UINT ("u128", U128) \
40 : TYPE_INT ("i8", I8) \
41 : TYPE_INT ("i16", I16) \
42 : TYPE_INT ("i32", I32) \
43 : TYPE_INT ("i64", I64) \
44 : TYPE_INT ("i128", I128) \
45 : TYPE1 ("f32", FloatType, FloatType::F32) \
46 : TYPE1 ("f64", FloatType, FloatType::F64) \
47 : TYPE0 ("usize", USizeType) \
48 : TYPE0 ("isize", ISizeType) \
49 : TYPE0 ("char", CharType) \
50 : TYPE0 ("str", StrType) \
51 : TYPE0 ("!", NeverType)
52 :
53 : // Define constants using X macros
54 :
55 : #define TYPE0(...) 1 +
56 : #define TYPE1(...) 1 +
57 : static constexpr size_t builtin_count = BUILTIN_TYPES 0;
58 : #undef TYPE0
59 : #undef TYPE1
60 :
61 : #define TYPE0(n, ...) n,
62 : #define TYPE1(n, ...) n,
63 : static constexpr const char *builtin_names[] = {BUILTIN_TYPES};
64 : #undef TYPE0
65 : #undef TYPE1
66 :
67 : class LangPreludeSingleton
68 : {
69 : std::array<NodeId, builtin_count> builtin_node_ids;
70 : std::array<HirId, builtin_count> builtin_hir_ids;
71 4845 : LangPreludeSingleton ()
72 : {
73 4845 : auto &mappings = Analysis::Mappings::get ();
74 96900 : for (size_t i = 0; i < builtin_node_ids.size (); i++)
75 : {
76 87210 : NodeId node_id = mappings.get_next_node_id ();
77 87210 : builtin_node_ids[i] = node_id;
78 87210 : HirId hirid = mappings.get_next_hir_id ();
79 87210 : builtin_hir_ids[i] = hirid;
80 : }
81 4845 : }
82 :
83 : public:
84 376345 : static LangPreludeSingleton &get ()
85 : {
86 376345 : static LangPreludeSingleton instance{};
87 376345 : return instance;
88 : }
89 :
90 : const std::array<NodeId, builtin_count> &get_node_ids ()
91 : {
92 : return builtin_node_ids;
93 : }
94 :
95 : const std::array<HirId, builtin_count> &get_hir_ids ()
96 : {
97 : return builtin_hir_ids;
98 : }
99 : };
100 :
101 : void
102 4869 : setup_lang_prelude (NameResolutionContext &ctx)
103 : {
104 : // insert into prelude rib
105 4869 : ctx.scoped (Rib::Kind::Prelude, 0, [&ctx] (void) -> void {
106 92511 : for (size_t i = 0; i < builtin_count; i++)
107 : {
108 175284 : rust_assert (
109 : ctx.types.insert (Identifier (builtin_names[i]),
110 : LangPreludeSingleton::get ().get_node_ids ()[i]));
111 : }
112 4869 : });
113 4869 : }
114 :
115 : void
116 4865 : setup_type_ctx ()
117 : {
118 4865 : auto &mappings = Analysis::Mappings::get ();
119 4865 : auto &ty_ctx = *Resolver::TypeCheckContext::get ();
120 :
121 4865 : TyTy::BaseType *types[builtin_count];
122 4865 : {
123 4865 : size_t i = 0;
124 : #define TYPE_BASE(stub) \
125 : types[i] = new TyTy::stub; \
126 : i++;
127 : #define TYPE0(n, ty) \
128 : TYPE_BASE (ty (LangPreludeSingleton::get ().get_hir_ids ()[i]))
129 : #define TYPE1(n, ty, p1) \
130 : TYPE_BASE (ty (LangPreludeSingleton::get ().get_hir_ids ()[i], TyTy::p1))
131 4865 : BUILTIN_TYPES
132 : #undef TYPE_BASE
133 : #undef TYPE0
134 : #undef TYPE1
135 : }
136 :
137 92435 : for (size_t i = 0; i < builtin_count; i++)
138 : {
139 87570 : NodeId node_id = LangPreludeSingleton::get ().get_node_ids ()[i];
140 87570 : HirId hir_id = LangPreludeSingleton::get ().get_hir_ids ()[i];
141 87570 : mappings.insert_node_to_hir (node_id, hir_id);
142 87570 : ty_ctx.insert_builtin (hir_id, node_id, types[i]);
143 : }
144 :
145 : // handle unit type separately
146 4865 : auto *unit_type = TyTy::TupleType::get_unit_type ();
147 4865 : ty_ctx.insert_builtin (unit_type->get_ref (), mappings.get_next_node_id (),
148 : unit_type);
149 4865 : }
150 :
151 : tl::optional<NodeId>
152 49414 : find_builtin_node_id (const std::string &name)
153 : {
154 669845 : for (size_t i = 0; i < builtin_count; i++)
155 646424 : if (strcmp (name.c_str (), builtin_names[i]) == 0)
156 25993 : return LangPreludeSingleton::get ().get_node_ids ()[i];
157 :
158 23421 : return tl::nullopt;
159 : }
160 :
161 : } // namespace Builtins
162 : } // namespace Resolver2_0
163 : } // namespace Rust
|