Skip to content

Commit 5b106f9

Browse files
committed
codegen IsRelease optional, always partial class, check struct values for using default instead of constant
1 parent 8bc747b commit 5b106f9

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

ExpressionDebugger/ExpressionCompilationOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public class ExpressionCompilationOptions
1111
public IEnumerable<Assembly> References { get; set; }
1212
public bool EmitFile { get; set; }
1313
public string RootPath { get; set; }
14-
public bool IsRelease { get; set; }
14+
public bool? IsRelease { get; set; }
1515
}
1616
}

ExpressionDebugger/ExpressionCompiler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.CodeAnalysis.Text;
55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
910
using System.Linq.Expressions;
@@ -85,12 +86,13 @@ from n in t.TypeNames
8586
var symbolsName = Path.ChangeExtension(assemblyName, "pdb");
8687

8788
var metadataReferences = references.Select(it => MetadataReference.CreateFromFile(it.Location));
89+
var isRelease = _options?.IsRelease ?? !Debugger.IsAttached;
8890
CSharpCompilation compilation = CSharpCompilation.Create(
8991
assemblyName,
9092
_codes,
9193
metadataReferences,
9294
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: new[] { "System" })
93-
.WithOptimizationLevel(_options?.IsRelease == true ? OptimizationLevel.Release : OptimizationLevel.Debug)
95+
.WithOptimizationLevel(isRelease ? OptimizationLevel.Release : OptimizationLevel.Debug)
9496
.WithPlatform(Platform.AnyCpu)
9597
);
9698

ExpressionDebugger/ExpressionDebuggerExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ public static Delegate CreateDelegate(this ExpressionTranslator translator, Asse
4848
var type = assembly.GetType(typeName);
4949
var method = type.GetMethod(definitions.MethodName ?? "Main");
5050
var obj = definitions.IsStatic ? null : Activator.CreateInstance(type);
51+
var flag = definitions.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
5152
foreach (var kvp in translator.Constants)
5253
{
53-
var field = type.GetField(kvp.Value);
54+
var field = type.GetField(kvp.Value, BindingFlags.NonPublic | flag);
5455
field.SetValue(obj, kvp.Key);
5556
}
5657
return method.CreateDelegate(translator.Expression.Type, obj);

ExpressionTranslator/ExpressionTranslator.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ namespace ExpressionDebugger
1313
{
1414
public class ExpressionTranslator : ExpressionVisitor
1515
{
16-
private const int Tabsize = 4;
16+
private const int _tabsize = 4;
1717
private StringWriter _writer;
1818
private int _indentLevel;
1919
private StringWriter _appendWriter;
2020

2121
private Dictionary<Type, string> _typeNames;
2222
private HashSet<string> _usings;
2323
private Dictionary<object, string> _constants;
24+
private Dictionary<Type, object> _defaults;
2425

2526
public Dictionary<object, string> Constants => _constants ?? (_constants = new Dictionary<object, string>());
2627
public Dictionary<Type, string> TypeNames => _typeNames ?? (_typeNames = new Dictionary<Type, string>());
@@ -74,7 +75,7 @@ private static int GetPrecedence(ExpressionType nodeType)
7475
case ExpressionType.MultiplyAssign:
7576
case ExpressionType.MultiplyAssignChecked:
7677
case ExpressionType.OrAssign:
77-
case ExpressionType.PowerAssign:
78+
//case ExpressionType.PowerAssign:
7879
case ExpressionType.Quote:
7980
case ExpressionType.RightShiftAssign:
8081
case ExpressionType.SubtractAssign:
@@ -156,9 +157,9 @@ private static int GetPrecedence(ExpressionType nodeType)
156157
case ExpressionType.UnaryPlus:
157158
return 13;
158159

159-
// Power
160-
case ExpressionType.Power:
161-
return 14;
160+
//// Power
161+
//case ExpressionType.Power:
162+
// return 14;
162163

163164
default:
164165
return 100;
@@ -191,7 +192,7 @@ private static bool ShouldGroup(Expression node, ExpressionType parentNodeType,
191192
case ExpressionType.Divide:
192193
case ExpressionType.Modulo:
193194
case ExpressionType.LeftShift:
194-
case ExpressionType.Power:
195+
//case ExpressionType.Power:
195196
case ExpressionType.RightShift:
196197
case ExpressionType.Subtract:
197198
case ExpressionType.SubtractChecked:
@@ -227,7 +228,7 @@ private void WriteLine()
227228
{
228229
_writer.WriteLine();
229230

230-
var spaceCount = _indentLevel * Tabsize;
231+
var spaceCount = _indentLevel * _tabsize;
231232
_writer.Write(new string(' ', spaceCount));
232233
}
233234

@@ -423,11 +424,6 @@ private string Translate(Type type)
423424
return "ushort";
424425
if (type == typeof(void))
425426
return "void";
426-
if (type.GetTypeInfo().IsNotPublic)
427-
{
428-
HasDynamic = true;
429-
return "dynamic";
430-
}
431427
#if !NETSTANDARD1_3
432428
if (typeof(IDynamicMetaObjectProvider).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
433429
{
@@ -791,6 +787,22 @@ private void WriteValue(object value)
791787
}
792788
else
793789
{
790+
var type = value.GetType();
791+
if (type.GetTypeInfo().IsValueType)
792+
{
793+
if (_defaults == null)
794+
_defaults = new Dictionary<Type, object>();
795+
if (!_defaults.TryGetValue(type, out var def))
796+
{
797+
def = Activator.CreateInstance(type);
798+
_defaults[type] = def;
799+
}
800+
if (value.Equals(def))
801+
{
802+
Write($"default({Translate(type)})");
803+
return;
804+
}
805+
}
794806
Write(GetConstant(value));
795807
}
796808
}
@@ -1792,7 +1804,7 @@ public override string ToString()
17921804
}
17931805

17941806
WriteModifier(true);
1795-
Write("class ", Definitions.TypeName);
1807+
Write("partial class ", Definitions.TypeName);
17961808
if (implements?.Any() == true)
17971809
{
17981810
Write(" : ", string.Join(", ", implements));
@@ -1803,7 +1815,7 @@ public override string ToString()
18031815
{
18041816
foreach (var constant in constants)
18051817
{
1806-
WriteModifier(true);
1818+
WriteModifier(false);
18071819
Write(constant);
18081820
}
18091821
WriteLine();

ExpressionTranslator/ExpressionTranslator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;netstandard1.3;net45;net40</TargetFrameworks>

0 commit comments

Comments
 (0)