aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/ast
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-10-19 14:05:54 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2022-10-19 15:01:53 +0200
commit540d896c3fa745efdc96ad34e7e5c585f9b7b93f (patch)
tree31b1e78080c9be4818314d424095ea05bf6f79bd /gcc/rust/ast
parent4f2c1499feafb46bf88824c4019ef15d2a68d3e8 (diff)
downloadgcc-540d896c3fa745efdc96ad34e7e5c585f9b7b93f.zip
gcc-540d896c3fa745efdc96ad34e7e5c585f9b7b93f.tar.gz
gcc-540d896c3fa745efdc96ad34e7e5c585f9b7b93f.tar.bz2
rust: Replace uses of ASTFragment -> Fragment
Diffstat (limited to 'gcc/rust/ast')
-rw-r--r--gcc/rust/ast/rust-ast.h132
-rw-r--r--gcc/rust/ast/rust-macro.h18
2 files changed, 9 insertions, 141 deletions
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 492faea..e0e10dc 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1858,138 +1858,6 @@ public:
}
};
-/* Basically, a "fragment" that can be incorporated into the AST, created as
- * a result of macro expansion. Really annoying to work with due to the fact
- * that macros can really expand to anything. As such, horrible representation
- * at the moment. */
-class ASTFragment
-{
-private:
- /* basic idea: essentially, a vector of tagged unions of different AST node
- * types. Now, this could actually be stored without a tagged union if the
- * different AST node types had a unified parent, but that would create
- * issues with the diamond problem or significant performance penalties. So
- * a tagged union had to be used instead. A vector is used to represent the
- * ability for a macro to expand to two statements, for instance. */
-
- std::vector<SingleASTNode> nodes;
- bool fragment_is_error;
-
- /**
- * We need to make a special case for Expression and Type fragments as only
- * one Node will be extracted from the `nodes` vector
- */
-
- bool is_single_fragment () const { return nodes.size () == 1; }
-
- bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const
- {
- return is_single_fragment () && nodes[0].get_kind () == expected;
- }
-
- void assert_single_fragment (SingleASTNode::NodeType expected) const
- {
- static const std::map<SingleASTNode::NodeType, const char *> str_map = {
- {SingleASTNode::NodeType::IMPL, "impl"},
- {SingleASTNode::NodeType::ITEM, "item"},
- {SingleASTNode::NodeType::TYPE, "type"},
- {SingleASTNode::NodeType::EXPRESSION, "expr"},
- {SingleASTNode::NodeType::STMT, "stmt"},
- {SingleASTNode::NodeType::EXTERN, "extern"},
- {SingleASTNode::NodeType::TRAIT, "trait"},
- {SingleASTNode::NodeType::TRAIT_IMPL, "trait impl"},
- };
-
- auto actual = nodes[0].get_kind ();
- auto fail = false;
-
- if (!is_single_fragment ())
- {
- rust_error_at (Location (), "fragment is not single");
- fail = true;
- }
-
- if (actual != expected)
- {
- rust_error_at (
- Location (),
- "invalid fragment operation: expected %qs node, got %qs node",
- str_map.find (expected)->second,
- str_map.find (nodes[0].get_kind ())->second);
- fail = true;
- }
-
- rust_assert (!fail);
- }
-
-public:
- ASTFragment (std::vector<SingleASTNode> nodes, bool fragment_is_error = false)
- : nodes (std::move (nodes)), fragment_is_error (fragment_is_error)
- {
- if (fragment_is_error)
- rust_assert (nodes.empty ());
- }
-
- ASTFragment (ASTFragment const &other)
- : fragment_is_error (other.fragment_is_error)
- {
- nodes.clear ();
- nodes.reserve (other.nodes.size ());
- for (auto &n : other.nodes)
- {
- nodes.push_back (n);
- }
- }
-
- ASTFragment &operator= (ASTFragment const &other)
- {
- fragment_is_error = other.fragment_is_error;
- nodes.clear ();
- nodes.reserve (other.nodes.size ());
- for (auto &n : other.nodes)
- {
- nodes.push_back (n);
- }
-
- return *this;
- }
-
- static ASTFragment create_error () { return ASTFragment ({}, true); }
-
- std::vector<SingleASTNode> &get_nodes () { return nodes; }
- bool is_error () const { return fragment_is_error; }
-
- bool should_expand () const { return !is_error (); }
-
- bool is_expression_fragment () const
- {
- return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION);
- }
-
- bool is_type_fragment () const
- {
- return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE);
- }
-
- std::unique_ptr<Expr> take_expression_fragment ()
- {
- assert_single_fragment (SingleASTNode::NodeType::EXPRESSION);
- return nodes[0].take_expr ();
- }
-
- std::unique_ptr<Type> take_type_fragment ()
- {
- assert_single_fragment (SingleASTNode::NodeType::TYPE);
- return nodes[0].take_type ();
- }
-
- void accept_vis (ASTVisitor &vis)
- {
- for (auto &node : nodes)
- node.accept_vis (vis);
- }
-};
-
// A crate AST object - holds all the data for a single compilation unit
struct Crate
{
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index ce515db..3afd86c 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -20,6 +20,7 @@
#define RUST_AST_MACRO_H
#include "rust-ast.h"
+#include "rust-ast-fragment.h"
#include "rust-location.h"
#include <string>
@@ -455,8 +456,7 @@ class MacroRulesDefinition : public MacroItem
std::vector<MacroRule> rules; // inlined form
Location locus;
- std::function<ASTFragment (Location, MacroInvocData &)>
- associated_transcriber;
+ std::function<Fragment (Location, MacroInvocData &)> associated_transcriber;
// Since we can't compare std::functions, we need to use an extra boolean
bool is_builtin_rule;
@@ -467,10 +467,10 @@ class MacroRulesDefinition : public MacroItem
* should make use of the actual rules. If the macro is builtin, then another
* associated transcriber should be used
*/
- static ASTFragment dummy_builtin (Location, MacroInvocData &)
+ static Fragment dummy_builtin (Location, MacroInvocData &)
{
gcc_unreachable ();
- return ASTFragment::create_error ();
+ return Fragment::create_error ();
}
/* NOTE: in rustc, macro definitions are considered (and parsed as) a type
@@ -490,9 +490,9 @@ public:
associated_transcriber (dummy_builtin), is_builtin_rule (false)
{}
- MacroRulesDefinition (Identifier builtin_name, DelimType delim_type,
- std::function<ASTFragment (Location, MacroInvocData &)>
- associated_transcriber)
+ MacroRulesDefinition (
+ Identifier builtin_name, DelimType delim_type,
+ std::function<Fragment (Location, MacroInvocData &)> associated_transcriber)
: outer_attrs (std::vector<Attribute> ()), rule_name (builtin_name),
delim_type (delim_type), rules (std::vector<MacroRule> ()),
locus (Location ()), associated_transcriber (associated_transcriber),
@@ -520,14 +520,14 @@ public:
const std::vector<MacroRule> &get_rules () const { return rules; }
bool is_builtin () const { return is_builtin_rule; }
- const std::function<ASTFragment (Location, MacroInvocData &)> &
+ const std::function<Fragment (Location, MacroInvocData &)> &
get_builtin_transcriber () const
{
rust_assert (is_builtin ());
return associated_transcriber;
}
void set_builtin_transcriber (
- std::function<ASTFragment (Location, MacroInvocData &)> transcriber)
+ std::function<Fragment (Location, MacroInvocData &)> transcriber)
{
associated_transcriber = transcriber;
is_builtin_rule = true;