Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions contrib/gcc-changelog/git_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
'gcc/testsuite/go.test/test/',
'libffi/',
'libgo/',
'libgrust/libcore/',
'libphobos/libdruntime/',
'libphobos/src/',
'libsanitizer/',
Expand Down
11 changes: 1 addition & 10 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,8 @@ GRS_OBJS = \
rust/rust-ice-finalizer.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 \
rust/rust-ast-resolve-base.o \
rust/rust-ast-resolve-item.o \
rust/rust-ast-resolve-pattern.o \
rust/rust-ast-resolve-expr.o \
rust/rust-ast-resolve-type.o \
rust/rust-ast-resolve-path.o \
rust/rust-ast-resolve-stmt.o \
rust/rust-ast-resolve-struct-expr-field.o \
rust/rust-resolve-builtins.o \
rust/rust-forever-stack.o \
rust/rust-hir-type-check.o \
rust/rust-privacy-check.o \
Expand Down
84 changes: 83 additions & 1 deletion gcc/rust/ast/rust-ast-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,63 @@ Builder::type_path (LangItem::Kind lang_item) const
return type_path (type_path_segment (lang_item));
}

TypePath
Builder::type_path_core (std::vector<std::unique_ptr<TypePathSegment>> &&segments) const
{
bool opening_scope;

auto &mappings = Analysis::Mappings::get ();
if (mappings.get_current_crate_name () == "core")
{
segments.emplace (segments.cbegin (), type_path_segment ("crate"));
opening_scope = false;
}
else
{
segments.emplace (segments.cbegin (), type_path_segment ("core"));
opening_scope = true;
}

return TypePath (std::move (segments), loc, opening_scope);
}

TypePath
Builder::type_path_core (std::vector<std::string> &&segments) const
{
auto type_segments = std::vector<std::unique_ptr<TypePathSegment>> ();
type_segments.reserve (segments.size () + 1);
bool opening_scope;

auto &mappings = Analysis::Mappings::get ();
if (mappings.get_current_crate_name () == "core")
{
type_segments.emplace_back (type_path_segment ("crate"));
opening_scope = false;
}
else
{
type_segments.emplace_back (type_path_segment ("core"));
opening_scope = true;
}

for (auto &&seg : segments)
type_segments.emplace_back (type_path_segment (seg));

return TypePath (std::move (type_segments), loc, opening_scope);
}

TypePath
Builder::type_path_core (std::initializer_list<const char *> segments) const
{
std::vector<std::string> segments_out;
segments_out.reserve (segments.size ());

for (auto seg : segments)
segments_out.emplace_back (seg);

return type_path_core (std::move (segments_out));
}

std::unique_ptr<Type>
Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
bool mutability) const
Expand All @@ -301,6 +358,31 @@ Builder::path_in_expression (LangItem::Kind lang_item) const
return PathInExpression (lang_item, {}, loc);
}

PathInExpression
Builder::path_in_expression_core (std::vector<std::string> &&segments) const
{
auto path_segments = std::vector<PathExprSegment> ();
path_segments.reserve (segments.size () + 1);
bool opening_scope;

auto &mappings = Analysis::Mappings::get ();
if (mappings.get_current_crate_name () == "core")
{
path_segments.emplace_back (path_segment ("crate"));
opening_scope = false;
}
else
{
path_segments.emplace_back (path_segment ("core"));
opening_scope = true;
}

for (auto &&seg : segments)
path_segments.emplace_back (path_segment (seg));

return PathInExpression (std::move (path_segments), {}, loc, opening_scope);
}

PathInExpression
Builder::variant_path (const std::string &enum_path,
const std::string &variant) const
Expand Down Expand Up @@ -544,7 +626,7 @@ std::unique_ptr<Stmt>
Builder::discriminant_value (std::string binding_name, std::string instance)
{
auto intrinsic = ptrify (
path_in_expression ({"core", "intrinsics", "discriminant_value"}, true));
path_in_expression_core ({"intrinsics", "discriminant_value"}));

return let (identifier_pattern (binding_name), nullptr,
call (std::move (intrinsic), identifier (instance)));
Expand Down
19 changes: 14 additions & 5 deletions gcc/rust/ast/rust-ast-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ class Builder
TypePath type_path (std::string type) const;
TypePath type_path (LangItem::Kind lang_item) const;

/**
* Creates a type path relative to libcore
* Either `::core...` or `crate...`
*/
TypePath type_path_core (std::vector<std::unique_ptr<TypePathSegment>> &&segments) const;
TypePath type_path_core (std::vector<std::string> &&segments) const;
TypePath type_path_core (std::initializer_list<const char *> segments) const;

std::unique_ptr<Type>
reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
bool mutability = false) const;
Expand All @@ -237,6 +245,12 @@ class Builder
*/
PathInExpression path_in_expression (LangItem::Kind lang_item) const;

/**
* Create a path in expression relative to libcore
* Either `::core...` or `crate...`
*/
PathInExpression path_in_expression_core (std::vector<std::string> &&segments) const;

/* Create the path to an enum's variant (`Result::Ok`) */
PathInExpression variant_path (const std::string &enum_path,
const std::string &variant) const;
Expand Down Expand Up @@ -331,11 +345,6 @@ class Builder

/* Location of the generated AST nodes */
location_t loc;

private:
/* Some constexpr helpers for some of the builders */
static constexpr std::initializer_list<const char *> discriminant_value_path
= {"core", "intrinsics", "discriminant_value"};
};

} // namespace AST
Expand Down
7 changes: 7 additions & 0 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,13 @@ TokenCollector::visit (AST::FormatArgs &fmt)
__FILE__, __LINE__);
}

void
TokenCollector::visit (AST::FormatArgsEager &fmt)
{
rust_sorry_at (fmt.get_locus (), "%s:%u: unimplemented FormatArgsEager visitor",
__FILE__, __LINE__);
}

void
TokenCollector::visit (AST::OffsetOf &offset_of)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ class TokenCollector : public ASTVisitor
void visit (BareFunctionType &type);

void visit (FormatArgs &fmt);
void visit (FormatArgsEager &fmt);
void visit (OffsetOf &offset_of);
};
} // namespace AST
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ class BareFunctionType;

// rust-builtin-ast-nodes.h
class FormatArgs;
class FormatArgsEager;
} // namespace AST
} // namespace Rust

Expand Down
9 changes: 9 additions & 0 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,15 @@ DefaultASTVisitor::visit (AST::FormatArgs &)
// FIXME: Do we have anything to do? any subnodes to visit? Probably, right?
}

void
DefaultASTVisitor::visit (AST::FormatArgsEager &fmt)
{
// FIXME: Do we have anything to do? any subnodes to visit? Probably, right?

// we need this to resolve/expand macros, at least
visit (fmt.get_template ());
}

void
DefaultASTVisitor::visit (AST::OffsetOf &offset_of)
{
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class ASTVisitor

// special AST nodes for certain builtin macros such as `asm!()`
virtual void visit (FormatArgs &fmt) = 0;
virtual void visit (FormatArgsEager &fmt) = 0;
virtual void visit (OffsetOf &fmt) = 0;

// TODO: rust-cond-compilation.h visiting? not currently used
Expand Down Expand Up @@ -413,6 +414,7 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::FunctionParam &param) override;
virtual void visit (AST::VariadicParam &param) override;
virtual void visit (AST::FormatArgs &fmt) override;
virtual void visit (AST::FormatArgsEager &fmt) override;
virtual void visit (AST::OffsetOf &fmt) override;

template <typename T> void visit (T &node) { node.accept_vis (*this); }
Expand Down
53 changes: 53 additions & 0 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5054,6 +5054,59 @@ FormatArgs::clone_expr_impl () const
return new FormatArgs (*this);
}

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

std::string
FormatArgsEager::as_string () const
{
// FIXME(Arthur): Improve
return "FormatArgsEager";
}

bool
FormatArgsEager::is_expr_without_block () const
{
return false;
}

void
FormatArgsEager::mark_for_strip ()
{
marked_for_strip = true;
}

bool
FormatArgsEager::is_marked_for_strip () const
{
return marked_for_strip;
}

std::vector<Attribute> &
FormatArgsEager::get_outer_attrs ()
{
rust_unreachable ();
}

void
FormatArgsEager::set_outer_attrs (std::vector<Attribute>)
{
rust_unreachable ();
}

FormatArgsEager *
FormatArgsEager::clone_expr_impl () const
{
// based on FormatArgs::clone_expr_impl
// TODO: should this be happening?
rust_debug_loc (get_locus (), "[ARTHUR/OWEN] cloning FormatArgsEager!");

return new FormatArgsEager (*this);
}

std::vector<Attribute> &
OffsetOf::get_outer_attrs ()
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ class Expr : public Visitable
LlvmInlineAsm,
Identifier,
FormatArgs,
FormatArgsEager,
OffsetOf,
MacroInvocation,
Borrow,
Expand Down
64 changes: 64 additions & 0 deletions gcc/rust/ast/rust-builtin-ast-nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,70 @@ class FormatArgs : public Expr
virtual Expr *clone_expr_impl () const override;
};

// a FormatArgs which hasn't had its first argument parsed yet
// used to handle eager expansion
class FormatArgsEager final : public Expr
{
public:
using Newline = FormatArgs::Newline;

FormatArgsEager (location_t loc, std::unique_ptr<Expr> &&template_expr,
FormatArguments &&arguments, Newline newline)
: loc (loc), template_expr (std::move (template_expr)),
arguments (std::move (arguments)), newline (newline)
{}

FormatArgsEager (const FormatArgsEager &other): Expr (other), loc (other.loc), template_expr (other.template_expr->clone_expr ()), arguments (other.arguments), newline (other.newline) {}

FormatArgsEager &operator= (const FormatArgsEager &other)
{
Expr::operator= (other);
loc = other.loc;
template_expr = other.template_expr->clone_expr ();
arguments = other.arguments;
newline = other.newline;
return *this;
}

FormatArgsEager (FormatArgsEager &&other) = default;
FormatArgsEager &operator= (FormatArgsEager &&other) = default;

void accept_vis (AST::ASTVisitor &vis) override;

const Expr &get_template () const { return *template_expr; }

Expr &get_template () { return *template_expr; }

std::unique_ptr<Expr> &get_template_ptr () { return template_expr; }

const FormatArguments &get_arguments () const { return arguments; }

FormatArguments &get_arguments () { return arguments; }

virtual location_t get_locus () const override { return loc; }

Newline get_newline () const { return newline; }

Expr::Kind get_expr_kind () const override { return Expr::Kind::FormatArgsEager; }

private:
location_t loc;
std::unique_ptr<Expr> template_expr;
FormatArguments arguments;
Newline newline;

bool marked_for_strip = false;

protected:
virtual std::string as_string () const override;
virtual bool is_expr_without_block () const override;
virtual void mark_for_strip () override;
virtual bool is_marked_for_strip () const override;
virtual std::vector<Attribute> &get_outer_attrs () override;
virtual void set_outer_attrs (std::vector<Attribute>) override;
virtual FormatArgsEager *clone_expr_impl () const override;
};

/**
* The node associated with the builtin offset_of!() macro
*/
Expand Down
Loading
Loading