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

Finish derive(Clone) for enums #3343

Merged
merged 15 commits into from
Jan 20, 2025
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 80 additions & 4 deletions gcc/rust/ast/rust-ast-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

#include "rust-ast-builder.h"
#include "rust-ast-builder-type.h"
#include "rust-ast.h"
#include "rust-common.h"
#include "rust-expr.h"
#include "rust-path.h"
#include "rust-item.h"
#include "rust-path.h"
#include "rust-system.h"
#include "rust-token.h"

namespace Rust {
Expand Down Expand Up @@ -99,12 +103,27 @@ Builder::type_path_segment (std::string seg) const
}

std::unique_ptr<TypePathSegment>
Builder::generic_type_path_segment (std::string seg, GenericArgs args) const
Builder::type_path_segment (LangItem::Kind lang_item) const
{
return std::unique_ptr<TypePathSegment> (
new TypePathSegment (lang_item, loc));
}

std::unique_ptr<TypePathSegment>
Builder::type_path_segment_generic (std::string seg, GenericArgs args) const
{
return std::unique_ptr<TypePathSegment> (
new TypePathSegmentGeneric (PathIdentSegment (seg, loc), false, args, loc));
}

std::unique_ptr<TypePathSegment>
Builder::type_path_segment_generic (LangItem::Kind lang_item,
GenericArgs args) const
{
return std::unique_ptr<TypePathSegment> (
new TypePathSegmentGeneric (lang_item, args, loc));
}

std::unique_ptr<Type>
Builder::single_type_path (std::string type) const
{
Expand All @@ -114,15 +133,52 @@ Builder::single_type_path (std::string type) const
return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
}

std::unique_ptr<Type>
Builder::single_type_path (LangItem::Kind lang_item) const
{
return std::unique_ptr<Type> (new TypePath (lang_item, {}, loc));
}

std::unique_ptr<Type>
Builder::single_generic_type_path (std::string type, GenericArgs args) const
{
auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
segments.emplace_back (generic_type_path_segment (type, args));
segments.emplace_back (type_path_segment_generic (type, args));

return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
}

std::unique_ptr<Type>
Builder::single_generic_type_path (LangItem::Kind lang_item,
GenericArgs args) const
{
auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
segments.emplace_back (type_path_segment_generic (lang_item, args));

return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
}

TypePath
Builder::type_path (std::unique_ptr<TypePathSegment> &&segment) const
{
auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
segments.emplace_back (std::move (segment));

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

TypePath
Builder::type_path (std::string type) const
{
return type_path (type_path_segment (type));
}

TypePath
Builder::type_path (LangItem::Kind lang_item) const
{
return type_path (type_path_segment (lang_item));
}

PathInExpression
Builder::path_in_expression (std::vector<std::string> &&segments) const
{
Expand Down Expand Up @@ -182,6 +238,19 @@ Builder::deref (std::unique_ptr<Expr> &&of) const
return std::unique_ptr<Expr> (new DereferenceExpr (std::move (of), {}, loc));
}

std::unique_ptr<Stmt>
Builder::struct_struct (std::string struct_name,
std::vector<std::unique_ptr<GenericParam>> &&generics,
std::vector<StructField> &&fields)
{
auto is_unit = fields.empty ();

return std::unique_ptr<Stmt> (
new StructStruct (std::move (fields), struct_name, std::move (generics),
WhereClause::create_empty (), is_unit,
Visibility::create_private (), {}, loc));
}

std::unique_ptr<Expr>
Builder::struct_expr_struct (std::string struct_name) const
{
Expand All @@ -193,10 +262,17 @@ std::unique_ptr<Expr>
Builder::struct_expr (
std::string struct_name,
std::vector<std::unique_ptr<StructExprField>> &&fields) const
{
return struct_expr (path_in_expression ({struct_name}), std::move (fields));
}

std::unique_ptr<Expr>
Builder::struct_expr (
PathInExpression struct_name,
std::vector<std::unique_ptr<StructExprField>> &&fields) const
{
return std::unique_ptr<Expr> (
new StructExprStructFields (path_in_expression ({struct_name}),
std::move (fields), loc));
new StructExprStructFields (struct_name, std::move (fields), loc));
}

std::unique_ptr<StructExprField>
Expand Down
49 changes: 48 additions & 1 deletion gcc/rust/ast/rust-ast-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,35 @@

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

namespace Rust {
namespace AST {

template <typename T>
std::vector<std::unique_ptr<T>>
vec (std::unique_ptr<T> &&t)
{
auto v = std::vector<std::unique_ptr<T>> ();

v.emplace_back (std::move (t));

return v;
}

template <typename T>
std::vector<std::unique_ptr<T>>
vec (std::unique_ptr<T> &&t1, std::unique_ptr<T> &&t2)
{
auto v = std::vector<std::unique_ptr<T>> ();

v.emplace_back (std::move (t1));
v.emplace_back (std::move (t2));

return v;
}
Comment on lines +30 to +51
Copy link
Member Author

Choose a reason for hiding this comment

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

this should probably be just one variadic function?


// TODO: Use this builder when expanding regular macros
/* Builder class with helper methods to create AST nodes. This builder is
* tailored towards generating multiple AST nodes from a single location, and
Expand Down Expand Up @@ -89,16 +114,27 @@ class Builder

/* And similarly for type path segments */
std::unique_ptr<TypePathSegment> type_path_segment (std::string seg) const;
std::unique_ptr<TypePathSegment>
type_path_segment (LangItem::Kind lang_item) const;

std::unique_ptr<TypePathSegment>
generic_type_path_segment (std::string seg, GenericArgs args) const;
type_path_segment_generic (std::string seg, GenericArgs args) const;
std::unique_ptr<TypePathSegment>
type_path_segment_generic (LangItem::Kind lang_item, GenericArgs args) const;

/* Create a Type from a single string - the most basic kind of type in our AST
*/
std::unique_ptr<Type> single_type_path (std::string type) const;
std::unique_ptr<Type> single_type_path (LangItem::Kind lang_item) const;

std::unique_ptr<Type> single_generic_type_path (std::string type,
GenericArgs args) const;
std::unique_ptr<Type> single_generic_type_path (LangItem::Kind lang_item,
GenericArgs args) const;

TypePath type_path (std::unique_ptr<TypePathSegment> &&segment) const;
TypePath type_path (std::string type) const;
TypePath type_path (LangItem::Kind lang_item) const;

/**
* Create a path in expression from multiple segments (`Clone::clone`). You
Expand All @@ -113,15 +149,26 @@ class Builder
*/
PathInExpression path_in_expression (LangItem::Kind lang_item) const;

/* Create a new struct */
std::unique_ptr<Stmt>
struct_struct (std::string struct_name,
std::vector<std::unique_ptr<GenericParam>> &&generics,
std::vector<StructField> &&fields);

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

/**
* Create an expression for struct instantiation with fields (`S { a, b: c }`)
* Tuple expressions are call expressions and can thus be constructed with
* `call`
*/
std::unique_ptr<Expr>
struct_expr (std::string struct_name,
std::vector<std::unique_ptr<StructExprField>> &&fields) const;
std::unique_ptr<Expr>
struct_expr (PathInExpression struct_name,
std::vector<std::unique_ptr<StructExprField>> &&fields) const;

/* Create a field expression for struct instantiation (`field_name: value`) */
std::unique_ptr<StructExprField>
Expand Down
Loading
Loading