-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoptimizer.d
75 lines (65 loc) · 1.22 KB
/
joptimizer.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module optimizer.joptimizer;
import semantic.variable;
import optimizer.warning;
import parser.all_nodes;
import std.stdio;
/**
* The JOptimizer class contains routines for optimizing an AST.
* The AST must first be semantically analyzed.
*/
class JOptimizer
{
///The AST
Node ast;
///The Environment
Environment env;
/**
* Removes all unused variables and displays warnings.
*/
void removeUnusedVariables()
{
//Used variables
SemanticVar[] usedVars;
//Check each variable
foreach(SemanticVar var; env.vars)
{
//Get the underlying node
VarDeclareNode node = var.var;
//Check uses
if(node.semInfo.uses == 0)
{
//Raise warning
(new UnusedVarWarning(node.name, node.location)).display();
}
else
{
usedVars ~= var;
}
}
//Remove all unused vars from list of variables
env.vars = usedVars;
}
/**
* Removes all nodes that have no effect.
*/
void removeUselessNodes()
{
//Go through AST and remove nodes that !hasEffect()
static void removeUseless(ref Node node)
{
if(!node.hasEffect())
{
node = new NullNode;
}
}
ast.each(&removeUseless);
}
/**
* Constructs the optimizer.
*/
this(Node ast, Environment env)
{
this.ast = ast;
this.env = env;
}
}