Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name resolution globbing #2796

Merged
merged 44 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8af64d2
nr2.0: Add new ImmutableNameResolutionCtx class.
CohenArthur Aug 24, 2023
0d5c3f1
sesh: Add late name resolution 2.0
CohenArthur Aug 4, 2023
f443069
session-manager: Dump name resolution pass.
CohenArthur Aug 23, 2023
8bf7f31
session manager: Init Immutable name resolver.
CohenArthur Aug 24, 2023
439a3b1
nr2.0: Add lookup of resolved nodes.
CohenArthur Aug 24, 2023
d015ac8
typecheck: Start using nr2.0 properly
CohenArthur Aug 24, 2023
8993c46
backend: Use new name resolver where necessary
CohenArthur Aug 25, 2023
ac72d09
nr2.0: Start using newtype pattern for Usage and Declaration
CohenArthur Sep 14, 2023
05bb370
late: Setup builtin types properly, change Rib API
CohenArthur Aug 28, 2023
b59ed31
Fix duplicate detection
P-E-P Sep 14, 2023
3fe1261
Emit error on identical use declarations
P-E-P Sep 15, 2023
b8eb9e4
Change error message on unresolved import
P-E-P Mar 24, 2024
b45d577
Prevent error emission on resolver reentry
P-E-P Sep 18, 2023
828e163
late: Add bool builtin type
CohenArthur Nov 8, 2023
9070329
Add modules to type namespace
P-E-P Jan 8, 2024
791f19a
Add name resolution for on globbing use decl
P-E-P Jan 9, 2024
c00d619
Shape up name resolver for normal direct calls
P-E-P Jan 10, 2024
41cb4ad
Add call to globbing visitor
P-E-P Jan 15, 2024
4691045
Make globbing definition shadowable by default
P-E-P Jan 15, 2024
6d50c9a
Add support for ambiguous use declarations
P-E-P Jan 16, 2024
9eef8be
Add tuple struct constructor to value namespace
P-E-P Jan 17, 2024
c484363
Change error message to match test
P-E-P Jan 17, 2024
c3b5f23
Visit function return type in default resolver
P-E-P Jan 24, 2024
b080ecb
Visit constant item type in default resolver
P-E-P Jan 24, 2024
c10d15d
Raw pointer type visitor didn't require overload
P-E-P Jan 24, 2024
4caad73
Values shall be inserted in the value namespace
P-E-P Jan 24, 2024
1bb562e
Unit struct constructor shall be resolved
P-E-P Jan 24, 2024
95a0547
Add tuple struct to the type namespace
P-E-P Jan 24, 2024
74d6449
Change enum namespace from value to type
P-E-P Jan 24, 2024
2a84cf0
Struct are types, not values
P-E-P Jan 24, 2024
74d47bc
Add constant identifiers to the value namespace
P-E-P Jan 24, 2024
86b75b2
Remove extern block scoping
P-E-P Feb 6, 2024
b6646bc
Remove unsafe block empty visit function
P-E-P Feb 6, 2024
cfdbadc
Use new name resolver to compile constant items
P-E-P Feb 6, 2024
58655fb
Reinject Self parameter in new resolver
P-E-P Feb 7, 2024
d6f2c80
Update assignment operator with cratenum
P-E-P Mar 13, 2024
180be5b
Prevent getting immutable context with classic nr
P-E-P Mar 13, 2024
2bdd499
Fix quoted string format
P-E-P Mar 13, 2024
9f03038
Add mappings for struct base and struct fields
P-E-P Mar 21, 2024
7c18255
Fix use rebind name resolution.
P-E-P Mar 21, 2024
da5f5bf
compile: resolve-path-ref: properly resolve nodeId with nr2.0
CohenArthur Mar 22, 2024
f5dcf49
nr2.0: Add new test cases.
CohenArthur Aug 23, 2023
33bed05
Add globbing name resolution 2.0 test
P-E-P Mar 26, 2024
721c6fa
Change dfs function return type to support gcc 4.8
P-E-P Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ GRS_OBJS = \
rust/rust-toplevel-name-resolver-2.0.o \
rust/rust-early-name-resolver-2.0.o \
rust/rust-late-name-resolver-2.0.o \
rust/rust-immutable-name-resolution-context.o \
rust/rust-early-name-resolver.o \
rust/rust-name-resolver.o \
rust/rust-ast-resolve.o \
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "rust-type-util.h"
#include "rust-compile-implitem.h"
#include "rust-attribute-values.h"
#include "rust-immutable-name-resolution-context.h"

#include "fold-const.h"
#include "stringpool.h"
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/backend/rust-compile-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "rust-hir-full.h"
#include "rust-mangle.h"
#include "rust-tree.h"
#include "rust-immutable-name-resolution-context.h"

namespace Rust {
namespace Compile {
Expand Down
22 changes: 17 additions & 5 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2311,11 +2311,23 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
if (is_block_expr)
{
auto body_mappings = function_body->get_mappings ();
Resolver::Rib *rib = nullptr;
bool ok
= ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
&rib);
rust_assert (ok);
if (flag_name_resolution_2_0)
{
auto nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

auto candidate = nr_ctx.values.to_rib (body_mappings.get_nodeid ());

rust_assert (candidate.has_value ());
}
else
{
Resolver::Rib *rib = nullptr;
bool ok
= ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
&rib);
rust_assert (ok);
}
}

tree enclosing_scope = NULL_TREE;
Expand Down
67 changes: 51 additions & 16 deletions gcc/rust/backend/rust-compile-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "rust-compile-item.h"
#include "rust-compile-implitem.h"
#include "rust-compile-extern.h"
#include "rust-immutable-name-resolution-context.h"

namespace Rust {
namespace Compile {
Expand Down Expand Up @@ -75,32 +76,48 @@ CompileItem::visit (HIR::StaticItem &var)
void
CompileItem::visit (HIR::ConstantItem &constant)
{
if (ctx->lookup_const_decl (constant.get_mappings ().get_hirid (),
&reference))
auto &mappings = constant.get_mappings ();

if (ctx->lookup_const_decl (mappings.get_hirid (), &reference))
return;

// resolve the type
TyTy::BaseType *resolved_type = nullptr;

bool ok
= ctx->get_tyctx ()->lookup_type (constant.get_mappings ().get_hirid (),
&resolved_type);
= ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &resolved_type);
rust_assert (ok);

// canonical path
const Resolver::CanonicalPath *canonical_path = nullptr;
ok = ctx->get_mappings ()->lookup_canonical_path (
constant.get_mappings ().get_nodeid (), &canonical_path);
rust_assert (ok);
Resolver::CanonicalPath canonical_path
= Resolver::CanonicalPath::create_empty ();

if (flag_name_resolution_2_0)
{
auto nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

canonical_path
= nr_ctx.values.to_canonical_path (mappings.get_nodeid ()).value ();
}
else
{
const Resolver::CanonicalPath *canonical_path_ptr = nullptr;
ok = ctx->get_mappings ()->lookup_canonical_path (mappings.get_nodeid (),
&canonical_path_ptr);
rust_assert (ok);
canonical_path = *canonical_path_ptr;
}

HIR::Expr *const_value_expr = constant.get_expr ().get ();
ctx->push_const_context ();
tree const_expr
= compile_constant_item (resolved_type, canonical_path, const_value_expr,
= compile_constant_item (resolved_type, &canonical_path, const_value_expr,
constant.get_locus ());
ctx->pop_const_context ();

ctx->push_const (const_expr);
ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
ctx->insert_const_decl (mappings.get_hirid (), const_expr);
reference = const_expr;
}

Expand Down Expand Up @@ -149,12 +166,30 @@ CompileItem::visit (HIR::Function &function)
}
}

const Resolver::CanonicalPath *canonical_path = nullptr;
bool ok = ctx->get_mappings ()->lookup_canonical_path (
function.get_mappings ().get_nodeid (), &canonical_path);
rust_assert (ok);
Resolver::CanonicalPath canonical_path
= Resolver::CanonicalPath::create_empty ();

if (flag_name_resolution_2_0)
{
auto nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

auto path = nr_ctx.values.to_canonical_path (
function.get_mappings ().get_nodeid ());

canonical_path = path.value ();
}
else
{
const Resolver::CanonicalPath *path = nullptr;
bool ok = ctx->get_mappings ()->lookup_canonical_path (
function.get_mappings ().get_nodeid (), &path);
rust_assert (ok);

canonical_path = *path;
}

const std::string asm_name = ctx->mangle_item (fntype, *canonical_path);
const std::string asm_name = ctx->mangle_item (fntype, canonical_path);

// items can be forward compiled which means we may not need to invoke this
// code. We might also have already compiled this generic function as well.
Expand All @@ -181,7 +216,7 @@ CompileItem::visit (HIR::Function &function)
function.get_function_params (),
function.get_qualifiers (), function.get_visibility (),
function.get_outer_attrs (), function.get_locus (),
function.get_definition ().get (), canonical_path,
function.get_definition ().get (), &canonical_path,
fntype);
reference = address_expression (fndecl, ref_locus);

Expand Down
112 changes: 68 additions & 44 deletions gcc/rust/backend/rust-compile-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
// <http://www.gnu.org/licenses/>.

#include "rust-compile-resolve-path.h"
#include "options.h"
#include "rust-compile-intrinsic.h"
#include "rust-compile-item.h"
#include "rust-compile-implitem.h"
#include "rust-compile-expr.h"
#include "rust-hir-map.h"
#include "rust-hir-trait-resolve.h"
#include "rust-hir-path-probe.h"
#include "rust-compile-extern.h"
#include "rust-constexpr.h"
#include "rust-tyty.h"

namespace Rust {
namespace Compile {
Expand All @@ -43,6 +46,50 @@ ResolvePathRef::visit (HIR::PathInExpression &expr)
expr.get_mappings (), expr.get_locus (), false);
}

tree
ResolvePathRef::attempt_constructor_expression_lookup (
TyTy::BaseType *lookup, Context *ctx, const Analysis::NodeMapping &mappings,
location_t expr_locus)
{
// it might be an enum data-less enum variant
if (lookup->get_kind () != TyTy::TypeKind::ADT)
return error_mark_node;

TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
if (adt->is_unit ())
return unit_expression (ctx, expr_locus);

if (!adt->is_enum ())
return error_mark_node;

HirId variant_id;
if (!ctx->get_tyctx ()->lookup_variant_definition (mappings.get_hirid (),
&variant_id))
return error_mark_node;

int union_disriminator = -1;
TyTy::VariantDef *variant = nullptr;
if (!adt->lookup_variant_by_id (variant_id, &variant, &union_disriminator))
return error_mark_node;

// this can only be for discriminant variants the others are built up
// using call-expr or struct-init
rust_assert (variant->get_variant_type ()
== TyTy::VariantDef::VariantType::NUM);

// we need the actual gcc type
tree compiled_adt_type = TyTyResolveCompile::compile (ctx, adt);

// make the ctor for the union
HIR::Expr *discrim_expr = variant->get_discriminant ();
tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
tree folded_discrim_expr = fold_expr (discrim_expr_node);
tree qualifier = folded_discrim_expr;

return Backend::constructor_expression (compiled_adt_type, true, {qualifier},
union_disriminator, expr_locus);
}

tree
ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
const Analysis::NodeMapping &mappings,
Expand All @@ -53,52 +100,29 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
rust_assert (ok);

// need to look up the reference for this identifier

// this can fail because it might be a Constructor for something
// in that case the caller should attempt ResolvePathType::Compile
NodeId ref_node_id = UNKNOWN_NODEID;
if (!ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
&ref_node_id))
if (flag_name_resolution_2_0)
{
auto nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

auto resolved = nr_ctx.lookup (mappings.get_nodeid ());

if (!resolved)
return attempt_constructor_expression_lookup (lookup, ctx, mappings,
expr_locus);

ref_node_id = *resolved;
}
else
{
// this can fail because it might be a Constructor for something
// in that case the caller should attempt ResolvePathType::Compile

// it might be an enum data-less enum variant
if (lookup->get_kind () != TyTy::TypeKind::ADT)
return error_mark_node;

TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
if (adt->is_unit ())
return unit_expression (ctx, expr_locus);

if (!adt->is_enum ())
return error_mark_node;

HirId variant_id;
if (!ctx->get_tyctx ()->lookup_variant_definition (mappings.get_hirid (),
&variant_id))
return error_mark_node;

int union_disriminator = -1;
TyTy::VariantDef *variant = nullptr;
if (!adt->lookup_variant_by_id (variant_id, &variant,
&union_disriminator))
return error_mark_node;

// this can only be for discriminant variants the others are built up
// using call-expr or struct-init
rust_assert (variant->get_variant_type ()
== TyTy::VariantDef::VariantType::NUM);

// we need the actual gcc type
tree compiled_adt_type = TyTyResolveCompile::compile (ctx, adt);

// make the ctor for the union
HIR::Expr *discrim_expr = variant->get_discriminant ();
tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
tree folded_discrim_expr = fold_expr (discrim_expr_node);
tree qualifier = folded_discrim_expr;

return Backend::constructor_expression (compiled_adt_type, true,
{qualifier}, union_disriminator,
expr_locus);
if (!ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
&ref_node_id))
return attempt_constructor_expression_lookup (lookup, ctx, mappings,
expr_locus);
}

HirId ref;
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/backend/rust-compile-resolve-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class ResolvePathRef : public HIRCompileBase, public HIR::HIRPatternVisitor
bool is_qualified_path);

tree resolved;

private:
tree
attempt_constructor_expression_lookup (TyTy::BaseType *lookup, Context *ctx,
const Analysis::NodeMapping &mappings,
location_t expr_locus);
};

} // namespace Compile
Expand Down
23 changes: 19 additions & 4 deletions gcc/rust/checks/lints/rust-lint-marklive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
// from live codes are live, and everything else is dead.

#include "rust-lint-marklive.h"
#include "options.h"
#include "rust-hir-full.h"
#include "rust-name-resolver.h"
#include "rust-immutable-name-resolution-context.h"

namespace Rust {
namespace Analysis {
Expand Down Expand Up @@ -270,12 +272,25 @@ MarkLive::mark_hir_id (HirId id)
void
MarkLive::find_ref_node_id (NodeId ast_node_id, NodeId &ref_node_id)
{
if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
if (flag_name_resolution_2_0)
{
if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id))
auto nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

nr_ctx.lookup (ast_node_id).map ([&ref_node_id] (NodeId resolved) {
ref_node_id = resolved;
});
}
else
{
if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
{
bool ok = resolver->lookup_resolved_misc (ast_node_id, &ref_node_id);
rust_assert (ok);
if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id))
{
bool ok
= resolver->lookup_resolved_misc (ast_node_id, &ref_node_id);
rust_assert (ok);
}
}
}
}
Expand Down
Loading
Loading