From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 0E78A3858284; Tue, 16 Jan 2024 17:46:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0E78A3858284 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705427198; bh=Nd5Cd96VfwKWa7OGwpOd6GitS+ibr5Vc4v9XzE7S82w=; h=From:To:Subject:Date:From; b=eb62U5Iy1OnCLx3lk1x525C89ePqdLCkH5TRweL6BJQvRsbVy7mmO1JQerItBlpQq XALtvGcByWHwfNWjExg9qNF6TM50gqE2pLp1btHjyaeCfWZAOrV0pIWBsDeln+5QrT sBPjmzEV5I6t3s5vYYZT4hxUb5Z1lHYInsGq9yrk= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-7629] gccrs: ast: Add AstBuilder class. X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 10d501fa9b781c571db4ba4c27033b21b3dee422 X-Git-Newrev: 7d9b2a8607f1efda97be89917b36b4cd467904f5 Message-Id: <20240116174638.0E78A3858284@sourceware.org> Date: Tue, 16 Jan 2024 17:46:38 +0000 (GMT) List-Id: https://gcc.gnu.org/g:7d9b2a8607f1efda97be89917b36b4cd467904f5 commit r14-7629-g7d9b2a8607f1efda97be89917b36b4cd467904f5 Author: Arthur Cohen Date: Thu May 25 14:18:47 2023 +0200 gccrs: ast: Add AstBuilder class. gcc/rust/ChangeLog: * ast/rust-ast-builder.cc: New file. * ast/rust-ast-builder.h: New file. Diff: --- gcc/rust/ast/rust-ast-builder.cc | 122 +++++++++++++++++++++++++++++++++++++++ gcc/rust/ast/rust-ast-builder.h | 98 +++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc new file mode 100644 index 00000000000..06116540c19 --- /dev/null +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -0,0 +1,122 @@ +// 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 +// . + +#include "rust-ast-builder.h" +#include "rust-ast-full.h" + +namespace Rust { +namespace AST { + +std::unique_ptr +AstBuilder::call (std::unique_ptr &&path, + std::vector> &&args) +{ + return std::unique_ptr ( + new CallExpr (std::move (path), std::move (args), {}, loc)); +} + +std::unique_ptr +AstBuilder::identifier (std::string name) +{ + return std::unique_ptr (new IdentifierExpr (name, {}, loc)); +} + +std::unique_ptr +AstBuilder::tuple_idx (std::string receiver, int idx) +{ + return std::unique_ptr ( + 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 +AstBuilder::type_path_segment (std::string seg) +{ + return std::unique_ptr ( + new TypePathSegment (seg, false, loc)); +} + +std::unique_ptr +AstBuilder::single_type_path (std::string type) +{ + auto segments = std::vector> (); + segments.emplace_back (type_path_segment (type)); + + return std::unique_ptr (new TypePath (std::move (segments), loc)); +} + +PathInExpression +AstBuilder::path_in_expression (std::vector &&segments) +{ + 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 +AstBuilder::struct_expr_struct (std::string struct_name) +{ + return std::unique_ptr ( + new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc)); +} + +std::unique_ptr +AstBuilder::block (std::vector> &&stmts, + std::unique_ptr &&tail_expr) +{ + return std::unique_ptr ( + new BlockExpr (std::move (stmts), std::move (tail_expr), {}, {}, loc, loc)); +} + +std::unique_ptr +AstBuilder::let (std::unique_ptr pattern, std::unique_ptr type, + std::unique_ptr init) +{ + return std::unique_ptr ( + new LetStmt (/* needs a pattern here, not just a name */ nullptr, + std::move (init), std::move (type), {}, loc)); +} + +std::unique_ptr +AstBuilder::ref (std::unique_ptr &&of, bool mut) +{ + return std::unique_ptr ( + new BorrowExpr (std::move (of), mut, /* is double */ false, {}, loc)); +} + +std::unique_ptr +AstBuilder::deref (std::unique_ptr &&of) +{ + return std::unique_ptr (new DereferenceExpr (std::move (of), {}, loc)); +} + +} // namespace AST +} // namespace Rust diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h new file mode 100644 index 00000000000..4bbd931a7f9 --- /dev/null +++ b/gcc/rust/ast/rust-ast-builder.h @@ -0,0 +1,98 @@ +// 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 +// . + +#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 identifier (std::string name); + + /* Create a tuple index expression (`receiver.0`) */ + std::unique_ptr tuple_idx (std::string receiver, int idx); + + /* Create a reference to an expression (`&of`) */ + std::unique_ptr ref (std::unique_ptr &&of, bool mut = false); + + /* Create a dereference of an expression (`*of`) */ + std::unique_ptr deref (std::unique_ptr &&of); + + /* Create a block with an optional tail expression */ + std::unique_ptr block (std::vector> &&stmts, + std::unique_ptr &&tail_expr = nullptr); + + /* Create a let binding with an optional type and initializer (`let : + * = `) */ + std::unique_ptr let (std::unique_ptr pattern, + std::unique_ptr type = nullptr, + std::unique_ptr init = nullptr); + + /** + * Create a call expression to a function, struct or enum variant, given its + * arguments (`path(arg0, arg1, arg2)`) + */ + std::unique_ptr call (std::unique_ptr &&path, + std::vector> &&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 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 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 &&segments); + + /* Create a struct expression for unit structs (`S`) */ + std::unique_ptr struct_expr_struct (std::string struct_name); + +private: + /** + * Location of the generated AST nodes + */ + Location loc; +}; + +} // namespace AST +} // namespace Rust + +#endif // AST_BUILDER_H