Skip to content

Commit dcb3150

Browse files
committed
more compiler code updating
1 parent 24faf30 commit dcb3150

File tree

8 files changed

+262
-483
lines changed

8 files changed

+262
-483
lines changed

Clojure/Clojure/CljCompiler/Ast/FnExpr.cs

Lines changed: 21 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,40 @@
1414
using System.Linq;
1515
using System.Reflection;
1616
using System.Reflection.Emit;
17+
using System.Transactions;
1718

1819
namespace clojure.lang.CljCompiler.Ast
1920
{
20-
public class FnExpr : ObjExpr
21+
public class FnExpr(object tag) : ObjExpr(tag)
2122
{
2223
#region Data
2324

2425
static readonly Keyword KW_ONCE = Keyword.intern(null, "once");
2526

2627
FnMethod _variadicMethod = null;
27-
public FnMethod VariadicMethod { get { return _variadicMethod; } }
28-
bool IsVariadic { get { return _variadicMethod != null; } }
28+
public FnMethod VariadicMethod => _variadicMethod;
29+
bool IsVariadic => _variadicMethod is not null;
2930

3031
bool _hasMeta;
31-
protected override bool SupportsMeta { get { return _hasMeta; } }
32+
protected override bool SupportsMeta => _hasMeta;
3233

3334
bool _hasEnclosingMethod;
3435

3536
private readonly int _dynMethodMapKey = RT.nextID();
36-
public int DynMethodMapKey { get { return _dynMethodMapKey; } }
37+
public int DynMethodMapKey => _dynMethodMapKey;
3738

3839
Type _cachedType;
3940

4041
#endregion
4142

42-
#region Ctors
43-
44-
public FnExpr(object tag)
45-
: base(tag)
46-
{
47-
}
48-
49-
#endregion
50-
5143
#region Misc
5244

5345
// This naming convention drawn from the Java code.
5446
internal void ComputeNames(ISeq form, string name)
5547
{
5648
ObjMethod enclosingMethod = (ObjMethod)Compiler.MethodVar.deref();
5749

58-
string baseName = enclosingMethod != null
50+
string baseName = enclosingMethod is not null
5951
? enclosingMethod.Objx.Name
6052
: Compiler.munge(Compiler.CurrentNamespace.Name.Name) + "$";
6153

@@ -67,7 +59,7 @@ internal void ComputeNames(ISeq form, string name)
6759
}
6860
else
6961
{
70-
if (name == null)
62+
if (name is null)
7163
name = "fn__" + RT.nextID();
7264
else if (enclosingMethod != null)
7365
name += "__" + RT.nextID();
@@ -83,20 +75,13 @@ internal void ComputeNames(ISeq form, string name)
8375

8476
#region Type munging
8577

86-
public override bool HasClrType
87-
{
88-
get
89-
{
90-
return true;
91-
}
92-
}
78+
public override bool HasClrType => true;
9379

9480
public override Type ClrType
9581
{
9682
get
9783
{
98-
if (_cachedType == null)
99-
_cachedType = _tag != null ? HostExpr.TagToType(_tag) : typeof(AFunction);
84+
_cachedType ??= _tag is not null ? HostExpr.TagToType(_tag) : typeof(AFunction);
10085
return _cachedType;
10186
}
10287
}
@@ -127,7 +112,7 @@ public static Expr Parse(ParserContext pcon, ISeq form, string name)
127112

128113
fn.ComputeNames(form, name);
129114

130-
List<string> prims = new();
115+
List<string> prims = [];
131116

132117
//arglist might be preceded by symbol naming this fn
133118
Symbol nm = RT.second(form) as Symbol;
@@ -148,7 +133,6 @@ public static Expr Parse(ParserContext pcon, ISeq form, string name)
148133
GenContext newContext = context.WithNewDynInitHelper(fn.InternalName + "__dynInitHelper_" + RT.nextID().ToString());
149134
Var.pushThreadBindings(RT.map(Compiler.CompilerContextVar, newContext));
150135

151-
152136
try
153137
{
154138
try
@@ -162,7 +146,7 @@ public static Expr Parse(ParserContext pcon, ISeq form, string name)
162146
Compiler.ProtocolCallsitesVar, PersistentVector.EMPTY,
163147
Compiler.VarCallsitesVar, Compiler.EmptyVarCallSites(),
164148
Compiler.NoRecurVar, null));
165-
SortedDictionary<int, FnMethod> methods = new();
149+
SortedDictionary<int, FnMethod> methods = [];
166150
FnMethod variadicMethod = null;
167151
bool usesThis = false;
168152

@@ -205,7 +189,7 @@ public static Expr Parse(ParserContext pcon, ISeq form, string name)
205189
for (ISeq s = RT.seq(allMethods); s != null; s = s.next())
206190
{
207191
FnMethod fm = s.first() as FnMethod;
208-
if (fm.Locals != null)
192+
if (fm.Locals is not null)
209193
{
210194
for (ISeq sl = RT.seq(RT.keys(fm.Locals)); sl != null; sl = sl.next())
211195
{
@@ -235,7 +219,7 @@ public static Expr Parse(ParserContext pcon, ISeq form, string name)
235219

236220

237221
IPersistentMap fmeta = RT.meta(origForm);
238-
if (fmeta != null)
222+
if (fmeta is not null)
239223
fmeta = fmeta.without(RT.LineKey).without(RT.ColumnKey).without(RT.SourceSpanKey).without(RT.FileKey).without(retKey);
240224
fn._hasMeta = RT.count(fmeta) > 0;
241225

@@ -259,7 +243,7 @@ public static Expr Parse(ParserContext pcon, ISeq form, string name)
259243
}
260244
finally
261245
{
262-
if (newContext != null)
246+
if (newContext is not null)
263247
Var.popThreadBindings();
264248
}
265249
}
@@ -269,26 +253,13 @@ internal void AddMethod(FnMethod method)
269253
Methods = RT.conj(Methods, method);
270254
}
271255

272-
273-
//static bool HasPrimDecls(ISeq forms)
274-
//{
275-
// for (ISeq s = forms; s != null; s = RT.next(s))
276-
// if (FnMethod.HasPrimInterface((ISeq)RT.first(s)))
277-
// return true;
278-
279-
// return false;
280-
//}
281-
282-
//static readonly MethodInfo Method_FnExpr_GetDynMethod = typeof(FnExpr).GetMethod("GetDynMethod");
283-
//static readonly MethodInfo Method_FnExpr_GetCompiledConstants = typeof(FnExpr).GetMethod("GetCompiledConstants");
284-
285-
static readonly Dictionary<int, Dictionary<int, DynamicMethod>> DynMethodMap = new();
286-
static readonly Dictionary<int, object[]> ConstantsMap = new();
256+
static readonly Dictionary<int, Dictionary<int, DynamicMethod>> DynMethodMap = [];
257+
static readonly Dictionary<int, object[]> ConstantsMap = [];
287258

288259
public static DynamicMethod GetDynMethod(int key, int arity)
289260
{
290261
DynamicMethod dm = DynMethodMap[key][arity];
291-
if (dm == null)
262+
if (dm is null)
292263
Console.WriteLine("Bad dynmeth retrieval");
293264
return dm;
294265
// Dictionary<int, WeakReference > dict = DynMethodMap[key];
@@ -299,49 +270,19 @@ public static DynamicMethod GetDynMethod(int key, int arity)
299270
public static object[] GetCompiledConstants(int key)
300271
{
301272
return ConstantsMap[key];
302-
//WeakReference wr = ConstantsMap[key];
303-
//return (object[])wr.Target;
304273
}
305274

306-
//private static int GetMethodKey(FnMethod method)
307-
//{
308-
// int arity = method.IsVariadic
309-
// ? method.RequiredArity + 1 // to avoid the non-variadics, the last of which may have NumParams == this method RequireArity
310-
// : method.NumParams;
311-
312-
// return arity;
313-
//}
314-
315-
//private void EmitGetDynMethod(int arity, CljILGen ilg)
316-
//{
317-
// ilg.EmitInt(DynMethodMapKey);
318-
// ilg.EmitInt(arity);
319-
// ilg.Emit(OpCodes.Call,Method_FnExpr_GetDynMethod);
320-
//}
321-
322-
//private void EmitGetCompiledConstants(CljILGen ilg)
323-
//{
324-
// ilg.EmitInt(DynMethodMapKey);
325-
// ilg.Emit(OpCodes.Call, Method_FnExpr_GetCompiledConstants);
326-
//}
327-
328275
#endregion
329276

330277
#region eval
331278

332-
public override object Eval()
333-
{
334-
return base.Eval();
335-
}
279+
public override object Eval() => base.Eval();
336280

337281
#endregion
338282

339283
#region Code generation
340284

341-
internal void EmitForDefn(ObjExpr objx, CljILGen ilg)
342-
{
343-
Emit(RHC.Expression, objx, ilg);
344-
}
285+
internal void EmitForDefn(ObjExpr objx, CljILGen ilg) => Emit(RHC.Expression, objx, ilg);
345286

346287
protected override void EmitMethods(TypeBuilder tb)
347288
{
@@ -354,7 +295,7 @@ protected override void EmitMethods(TypeBuilder tb)
354295
if (IsVariadic)
355296
EmitGetRequiredArityMethod(TypeBuilder, _variadicMethod.RequiredArity);
356297

357-
List<int> supportedArities = new();
298+
List<int> supportedArities = [];
358299
for (ISeq s = RT.seq(Methods); s != null; s = s.next())
359300
{
360301
FnMethod method = (FnMethod)s.first();

Clojure/Clojure/CljCompiler/Ast/FnMethod.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class FnMethod : ObjMethod
2222
#region Data
2323

2424
protected IPersistentVector _reqParms = PersistentVector.EMPTY; // localbinding => localbinding
25-
public IPersistentVector ReqParms { get { return _reqParms; } }
25+
public IPersistentVector ReqParms => _reqParms;
2626

2727
protected LocalBinding _restParm = null;
28-
public LocalBinding RestParm { get { return _restParm; } }
28+
public LocalBinding RestParm => _restParm;
2929

3030
Type[] _argTypes;
3131
// Accessor for _argTypes: see below.
@@ -34,7 +34,7 @@ public class FnMethod : ObjMethod
3434
// accessor for _retType: see below.
3535

3636
string _prim;
37-
public override string Prim { get { return _prim; } }
37+
public override string Prim => _prim;
3838

3939
#endregion
4040

@@ -59,13 +59,13 @@ public FnMethod(FnExpr fn, ObjMethod parent, BodyExpr body)
5959

6060
#region ObjMethod methods
6161

62-
public override bool IsVariadic { get { return _restParm != null; } }
62+
public override bool IsVariadic => _restParm is not null;
6363

64-
public override int NumParams { get { return _reqParms.count() + (IsVariadic ? 1 : 0); } }
64+
public override int NumParams => _reqParms.count() + (IsVariadic ? 1 : 0);
6565

66-
public override int RequiredArity { get { return _reqParms.count(); } }
66+
public override int RequiredArity => _reqParms.count();
6767

68-
public override string MethodName { get { return IsVariadic ? "doInvoke" : "invoke"; } }
68+
public override string MethodName => IsVariadic ? "doInvoke" : "invoke";
6969

7070
public override Type[] ArgTypes
7171
{
@@ -87,7 +87,7 @@ public override Type ReturnType
8787
{
8888
get
8989
{
90-
if (_prim != null) // Objx.IsStatic)
90+
if (_prim is not null) // Objx.IsStatic)
9191
return _retType;
9292

9393
return typeof(object);
@@ -154,16 +154,16 @@ internal static FnMethod Parse(FnExpr fn, ISeq form, object retTag)
154154

155155
ParamParseState paramState = ParamParseState.Required;
156156
IPersistentVector argLocals = PersistentVector.EMPTY;
157-
List<Type> argTypes = new();
157+
List<Type> argTypes = [];
158158

159159
int parmsCount = parms.count();
160160

161161
for (int i = 0; i < parmsCount; i++)
162162
{
163-
if (!(parms.nth(i) is Symbol))
163+
if (parms.nth(i) is not Symbol)
164164
throw new ParseException("fn params must be Symbols");
165165
Symbol p = (Symbol)parms.nth(i);
166-
if (p.Namespace != null)
166+
if (p.Namespace is not null)
167167
throw new ParseException("Can't use qualified name as parameter: " + p);
168168
if (p.Equals(Compiler.AmpersandSym))
169169
{
@@ -212,7 +212,7 @@ internal static FnMethod Parse(FnExpr fn, ISeq form, object retTag)
212212
throw new ParseException(string.Format("Can't specify more than {0} parameters", Compiler.MaxPositionalArity));
213213
Compiler.LoopLocalsVar.set(argLocals);
214214
method.ArgLocals = argLocals;
215-
method._argTypes = argTypes.ToArray();
215+
method._argTypes = [.. argTypes];
216216
method.Body = (new BodyExpr.Parser()).Parse(new ParserContext(RHC.Return), body);
217217
return method;
218218
}
@@ -228,14 +228,9 @@ internal static FnMethod Parse(FnExpr fn, ISeq form, object retTag)
228228

229229
public static char TypeChar(object x)
230230
{
231-
//Type t = null;
232-
//if (x is Type)
233-
// t = (Type)x;
234-
//else if (x is Symbol)
235-
// t = Compiler.PrimType((Symbol)x);
236231
Type t = x as Type ?? Compiler.PrimType(x as Symbol);
237232

238-
if (t == null || !t.IsPrimitive)
233+
if (t is null || !t.IsPrimitive)
239234
return 'O';
240235
if (t == typeof(long))
241236
return 'L';
@@ -408,7 +403,6 @@ private void DoEmitStatic(ObjExpr fn, TypeBuilder tb)
408403
if (Body.HasNormalExit())
409404
primIlg.Emit(OpCodes.Ret);
410405
}
411-
412406
}
413407

414408
private void DoEmitPrim(ObjExpr fn, TypeBuilder tb)

0 commit comments

Comments
 (0)