Skip to content

Commit a7fea0d

Browse files
authored
Merge branch 'master' into refactor-compile-drop
2 parents cf5c49f + 6dae3d0 commit a7fea0d

17 files changed

Lines changed: 189 additions & 46 deletions

gcc/rust/backend/rust-compile-intrinsic.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ using IValue = Values::Intrinsics;
3030
static const std::map<std::string, handlers::HandlerBuilder> generic_intrinsics
3131
= {{IValue::OFFSET, handlers::offset},
3232
{IValue::SIZE_OF, handlers::sizeof_handler},
33+
{IValue::MIN_ALIGN_OF, handlers::min_align_of_handler},
3334
{IValue::TRANSMUTE, handlers::transmute},
3435
{IValue::ROTATE_LEFT, handlers::rotate_left},
3536
{IValue::ROTATE_RIGHT, handlers::rotate_right},

gcc/rust/backend/rust-intrinsic-handlers.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,45 @@ sizeof_handler (Context *ctx, TyTy::FnType *fntype)
16121612
return fndecl;
16131613
}
16141614

1615+
/**
1616+
* pub fn min_align_of<T>() -> usize;
1617+
*/
1618+
tree
1619+
min_align_of_handler (Context *ctx, TyTy::FnType *fntype)
1620+
{
1621+
// min_align_of has _zero_ parameters its parameter is the generic one
1622+
rust_assert (fntype->get_params ().size () == 0);
1623+
1624+
tree lookup = NULL_TREE;
1625+
if (check_for_cached_intrinsic (ctx, fntype, &lookup))
1626+
return lookup;
1627+
1628+
auto fndecl = compile_intrinsic_function (ctx, fntype);
1629+
1630+
// get the template parameter type tree fn min_align_of<T>();
1631+
rust_assert (fntype->get_num_substitutions () == 1);
1632+
auto &param_mapping = fntype->get_substs ().at (0);
1633+
const auto param_tyty = param_mapping.get_param_ty ();
1634+
auto resolved_tyty = param_tyty->resolve ();
1635+
tree template_parameter_type
1636+
= TyTyResolveCompile::compile (ctx, resolved_tyty);
1637+
1638+
enter_intrinsic_block (ctx, fndecl);
1639+
1640+
// BUILTIN min_align_of FN BODY BEGIN
1641+
tree align_expr
1642+
= build_int_cst (size_type_node, TYPE_ALIGN_UNIT (template_parameter_type));
1643+
1644+
auto return_statement
1645+
= Backend::return_statement (fndecl, align_expr, UNDEF_LOCATION);
1646+
ctx->add_statement (return_statement);
1647+
// BUILTIN min_align_of FN BODY END
1648+
1649+
finalize_intrinsic_block (ctx, fndecl);
1650+
1651+
return fndecl;
1652+
}
1653+
16151654
tree
16161655
offset (Context *ctx, TyTy::FnType *fntype)
16171656
{

gcc/rust/backend/rust-intrinsic-handlers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ tree rotate_right (Context *ctx, TyTy::FnType *fntype);
5757
const HandlerBuilder wrapping_op (tree_code op);
5858
tree offset (Context *ctx, TyTy::FnType *fntype);
5959
tree sizeof_handler (Context *ctx, TyTy::FnType *fntype);
60+
tree min_align_of_handler (Context *ctx, TyTy::FnType *fntype);
6061
tree transmute (Context *ctx, TyTy::FnType *fntype);
6162
tree rotate (Context *ctx, TyTy::FnType *fntype, tree_code op);
6263
tree uninit (Context *ctx, TyTy::FnType *fntype);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,34 @@ resolve_type_path_like (NameResolutionContext &ctx, bool block_big_self,
537537
"declared identifiers");
538538
}
539539

540+
if (Analysis::Mappings::get ().is_module (
541+
resolved->definition.get_node_id ()))
542+
{
543+
if (type.get_segments ().size () == 1)
544+
{
545+
if (auto resolved
546+
= Builtins::find_builtin_node_id (type.as_string ()))
547+
{
548+
ctx.map_usage (Usage (type.get_node_id ()),
549+
Definition (*resolved), Namespace::Types);
550+
551+
// In that specific case, we also override the segment resolution
552+
// as it causes issues later down the line during typechecking
553+
ctx.map_usage (Usage (unwrap_segment_node_id (
554+
type.get_segments ().front ())),
555+
Definition (*resolved), Namespace::Types);
556+
}
557+
else
558+
{
559+
rust_error_at (type.get_locus (), ErrorCode::E0573,
560+
"expected type, found module %qs",
561+
unwrap_segment_error_string (type).c_str ());
562+
}
563+
}
564+
565+
return;
566+
}
567+
540568
ctx.map_usage (Usage (type.get_node_id ()),
541569
Definition (resolved->definition.get_node_id ()),
542570
Namespace::Types);

gcc/rust/resolve/rust-name-resolution-context.hxx

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,6 @@ NameResolutionContext::resolve_path (
180180
// TODO: does NonShadowable matter?
181181
return Rib::Definition::NonShadowable (id);
182182
}
183-
else
184-
{
185-
// HACK: check for a module after we check the language prelude
186-
for (auto &kv :
187-
stack.find_closest_module (starting_point.get ()).children)
188-
{
189-
auto &link = kv.first;
190-
191-
if (link.path.map_or (
192-
[&seg] (Identifier path) {
193-
auto &path_str = path.as_string ();
194-
return path_str == seg.name;
195-
},
196-
false))
197-
{
198-
// FIXME: Is the NS to insert_segment_resolution valid?
199-
insert_segment_resolution (Usage (seg.node_id),
200-
Definition (kv.second.id), N);
201-
return Rib::Definition::NonShadowable (kv.second.id);
202-
}
203-
}
204-
}
205183
}
206184

207185
// FIXME: Is the NS to insert_segment_resolution valid?
@@ -262,27 +240,6 @@ NameResolutionContext::resolve_path (
262240
if (!res)
263241
res = stack.get_lang_prelude (seg_name);
264242

265-
if (N == Namespace::Types && !res)
266-
{
267-
// HACK: check for a module after we check the language prelude
268-
for (auto &kv : final_node.children)
269-
{
270-
auto &link = kv.first;
271-
272-
if (link.path.map_or (
273-
[&seg_name] (Identifier path) {
274-
auto &path_str = path.as_string ();
275-
return path_str == seg_name;
276-
},
277-
false))
278-
{
279-
insert_segment_resolution (Usage (seg.node_id),
280-
Definition (kv.second.id), N);
281-
return Rib::Definition::NonShadowable (kv.second.id);
282-
}
283-
}
284-
}
285-
286243
if (res && !res->is_ambiguous ())
287244
insert_segment_resolution (Usage (seg.node_id),
288245
Definition (res->get_node_id ()), N);

gcc/rust/resolve/rust-name-resolution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef RUST_NAME_RESOLVER_2_0_H
2020
#define RUST_NAME_RESOLVER_2_0_H
2121

22+
#include "rust-mapping-common.h"
23+
2224
namespace Rust {
2325
namespace Resolver2_0 {
2426

@@ -39,6 +41,7 @@ class Usage
3941
class Definition
4042
{
4143
public:
44+
explicit Definition () : id (UNKNOWN_NODEID) {}
4245
explicit Definition (NodeId id) : id (id) {}
4346

4447
NodeId id;

gcc/rust/resolve/rust-resolve-builtins.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ setup_type_ctx ()
120120
unit_type);
121121
}
122122

123+
tl::optional<NodeId>
124+
find_builtin_node_id (const std::string &name)
125+
{
126+
for (size_t i = 0; i < builtin_count; i++)
127+
if (strcmp (name.c_str (), builtin_names[i]) == 0)
128+
return builtin_node_ids[i];
129+
130+
return tl::nullopt;
131+
}
132+
123133
} // namespace Builtins
124134
} // namespace Resolver2_0
125135
} // namespace Rust

gcc/rust/resolve/rust-resolve-builtins.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#ifndef RUST_RESOLVE_BUILTINS_H
2020
#define RUST_RESOLVE_BUILTINS_H
2121

22+
#include "optional.h"
23+
#include "rust-ast.h"
24+
2225
namespace Rust {
2326
namespace Resolver2_0 {
2427

@@ -30,6 +33,9 @@ namespace Builtins {
3033
void setup_lang_prelude (NameResolutionContext &ctx);
3134
void setup_type_ctx ();
3235

36+
// Return the NodeId associated with a builtin type name if it exists
37+
tl::optional<NodeId> find_builtin_node_id (const std::string &name);
38+
3339
} // namespace Builtins
3440
} // namespace Resolver2_0
3541
} // namespace Rust

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,16 @@ TopLevel::go (AST::Crate &crate)
107107
void
108108
TopLevel::visit (AST::Module &module)
109109
{
110-
DefaultResolver::visit (module);
111-
112110
if (Analysis::Mappings::get ().lookup_glob_container (module.get_node_id ())
113111
== tl::nullopt)
114112
Analysis::Mappings::get ().insert_glob_container (module.get_node_id (),
115113
&module);
114+
115+
insert_or_error_out (module.get_name (), module, Namespace::Types);
116+
117+
Analysis::Mappings::get ().insert_module_id (module.get_node_id ());
118+
119+
DefaultResolver::visit (module);
116120
}
117121

118122
void

gcc/rust/typecheck/rust-hir-type-check-type.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "rust-type-util.h"
3232
#include "rust-system.h"
3333
#include "rust-compile-base.h"
34+
#include "rust-resolve-builtins.h"
3435

3536
namespace Rust {
3637
namespace Resolver {
@@ -349,6 +350,14 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
349350
// assign the ref_node_id if we've found something
350351
nr_ctx.lookup (ast_node_id, Resolver2_0::Namespace::Types)
351352
.map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
353+
354+
// TODO: Should we add a special method to the name resolver to handle
355+
// that case? Resolving something in the Types NS when we want to
356+
// prioritize builtin types over modules or other conflicting things?
357+
if (auto builtin_type_id
358+
= Resolver2_0::Builtins::find_builtin_node_id (seg->to_string ()))
359+
if (mappings.is_module (ref_node_id))
360+
ref_node_id = builtin_type_id.value ();
352361
}
353362

354363
// ref_node_id is the NodeId that the segments refers to.
@@ -411,7 +420,8 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
411420
// A::B::C::this_is_a_module
412421
// ^^^^^^^^^^^^^^^^
413422
// This is an error, we are not expecting a module.
414-
rust_error_at (seg->get_locus (), "expected value");
423+
rust_error_at (seg->get_locus (), "expected value, got module");
424+
415425
return new TyTy::ErrorType (path.get_mappings ().get_hirid ());
416426
}
417427

0 commit comments

Comments
 (0)