@@ -1491,21 +1491,21 @@ class SingleASTNode
1491
1491
};
1492
1492
1493
1493
SingleASTNode (std::unique_ptr<Expr> expr)
1494
- : type (EXPRESSION), expr (std::move (expr)), item (nullptr ), stmt (nullptr )
1494
+ : kind (EXPRESSION), expr (std::move (expr)), item (nullptr ), stmt (nullptr )
1495
1495
{}
1496
1496
1497
1497
SingleASTNode (std::unique_ptr<Item> item)
1498
- : type (ITEM), expr (nullptr ), item (std::move (item)), stmt (nullptr )
1498
+ : kind (ITEM), expr (nullptr ), item (std::move (item)), stmt (nullptr )
1499
1499
{}
1500
1500
1501
1501
SingleASTNode (std::unique_ptr<Stmt> stmt)
1502
- : type (STMT), expr (nullptr ), item (nullptr ), stmt (std::move (stmt))
1502
+ : kind (STMT), expr (nullptr ), item (nullptr ), stmt (std::move (stmt))
1503
1503
{}
1504
1504
1505
1505
SingleASTNode (SingleASTNode const &other)
1506
1506
{
1507
- type = other.type ;
1508
- switch (type )
1507
+ kind = other.kind ;
1508
+ switch (kind )
1509
1509
{
1510
1510
case EXPRESSION:
1511
1511
expr = other.expr ->clone_expr ();
@@ -1523,8 +1523,8 @@ class SingleASTNode
1523
1523
1524
1524
SingleASTNode operator = (SingleASTNode const &other)
1525
1525
{
1526
- type = other.type ;
1527
- switch (type )
1526
+ kind = other.kind ;
1527
+ switch (kind )
1528
1528
{
1529
1529
case EXPRESSION:
1530
1530
expr = other.expr ->clone_expr ();
@@ -1544,27 +1544,52 @@ class SingleASTNode
1544
1544
SingleASTNode (SingleASTNode &&other) = default ;
1545
1545
SingleASTNode &operator = (SingleASTNode &&other) = default ;
1546
1546
1547
- std::unique_ptr<Expr> &get_expr ()
1547
+ NodeType get_kind () const { return kind; }
1548
+
1549
+ std::unique_ptr<Expr> &get_inner ()
1548
1550
{
1549
- rust_assert (type == EXPRESSION);
1551
+ rust_assert (kind == EXPRESSION);
1550
1552
return expr;
1551
1553
}
1552
1554
1553
1555
std::unique_ptr<Item> &get_item ()
1554
1556
{
1555
- rust_assert (type == ITEM);
1557
+ rust_assert (kind == ITEM);
1556
1558
return item;
1557
1559
}
1558
1560
1559
1561
std::unique_ptr<Stmt> &get_stmt ()
1560
1562
{
1561
- rust_assert (type == STMT);
1563
+ rust_assert (kind == STMT);
1562
1564
return stmt;
1563
1565
}
1564
1566
1567
+ /* *
1568
+ * Access the inner nodes and take ownership of them.
1569
+ * You can only call these functions once per node
1570
+ */
1571
+
1572
+ std::unique_ptr<Stmt> take_stmt ()
1573
+ {
1574
+ rust_assert (!is_error ());
1575
+ return std::move (stmt);
1576
+ }
1577
+
1578
+ std::unique_ptr<Expr> take_expr ()
1579
+ {
1580
+ rust_assert (!is_error ());
1581
+ return std::move (expr);
1582
+ }
1583
+
1584
+ std::unique_ptr<Item> take_item ()
1585
+ {
1586
+ rust_assert (!is_error ());
1587
+ return std::move (item);
1588
+ }
1589
+
1565
1590
void accept_vis (ASTVisitor &vis)
1566
1591
{
1567
- switch (type )
1592
+ switch (kind )
1568
1593
{
1569
1594
case EXPRESSION:
1570
1595
expr->accept_vis (vis);
@@ -1580,8 +1605,38 @@ class SingleASTNode
1580
1605
}
1581
1606
}
1582
1607
1608
+ bool is_error ()
1609
+ {
1610
+ switch (kind)
1611
+ {
1612
+ case EXPRESSION:
1613
+ return expr == nullptr ;
1614
+ case ITEM:
1615
+ return item == nullptr ;
1616
+ case STMT:
1617
+ return stmt == nullptr ;
1618
+ default :
1619
+ return true ;
1620
+ }
1621
+ }
1622
+
1623
+ std::string as_string ()
1624
+ {
1625
+ switch (kind)
1626
+ {
1627
+ case EXPRESSION:
1628
+ return " Expr: " + expr->as_string ();
1629
+ case ITEM:
1630
+ return " Item: " + item->as_string ();
1631
+ case STMT:
1632
+ return " Stmt: " + stmt->as_string ();
1633
+ default :
1634
+ return " " ;
1635
+ }
1636
+ }
1637
+
1583
1638
private:
1584
- NodeType type ;
1639
+ NodeType kind ;
1585
1640
1586
1641
// FIXME make this a union
1587
1642
std::unique_ptr<Expr> expr;
@@ -1604,11 +1659,18 @@ class ASTFragment
1604
1659
* ability for a macro to expand to two statements, for instance. */
1605
1660
1606
1661
std::vector<SingleASTNode> nodes;
1662
+ bool fragment_is_error;
1607
1663
1608
1664
public:
1609
- ASTFragment (std::vector<SingleASTNode> nodes) : nodes (std::move (nodes)) {}
1665
+ ASTFragment (std::vector<SingleASTNode> nodes, bool fragment_is_error = false )
1666
+ : nodes (std::move (nodes)), fragment_is_error (fragment_is_error)
1667
+ {
1668
+ if (fragment_is_error)
1669
+ rust_assert (nodes.empty ());
1670
+ }
1610
1671
1611
1672
ASTFragment (ASTFragment const &other)
1673
+ : fragment_is_error (other.fragment_is_error)
1612
1674
{
1613
1675
nodes.clear ();
1614
1676
nodes.reserve (other.nodes .size ());
@@ -1620,18 +1682,47 @@ class ASTFragment
1620
1682
1621
1683
ASTFragment &operator = (ASTFragment const &other)
1622
1684
{
1685
+ fragment_is_error = other.fragment_is_error ;
1623
1686
nodes.clear ();
1624
1687
nodes.reserve (other.nodes .size ());
1625
1688
for (auto &n : other.nodes )
1626
1689
{
1627
1690
nodes.push_back (n);
1628
1691
}
1692
+
1629
1693
return *this ;
1630
1694
}
1631
1695
1632
1696
static ASTFragment create_empty () { return ASTFragment ({}); }
1697
+ static ASTFragment create_error () { return ASTFragment ({}, true ); }
1633
1698
1634
1699
std::vector<SingleASTNode> &get_nodes () { return nodes; }
1700
+ bool is_error () const { return fragment_is_error; }
1701
+
1702
+ bool should_expand () const { return !is_error () && !nodes.empty (); }
1703
+
1704
+ /* *
1705
+ * We need to make a special case for Expression fragments as only one
1706
+ * Node will be extracted from the `nodes` vector
1707
+ */
1708
+
1709
+ bool is_expression_fragment () const
1710
+ {
1711
+ return nodes.size () == 1
1712
+ && nodes[0 ].get_kind () == SingleASTNode::NodeType::EXPRESSION;
1713
+ }
1714
+
1715
+ std::unique_ptr<Expr> take_expression_fragment ()
1716
+ {
1717
+ rust_assert (is_expression_fragment ());
1718
+ return nodes[0 ].take_expr ();
1719
+ }
1720
+
1721
+ void accept_vis (ASTVisitor &vis)
1722
+ {
1723
+ for (auto &node : nodes)
1724
+ node.accept_vis (vis);
1725
+ }
1635
1726
};
1636
1727
1637
1728
// A crate AST object - holds all the data for a single compilation unit
0 commit comments