|
6 | 6 | import rust |
7 | 7 | import codeql.rust.elements.internal.generated.ParentChild |
8 | 8 |
|
9 | | -private newtype TPrintAstConfiguration = TMakePrintAstConfiguration() |
| 9 | +signature predicate shouldPrintSig(Locatable e); |
10 | 10 |
|
11 | | -/** |
12 | | - * The hook to customize the files and functions printed by this module. |
13 | | - */ |
14 | | -class PrintAstConfiguration extends TPrintAstConfiguration { |
| 11 | +module PrintAstNode<shouldPrintSig/1 shouldPrint> { |
15 | 12 | /** |
16 | | - * Gets the string representation of this singleton |
| 13 | + * An AST node that should be printed. |
17 | 14 | */ |
18 | | - string toString() { result = "PrintAstConfiguration" } |
| 15 | + private newtype TPrintAstNode = TPrintLocatable(Locatable ast) { shouldPrint(ast) } |
19 | 16 |
|
20 | 17 | /** |
21 | | - * Holds if the AST for `e` should be printed. By default, holds for all. |
| 18 | + * A node in the output tree. |
22 | 19 | */ |
23 | | - predicate shouldPrint(Locatable e) { any() } |
24 | | -} |
25 | | - |
26 | | -private predicate shouldPrint(Locatable e) { any(PrintAstConfiguration config).shouldPrint(e) } |
27 | | - |
28 | | -/** |
29 | | - * An AST node that should be printed. |
30 | | - */ |
31 | | -private newtype TPrintAstNode = TPrintLocatable(Locatable ast) |
32 | | - |
33 | | -/** |
34 | | - * A node in the output tree. |
35 | | - */ |
36 | | -class PrintAstNode extends TPrintAstNode { |
37 | | - /** |
38 | | - * Gets a textual representation of this node. |
39 | | - */ |
40 | | - abstract string toString(); |
41 | | - |
42 | | - /** |
43 | | - * Gets the child node at index `index`. Child indices must be unique, |
44 | | - * but need not be contiguous. |
45 | | - */ |
46 | | - abstract predicate hasChild(PrintAstNode child, int index, string label); |
47 | | - |
48 | | - /** |
49 | | - * Holds if this node should be printed in the output. |
50 | | - */ |
51 | | - abstract predicate shouldBePrinted(); |
| 20 | + class PrintAstNode extends TPrintAstNode { |
| 21 | + /** |
| 22 | + * Gets a textual representation of this node. |
| 23 | + */ |
| 24 | + abstract string toString(); |
| 25 | + |
| 26 | + /** |
| 27 | + * Gets the child node at index `index`. Child indices must be unique, |
| 28 | + * but need not be contiguous. |
| 29 | + */ |
| 30 | + abstract predicate hasChild(PrintAstNode child, int index, string label); |
| 31 | + |
| 32 | + /** |
| 33 | + * Gets the location of this node in the source code. |
| 34 | + */ |
| 35 | + abstract Location getLocation(); |
| 36 | + |
| 37 | + /** |
| 38 | + * Gets the value of an additional property of this node, where the name of |
| 39 | + * the property is `key`. |
| 40 | + */ |
| 41 | + string getProperty(string key) { none() } |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets the underlying AST node, if any. |
| 45 | + */ |
| 46 | + abstract Locatable getAstNode(); |
| 47 | + } |
52 | 48 |
|
53 | | - /** |
54 | | - * Gets the location of this node in the source code. |
55 | | - */ |
56 | | - abstract Location getLocation(); |
| 49 | + private string prettyPrint(Locatable e) { result = "[" + e.getPrimaryQlClasses() + "] " + e } |
57 | 50 |
|
58 | | - /** |
59 | | - * Gets the value of an additional property of this node, where the name of |
60 | | - * the property is `key`. |
61 | | - */ |
62 | | - string getProperty(string key) { none() } |
| 51 | + private class Unresolved extends Locatable { |
| 52 | + Unresolved() { this != this.resolve() } |
| 53 | + } |
63 | 54 |
|
64 | 55 | /** |
65 | | - * Gets the underlying AST node, if any. |
| 56 | + * A graph node representing a real Locatable node. |
66 | 57 | */ |
67 | | - abstract Locatable getAstNode(); |
68 | | -} |
| 58 | + class PrintLocatable extends PrintAstNode, TPrintLocatable { |
| 59 | + Locatable ast; |
69 | 60 |
|
70 | | -private string prettyPrint(Locatable e) { |
71 | | - result = "[" + concat(e.getPrimaryQlClasses(), ", ") + "] " + e |
72 | | -} |
| 61 | + PrintLocatable() { this = TPrintLocatable(ast) } |
73 | 62 |
|
74 | | -private class Unresolved extends Locatable { |
75 | | - Unresolved() { this != this.resolve() } |
76 | | -} |
| 63 | + override string toString() { result = prettyPrint(ast) } |
77 | 64 |
|
78 | | -/** |
79 | | - * A graph node representing a real Locatable node. |
80 | | - */ |
81 | | -class PrintLocatable extends PrintAstNode, TPrintLocatable { |
82 | | - Locatable ast; |
| 65 | + override predicate hasChild(PrintAstNode child, int index, string label) { |
| 66 | + child = TPrintLocatable(any(Locatable c | c = getChildAndAccessor(ast, index, label))) |
| 67 | + } |
83 | 68 |
|
84 | | - PrintLocatable() { this = TPrintLocatable(ast) } |
| 69 | + final override Locatable getAstNode() { result = ast } |
85 | 70 |
|
86 | | - override string toString() { result = prettyPrint(ast) } |
87 | | - |
88 | | - final override predicate shouldBePrinted() { shouldPrint(ast) } |
89 | | - |
90 | | - override predicate hasChild(PrintAstNode child, int index, string label) { |
91 | | - child = TPrintLocatable(any(Locatable c | c = getChildAndAccessor(ast, index, label))) |
| 71 | + final override Location getLocation() { result = ast.getLocation() } |
92 | 72 | } |
93 | 73 |
|
94 | | - final override Locatable getAstNode() { result = ast } |
95 | | - |
96 | | - final override Location getLocation() { result = ast.getLocation() } |
97 | | -} |
98 | | - |
99 | | -/** |
100 | | - * A specialization of graph node for "unresolved" children, that is nodes in |
101 | | - * the parallel conversion AST. |
102 | | - */ |
103 | | -class PrintUnresolved extends PrintLocatable { |
104 | | - override Unresolved ast; |
| 74 | + /** |
| 75 | + * A specialization of graph node for "unresolved" children, that is nodes in |
| 76 | + * the parallel conversion AST. |
| 77 | + */ |
| 78 | + class PrintUnresolved extends PrintLocatable { |
| 79 | + override Unresolved ast; |
105 | 80 |
|
106 | | - override predicate hasChild(PrintAstNode child, int index, string label) { |
107 | | - // only print immediate unresolved children from the "parallel" AST |
108 | | - child = TPrintLocatable(getImmediateChildAndAccessor(ast, index, label).(Unresolved)) |
| 81 | + override predicate hasChild(PrintAstNode child, int index, string label) { |
| 82 | + // only print immediate unresolved children from the "parallel" AST |
| 83 | + child = TPrintLocatable(getImmediateChildAndAccessor(ast, index, label).(Unresolved)) |
| 84 | + } |
109 | 85 | } |
110 | 86 | } |
0 commit comments