// Copyright (C) 2020-2024 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// .
#include "rust-ast-builder.h"
#include "rust-ast-full-decls.h"
#include "rust-ast-full.h"
#include "rust-expr.h"
#include "rust-token.h"
#include "rust-make-unique.h"
namespace Rust {
namespace AST {
std::unique_ptr
Builder::literal_string (std::string &&content) const
{
return std::unique_ptr (
new AST::LiteralExpr (std::move (content), Literal::LitType::STRING,
PrimitiveCoreType::CORETYPE_STR, {}, loc));
}
std::unique_ptr
Builder::call (std::unique_ptr &&path,
std::vector> &&args) const
{
return std::unique_ptr (
new CallExpr (std::move (path), std::move (args), {}, loc));
}
std::unique_ptr
Builder::array (std::vector> &&members) const
{
auto elts = Rust::make_unique (std::move (members), loc);
return std::unique_ptr (new ArrayExpr (std::move (elts), {}, {}, loc));
}
std::unique_ptr
Builder::identifier (std::string name) const
{
return std::unique_ptr (new IdentifierExpr (name, {}, loc));
}
std::unique_ptr
Builder::tuple_idx (std::string receiver, int idx) const
{
return std::unique_ptr (
new TupleIndexExpr (identifier (receiver), idx, {}, loc));
}
FunctionQualifiers
Builder::fn_qualifiers () const
{
return FunctionQualifiers (loc, Async::No, Const::No, Unsafety::Normal);
}
PathExprSegment
Builder::path_segment (std::string seg) const
{
return PathExprSegment (PathIdentSegment (seg, loc), loc);
}
std::unique_ptr
Builder::type_path_segment (std::string seg) const
{
return std::unique_ptr (
new TypePathSegment (seg, false, loc));
}
std::unique_ptr
Builder::single_type_path (std::string type) const
{
auto segments = std::vector> ();
segments.emplace_back (type_path_segment (type));
return std::unique_ptr (new TypePath (std::move (segments), loc));
}
PathInExpression
Builder::path_in_expression (std::vector &&segments) const
{
auto path_segments = std::vector ();
for (auto &seg : segments)
path_segments.emplace_back (path_segment (seg));
return PathInExpression (std::move (path_segments), {}, loc);
}
std::unique_ptr
Builder::block (std::vector> &&stmts,
std::unique_ptr &&tail_expr) const
{
return std::unique_ptr (new BlockExpr (std::move (stmts),
std::move (tail_expr), {}, {},
LoopLabel::error (), loc, loc));
}
std::unique_ptr
Builder::let (std::unique_ptr pattern, std::unique_ptr type,
std::unique_ptr init) const
{
return std::unique_ptr (new LetStmt (std::move (pattern),
std::move (init), std::move (type),
{}, loc));
}
std::unique_ptr
Builder::ref (std::unique_ptr &&of, bool mut) const
{
return std::unique_ptr (
new BorrowExpr (std::move (of), mut, /* is double */ false, {}, loc));
}
std::unique_ptr
Builder::deref (std::unique_ptr &&of) const
{
return std::unique_ptr (new DereferenceExpr (std::move (of), {}, loc));
}
std::unique_ptr
Builder::struct_expr_struct (std::string struct_name) const
{
return std::unique_ptr (
new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc));
}
std::unique_ptr
Builder::struct_expr (
std::string struct_name,
std::vector> &&fields) const
{
return std::unique_ptr (
new StructExprStructFields (path_in_expression ({struct_name}),
std::move (fields), loc));
}
std::unique_ptr
Builder::struct_expr_field (std::string field_name,
std::unique_ptr &&value) const
{
return std::unique_ptr (
new StructExprFieldIdentifierValue (field_name, std::move (value), loc));
}
std::unique_ptr
Builder::field_access (std::unique_ptr &&instance,
std::string field) const
{
return std::unique_ptr (
new FieldAccessExpr (std::move (instance), field, {}, loc));
}
std::unique_ptr
Builder::wildcard () const
{
return std::unique_ptr (new WildcardPattern (loc));
}
} // namespace AST
} // namespace Rust