2323
2424namespace clang {
2525 using Result = Lexicographic::Result;
26+ class OperatorNode ;
2627
2728 class Node {
2829 public:
29- enum class NodeKind { BinaryNode , LeafExprNode };
30+ enum class NodeKind { OperatorNode , LeafExprNode };
3031
3132 NodeKind Kind;
32- Node *Parent;
33+ OperatorNode *Parent;
3334
34- Node (NodeKind Kind, Node *Parent) :
35+ Node (NodeKind Kind, OperatorNode *Parent) :
3536 Kind (Kind), Parent(Parent) {}
3637 };
3738
38- class BinaryNode : public Node {
39+ class OperatorNode : public Node {
3940 public:
4041 BinaryOperator::Opcode Opc;
42+ // Note: An OperatorNode has a list of children because the preorder AST is
43+ // an n-ary tree.
4144 llvm::SmallVector<Node *, 2 > Children;
4245
43- BinaryNode (BinaryOperator::Opcode Opc, Node *Parent) :
44- Node (NodeKind::BinaryNode , Parent),
46+ OperatorNode (BinaryOperator::Opcode Opc, OperatorNode *Parent) :
47+ Node (NodeKind::OperatorNode , Parent),
4548 Opc (Opc) {}
4649
4750 static bool classof (const Node *N) {
48- return N->Kind == NodeKind::BinaryNode ;
51+ return N->Kind == NodeKind::OperatorNode ;
4952 }
5053
5154 // Is the operator commutative and associative?
@@ -58,7 +61,7 @@ namespace clang {
5861 public:
5962 Expr *E;
6063
61- LeafExprNode (Expr *E, Node *Parent) :
64+ LeafExprNode (Expr *E, OperatorNode *Parent) :
6265 Node (NodeKind::LeafExprNode, Parent),
6366 E (E) {}
6467
@@ -81,27 +84,32 @@ namespace clang {
8184 // Create a PreorderAST for the expression E.
8285 // @param[in] E is the sub expression to be added to a new node.
8386 // @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 );
8588
8689 // Add a new node to the AST.
8790 // @param[in] Node is the current node to be added.
8891 // @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);
9093
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);
9698
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
98106 // associative operator.
99107 // @param[in] N is current node of the AST. Initial value is Root.
100108 // @param[in] Changed indicates whether a node was coalesced. We need this
101109 // to control when to stop recursive coalescing.
102110 void Coalesce (Node *N, bool &Changed);
103111
104- // Sort the children expressions in a binary node of the AST.
112+ // Sort the children expressions in a OperatorNode of the AST.
105113 // @param[in] N is current node of the AST. Initial value is Root.
106114 void Sort (Node *N);
107115
0 commit comments