--- /dev/null
+// Copyright (C) 2020-2023 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
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-ast-builder.h"
+#include "rust-ast-full.h"
+
+namespace Rust {
+namespace AST {
+
+std::unique_ptr<Expr>
+AstBuilder::call (std::unique_ptr<Expr> &&path,
+ std::vector<std::unique_ptr<Expr>> &&args)
+{
+ return std::unique_ptr<Expr> (
+ new CallExpr (std::move (path), std::move (args), {}, loc));
+}
+
+std::unique_ptr<Expr>
+AstBuilder::identifier (std::string name)
+{
+ return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc));
+}
+
+std::unique_ptr<Expr>
+AstBuilder::tuple_idx (std::string receiver, int idx)
+{
+ return std::unique_ptr<Expr> (
+ new TupleIndexExpr (identifier (receiver), idx, {}, loc));
+}
+
+FunctionQualifiers
+AstBuilder::fn_qualifiers ()
+{
+ return FunctionQualifiers (loc, AsyncConstStatus::NONE, false);
+}
+
+PathExprSegment
+AstBuilder::path_segment (std::string seg)
+{
+ return PathExprSegment (PathIdentSegment (seg, loc), loc);
+}
+
+std::unique_ptr<TypePathSegment>
+AstBuilder::type_path_segment (std::string seg)
+{
+ return std::unique_ptr<TypePathSegment> (
+ new TypePathSegment (seg, false, loc));
+}
+
+std::unique_ptr<Type>
+AstBuilder::single_type_path (std::string type)
+{
+ auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
+ segments.emplace_back (type_path_segment (type));
+
+ return std::unique_ptr<Type> (new TypePath (std::move (segments), loc));
+}
+
+PathInExpression
+AstBuilder::path_in_expression (std::vector<std::string> &&segments)
+{
+ auto path_segments = std::vector<PathExprSegment> ();
+ for (auto &seg : segments)
+ path_segments.emplace_back (path_segment (seg));
+
+ return PathInExpression (std::move (path_segments), {}, loc);
+}
+
+std::unique_ptr<Expr>
+AstBuilder::struct_expr_struct (std::string struct_name)
+{
+ return std::unique_ptr<Expr> (
+ new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc));
+}
+
+std::unique_ptr<Expr>
+AstBuilder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
+ std::unique_ptr<Expr> &&tail_expr)
+{
+ return std::unique_ptr<Expr> (
+ new BlockExpr (std::move (stmts), std::move (tail_expr), {}, {}, loc, loc));
+}
+
+std::unique_ptr<Stmt>
+AstBuilder::let (std::unique_ptr<Pattern> pattern, std::unique_ptr<Type> type,
+ std::unique_ptr<Expr> init)
+{
+ return std::unique_ptr<Stmt> (
+ new LetStmt (/* needs a pattern here, not just a name */ nullptr,
+ std::move (init), std::move (type), {}, loc));
+}
+
+std::unique_ptr<Expr>
+AstBuilder::ref (std::unique_ptr<Expr> &&of, bool mut)
+{
+ return std::unique_ptr<Expr> (
+ new BorrowExpr (std::move (of), mut, /* is double */ false, {}, loc));
+}
+
+std::unique_ptr<Expr>
+AstBuilder::deref (std::unique_ptr<Expr> &&of)
+{
+ return std::unique_ptr<Expr> (new DereferenceExpr (std::move (of), {}, loc));
+}
+
+} // namespace AST
+} // namespace Rust
--- /dev/null
+// Copyright (C) 2020-2023 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
+// <http://www.gnu.org/licenses/>.
+
+#ifndef AST_BUILDER_H
+#define AST_BUILDER_H
+
+#include "rust-ast-full.h"
+
+namespace Rust {
+namespace AST {
+
+// 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
+ * may not be suitable to other purposes */
+class AstBuilder
+{
+public:
+ AstBuilder (Location loc) : loc (loc) {}
+
+ /* Create an identifier expression (`variable`) */
+ std::unique_ptr<Expr> identifier (std::string name);
+
+ /* Create a tuple index expression (`receiver.0`) */
+ std::unique_ptr<Expr> tuple_idx (std::string receiver, int idx);
+
+ /* Create a reference to an expression (`&of`) */
+ std::unique_ptr<Expr> ref (std::unique_ptr<Expr> &&of, bool mut = false);
+
+ /* Create a dereference of an expression (`*of`) */
+ std::unique_ptr<Expr> deref (std::unique_ptr<Expr> &&of);
+
+ /* Create a block with an optional tail expression */
+ std::unique_ptr<Expr> block (std::vector<std::unique_ptr<Stmt>> &&stmts,
+ std::unique_ptr<Expr> &&tail_expr = nullptr);
+
+ /* Create a let binding with an optional type and initializer (`let <name> :
+ * <type> = <init>`) */
+ std::unique_ptr<Stmt> let (std::unique_ptr<Pattern> pattern,
+ std::unique_ptr<Type> type = nullptr,
+ std::unique_ptr<Expr> init = nullptr);
+
+ /**
+ * Create a call expression to a function, struct or enum variant, given its
+ * arguments (`path(arg0, arg1, arg2)`)
+ */
+ std::unique_ptr<Expr> call (std::unique_ptr<Expr> &&path,
+ std::vector<std::unique_ptr<Expr>> &&args);
+
+ /* Empty function qualifiers, with no specific qualifiers */
+ FunctionQualifiers fn_qualifiers ();
+
+ /* Create a single path segment from one string */
+ PathExprSegment path_segment (std::string seg);
+
+ /* And similarly for type path segments */
+ std::unique_ptr<TypePathSegment> type_path_segment (std::string seg);
+
+ /* 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);
+
+ /**
+ * Create a path in expression from multiple segments (`Clone::clone`). You
+ * do not need to separate the segments using `::`, you can simply provide a
+ * vector of strings to the functions which will get turned into path segments
+ */
+ PathInExpression path_in_expression (std::vector<std::string> &&segments);
+
+ /* Create a struct expression for unit structs (`S`) */
+ std::unique_ptr<Expr> struct_expr_struct (std::string struct_name);
+
+private:
+ /**
+ * Location of the generated AST nodes
+ */
+ Location loc;
+};
+
+} // namespace AST
+} // namespace Rust
+
+#endif // AST_BUILDER_H