|
1 | 1 | import python |
2 | 2 |
|
| 3 | +/** Syntactic node (Class, Function, Module, Expr, Stmt or Comprehension) corresponding to a flow node */ |
| 4 | +abstract class AstNode extends AstNode_ { |
| 5 | + |
| 6 | + /** Gets the scope that this node occurs in */ |
| 7 | + abstract Scope getScope(); |
| 8 | + |
| 9 | + /** Gets a flow node corresponding directly to this node. |
| 10 | + * NOTE: For some statements and other purely syntactic elements, |
| 11 | + * there may not be a `ControlFlowNode` */ |
| 12 | + ControlFlowNode getAFlowNode() { |
| 13 | + py_flow_bb_node(result, this, _, _) |
| 14 | + } |
| 15 | + |
| 16 | + /** Gets the location for this AST node */ |
| 17 | + Location getLocation() { |
| 18 | + none() |
| 19 | + } |
| 20 | + |
| 21 | + /** Whether this syntactic element is artificial, that is it is generated |
| 22 | + * by the compiler and is not present in the source */ |
| 23 | + predicate isArtificial() { |
| 24 | + none() |
| 25 | + } |
| 26 | + |
| 27 | + /** Gets a child node of this node in the AST. This predicate exists to aid exploration of the AST |
| 28 | + * and other experiments. The child-parent relation may not be meaningful. |
| 29 | + * For a more meaningful relation in terms of dependency use |
| 30 | + * Expr.getASubExpression(), Stmt.getASubStatement(), Stmt.getASubExpression() or |
| 31 | + * Scope.getAStmt(). |
| 32 | + */ |
| 33 | + abstract AstNode getAChildNode(); |
| 34 | + |
| 35 | + /** Gets the parent node of this node in the AST. This predicate exists to aid exploration of the AST |
| 36 | + * and other experiments. The child-parent relation may not be meaningful. |
| 37 | + * For a more meaningful relation in terms of dependency use |
| 38 | + * Expr.getASubExpression(), Stmt.getASubStatement(), Stmt.getASubExpression() or |
| 39 | + * Scope.getAStmt() applied to the parent. |
| 40 | + */ |
| 41 | + AstNode getParentNode() { |
| 42 | + result.getAChildNode() = this |
| 43 | + } |
| 44 | + |
| 45 | + /** Whether this contains `inner` syntactically */ |
| 46 | + predicate contains(AstNode inner) { |
| 47 | + this.getAChildNode+() = inner |
| 48 | + } |
| 49 | + |
| 50 | + /** Whether this contains `inner` syntactically and `inner` has the same scope as `this` */ |
| 51 | + predicate containsInScope(AstNode inner) { |
| 52 | + this.contains(inner) and |
| 53 | + this.getScope() = inner.getScope() and |
| 54 | + not inner instanceof Scope |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
3 | 59 | /* Parents */ |
4 | 60 |
|
5 | 61 | /** Internal implementation class */ |
|
0 commit comments