Python: Convenience methods

asVar, asCfgNode, and asExpr
This commit is contained in:
Rasmus Lerchedahl Petersen
2020-08-20 15:16:23 +02:00
parent 5a734730de
commit 94e6fd9199

View File

@@ -51,6 +51,15 @@ class Node extends TNode {
) { ) {
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
} }
/** Convenience method for casting to EssaNode and calling getVar. */
EssaVariable asVar() { none() }
/** Convenience method for casting to CfgNode and calling getNode. */
DataFlowCfgNode asCfgNode() { none() }
/** Convenience method for casting to ExprNode and calling getNode and getNode again. */
Expr asExpr() { none() }
} }
class EssaNode extends Node, TEssaNode { class EssaNode extends Node, TEssaNode {
@@ -60,6 +69,8 @@ class EssaNode extends Node, TEssaNode {
EssaVariable getVar() { result = var } EssaVariable getVar() { result = var }
override EssaVariable asVar() { result = var }
/** Gets a textual representation of this element. */ /** Gets a textual representation of this element. */
override string toString() { result = var.toString() } override string toString() { result = var.toString() }
@@ -75,6 +86,8 @@ class CfgNode extends Node, TCfgNode {
DataFlowCfgNode getNode() { result = node } DataFlowCfgNode getNode() { result = node }
override DataFlowCfgNode asCfgNode() { result = node }
/** Gets a textual representation of this element. */ /** Gets a textual representation of this element. */
override string toString() { result = node.toString() } override string toString() { result = node.toString() }
@@ -92,6 +105,8 @@ class CfgNode extends Node, TCfgNode {
*/ */
class ExprNode extends CfgNode { class ExprNode extends CfgNode {
ExprNode() { isExpressionNode(node) } ExprNode() { isExpressionNode(node) }
override Expr asExpr() { result = node.getNode() }
} }
/** Gets a node corresponding to expression `e`. */ /** Gets a node corresponding to expression `e`. */