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

Prepare lang-item {AST, HIR}::PathInExpressions #3378

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
18 changes: 16 additions & 2 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-ast-collector.h"
#include "rust-ast.h"
#include "rust-diagnostics.h"
#include "rust-expr.h"
#include "rust-item.h"
#include "rust-keyword-values.h"
#include "rust-path.h"
#include "rust-system.h"
#include "rust-token.h"

Expand Down Expand Up @@ -530,11 +532,23 @@ TokenCollector::visit (PathExprSegment &segment)
void
TokenCollector::visit (PathInExpression &path)
{
if (path.opening_scope_resolution ())
if (path.is_lang_item ())
{
push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
push (Rust::Token::make (TokenId::HASH, path.get_locus ()));
push (Rust::Token::make (TokenId::LEFT_SQUARE, path.get_locus ()));
push (Rust::Token::make_identifier (path.get_locus (), "lang"));
push (Rust::Token::make (TokenId::EQUAL, path.get_locus ()));
push (
Rust::Token::make_string (path.get_locus (),
LangItem::ToString (path.get_lang_item ())));
push (Rust::Token::make (TokenId::RIGHT_SQUARE, path.get_locus ()));

return;
}

if (path.opening_scope_resolution ())
push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));

visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
}

Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ void
DefaultASTVisitor::visit (AST::PathInExpression &path)
{
visit_outer_attrs (path);
for (auto &segment : path.get_segments ())
visit (segment);

if (!path.is_lang_item ())
for (auto &segment : path.get_segments ())
visit (segment);
}

void
Expand Down
9 changes: 9 additions & 0 deletions gcc/rust/ast/rust-collect-lang-items.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "rust-collect-lang-items.h"
#include "optional.h"
#include "rust-ast-collector.h"
#include "rust-ast-visitor.h"
#include "rust-ast.h"
#include "rust-attribute-values.h"
#include "rust-attributes.h"
Expand Down Expand Up @@ -100,5 +101,13 @@ CollectLangItems::visit (AST::StructStruct &item)
DefaultASTVisitor::visit (item);
}

void
CollectLangItems::visit (AST::EnumItem &item)
{
maybe_add_lang_item (item);

DefaultASTVisitor::visit (item);
}

} // namespace AST
} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/ast/rust-collect-lang-items.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CollectLangItems : public DefaultASTVisitor
void visit (AST::TraitItemType &item) override;
void visit (AST::Function &item) override;
void visit (AST::StructStruct &item) override;
void visit (AST::EnumItem &item) override;

private:
template <typename T> void maybe_add_lang_item (const T &item);
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ class Path : public Pattern

std::string as_string () const override;

bool is_lang_item () const { return kind == Kind::LangItem; }

// TODO: this seems kinda dodgy
std::vector<PathExprSegment> &get_segments ()
{
Expand Down
22 changes: 18 additions & 4 deletions gcc/rust/backend/rust-compile-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,29 @@ namespace Compile {
void
ResolvePathRef::visit (HIR::QualifiedPathInExpression &expr)
{
resolved = resolve (expr.get_final_segment ().get_segment (),
expr.get_mappings (), expr.get_locus (), true);
auto final_segment = HIR::PathIdentSegment::create_error ();
if (expr.is_lang_item ())
final_segment
= HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
Copy link
Contributor

@liamnaddell liamnaddell Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this end up getting confused if there are multiple Some declarations floating around?

I.e.

fn Some () {
 let _ = option_env!("PWD"); // Yields lang-item path to Some("/tmp")
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe - I'm adding a different implementation right now which is more precise and makes use of the lang item's known NodeId

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the version you have up now works, at least from tracing the code by eye.

else
final_segment = expr.get_final_segment ().get_segment ();

resolved
= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
}

void
ResolvePathRef::visit (HIR::PathInExpression &expr)
{
resolved = resolve (expr.get_final_segment ().get_segment (),
expr.get_mappings (), expr.get_locus (), false);
auto final_segment = HIR::PathIdentSegment::create_error ();
if (expr.is_lang_item ())
final_segment
= HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
else
final_segment = expr.get_final_segment ().get_segment ();

resolved
= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
}

tree
Expand Down
16 changes: 12 additions & 4 deletions gcc/rust/checks/lints/rust-lint-marklive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "rust-lint-marklive.h"
#include "options.h"
#include "rust-hir-full.h"
#include "rust-hir-map.h"
#include "rust-hir-path.h"
#include "rust-name-resolver.h"
#include "rust-immutable-name-resolution-context.h"
#include "rust-system.h"
Expand Down Expand Up @@ -99,15 +101,21 @@ MarkLive::visit (HIR::PathInExpression &expr)
{
// We should iterate every path segment in order to mark the struct which
// is used in expression like Foo::bar(), we should mark the Foo alive.
expr.iterate_path_segments ([&] (HIR::PathExprSegment &seg) -> bool {
return visit_path_segment (seg);
});
if (!expr.is_lang_item ())
expr.iterate_path_segments ([&] (HIR::PathExprSegment &seg) -> bool {
return visit_path_segment (seg);
});

// after iterate the path segments, we should mark functions and associated
// functions alive.
NodeId ast_node_id = expr.get_mappings ().get_nodeid ();
NodeId ref_node_id = UNKNOWN_NODEID;
find_ref_node_id (ast_node_id, ref_node_id);

if (expr.is_lang_item ())
ref_node_id
= Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ());
else
find_ref_node_id (ast_node_id, ref_node_id);

// node back to HIR
tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id);
Expand Down
10 changes: 7 additions & 3 deletions gcc/rust/expand/rust-cfg-strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "rust-cfg-strip.h"
#include "rust-ast-full.h"
#include "rust-ast-visitor.h"
#include "rust-path.h"
#include "rust-session-manager.h"
#include "rust-attribute-values.h"

Expand Down Expand Up @@ -434,10 +435,13 @@ CfgStrip::visit (AST::PathInExpression &path)
return;
}

for (auto &segment : path.get_segments ())
if (!path.is_lang_item ())
{
if (segment.has_generic_args ())
maybe_strip_generic_args (segment.get_generic_args ());
for (auto &segment : path.get_segments ())
{
if (segment.has_generic_args ())
maybe_strip_generic_args (segment.get_generic_args ());
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions gcc/rust/expand/rust-expand-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,10 @@ ExpandVisitor::visit (AST::MacroInvocation &macro_invoc)
void
ExpandVisitor::visit (AST::PathInExpression &path)
{
for (auto &segment : path.get_segments ())
if (segment.has_generic_args ())
expand_generic_args (segment.get_generic_args ());
if (!path.is_lang_item ())
for (auto &segment : path.get_segments ())
if (segment.has_generic_args ())
expand_generic_args (segment.get_generic_args ());
}

void
Expand Down
35 changes: 13 additions & 22 deletions gcc/rust/hir/rust-ast-lower.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,24 +418,7 @@ ASTLoweringExprWithBlock::visit (AST::WhileLoopExpr &expr)
void
ASTLoweringExprWithBlock::visit (AST::ForLoopExpr &expr)
{
// TODO FIXME

// HIR::BlockExpr *loop_block
// = ASTLoweringBlock::translate (expr.get_loop_block ().get (),
// &terminated);
// HIR::LoopLabel loop_label = lower_loop_label (expr.get_loop_label ());
// HIR::Expr *iterator_expr
// = ASTLoweringExpr::translate (expr.get_iterator_expr ().get (),
// &terminated);
// HIR::Pattern *loop_pattern
// = ASTLoweringPattern::translate (expr.get_pattern ().get ());

// auto crate_num = mappings->get_current_crate ();
// Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
// mappings->get_next_hir_id (crate_num),
// UNKNOWN_LOCAL_DEFID);

gcc_unreachable ();
rust_unreachable ();
}

void
Expand Down Expand Up @@ -494,6 +477,18 @@ ASTLoweringExprWithBlock::visit (AST::MatchExpr &expr)
void
ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
{
auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

if (expr.is_lang_item ())
{
translated = new HIR::PathInExpression (mapping, expr.get_lang_item (),
expr.get_locus (), false);
return;
}

std::vector<HIR::PathExprSegment> path_segments;
auto &segments = expr.get_segments ();
for (auto &s : segments)
Expand All @@ -504,10 +499,6 @@ ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
HIR::PathExprSegment *lowered_seg = &path_segments.back ();
mappings.insert_hir_path_expr_seg (lowered_seg);
}
auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

translated = new HIR::PathInExpression (mapping, std::move (path_segments),
expr.get_locus (),
Expand Down
20 changes: 20 additions & 0 deletions gcc/rust/hir/tree/rust-hir-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ PathExprSegment::operator= (PathExprSegment const &other)
void
PathPattern::iterate_path_segments (std::function<bool (PathExprSegment &)> cb)
{
rust_assert (kind == Kind::Segmented);

for (auto it = segments.begin (); it != segments.end (); it++)
{
if (!cb (*it))
Expand All @@ -150,6 +152,15 @@ PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
{}

PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
LangItem::Kind lang_item, location_t locus,
bool has_opening_scope_resolution,
std::vector<AST::Attribute> outer_attrs)
: PathPattern (lang_item),
PathExpr (std::move (mappings), std::move (outer_attrs)),
has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
{}

bool
PathInExpression::is_self () const

Expand Down Expand Up @@ -358,6 +369,15 @@ QualifiedPathInExpression::QualifiedPathInExpression (
path_type (std::move (qual_path_type)), locus (locus)
{}

QualifiedPathInExpression::QualifiedPathInExpression (
Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
LangItem::Kind lang_item, location_t locus,
std::vector<AST::Attribute> outer_attrs)
: PathPattern (lang_item),
PathExpr (std::move (mappings), std::move (outer_attrs)),
path_type (std::move (qual_path_type)), locus (locus)
{}

QualifiedPathInType::QualifiedPathInType (
Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
std::unique_ptr<TypePathSegment> associated_segment,
Expand Down
Loading
Loading