23
23
24
24
namespace clang {
25
25
using Result = Lexicographic::Result;
26
+ class OperatorNode ;
26
27
27
28
class Node {
28
29
public:
29
- enum class NodeKind { BinaryNode , LeafExprNode };
30
+ enum class NodeKind { OperatorNode , LeafExprNode };
30
31
31
32
NodeKind Kind;
32
- Node *Parent;
33
+ OperatorNode *Parent;
33
34
34
- Node (NodeKind Kind, Node *Parent) :
35
+ Node (NodeKind Kind, OperatorNode *Parent) :
35
36
Kind (Kind), Parent(Parent) {}
36
37
};
37
38
38
- class BinaryNode : public Node {
39
+ class OperatorNode : public Node {
39
40
public:
40
41
BinaryOperator::Opcode Opc;
42
+ // Note: An OperatorNode has a list of children because the preorder AST is
43
+ // an n-ary tree.
41
44
llvm::SmallVector<Node *, 2 > Children;
42
45
43
- BinaryNode (BinaryOperator::Opcode Opc, Node *Parent) :
44
- Node (NodeKind::BinaryNode , Parent),
46
+ OperatorNode (BinaryOperator::Opcode Opc, OperatorNode *Parent) :
47
+ Node (NodeKind::OperatorNode , Parent),
45
48
Opc (Opc) {}
46
49
47
50
static bool classof (const Node *N) {
48
- return N->Kind == NodeKind::BinaryNode ;
51
+ return N->Kind == NodeKind::OperatorNode ;
49
52
}
50
53
51
54
// Is the operator commutative and associative?
@@ -58,7 +61,7 @@ namespace clang {
58
61
public:
59
62
Expr *E;
60
63
61
- LeafExprNode (Expr *E, Node *Parent) :
64
+ LeafExprNode (Expr *E, OperatorNode *Parent) :
62
65
Node (NodeKind::LeafExprNode, Parent),
63
66
E (E) {}
64
67
@@ -81,27 +84,32 @@ namespace clang {
81
84
// Create a PreorderAST for the expression E.
82
85
// @param[in] E is the sub expression to be added to a new node.
83
86
// @param[in] Parent is the parent of the new node.
84
- void Create (Expr *E, Node *Parent = nullptr );
87
+ void Create (Expr *E, OperatorNode *Parent = nullptr );
85
88
86
89
// Add a new node to the AST.
87
90
// @param[in] Node is the current node to be added.
88
91
// @param[in] Parent is the parent of the node to be added.
89
- void AddNode (Node *N, Node *Parent);
92
+ void AddNode (Node *N, OperatorNode *Parent);
90
93
91
- // Move the children (if any) of node N to its parent and then remove N.
92
- // @param[in] N is the current node.
93
- // @param[in] P is the parent of the node to be removed. P should be a
94
- // BinaryNode.
95
- void RemoveNode (Node *N, Node *P);
94
+ // Coalesce the OperatorNode O with its parent. This involves moving the
95
+ // children (if any) of node O to its parent and then removing O.
96
+ // @param[in] O is the current node. O should be a OperatorNode.
97
+ void CoalesceNode (OperatorNode *O);
96
98
97
- // Recursively coalesce binary nodes having the same commutative and
99
+ // Determines if a OperatorNode could be coalesced into its parent.
100
+ // @param[in] O is the current node. O should be a OperatorNode.
101
+ // @return Return true if O can be coalesced into its parent, false
102
+ // otherwise.
103
+ bool CanCoalesceNode (OperatorNode *O);
104
+
105
+ // Recursively coalesce OperatoreNodes having the same commutative and
98
106
// associative operator.
99
107
// @param[in] N is current node of the AST. Initial value is Root.
100
108
// @param[in] Changed indicates whether a node was coalesced. We need this
101
109
// to control when to stop recursive coalescing.
102
110
void Coalesce (Node *N, bool &Changed);
103
111
104
- // Sort the children expressions in a binary node of the AST.
112
+ // Sort the children expressions in a OperatorNode of the AST.
105
113
// @param[in] N is current node of the AST. Initial value is Root.
106
114
void Sort (Node *N);
107
115
0 commit comments