namespace Rust {
namespace AST {
-Fragment::Fragment (std::vector<SingleASTNode> nodes, bool fragment_is_error)
- : kind (fragment_is_error ? FragmentKind::Error : FragmentKind::Complete),
- nodes (std::move (nodes))
-{
- if (fragment_is_error)
- rust_assert (nodes.empty ());
-}
+Fragment::Fragment (FragmentKind kind, std::vector<SingleASTNode> nodes)
+ : kind (kind), nodes (std::move (nodes))
+{}
Fragment::Fragment (Fragment const &other) : kind (other.get_kind ())
{
- nodes.clear ();
- nodes.reserve (other.nodes.size ());
- for (auto &n : other.nodes)
- {
- nodes.push_back (n);
- }
+ *this = other;
}
Fragment &
Fragment
Fragment::create_error ()
{
- return Fragment ({}, true);
+ return Fragment (FragmentKind::Error, {});
+}
+
+Fragment
+Fragment::complete (std::vector<AST::SingleASTNode> nodes)
+{
+ return Fragment (FragmentKind::Complete, std::move (nodes));
+}
+
+Fragment
+Fragment::unexpanded ()
+{
+ return Fragment (FragmentKind::Unexpanded, {});
}
std::vector<SingleASTNode> &
class Fragment
{
public:
- Fragment (std::vector<SingleASTNode> nodes, bool fragment_is_error = false);
Fragment (Fragment const &other);
+ Fragment &operator= (Fragment const &other);
+
+ /**
+ * Create an error fragment
+ */
static Fragment create_error ();
- Fragment &operator= (Fragment const &other);
+ /**
+ * Create a complete AST fragment
+ */
+ static Fragment complete (std::vector<AST::SingleASTNode> nodes);
+
+ /**
+ * Create a fragment which contains unexpanded nodes
+ */
+ static Fragment unexpanded ();
FragmentKind get_kind () const;
std::vector<SingleASTNode> &get_nodes ();
void accept_vis (ASTVisitor &vis);
private:
+ Fragment (FragmentKind kind, std::vector<SingleASTNode> nodes);
+
FragmentKind kind;
/**
= Session::get_instance ().linemap->location_file (invoc_locus);
auto file_str = AST::SingleASTNode (make_string (invoc_locus, current_file));
- return AST::Fragment ({file_str});
+ return AST::Fragment::complete ({file_str});
}
AST::Fragment
new AST::LiteralExpr (std::to_string (current_column), AST::Literal::INT,
PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus)));
- return AST::Fragment ({column_no});
+ return AST::Fragment::complete ({column_no});
}
/* Expand builtin macro include_bytes!("filename"), which includes the contents
new AST::BorrowExpr (std::move (array), false, false, {}, invoc_locus));
auto node = AST::SingleASTNode (std::move (borrow));
- return AST::Fragment ({node});
+ return AST::Fragment::complete ({node});
}
/* Expand builtin macro include_str!("filename"), which includes the contents
std::string str ((const char *) &bytes[0], bytes.size ());
auto node = AST::SingleASTNode (make_string (invoc_locus, str));
- return AST::Fragment ({node});
+ return AST::Fragment::complete ({node});
}
/* Expand builtin macro compile_error!("error"), which forces a compile error
return AST::Fragment::create_error ();
auto node = AST::SingleASTNode (make_string (invoc_locus, str));
- return AST::Fragment ({node});
+ return AST::Fragment::complete ({node});
}
/* Expand builtin macro env!(), which inspects an environment variable at
}
auto node = AST::SingleASTNode (make_string (invoc_locus, env_value));
- return AST::Fragment ({node});
+ return AST::Fragment::complete ({node});
}
AST::Fragment
new AST::LiteralExpr (result ? "true" : "false", AST::Literal::BOOL,
PrimitiveCoreType::CORETYPE_BOOL, {}, invoc_locus)));
- return AST::Fragment ({literal_exp});
+ return AST::Fragment::complete ({literal_exp});
}
/* Expand builtin macro include!(), which includes a source file at the current
nodes.push_back (node);
}
- return AST::Fragment (nodes);
+ return AST::Fragment::complete (nodes);
}
AST::Fragment
new AST::LiteralExpr (std::to_string (current_line), AST::Literal::INT,
PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus)));
- return AST::Fragment ({line_no});
+ return AST::Fragment::complete ({line_no});
}
} // namespace Rust