Skip to content

Commit 04694e1

Browse files
committed
Allow early resolution to use the language prelude
gcc/rust/ChangeLog: * Make-lang.in (GRS_OBJS): Add "rust-resolve-builtins.o". * resolve/rust-late-name-resolver-2.0.cc: Include "rust-resolve-builtins.h". (next_node_id): Remove function. (next_hir_id): Likewise. (Late::setup_builtin_types): Likewise. (Late::go): Use Builtins::setup_type_ctx instead of Late::setup_builtin_types. * resolve/rust-late-name-resolver-2.0.h (Late::setup_builtin_types): Remove function. * rust-session-manager.cc: Include "rust-resolve-builtins.h". (Session::expansion): Call Builtins::setup_lang_prelude. * resolve/rust-resolve-builtins.cc: New file. * resolve/rust-resolve-builtins.h: New file. gcc/testsuite/ChangeLog: * rust/compile/primitive-import.rs: New test. Signed-off-by: Owen Avery <[email protected]>
1 parent 5b7a0d7 commit 04694e1

File tree

7 files changed

+176
-78
lines changed

7 files changed

+176
-78
lines changed

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ GRS_OBJS = \
149149
rust/rust-immutable-name-resolution-context.o \
150150
rust/rust-early-name-resolver.o \
151151
rust/rust-name-resolver.o \
152+
rust/rust-resolve-builtins.o \
152153
rust/rust-ast-resolve.o \
153154
rust/rust-ast-resolve-base.o \
154155
rust/rust-ast-resolve-item.o \

gcc/rust/resolve/rust-late-name-resolver-2.0.cc

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "rust-late-name-resolver-2.0.h"
2525
#include "rust-default-resolver.h"
2626
#include "rust-name-resolution-context.h"
27+
#include "rust-resolve-builtins.h"
2728
#include "rust-path.h"
2829
#include "rust-system.h"
2930
#include "rust-tyty.h"
@@ -38,84 +39,10 @@ Late::Late (NameResolutionContext &ctx)
3839
: DefaultResolver (ctx), funny_error (false), block_big_self (false)
3940
{}
4041

41-
static NodeId
42-
next_node_id ()
43-
{
44-
return Analysis::Mappings::get ().get_next_node_id ();
45-
};
46-
47-
static HirId
48-
next_hir_id ()
49-
{
50-
return Analysis::Mappings::get ().get_next_hir_id ();
51-
};
52-
53-
void
54-
Late::setup_builtin_types ()
55-
{
56-
// access the global type context to setup the TyTys
57-
auto &ty_ctx = *Resolver::TypeCheckContext::get ();
58-
59-
// Late builtin type struct helper
60-
struct LType
61-
{
62-
std::string name;
63-
NodeId node_id;
64-
NodeId hir_id;
65-
TyTy::BaseType *type;
66-
67-
explicit LType (std::string name, TyTy::BaseType *type)
68-
: name (name), node_id (next_node_id ()), hir_id (type->get_ref ()),
69-
type (type)
70-
{}
71-
};
72-
73-
static const LType builtins[] = {
74-
{LType ("bool", new TyTy::BoolType (next_hir_id ()))},
75-
{LType ("u8", new TyTy::UintType (next_hir_id (), TyTy::UintType::U8))},
76-
{LType ("u16", new TyTy::UintType (next_hir_id (), TyTy::UintType::U16))},
77-
{LType ("u32", new TyTy::UintType (next_hir_id (), TyTy::UintType::U32))},
78-
{LType ("u64", new TyTy::UintType (next_hir_id (), TyTy::UintType::U64))},
79-
{LType ("u128", new TyTy::UintType (next_hir_id (), TyTy::UintType::U128))},
80-
{LType ("i8", new TyTy::IntType (next_hir_id (), TyTy::IntType::I8))},
81-
{LType ("i16", new TyTy::IntType (next_hir_id (), TyTy::IntType::I16))},
82-
{LType ("i32", new TyTy::IntType (next_hir_id (), TyTy::IntType::I32))},
83-
{LType ("i64", new TyTy::IntType (next_hir_id (), TyTy::IntType::I64))},
84-
{LType ("i128", new TyTy::IntType (next_hir_id (), TyTy::IntType::I128))},
85-
{LType ("f32", new TyTy::FloatType (next_hir_id (), TyTy::FloatType::F32))},
86-
{LType ("f64", new TyTy::FloatType (next_hir_id (), TyTy::FloatType::F64))},
87-
{LType ("usize", new TyTy::USizeType (next_hir_id ()))},
88-
{LType ("isize", new TyTy::ISizeType (next_hir_id ()))},
89-
{LType ("char", new TyTy::CharType (next_hir_id ()))},
90-
{LType ("str", new TyTy::StrType (next_hir_id ()))},
91-
{LType ("!", new TyTy::NeverType (next_hir_id ()))},
92-
93-
// the unit type `()` does not play a part in name-resolution - so we only
94-
// insert it in the type context...
95-
};
96-
97-
// There's a special Rib for putting prelude items, since prelude items need
98-
// to satisfy certain special rules.
99-
ctx.scoped (Rib::Kind::Prelude, 0, [this, &ty_ctx] (void) -> void {
100-
for (const auto &builtin : builtins)
101-
{
102-
auto ok = ctx.types.insert (builtin.name, builtin.node_id);
103-
rust_assert (ok);
104-
105-
ctx.mappings.insert_node_to_hir (builtin.node_id, builtin.hir_id);
106-
ty_ctx.insert_builtin (builtin.hir_id, builtin.node_id, builtin.type);
107-
}
108-
});
109-
110-
// ...here!
111-
auto *unit_type = TyTy::TupleType::get_unit_type ();
112-
ty_ctx.insert_builtin (unit_type->get_ref (), next_node_id (), unit_type);
113-
}
114-
11542
void
11643
Late::go (AST::Crate &crate)
11744
{
118-
setup_builtin_types ();
45+
Builtins::setup_type_ctx ();
11946

12047
visit (crate);
12148
}

gcc/rust/resolve/rust-late-name-resolver-2.0.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ class Late : public DefaultResolver
7575
private:
7676
void resolve_label (AST::Lifetime &lifetime);
7777

78-
/* Setup Rust's builtin types (u8, i32, !...) in the resolver */
79-
void setup_builtin_types ();
80-
8178
bool funny_error;
8279

8380
/* used to prevent "impl Self {}", "impl (Self, i32) {}", etc */
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
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 < builtin_count; i++)
77+
{
78+
NodeId node_id = mappings.get_next_node_id ();
79+
rust_assert (ctx.types.insert (Identifier (builtin_names[i]), 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+
HirId hir_ids[builtin_count];
92+
for (size_t i = 0; i < builtin_count; i++)
93+
hir_ids[i] = mappings.get_next_hir_id ();
94+
95+
TyTy::BaseType *types[builtin_count];
96+
{
97+
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+
BUILTIN_TYPES
104+
#undef TYPE_BASE
105+
#undef TYPE0
106+
#undef TYPE1
107+
}
108+
109+
for (size_t i = 0; i < builtin_count; i++)
110+
{
111+
NodeId node_id = builtin_node_ids[i];
112+
HirId hir_id = hir_ids[i];
113+
mappings.insert_node_to_hir (node_id, hir_id);
114+
ty_ctx.insert_builtin (hir_id, node_id, types[i]);
115+
}
116+
117+
// handle unit type separately
118+
auto *unit_type = TyTy::TupleType::get_unit_type ();
119+
ty_ctx.insert_builtin (unit_type->get_ref (), mappings.get_next_node_id (),
120+
unit_type);
121+
}
122+
123+
} // namespace Builtins
124+
} // namespace Resolver2_0
125+
} // namespace Rust
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
#ifndef RUST_RESOLVE_BUILTINS_H
20+
#define RUST_RESOLVE_BUILTINS_H
21+
22+
namespace Rust {
23+
namespace Resolver2_0 {
24+
25+
// forward declare
26+
class NameResolutionContext;
27+
28+
namespace Builtins {
29+
30+
void setup_lang_prelude (NameResolutionContext &ctx);
31+
void setup_type_ctx ();
32+
33+
} // namespace Builtins
34+
} // namespace Resolver2_0
35+
} // namespace Rust
36+
37+
#endif // RUST_RESOLVE_BUILTINS_H

gcc/rust/rust-session-manager.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "rust-name-resolution-context.h"
5151
#include "rust-early-name-resolver-2.0.h"
5252
#include "rust-late-name-resolver-2.0.h"
53+
#include "rust-resolve-builtins.h"
5354
#include "rust-cfg-strip.h"
5455
#include "rust-expand-visitor.h"
5556
#include "rust-unicode.h"
@@ -936,6 +937,9 @@ Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx)
936937
MacroExpander expander (crate, cfg, *this);
937938
std::vector<Error> macro_errors;
938939

940+
if (flag_name_resolution_2_0)
941+
Resolver2_0::Builtins::setup_lang_prelude (ctx);
942+
939943
while (!fixed_point_reached && iterations < cfg.recursion_limit)
940944
{
941945
CfgStrip ().go (crate);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mod primitive {
2+
pub use i32;
3+
}
4+
5+
pub fn foo() -> primitive::i32 {
6+
1
7+
}

0 commit comments

Comments
 (0)