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

Refactor lang item paths #3366

Merged
merged 11 commits into from
Jan 16, 2025
20 changes: 1 addition & 19 deletions gcc/rust/ast/rust-ast-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ Builder::call (std::unique_ptr<Expr> &&path,
new CallExpr (std::move (path), std::move (args), {}, loc));
}

std::unique_ptr<Expr>
Builder::call (std::unique_ptr<Path> &&path,
std::vector<std::unique_ptr<Expr>> &&args) const
{
return call (std::unique_ptr<Expr> (
new PathInExpression (std::move (path), {}, loc)),
std::move (args));
}

std::unique_ptr<Expr>
Builder::call (std::unique_ptr<Expr> &&path, std::unique_ptr<Expr> &&arg) const
{
Expand All @@ -60,15 +51,6 @@ Builder::call (std::unique_ptr<Expr> &&path, std::unique_ptr<Expr> &&arg) const
return call (std::move (path), std::move (args));
}

std::unique_ptr<Expr>
Builder::call (std::unique_ptr<Path> &&path, std::unique_ptr<Expr> &&arg) const
{
auto args = std::vector<std::unique_ptr<Expr>> ();
args.emplace_back (std::move (arg));

return call (std::move (path), std::move (args));
}

std::unique_ptr<Expr>
Builder::array (std::vector<std::unique_ptr<Expr>> &&members) const
{
Expand Down Expand Up @@ -242,7 +224,7 @@ Builder::wildcard () const
std::unique_ptr<Path>
Builder::lang_item_path (LangItem::Kind kind) const
{
return std::unique_ptr<Path> (new LangItemPath (kind, loc));
return std::unique_ptr<Path> (new PathInExpression (kind, {}, loc));
}

std::unique_ptr<Expr>
Expand Down
4 changes: 0 additions & 4 deletions gcc/rust/ast/rust-ast-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ class Builder
*/
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
std::vector<std::unique_ptr<Expr>> &&args) const;
std::unique_ptr<Expr> call (std::unique_ptr<Path> &&path,
std::vector<std::unique_ptr<Expr>> &&args) const;
std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
std::unique_ptr<Expr> &&arg) const;
std::unique_ptr<Expr> call (std::unique_ptr<Path> &&path,
std::unique_ptr<Expr> &&arg) const;

/**
* Create an array expression (`[member0, member1, member2]`)
Expand Down
41 changes: 16 additions & 25 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -538,28 +538,19 @@ TokenCollector::visit (PathInExpression &path)
visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
}

void
TokenCollector::visit (RegularPath &path)
{
// FIXME: We probably want to have a proper implementation here, and call this
// function from things like the PathInExpression visitor
}

void
TokenCollector::visit (LangItemPath &path)
{
// TODO: Implement proper token collection for lang item paths
}

void
TokenCollector::visit (TypePathSegment &segment)
{
// Syntax:
// PathIdentSegment
auto ident_segment = segment.get_ident_segment ();
auto id = ident_segment.as_string ();
push (
Rust::Token::make_identifier (ident_segment.get_locus (), std::move (id)));

auto locus = segment.is_lang_item ()
? segment.get_locus ()
: segment.get_ident_segment ().get_locus ();
auto segment_string = segment.is_lang_item ()
? LangItem::PrettyString (segment.get_lang_item ())
: segment.get_ident_segment ().as_string ();
push (Rust::Token::make_identifier (locus, std::move (segment_string)));
}

void
Expand All @@ -571,10 +562,13 @@ TokenCollector::visit (TypePathSegmentGeneric &segment)
// `<` `>`
// | `<` ( GenericArg `,` )* GenericArg `,`? `>`

auto ident_segment = segment.get_ident_segment ();
auto id = ident_segment.as_string ();
push (
Rust::Token::make_identifier (ident_segment.get_locus (), std::move (id)));
auto locus = segment.is_lang_item ()
? segment.get_locus ()
: segment.get_ident_segment ().get_locus ();
auto segment_string = segment.is_lang_item ()
? LangItem::PrettyString (segment.get_lang_item ())
: segment.get_ident_segment ().as_string ();
push (Rust::Token::make_identifier (locus, std::move (segment_string)));

if (segment.get_separating_scope_resolution ())
push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));
Expand Down Expand Up @@ -2476,10 +2470,7 @@ TokenCollector::visit (StructPattern &pattern)
void
TokenCollector::visit (TupleStructItemsNoRange &pattern)
{
for (auto &pat : pattern.get_patterns ())
{
visit (pat);
}
visit_items_joined_by_separator (pattern.get_patterns ());
}

void
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ class TokenCollector : public ASTVisitor
void visit (PathExprSegment &segment);
void visit (PathIdentSegment &segment);
void visit (PathInExpression &path);
void visit (RegularPath &path);
void visit (LangItemPath &path);
void visit (TypePathSegment &segment);
void visit (TypePathSegmentGeneric &segment);
void visit (TypePathSegmentFunction &segment);
Expand Down
11 changes: 0 additions & 11 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@ DefaultASTVisitor::visit (AST::ConstGenericParam &const_param)
visit (const_param.get_default_value ());
}

void
DefaultASTVisitor::visit (AST::RegularPath &path)
{
for (auto &segment : path.get_segments ())
visit (segment);
}

void
DefaultASTVisitor::visit (AST::LangItemPath &path)
{}

void
DefaultASTVisitor::visit (AST::PathInExpression &path)
{
Expand Down
4 changes: 0 additions & 4 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class ASTVisitor
// virtual void visit(TraitImplItem& trait_impl_item) = 0;

// rust-path.h
virtual void visit (RegularPath &path) = 0;
virtual void visit (LangItemPath &path) = 0;
virtual void visit (PathInExpression &path) = 0;
virtual void visit (TypePathSegment &segment) = 0;
virtual void visit (TypePathSegmentGeneric &segment) = 0;
Expand Down Expand Up @@ -252,8 +250,6 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::Lifetime &lifetime) override;
virtual void visit (AST::LifetimeParam &lifetime_param) override;
virtual void visit (AST::ConstGenericParam &const_param) override;
virtual void visit (AST::RegularPath &path) override;
virtual void visit (AST::LangItemPath &path) override;
virtual void visit (AST::PathInExpression &path) override;
virtual void visit (AST::TypePathSegment &segment) override;
virtual void visit (AST::TypePathSegmentGeneric &segment) override;
Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ TraitImpl::as_string () const
else
str += "false";

str += "\n TypePath (to trait): " + trait_path->as_string ();
str += "\n TypePath (to trait): " + trait_path.as_string ();

str += "\n Type (struct to impl on): " + trait_type->as_string ();

Expand Down Expand Up @@ -1561,7 +1561,7 @@ QualifiedPathType::as_string () const
str += type_to_invoke_on->as_string ();

if (has_as_clause ())
str += " as " + trait_path->as_string ();
str += " as " + trait_path.as_string ();

return str + ">";
}
Expand Down Expand Up @@ -4270,7 +4270,7 @@ BlockExpr::normalize_tail_expr ()

if (!stmt.is_semicolon_followed ())
{
expr = std::move (stmt.take_expr ());
expr = stmt.take_expr ();
statements.pop_back ();
}
}
Expand Down
12 changes: 11 additions & 1 deletion gcc/rust/ast/rust-collect-lang-items.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rust-attribute-values.h"
#include "rust-attributes.h"
#include "rust-hir-map.h"
#include "rust-item.h"

namespace Rust {
namespace AST {
Expand All @@ -36,7 +37,8 @@ get_lang_item_attr (const T &maybe_lang_item)
const auto &str_path = attr.get_path ().as_string ();
if (!Analysis::Attributes::is_known (str_path))
{
rust_error_at (attr.get_locus (), "unknown attribute");
rust_error_at (attr.get_locus (), "unknown attribute %qs",
str_path.c_str ());
continue;
}

Expand Down Expand Up @@ -90,5 +92,13 @@ CollectLangItems::visit (AST::Function &item)
DefaultASTVisitor::visit (item);
}

void
CollectLangItems::visit (AST::StructStruct &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 @@ -48,6 +48,7 @@ class CollectLangItems : public DefaultASTVisitor
void visit (AST::Trait &item) override;
void visit (AST::TraitItemType &item) override;
void visit (AST::Function &item) override;
void visit (AST::StructStruct &item) override;

private:
template <typename T> void maybe_add_lang_item (const T &item);
Expand Down
40 changes: 7 additions & 33 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,7 @@ class TraitImpl : public Impl
{
bool has_unsafe;
bool has_exclam;
std::unique_ptr<Path> trait_path;
TypePath trait_path;

// bool has_impl_items;
std::vector<std::unique_ptr<AssociatedItem>> impl_items;
Expand All @@ -3266,7 +3266,7 @@ class TraitImpl : public Impl
bool has_impl_items () const { return !impl_items.empty (); }

// Mega-constructor
TraitImpl (std::unique_ptr<Path> trait_path, bool is_unsafe, bool has_exclam,
TraitImpl (TypePath trait_path, bool is_unsafe, bool has_exclam,
std::vector<std::unique_ptr<AssociatedItem>> impl_items,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> trait_type, WhereClause where_clause,
Expand All @@ -3275,29 +3275,14 @@ class TraitImpl : public Impl
: Impl (std::move (generic_params), std::move (trait_type),
std::move (where_clause), std::move (vis), std::move (inner_attrs),
std::move (outer_attrs), locus),
has_unsafe (is_unsafe), has_exclam (has_exclam),
trait_path (std::move (trait_path)), impl_items (std::move (impl_items))
{}

// Delegating constructor for TypePath
TraitImpl (TypePath trait_path, bool is_unsafe, bool has_exclam,
std::vector<std::unique_ptr<AssociatedItem>> impl_items,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> trait_type, WhereClause where_clause,
Visibility vis, std::vector<Attribute> inner_attrs,
std::vector<Attribute> outer_attrs, location_t locus)
: TraitImpl (std::unique_ptr<Path> (new TypePath (trait_path)), is_unsafe,
has_exclam, std::move (impl_items), std::move (generic_params),
std::move (trait_type), std::move (where_clause),
std::move (vis), std::move (inner_attrs),
std::move (outer_attrs), locus)
has_unsafe (is_unsafe), has_exclam (has_exclam), trait_path (trait_path),
impl_items (std::move (impl_items))
{}

// Copy constructor with vector clone
TraitImpl (TraitImpl const &other)
: Impl (other), has_unsafe (other.has_unsafe),
has_exclam (other.has_exclam),
trait_path (other.trait_path->clone_path ())
has_exclam (other.has_exclam), trait_path (other.trait_path)
{
impl_items.reserve (other.impl_items.size ());
for (const auto &e : other.impl_items)
Expand All @@ -3308,7 +3293,7 @@ class TraitImpl : public Impl
TraitImpl &operator= (TraitImpl const &other)
{
Impl::operator= (other);
trait_path = other.trait_path->clone_path ();
trait_path = other.trait_path;
has_unsafe = other.has_unsafe;
has_exclam = other.has_exclam;

Expand Down Expand Up @@ -3339,18 +3324,7 @@ class TraitImpl : public Impl
}

// TODO: is this better? Or is a "vis_block" better?
Path &get_trait_path ()
{
// TODO: assert that trait path is not empty?
return *trait_path;
}

Type &get_trait_path_type ()
{
rust_assert (trait_path->get_path_kind () == Path::Kind::Type);

return (AST::Type &) static_cast<AST::TypePath &> (*trait_path);
}
TypePath &get_trait_path () { return trait_path; }

protected:
/* Use covariance to implement clone function as returning this object
Expand Down
31 changes: 9 additions & 22 deletions gcc/rust/ast/rust-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ PathExprSegment::as_string () const
}

std::string
RegularPath::as_string () const
Path::as_string () const
{
// FIXME: Impl for lang items
rust_assert (kind == Kind::Regular);

std::string str;

for (const auto &segment : segments)
Expand All @@ -149,15 +152,11 @@ RegularPath::as_string () const
return str;
}

std::string
LangItemPath::as_string () const
{
return "#[lang = \"" + LangItem::ToString (kind) + "\"]";
}

SimplePath
RegularPath::convert_to_simple_path (bool with_opening_scope_resolution) const
Path::convert_to_simple_path (bool with_opening_scope_resolution) const
{
rust_assert (kind == Kind::Regular);

if (!has_segments ())
return SimplePath::create_empty ();

Expand Down Expand Up @@ -190,18 +189,6 @@ RegularPath::convert_to_simple_path (bool with_opening_scope_resolution) const
locus);
}

void
RegularPath::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
}

void
LangItemPath::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
}

void
PathInExpression::accept_vis (ASTVisitor &vis)
{
Expand All @@ -216,7 +203,7 @@ PathInExpression::as_string () const
if (has_opening_scope_resolution)
str = "::";

return str + path->as_string ();
return str + Path::as_string ();
}

std::string
Expand Down Expand Up @@ -316,7 +303,7 @@ TypePathFunction::as_string () const
std::string
QualifiedPathInExpression::as_string () const
{
return path_type.as_string () + "::" + path->as_string ();
return path_type.as_string () + "::" + Path::as_string ();
}

std::string
Expand Down
Loading
Loading