summaryrefslogtreecommitdiff
path: root/Source/Dafny/Compiler.cs
diff options
context:
space:
mode:
authorGravatar wuestholz <unknown>2015-01-28 10:05:35 +0100
committerGravatar wuestholz <unknown>2015-01-28 10:05:35 +0100
commit47eca20d9f2b200161520448b9ae24162dfa2704 (patch)
treeb4a41a8c77dc3b3b8c637438fa85198f0f5a2ec0 /Source/Dafny/Compiler.cs
parent73762c8d408429241d44983bbc0752965ed8e0da (diff)
Did some refactoring to improve the name generation.
Diffstat (limited to 'Source/Dafny/Compiler.cs')
-rw-r--r--Source/Dafny/Compiler.cs42
1 files changed, 21 insertions, 21 deletions
diff --git a/Source/Dafny/Compiler.cs b/Source/Dafny/Compiler.cs
index b381cbc0..7fff33c1 100644
--- a/Source/Dafny/Compiler.cs
+++ b/Source/Dafny/Compiler.cs
@@ -27,10 +27,12 @@ namespace Microsoft.Dafny {
TextWriter wr;
Method enclosingMethod; // non-null when a method body is being translated
- static int currentUniqueId = -1;
+ FreshIdGenerator idGenerator = new FreshIdGenerator();
+
+ static FreshIdGenerator compileNameIdGenerator = new FreshIdGenerator();
public static int FreshId()
{
- return System.Threading.Interlocked.Increment(ref currentUniqueId);
+ return compileNameIdGenerator.FreshNumericId();
}
Dictionary<Expression, int> uniqueAstNumbers = new Dictionary<Expression, int>();
@@ -1161,7 +1163,7 @@ namespace Microsoft.Dafny {
} else if (stmt is BreakStmt) {
var s = (BreakStmt)stmt;
Indent(indent);
- wr.WriteLine("goto after_{0};", s.TargetStmt.Labels.Data.AssignUniqueId("after_", IdGenerator));
+ wr.WriteLine("goto after_{0};", s.TargetStmt.Labels.Data.AssignUniqueId("after_", idGenerator));
} else if (stmt is ProduceStmt) {
var s = (ProduceStmt)stmt;
if (s.hiddenUpdate != null)
@@ -1190,7 +1192,7 @@ namespace Microsoft.Dafny {
if (!(rhs is HavocRhs)) {
lvalues.Add(CreateLvalue(lhs, indent));
- string target = IdGenerator.FreshId("_rhs");
+ string target = idGenerator.FreshId("_rhs");
rhss.Add(target);
TrRhs("var " + target, null, rhs, indent);
}
@@ -1251,7 +1253,7 @@ namespace Microsoft.Dafny {
Contract.Assert(s.Bounds != null); // follows from s.MissingBounds == null
var n = s.Lhss.Count;
Contract.Assert(s.Bounds.Count == n);
- var c = IdGenerator.FreshNumericId("_ASSIGN_SUCH_THAT_+_iterLimit_");
+ var c = idGenerator.FreshNumericId("_ASSIGN_SUCH_THAT_+_iterLimit_");
var doneLabel = "_ASSIGN_SUCH_THAT_" + c;
var iterLimit = "_iterLimit_" + c;
@@ -1270,7 +1272,7 @@ namespace Microsoft.Dafny {
Indent(ind);
wr.WriteLine("var {0}_{1} = {0};", iterLimit, i);
}
- var tmpVar = IdGenerator.FreshId("_assign_such_that_");
+ var tmpVar = idGenerator.FreshId("_assign_such_that_");
Indent(ind);
if (bound is ComprehensionExpr.BoolBoundedPool) {
wr.WriteLine("foreach (var {0} in Dafny.Helpers.AllBooleans) {{ @{1} = {0};", tmpVar, bv.CompileName);
@@ -1486,7 +1488,7 @@ namespace Microsoft.Dafny {
// be nested.
// Temporary names
- var c = IdGenerator.FreshNumericId("_ingredients+_tup");
+ var c = idGenerator.FreshNumericId("_ingredients+_tup");
string ingredients = "_ingredients" + c;
string tup = "_tup" + c;
@@ -1643,7 +1645,7 @@ namespace Microsoft.Dafny {
// ...
// }
if (s.Cases.Count != 0) {
- string source = IdGenerator.FreshId("_source");
+ string source = idGenerator.FreshId("_source");
Indent(indent);
wr.Write("{0} {1} = ", TypeName(cce.NonNull(s.Source.Type)), source);
TrExpr(s.Source);
@@ -1686,7 +1688,7 @@ namespace Microsoft.Dafny {
return "@" + ll.Var.CompileName;
} else if (lhs is MemberSelectExpr) {
var ll = (MemberSelectExpr)lhs;
- string obj = IdGenerator.FreshId("_obj");
+ string obj = idGenerator.FreshId("_obj");
Indent(indent);
wr.Write("var {0} = ", obj);
TrExpr(ll.Obj);
@@ -1694,7 +1696,7 @@ namespace Microsoft.Dafny {
return string.Format("{0}.@{1}", obj, ll.Member.CompileName);
} else if (lhs is SeqSelectExpr) {
var ll = (SeqSelectExpr)lhs;
- var c = IdGenerator.FreshNumericId("_arr+_index");
+ var c = idGenerator.FreshNumericId("_arr+_index");
string arr = "_arr" + c;
string index = "_index" + c;
Indent(indent);
@@ -1708,7 +1710,7 @@ namespace Microsoft.Dafny {
return string.Format("{0}[(int){1}]", arr, index);
} else {
var ll = (MultiSelectExpr)lhs;
- var c = IdGenerator.FreshNumericId("_arr+_index");
+ var c = idGenerator.FreshNumericId("_arr+_index");
string arr = "_arr" + c;
Indent(indent);
wr.Write("var {0} = ", arr);
@@ -1735,7 +1737,7 @@ namespace Microsoft.Dafny {
Contract.Requires((target == null) != (targetExpr == null));
var tRhs = rhs as TypeRhs;
if (tRhs != null && tRhs.InitCall != null) {
- string nw = IdGenerator.FreshId("_nw");
+ string nw = idGenerator.FreshId("_nw");
Indent(indent);
wr.Write("var {0} = ", nw);
TrAssignmentRhs(rhs); // in this case, this call will not require us to spill any let variables first
@@ -1786,7 +1788,7 @@ namespace Microsoft.Dafny {
// TODO: What to do here? When does this happen, what does it mean?
} else if (!s.Method.IsStatic) {
- string inTmp = IdGenerator.FreshId("_in");
+ string inTmp = idGenerator.FreshId("_in");
inTmps.Add(inTmp);
Indent(indent);
wr.Write("var {0} = ", inTmp);
@@ -1796,7 +1798,7 @@ namespace Microsoft.Dafny {
for (int i = 0; i < s.Method.Ins.Count; i++) {
Formal p = s.Method.Ins[i];
if (!p.IsGhost) {
- string inTmp = IdGenerator.FreshId("_in");
+ string inTmp = idGenerator.FreshId("_in");
inTmps.Add(inTmp);
Indent(indent);
wr.Write("var {0} = ", inTmp);
@@ -1838,7 +1840,7 @@ namespace Microsoft.Dafny {
for (int i = 0; i < s.Method.Outs.Count; i++) {
Formal p = s.Method.Outs[i];
if (!p.IsGhost) {
- string target = IdGenerator.FreshId("_out");
+ string target = idGenerator.FreshId("_out");
outTmps.Add(target);
Indent(indent);
wr.WriteLine("{0} {1};", TypeName(s.Lhs[i].Type), target);
@@ -1894,8 +1896,6 @@ namespace Microsoft.Dafny {
}
}
- FreshIdGenerator IdGenerator = new FreshIdGenerator();
-
/// <summary>
/// Before calling TrAssignmentRhs(rhs), the caller must have spilled the let variables declared in "rhs".
/// </summary>
@@ -1941,7 +1941,7 @@ namespace Microsoft.Dafny {
TrStmt(ss, indent + IndentAmount);
if (ss.Labels != null) {
Indent(indent); // labels are not indented as much as the statements
- wr.WriteLine("after_{0}: ;", ss.Labels.Data.AssignUniqueId("after_", IdGenerator));
+ wr.WriteLine("after_{0}: ;", ss.Labels.Data.AssignUniqueId("after_", idGenerator));
}
}
}
@@ -2262,7 +2262,7 @@ namespace Microsoft.Dafny {
string arg;
var fce = actual as FunctionCallExpr;
if (fce == null || fce.CoCall != FunctionCallExpr.CoCallResolution.Yes) {
- string varName = IdGenerator.FreshId("_ac");
+ string varName = idGenerator.FreshId("_ac");
arg = varName;
wr.Write("var {0} = ", varName);
@@ -2271,7 +2271,7 @@ namespace Microsoft.Dafny {
} else {
var sw = new StringWriter();
CompileFunctionCallExpr(fce, sw, (exp) => {
- string varName = IdGenerator.FreshId("_ac");
+ string varName = idGenerator.FreshId("_ac");
sw.Write(varName);
wr.Write("var {0} = ", varName);
@@ -2626,7 +2626,7 @@ namespace Microsoft.Dafny {
// }
// }(src)
- string source = IdGenerator.FreshId("_source");
+ string source = idGenerator.FreshId("_source");
wr.Write("new Dafny.Helpers.Function<{0}, {1}>(delegate ({0} {2}) {{ ", TypeName(e.Source.Type), TypeName(e.Type), source);
if (e.Cases.Count == 0) {