Branch data Line data Source code
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 : : // 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 : : static NodeId builtin_node_ids[builtin_count];
68 : :
69 : : void
70 : 4449 : setup_lang_prelude (NameResolutionContext &ctx)
71 : : {
72 : 4449 : auto &mappings = Analysis::Mappings::get ();
73 : :
74 : : // insert into prelude rib
75 : 4449 : ctx.scoped (Rib::Kind::Prelude, 0, [&mappings, &ctx] (void) -> void {
76 : 84531 : for (size_t i = 0; i < builtin_count; i++)
77 : : {
78 : 80082 : NodeId node_id = mappings.get_next_node_id ();
79 : 160164 : rust_assert (ctx.types.insert (Identifier (builtin_names[i]), node_id));
80 : 80082 : builtin_node_ids[i] = node_id;
81 : : }
82 : 4449 : });
83 : 4449 : }
84 : :
85 : : void
86 : 4444 : setup_type_ctx ()
87 : : {
88 : 4444 : auto &mappings = Analysis::Mappings::get ();
89 : 4444 : auto &ty_ctx = *Resolver::TypeCheckContext::get ();
90 : :
91 : 4444 : HirId hir_ids[builtin_count];
92 : 84436 : for (size_t i = 0; i < builtin_count; i++)
93 : 79992 : hir_ids[i] = mappings.get_next_hir_id ();
94 : :
95 : 4444 : TyTy::BaseType *types[builtin_count];
96 : 4444 : {
97 : 4444 : size_t i = 0;
98 : : #define TYPE_BASE(stub) \
99 : : types[i] = new TyTy::stub; \
100 : : i++;
101 : : #define TYPE0(n, ty) TYPE_BASE (ty (hir_ids[i]))
102 : : #define TYPE1(n, ty, p1) TYPE_BASE (ty (hir_ids[i], TyTy::p1))
103 : 4444 : BUILTIN_TYPES
104 : : #undef TYPE_BASE
105 : : #undef TYPE0
106 : : #undef TYPE1
107 : : }
108 : :
109 : 84436 : for (size_t i = 0; i < builtin_count; i++)
110 : : {
111 : 79992 : NodeId node_id = builtin_node_ids[i];
112 : 79992 : HirId hir_id = hir_ids[i];
113 : 79992 : mappings.insert_node_to_hir (node_id, hir_id);
114 : 79992 : ty_ctx.insert_builtin (hir_id, node_id, types[i]);
115 : : }
116 : :
117 : : // handle unit type separately
118 : 4444 : auto *unit_type = TyTy::TupleType::get_unit_type ();
119 : 4444 : ty_ctx.insert_builtin (unit_type->get_ref (), mappings.get_next_node_id (),
120 : : unit_type);
121 : 4444 : }
122 : :
123 : : } // namespace Builtins
124 : : } // namespace Resolver2_0
125 : : } // namespace Rust
|