Skip to content

Commit 0eae960

Browse files
committed
Code improvements in (Instance/Static/)MethodExpr and Reflector.
1 parent d37af3b commit 0eae960

File tree

4 files changed

+63
-100
lines changed

4 files changed

+63
-100
lines changed

Clojure/Clojure/CljCompiler/Ast/InstanceMethodExpr.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
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;
1814

19-
2015
namespace clojure.lang.CljCompiler.Ast
2116
{
2217
public class InstanceMethodExpr : MethodExpr
@@ -48,7 +43,7 @@ public InstanceMethodExpr(
4843
_target = target;
4944
_qualifyingType = qualifyingType ?? (target.HasClrType ? target.ClrType : null);
5045

51-
if (target.HasClrType && target.ClrType == null)
46+
if (target.HasClrType && target.ClrType is null)
5247
throw new ArgumentException(String.Format("Attempt to call instance method {0} on nil", methodName));
5348

5449
if (_qualifyingType != null)
@@ -94,9 +89,9 @@ public override object Eval()
9489
object[] argvals = new object[_args.Count];
9590
for (int i = 0; i < _args.Count; i++)
9691
argvals[i] = _args[i].ArgExpr.Eval();
97-
if (_method != null)
92+
if (_method is not null)
9893
return Reflector.InvokeMethod(_method, targetVal, argvals);
99-
if (_qualifyingType != null)
94+
if (_qualifyingType is not null)
10095
return Reflector.CallInstanceMethod(_qualifyingType, _methodName, _typeArgs, targetVal, argvals);
10196
else
10297
return Reflector.CallInstanceMethod(_methodName, _typeArgs, targetVal, argvals);
@@ -115,17 +110,13 @@ public override object Eval()
115110

116111
#region Type mangling
117112

118-
public override bool HasClrType
119-
{
120-
get { return _method != null || _tag != null; }
121-
}
122-
113+
public override bool HasClrType => _method is not null || _tag is not null;
114+
123115
public override Type ClrType
124116
{
125117
get
126118
{
127-
if (_cachedType == null)
128-
_cachedType = Compiler.RetType((_tag != null ? HostExpr.TagToType(_tag) : null), _method?.ReturnType);
119+
_cachedType ??= Compiler.RetType((_tag is not null ? HostExpr.TagToType(_tag) : null), _method?.ReturnType);
129120
return _cachedType;
130121
}
131122
}

Clojure/Clojure/CljCompiler/Ast/MethodExpr.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public override void Emit(RHC rhc, ObjExpr objx, CljILGen ilg)
7878

7979
Type retType;
8080

81-
if (_method != null)
81+
if (_method is not null)
8282
{
8383
EmitForMethod(objx, ilg);
8484
retType = _method.ReturnType;
@@ -98,7 +98,7 @@ public override void EmitUnboxed(RHC rhc, ObjExpr objx, CljILGen ilg)
9898
{
9999
GenContext.EmitDebugInfo(ilg, _spanMap);
100100

101-
if (_method != null)
101+
if (_method is not null)
102102
{
103103
EmitForMethod(objx, ilg);
104104
}
@@ -144,7 +144,7 @@ private void EmitForMethod(ObjExpr objx, CljILGen ilg)
144144
}
145145
public static readonly MethodInfo Method_MethodExpr_GetDelegate = typeof(MethodExpr).GetMethod("GetDelegate");
146146

147-
public static readonly Dictionary<int, Delegate> DelegatesMap = new();
147+
public static readonly Dictionary<int, Delegate> DelegatesMap = [];
148148

149149
public static Delegate GetDelegate(int key)
150150
{
@@ -178,7 +178,7 @@ private void EmitComplexCall(ObjExpr objx, CljILGen ilg)
178178
{
179179
i++;
180180
Expr e = ha.ArgExpr;
181-
Type argType = e.HasClrType && e.ClrType != null && e.ClrType.IsPrimitive ? e.ClrType : typeof(object);
181+
Type argType = e.HasClrType && e.ClrType is not null && e.ClrType.IsPrimitive ? e.ClrType : typeof(object);
182182

183183
switch (ha.ParamType)
184184
{
@@ -218,22 +218,27 @@ private void EmitComplexCall(ObjExpr objx, CljILGen ilg)
218218
// Unfortunately, the Expression.Dynamic method does not respect byRef parameters.
219219
// The workaround appears to be to roll your delegate type and then use Expression.MakeDynamic, as below.
220220

221-
List<Type> callsiteParamTypes = new(paramTypes.Count + 1)
222-
{
223-
typeof(System.Runtime.CompilerServices.CallSite)
224-
};
225-
callsiteParamTypes.AddRange(paramTypes);
221+
List<Type> callsiteParamTypes =
222+
[
223+
typeof(System.Runtime.CompilerServices.CallSite), .. paramTypes
224+
];
226225

227226
// PLAN9: Seeing if replacing this helps.
228227
//Type dynType = Microsoft.Scripting.Generation.Snippets.Shared.DefineDelegate("__interop__", returnType, callsiteParamTypes.ToArray());
229228
GenContext context = Compiler.CompilerContextVar.deref() as GenContext;
230-
DynInitHelper dih = context?.DynInitHelper;
231-
if (dih is null)
232-
throw new InvalidOperationException("Don't know how to handle callsite in this case");
233-
Type dynType = dih.MakeDelegateType("__interop__", callsiteParamTypes.ToArray(), returnType);
229+
DynInitHelper dih = (context?.DynInitHelper) ?? throw new InvalidOperationException("Don't know how to handle callsite in this case");
230+
Type dynType = dih.MakeDelegateType("__interop__", [.. callsiteParamTypes], returnType);
234231

235232
DynamicExpression dyn = Expression.MakeDynamic(dynType, binder, paramExprs);
236-
EmitDynamicCallPreamble(dyn, _spanMap, "__interop_" + _methodName + RT.nextID(), returnType, paramExprs, paramTypes.ToArray(), ilg, out Type delType, out MethodBuilder mbLambda);
233+
EmitDynamicCallPreamble(dyn,
234+
_spanMap,
235+
"__interop_" + _methodName + RT.nextID(),
236+
returnType,
237+
paramExprs,
238+
[.. paramTypes],
239+
ilg,
240+
out _,
241+
out MethodBuilder mbLambda);
237242

238243
// Emit target + args
239244

@@ -291,11 +296,11 @@ static FieldInfo GetCallSiteTarget(Type siteType)
291296
}
292297

293298

299+
#pragma warning disable IDE0060 // Remove unused parameter
294300
static public void EmitDynamicCallPreamble(DynamicExpression dyn, IPersistentMap spanMap, string methodName, Type returnType, IList<ParameterExpression> paramExprs, Type[] paramTypes, CljILGen ilg, out Type delType, out MethodBuilder mbLambda)
301+
#pragma warning restore IDE0060 // Remove unused parameter
295302
{
296-
GenContext context = Compiler.CompilerContextVar.deref() as GenContext;
297-
298-
if (context is null || context.DynInitHelper is null)
303+
if (Compiler.CompilerContextVar.deref() is not GenContext context || context.DynInitHelper is null)
299304
throw new InvalidOperationException("Don't know how to handle callsite in this case");
300305

301306
DynInitHelper.SiteInfo siteInfo = context.DynInitHelper.ComputeSiteInfo(dyn);
@@ -340,7 +345,6 @@ static public void EmitDynamicCallPreamble(DynamicExpression dyn, IPersistentMap
340345
ilg2.Emit(OpCodes.Ret);
341346
}
342347

343-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Standard API")]
344348
static public void EmitDynamicCallPostlude(MethodBuilder mbLambda, CljILGen ilg)
345349
{
346350
ilg.Emit(OpCodes.Call, mbLambda);
@@ -455,7 +459,7 @@ internal static void EmitConvertToType(CljILGen ilg, Type typeFrom, Type typeTo,
455459
// If the DLR folks had made this method public (instead of internal), I could call it directly.
456460
// Didn't feel like copying their code due to license/copyright.
457461

458-
MI_EmitConvertToType.Invoke(ilg, new Object[] { typeFrom, typeTo, isChecked });
462+
MI_EmitConvertToType.Invoke(ilg, [typeFrom, typeTo, isChecked]);
459463
}
460464

461465
#endregion

Clojure/Clojure/CljCompiler/Ast/StaticMethodExpr.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,14 @@ public static bool IsBoxedMath(MethodBase m)
7575

7676
#region Type mangling
7777

78-
public override bool HasClrType
79-
{
80-
get { return _method != null || _tag != null; }
81-
}
78+
public override bool HasClrType => _method is not null || _tag is not null;
79+
8280

8381
public override Type ClrType
8482
{
8583
get
8684
{
87-
if (_cachedType == null)
88-
_cachedType = Compiler.RetType((_tag != null) ? HostExpr.TagToType(_tag) : null, _method?.ReturnType);
85+
_cachedType ??= Compiler.RetType((_tag != null) ? HostExpr.TagToType(_tag) : null, _method?.ReturnType);
8986
return _cachedType;
9087
}
9188
}
@@ -101,7 +98,7 @@ public override object Eval()
10198
object[] argvals = new object[_args.Count];
10299
for (int i = 0; i < _args.Count; i++)
103100
argvals[i] = _args[i].ArgExpr.Eval();
104-
if (_method != null)
101+
if (_method is not null)
105102
return Reflector.InvokeMethod(_method, null, argvals);
106103
return Reflector.CallStaticMethod(_methodName, _typeArgs, _type, argvals);
107104
}
@@ -119,10 +116,7 @@ public override object Eval()
119116

120117
#region Code generation
121118

122-
protected override bool IsStaticCall
123-
{
124-
get { return true; }
125-
}
119+
protected override bool IsStaticCall => true;
126120

127121
protected override void EmitTargetExpression(ObjExpr objx, CljILGen ilg)
128122
{
@@ -135,16 +129,14 @@ protected override Type GetTargetType()
135129
return typeof(Type);
136130
}
137131

138-
internal bool CanEmitIntrinsicPredicate()
139-
{
140-
return _method != null && Intrinsics.HasPred(_method);
141-
}
132+
internal bool CanEmitIntrinsicPredicate() => _method is not null && Intrinsics.HasPred(_method);
133+
142134
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Standard API")]
143135
internal void EmitIntrinsicPredicate(RHC rhc, ObjExpr objx, CljILGen ilg, Label falseLabel)
144136
{
145137
GenContext.EmitDebugInfo(ilg, _spanMap);
146138

147-
if (_method != null)
139+
if (_method is not null)
148140
{
149141
MethodExpr.EmitTypedArgs(objx, ilg, _method.GetParameters(), _args);
150142
// JVM: clear locals

0 commit comments

Comments
 (0)