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

Desugar AST::ForLoopExprs as part of HIR lowering #2903

Draft
wants to merge 35 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d96e18a
hir: Add LangItem paths to PathPattern class
CohenArthur Dec 16, 2024
da9feed
wip: fixup? path: Switch PathPattern to enable lang item paths
CohenArthur Dec 16, 2024
363a345
path: Add get_lang_item_kind()
CohenArthur Dec 23, 2024
1616513
lang-item: Add Option::{None, Some}, Iterator::next, IntoIter::into_iter
CohenArthur Dec 16, 2024
f5af730
lang-items: Collect trait functions that are lang items
CohenArthur Dec 18, 2024
3537b25
for-loop: Progress on typechecker
CohenArthur Dec 23, 2024
5ac5845
typecheck-path: Fix typos
CohenArthur Dec 20, 2024
e6addb0
fixup! typos: reciever -> receiver
CohenArthur Dec 20, 2024
e94e755
hir-dump: Fix more segfaults in the HIR dump
CohenArthur Dec 20, 2024
a431cdb
for-loop: Setup 100% more substitutions
CohenArthur Dec 23, 2024
1f64b2f
move before rust-hir-dump: hir: Add has_{type, trait} methods to Qual…
CohenArthur Dec 20, 2024
ada7ae2
wip: Cleanup our fix to hir-path.cc
CohenArthur Dec 20, 2024
5e7685c
ast: Add new Kind enums for more precise downcasting
CohenArthur Dec 21, 2024
46264a5
wip: Base for AST for-loop desugar
CohenArthur Dec 22, 2024
4a7ae36
fixup: add onto static_dispatch
CohenArthur Dec 22, 2024
15145dd
wip: Add base for desugaring for-loops during AST
CohenArthur Dec 21, 2024
f369d2f
ast-builder: Add more methods
CohenArthur Dec 21, 2024
5d39f8e
wip: ast: PathInExpression: new constructors?
CohenArthur Dec 21, 2024
49fa7b2
session-manager: Call DesugarForLoops
CohenArthur Dec 21, 2024
c917367
for-loops: Go further!
CohenArthur Dec 24, 2024
4392b2c
for-loops: more desugar
CohenArthur Dec 23, 2024
0197e1c
lang-items: Collect enum variants as lang items
CohenArthur Dec 23, 2024
ebdbedd
wip: Adapt more visitors to LangItem PathInExprs
CohenArthur Dec 23, 2024
4a62383
lower: Lower LangItem PathInExprs properly
CohenArthur Dec 23, 2024
b2a7d6a
tychck: format
CohenArthur Dec 23, 2024
eb15aa8
lang-items: Add LangItem::IsEnumVariant
CohenArthur Dec 23, 2024
59996eb
wip: Typechecker langitem paths more better
CohenArthur Dec 23, 2024
b8e6b9a
rust-macro.h: Format
CohenArthur Dec 23, 2024
8074307
desugar?
CohenArthur Dec 23, 2024
1e2337d
wip: split me up
CohenArthur Dec 22, 2024
ae3c309
[cleanup]: path-compile: Resolve lang item paths properly
CohenArthur Dec 24, 2024
45a9d78
for-loops: Close! they're an infinite loop rn
CohenArthur Dec 24, 2024
9337460
backend: Allow everything as a match scrutinee [rework]
CohenArthur Dec 24, 2024
079736a
hack: Work around MarkLive for lang items
CohenArthur Dec 24, 2024
5874385
wip: typecheck: query type just to make sure?
CohenArthur Dec 24, 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 @@ -230,6 +230,7 @@ GRS_OBJS = \
rust/rust-expand-format-args.o \
rust/rust-lang-item.o \
rust/rust-collect-lang-items.o \
rust/rust-desugar-for-loops.o \
$(END)
# removed object files from here

Expand Down
90 changes: 90 additions & 0 deletions gcc/rust/ast/rust-ast-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "rust-ast-builder-type.h"
#include "rust-common.h"
#include "rust-expr.h"
#include "rust-path.h"
#include "rust-token.h"
#include "rust-make-unique.h"

Expand All @@ -42,6 +43,33 @@ 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
{
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::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 All @@ -56,6 +84,12 @@ Builder::identifier (std::string name) const
return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc));
}

std::unique_ptr<Pattern>
Builder::identifier_pattern (std::string name, bool mut) const
{
return std::unique_ptr<Pattern> (new IdentifierPattern (name, loc, false, mut));
}

std::unique_ptr<Expr>
Builder::tuple_idx (std::string receiver, int idx) const
{
Expand Down Expand Up @@ -117,6 +151,22 @@ Builder::path_in_expression (std::vector<std::string> &&segments) const
return PathInExpression (std::move (path_segments), {}, loc);
}

PathInExpression
Builder::path_in_expression (LangItem::Kind lang_item) const
{
return PathInExpression (lang_item, {}, loc);
}

std::unique_ptr<Expr>
Builder::block (std::unique_ptr<Stmt> &&stmt,
std::unique_ptr<Expr> &&tail_expr) const
{
auto stmts = std::vector<std::unique_ptr<Stmt>>();
stmts.emplace_back(std::move(stmt));

return block(std::move(stmts), std::move(tail_expr));
}

std::unique_ptr<Expr>
Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
std::unique_ptr<Expr> &&tail_expr) const
Expand Down Expand Up @@ -189,6 +239,46 @@ Builder::wildcard () const
return std::unique_ptr<Pattern> (new WildcardPattern (loc));
}

std::unique_ptr<Path>
Builder::lang_item_path (LangItem::Kind kind) const
{
return std::unique_ptr<Path> (new LangItemPath (kind, loc));
}

std::unique_ptr<Expr>
Builder::match (std::unique_ptr<Expr> &&scrutinee,
std::vector<MatchCase> &&cases)
{
return std::unique_ptr<Expr> (
new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc));
}

MatchArm
Builder::match_arm (std::unique_ptr<Pattern> &&pattern)
{
auto patterns = std::vector<std::unique_ptr<Pattern>> ();
patterns.emplace_back (std::move (pattern));

return MatchArm (std::move (patterns), loc);
}

MatchCase
Builder::match_case (std::unique_ptr<Pattern> &&pattern,
std::unique_ptr<Expr> &&expr)
{
return MatchCase (match_arm (std::move (pattern)), std::move (expr));
}

std::unique_ptr<Expr>
Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts)
{
auto block = std::unique_ptr<BlockExpr> (
new BlockExpr (std::move (stmts), nullptr, {}, {}, LoopLabel::error (), loc,
loc));

return std::unique_ptr<Expr> (new LoopExpr (std::move (block), loc));
}

std::unique_ptr<Type>
Builder::new_type (Type &type)
{
Expand Down
29 changes: 29 additions & 0 deletions gcc/rust/ast/rust-ast-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define AST_BUILDER_H

#include "rust-ast-full.h"
#include "rust-expr.h"

namespace Rust {
namespace AST {
Expand All @@ -38,6 +39,7 @@ class Builder

/* Create an identifier expression (`variable`) */
std::unique_ptr<Expr> identifier (std::string name) const;
std::unique_ptr<Pattern> identifier_pattern (std::string name, bool mut = false) const;

/* Create a tuple index expression (`receiver.0`) */
std::unique_ptr<Expr> tuple_idx (std::string receiver, int idx) const;
Expand All @@ -53,6 +55,9 @@ class Builder
std::unique_ptr<Expr> block (std::vector<std::unique_ptr<Stmt>> &&stmts,
std::unique_ptr<Expr> &&tail_expr
= nullptr) const;
std::unique_ptr<Expr> block (std::unique_ptr<Stmt> &&stmt,
std::unique_ptr<Expr> &&tail_expr
= nullptr) const;

/* Create a let binding with an optional type and initializer (`let <name> :
* <type> = <init>`) */
Expand All @@ -66,6 +71,12 @@ 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 Expand Up @@ -100,6 +111,11 @@ class Builder
PathInExpression
path_in_expression (std::vector<std::string> &&segments) const;

/**
* Create a path in expression from a lang item.
*/
PathInExpression path_in_expression (LangItem::Kind lang_item) const;

/* Create a struct expression for unit structs (`S`) */
std::unique_ptr<Expr> struct_expr_struct (std::string struct_name) const;

Expand All @@ -122,6 +138,19 @@ class Builder
/* Create a wildcard pattern (`_`) */
std::unique_ptr<Pattern> wildcard () const;

/* Create a lang item path usable as a general path */
std::unique_ptr<Path> lang_item_path (LangItem::Kind) const;

/* Create match expressions and their components */
std::unique_ptr<Expr> match (std::unique_ptr<Expr> &&scrutinee,
std::vector<MatchCase> &&cases);
MatchArm match_arm (std::unique_ptr<Pattern> &&pattern);
MatchCase match_case (std::unique_ptr<Pattern> &&pattern,
std::unique_ptr<Expr> &&expr);

/* Create a loop expression */
std::unique_ptr<Expr> loop (std::vector<std::unique_ptr<Stmt>> &&stmts);

static std::unique_ptr<Type> new_type (Type &type);

static std::unique_ptr<GenericParam>
Expand Down
19 changes: 17 additions & 2 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#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 @@ -553,11 +554,25 @@ TokenCollector::visit (PathExprSegment &segment)
void
TokenCollector::visit (PathInExpression &path)
{
if (path.opening_scope_resolution ())
if (path.get_path ().get_path_kind () == Path::Kind::LangItem)
{
push (Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
auto lang_item = static_cast<LangItemPath &> (path.get_path ());

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 (
lang_item.get_lang_item_kind ())));
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 @@ -100,8 +100,10 @@ void
DefaultASTVisitor::visit (AST::PathInExpression &path)
{
visit_outer_attrs (path);
for (auto &segment : path.get_segments ())
visit (segment);

if (path.get_path ().get_path_kind () != AST::Path::Kind::LangItem)
for (auto &segment : path.get_segments ())
visit (segment);
}

void
Expand Down
Loading
Loading