Skip to content

Commit 5bc4328

Browse files
committed
Yet more compiler code updates.
1 parent dcb3150 commit 5bc4328

File tree

12 files changed

+253
-445
lines changed

12 files changed

+253
-445
lines changed

Clojure/Clojure/CljCompiler/Ast/CaseExpr.cs

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using clojure.lang.CljCompiler.Context;
1212
using System;
1313
using System.Collections.Generic;
14-
using System.Linq;
1514
using System.Reflection.Emit;
1615

1716
namespace clojure.lang.CljCompiler.Ast
@@ -21,39 +20,39 @@ public class CaseExpr : Expr, MaybePrimitiveExpr
2120
#region Data
2221

2322
readonly LocalBindingExpr _expr;
24-
public LocalBindingExpr Expr { get { return _expr; } }
23+
public LocalBindingExpr Expr => _expr;
2524

2625
readonly int _shift, _mask;
27-
public int Shift { get { return _shift; } }
28-
public int Mask { get { return _mask; } }
26+
public int Shift => _shift;
27+
public int Mask => _mask;
2928

3029
readonly int _low, _high;
31-
public int Low { get { return _low; } }
32-
public int High { get { return _high; } }
30+
public int Low => _low;
31+
public int High => _high;
3332

3433
readonly Expr _defaultExpr;
35-
public Expr DefaultExpr { get { return _defaultExpr; } }
34+
public Expr DefaultExpr => _defaultExpr;
3635

3736
readonly SortedDictionary<int, Expr> _tests;
38-
public SortedDictionary<int, Expr> Tests { get { return _tests; } }
37+
public SortedDictionary<int, Expr> Tests => _tests;
3938

4039
readonly Dictionary<int, Expr> _thens;
41-
public Dictionary<int, Expr> Thens { get { return _thens; } }
40+
public Dictionary<int, Expr> Thens => _thens;
4241

4342
readonly IPersistentMap _sourceSpan;
44-
public IPersistentMap SourceSpan { get { return _sourceSpan; } }
43+
public IPersistentMap SourceSpan => _sourceSpan;
4544

4645
readonly Keyword _switchType;
47-
public Keyword SwitchType { get { return _switchType; } }
46+
public Keyword SwitchType => _switchType;
4847

4948
readonly Keyword _testType;
50-
public Keyword TestType { get { return _testType; } }
49+
public Keyword TestType => _testType;
5150

5251
readonly IPersistentSet _skipCheck;
53-
public IPersistentSet SkipCheck { get { return _skipCheck; } }
52+
public IPersistentSet SkipCheck => _skipCheck;
5453

5554
readonly Type _returnType;
56-
public Type ReturnType { get { return _returnType; } }
55+
public Type ReturnType => _returnType;
5756

5857
#endregion
5958

@@ -103,15 +102,9 @@ public CaseExpr(IPersistentMap sourceSpan, LocalBindingExpr expr, int shift, int
103102

104103
#region Type munging
105104

106-
public bool HasClrType
107-
{
108-
get { return _returnType != null; }
109-
}
110-
111-
public Type ClrType
112-
{
113-
get { return _returnType; }
114-
}
105+
public bool HasClrType => _returnType is not null;
106+
107+
public Type ClrType => _returnType;
115108

116109
#endregion
117110

@@ -147,8 +140,8 @@ public Expr Parse(ParserContext pcon, object frm)
147140
LocalBindingExpr testexpr = (LocalBindingExpr)Compiler.Analyze(pcon.SetRhc(RHC.Expression), exprForm);
148141

149142

150-
SortedDictionary<int, Expr> tests = new();
151-
Dictionary<int, Expr> thens = new();
143+
SortedDictionary<int, Expr> tests = [];
144+
Dictionary<int, Expr> thens = [];
152145

153146
foreach (IMapEntry me in caseMap)
154147
{
@@ -188,11 +181,8 @@ public Expr Parse(ParserContext pcon, object frm)
188181

189182
#region eval
190183

191-
public object Eval()
192-
{
193-
throw new InvalidOperationException("Can't eval case");
194-
}
195-
184+
public object Eval() => throw new InvalidOperationException("Can't eval case");
185+
196186
#endregion
197187

198188
#region Code generation
@@ -211,19 +201,16 @@ public object Eval()
211201
// end
212202
// end_label:
213203

214-
public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
215-
{
216-
DoEmit(rhc, objx, ilg, false);
217-
}
218-
204+
public void Emit(RHC rhc, ObjExpr objx, CljILGen ilg) => DoEmit(rhc, objx, ilg, false);
205+
219206
public void DoEmit(RHC rhc, ObjExpr objx, CljILGen ilg, bool emitUnboxed)
220207
{
221208
GenContext.EmitDebugInfo(ilg, _sourceSpan);
222209

223210
Label defaultLabel = ilg.DefineLabel();
224211
Label endLabel = ilg.DefineLabel();
225212

226-
SortedDictionary<int, Label> thenLabels = new();
213+
SortedDictionary<int, Label> thenLabels = [];
227214
foreach (int i in _tests.Keys)
228215
thenLabels[i] = ilg.DefineLabel();
229216

@@ -247,20 +234,20 @@ public void DoEmit(RHC rhc, ObjExpr objx, CljILGen ilg, bool emitUnboxed)
247234
GenContext.SetLocalName(hashLoc, "test");
248235
ilg.Emit(OpCodes.Stloc, hashLoc);
249236

250-
EmitSparseCaseTests(
237+
CaseExpr.EmitSparseCaseTests(
251238
rhc, objx, ilg,
252239
hashLoc,
253240
0, thenLabels.Count - 1,
254-
_tests.Keys.ToArray<int>(),
241+
[.. _tests.Keys],
255242
testLabels,
256-
thenLabels.Values.ToArray<Label>(),
243+
[.. thenLabels.Values],
257244
defaultLabel);
258245
}
259246
else
260247
{
261248
Label[] la = new Label[(_high - _low) + 1];
262249
for (int i = _low; i <= _high; i++)
263-
la[i - _low] = thenLabels.ContainsKey(i) ? thenLabels[i] : defaultLabel;
250+
la[i - _low] = thenLabels.TryGetValue(i, out Label value) ? value : defaultLabel;
264251
ilg.EmitInt(_low);
265252
ilg.Emit(OpCodes.Sub);
266253
ilg.Emit(OpCodes.Switch, la);
@@ -289,9 +276,9 @@ public void DoEmit(RHC rhc, ObjExpr objx, CljILGen ilg, bool emitUnboxed)
289276
}
290277

291278

292-
private int ComputeMidIndex(int leftIndex, int rightIndex) => leftIndex + (rightIndex - leftIndex) / 2;
279+
private static int ComputeMidIndex(int leftIndex, int rightIndex) => leftIndex + (rightIndex - leftIndex) / 2;
293280

294-
private void EmitSparseCaseTests(
281+
private static void EmitSparseCaseTests(
295282
RHC rhc,
296283
ObjExpr objx,
297284
CljILGen ilg,
@@ -310,7 +297,7 @@ private void EmitSparseCaseTests(
310297
return;
311298
}
312299

313-
int midIndex = ComputeMidIndex(leftIndex, rightIndex);
300+
int midIndex = CaseExpr.ComputeMidIndex(leftIndex, rightIndex);
314301

315302
ilg.MarkLabel(testLabels[midIndex]);
316303

@@ -339,31 +326,31 @@ private void EmitSparseCaseTests(
339326
ilg.Emit(OpCodes.Ldloc, hashLoc); // load the hash value of the test expression
340327
ilg.EmitInt(thenValues[midIndex]);
341328

342-
ilg.Emit(OpCodes.Bgt, testLabels[ComputeMidIndex(newLeft, rightIndex)]); // branch right
343-
ilg.Emit(OpCodes.Br, testLabels[ComputeMidIndex(leftIndex, newRight)]); // branch left
329+
ilg.Emit(OpCodes.Bgt, testLabels[CaseExpr.ComputeMidIndex(newLeft, rightIndex)]); // branch right
330+
ilg.Emit(OpCodes.Br, testLabels[CaseExpr.ComputeMidIndex(leftIndex, newRight)]); // branch left
344331

345-
EmitSparseCaseTests(rhc, objx, ilg, hashLoc, leftIndex, newRight, thenValues, testLabels, thenLabels, defaultLabel);
346-
EmitSparseCaseTests(rhc, objx, ilg, hashLoc, newLeft, rightIndex, thenValues, testLabels, thenLabels, defaultLabel);
332+
CaseExpr.EmitSparseCaseTests(rhc, objx, ilg, hashLoc, leftIndex, newRight, thenValues, testLabels, thenLabels, defaultLabel);
333+
CaseExpr.EmitSparseCaseTests(rhc, objx, ilg, hashLoc, newLeft, rightIndex, thenValues, testLabels, thenLabels, defaultLabel);
347334
}
348335
else if (leftExists)
349336
{
350337
// No branch to right
351338
ilg.Emit(OpCodes.Ldloc, hashLoc); // load the hash value of the test expression
352339
ilg.EmitInt(thenValues[midIndex]);
353-
ilg.Emit(OpCodes.Blt, testLabels[ComputeMidIndex(leftIndex, newRight)]); // branch left
340+
ilg.Emit(OpCodes.Blt, testLabels[CaseExpr.ComputeMidIndex(leftIndex, newRight)]); // branch left
354341
ilg.Emit(OpCodes.Br, defaultLabel); // no right, so default
355342

356-
EmitSparseCaseTests(rhc, objx, ilg, hashLoc, leftIndex, newRight, thenValues, testLabels, thenLabels, defaultLabel);
343+
CaseExpr.EmitSparseCaseTests(rhc, objx, ilg, hashLoc, leftIndex, newRight, thenValues, testLabels, thenLabels, defaultLabel);
357344
}
358345
else if (rightExists)
359346
{
360347
// No branch to left
361348
ilg.Emit(OpCodes.Ldloc, hashLoc); // load the hash value of the test expression
362349
ilg.EmitInt(thenValues[midIndex]);
363-
ilg.Emit(OpCodes.Bgt, testLabels[ComputeMidIndex(newLeft, rightIndex)]); // branch right
350+
ilg.Emit(OpCodes.Bgt, testLabels[CaseExpr.ComputeMidIndex(newLeft, rightIndex)]); // branch right
364351
ilg.Emit(OpCodes.Br, defaultLabel); // no left, so default
365352

366-
EmitSparseCaseTests(rhc, objx, ilg, hashLoc, newLeft, rightIndex, thenValues, testLabels, thenLabels, defaultLabel);
353+
CaseExpr.EmitSparseCaseTests(rhc, objx, ilg, hashLoc, newLeft, rightIndex, thenValues, testLabels, thenLabels, defaultLabel);
367354
}
368355
else
369356
{
@@ -373,7 +360,7 @@ private void EmitSparseCaseTests(
373360
}
374361
}
375362

376-
bool IsShiftMasked { get { return _mask != 0; } }
363+
bool IsShiftMasked => _mask != 0;
377364

378365
void EmitShiftMask(CljILGen ilg)
379366
{
@@ -388,7 +375,7 @@ void EmitShiftMask(CljILGen ilg)
388375

389376
private void EmitExprForInts(ObjExpr objx, CljILGen ilg, Type exprType, Label defaultLabel)
390377
{
391-
if (exprType == null)
378+
if (exprType is null)
392379
{
393380
if (RT.booleanCast(RT.WarnOnReflectionVar.deref()))
394381
{
@@ -425,7 +412,7 @@ private void EmitExprForInts(ObjExpr objx, CljILGen ilg, Type exprType, Label de
425412

426413
private void EmitThenForInts(ObjExpr objx, CljILGen ilg, Type exprType, Expr test, Expr then, Label defaultLabel, bool emitUnboxed)
427414
{
428-
if (exprType == null)
415+
if (exprType is null)
429416
{
430417
_expr.Emit(RHC.Expression, objx, ilg);
431418
test.Emit(RHC.Expression, objx, ilg);
@@ -514,11 +501,8 @@ public bool HasNormalExit()
514501

515502
public bool CanEmitPrimitive => Util.IsPrimitive(_returnType);
516503

517-
public void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
518-
{
519-
DoEmit(rhc, objx, ilg, true);
520-
}
521-
504+
public void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg) => DoEmit(rhc, objx, ilg, true);
505+
522506
#endregion
523507
}
524508
}

Clojure/Clojure/CljCompiler/Ast/FieldExpr.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88
* You must not remove this notice, or any other, from this software.
99
**/
1010

11-
/**
12-
* Author: David Miller
13-
**/
14-
1511
using System;
1612

17-
1813
namespace clojure.lang.CljCompiler.Ast
1914
{
2015
public abstract class FieldOrPropertyExpr : HostExpr, AssignableExpr

Clojure/Clojure/CljCompiler/Ast/FnExpr.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using System.Linq;
1515
using System.Reflection;
1616
using System.Reflection.Emit;
17-
using System.Transactions;
1817

1918
namespace clojure.lang.CljCompiler.Ast
2019
{

Clojure/Clojure/CljCompiler/Ast/HostExpr.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
* You must not remove this notice, or any other, from this software.
99
**/
1010

11-
/**
12-
* Author: David Miller
13-
**/
14-
1511
using System;
1612
using System.Collections.Generic;
1713
using System.Reflection;

0 commit comments

Comments
 (0)