Skip to content

Commit

Permalink
Some invalid CS is being written.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrawehr committed Apr 7, 2024
1 parent a3ee3f7 commit 729e432
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 9 deletions.
3 changes: 2 additions & 1 deletion NuGet.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<!--Begin: Package sources managed by Dependency Flow automation. Do not edit the sources below.-->
<!-- Begin: Package sources from dotnet-runtime -->
<!-- End: Package sources from dotnet-runtime -->
Expand All @@ -12,6 +11,8 @@
<add key="dotnet3.1" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet3.1/nuget/v3/index.json" />
<add key="dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
<add key="nightly_iot_builds" value="https://pkgs.dev.azure.com/dotnet/IoT/_packaging/nightly_iot_builds/nuget/v3/index.json" />
<add key="local" value="C:\projects\ILSpy\ICSharpCode.Decompiler\bin\Debug" />

</packageSources>
<disabledPackageSources />
</configuration>
2 changes: 1 addition & 1 deletion src/devices/Arduino/FirmataDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ internal void DisableAnalogReporting(int pinNumber, int analogChannel)
pwmCommandSequence.WriteByte((byte)0);
pwmCommandSequence.WriteByte((byte)FirmataCommand.END_SYSEX);
SendCommand(pwmCommandSequence);
}
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions tools/ArduinoCsCompiler/ArduinoCsCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ICSharpCode.Decompiler" Version="9.0.0.7618-preview1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.8.0" />
</ItemGroup>
<ItemGroup Condition="$(Configuration)=='Debug'">
<ProjectReference Include="..\..\src\devices\Arduino\Arduino.csproj" />
<ProjectReference Include="..\..\src\System.Device.Gpio\System.Device.Gpio.csproj" />
</ItemGroup>
<ItemGroup Condition="$(Configuration)!='Debug'">
<ProjectReference Include="..\..\src\System.Device.Gpio\System.Device.Gpio.csproj"/>
<ProjectReference Include="..\..\src\Iot.Device.Bindings\Iot.Device.Bindings.csproj"/>
<ProjectReference Include="..\..\src\System.Device.Gpio\System.Device.Gpio.csproj" />
<ProjectReference Include="..\..\src\Iot.Device.Bindings\Iot.Device.Bindings.csproj" />
</ItemGroup>

<ItemGroup>
Expand All @@ -41,4 +42,9 @@
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<Compile Include="NanoGenerator\ClassWrapper.cs" />
<Compile Include="NanoGenerator\FieldWrapper.cs" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions tools/ArduinoCsCompiler/ClassDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public ClassDeclaration(Type type, int dynamicSize, int staticSize, int newToken
NewToken = newToken;
_interfaces = interfaces;
Name = type.ClassSignature(true);
Namespace = type.Namespace ?? string.Empty;
SimpleName = type.Name;
ReadOnly = false;
}

Expand All @@ -38,6 +40,10 @@ public bool ReadOnly
internal set;
}

public string Namespace { get; }

public string SimpleName { get; }

public int NewToken
{
get;
Expand Down
17 changes: 12 additions & 5 deletions tools/ArduinoCsCompiler/ExecutionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,11 +1506,8 @@ public void SuppressNamespace(string namespacePrefix, bool includingSubNamespace

public void WritePatchedCodeFile(string file, IlCapabilities ilCapabilities)
{
using TextWriter tw = new StreamWriter("output.il");
foreach (var cls in _classes)
{
IlWriter.WriteClass(tw, cls, this);
}
var writer = new IlWriter(this, ilCapabilities);
writer.Write(CompilerSettings.ProcessName, file);
}

public void WriteMapFile(string tokenMapFile, IlCapabilities ilCapabilities)
Expand Down Expand Up @@ -1729,5 +1726,15 @@ public void AddReverseFieldLookupTable()

_logger.LogDebug($"Got {fieldOrCtorTokenToClass.Count} members in reverse lookup index");
}

/// <summary>
/// Returns the class declaration for a standard class type
/// </summary>
/// <param name="type">The type to search</param>
/// <returns>The class or null</returns>
public ClassDeclaration? GetClass(Type? type)
{
return Classes.FirstOrDefault(x => x.TheType == type);
}
}
}
2 changes: 2 additions & 0 deletions tools/ArduinoCsCompiler/Frontend/CompilerRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ public override bool RunCommand()

RunCompiler(inputInfo);
}
#if !DEBUG
catch (Exception x) when (!(x is NullReferenceException))
{
Logger.LogError(x.Message);
return false;
}
#endif
finally
{
board?.Dispose();
Expand Down
51 changes: 51 additions & 0 deletions tools/ArduinoCsCompiler/IlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using ArduinoCsCompiler.NanoGenerator;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.TypeSystem;

namespace ArduinoCsCompiler
{
public class IlWriter
{
private readonly ExecutionSet _executionSet;
private readonly IlCapabilities _ilCapabilities;

public IlWriter(ExecutionSet executionSet, IlCapabilities ilCapabilities)
{
_executionSet = executionSet;
_ilCapabilities = ilCapabilities;
}

public static void WriteMethodHeader(TextWriter w, ArduinoMethodDeclaration code)
{
string signature = code.MethodBase.MethodSignature(false, true);
Expand Down Expand Up @@ -53,5 +71,38 @@ public static void WriteMethod(TextWriter w, ArduinoMethodDeclaration code, Exec
w.WriteLine(methodBody);
w.WriteLine("}");
}

public void Write(string sourceFile, string outFile)
{
var settings = new DecompilerSettings()
{
UseSdkStyleProjectFormat = false,
};

using TextWriter tw = new StreamWriter("output.cs");
SyntaxTree tree = new SyntaxTree();
var node = new NamespaceDeclaration("Decompiled");
tree.AddChild(node, SyntaxTree.MemberRole);

foreach (var cls in _executionSet.Classes)
{
var typeSystemAstBuilder = new TypeSystemAstBuilder();
var wrapped = new ClassWrapper(cls, _executionSet);
EntityDeclaration decl = typeSystemAstBuilder.ConvertEntity(wrapped);
node.AddChild(decl, SyntaxTree.MemberRole);
foreach (var member in cls.Members)
{
if (member.Field != null)
{
var fieldWrapped = new FieldWrapper(cls, member, _executionSet);
EntityDeclaration fld = typeSystemAstBuilder.ConvertEntity(fieldWrapped);
decl.AddChild(fld, Roles.TypeMemberRole);
}
}
}

var formatting = FormattingOptionsFactory.CreateSharpDevelop();
tree.AcceptVisitor(new CSharpOutputVisitor(tw, formatting));
}
}
}
Loading

0 comments on commit 729e432

Please sign in to comment.