Skip to content

Commit

Permalink
Fixed #200, moved FluentIL to main repo
Browse files Browse the repository at this point in the history
  • Loading branch information
pamidur committed Nov 14, 2022
1 parent cc925a4 commit 40e6285
Show file tree
Hide file tree
Showing 28 changed files with 1,908 additions and 319 deletions.
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

614 changes: 307 additions & 307 deletions AspectInjector.sln

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/AspectInjector.Analyzer.Vsix/AspectInjector.Analyzer.Vsix.csproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\external\FluentIL\src\FluentIL.Common\FluentIL.Common.csproj">
<ProjectReference Include="..\FluentIL.Common\FluentIL.Common.csproj">
<Project>{7bd22418-f4a9-422b-9fde-a20708ba0648}</Project>
<Name>FluentIL.Common</Name>
</ProjectReference>
Expand Down
4 changes: 2 additions & 2 deletions src/AspectInjector.Analyzer/AspectInjector.Analyzer.csproj
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\external\FluentIL\src\FluentIL.Common\FluentIL.Common.csproj" />
<ProjectReference Include="..\FluentIL.Common\FluentIL.Common.csproj" />
<ProjectReference Include="..\AspectInjector.Broker\AspectInjector.Broker.csproj" />
<ProjectReference Include="..\AspectInjector.Rules\AspectInjector.Rules.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/AspectInjector.Core/AspectInjector.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>This is core library of Aspect Injector.</Description>
Expand All @@ -9,7 +9,7 @@
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\external\FluentIL\src\FluentIL\FluentIL.csproj" />
<ProjectReference Include="..\FluentIL\FluentIL.csproj" />
<ProjectReference Include="..\AspectInjector.Broker\AspectInjector.Broker.csproj" />
<ProjectReference Include="..\AspectInjector.Rules\AspectInjector.Rules.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/AspectInjector.Rules/AspectInjector.Rules.csproj
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\external\FluentIL\src\FluentIL.Common\FluentIL.Common.csproj" />
<ProjectReference Include="..\FluentIL.Common\FluentIL.Common.csproj" />
<ProjectReference Include="..\AspectInjector.Broker\AspectInjector.Broker.csproj" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion src/AspectInjector/AspectInjector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
<TrimmerDefaultAction>link</TrimmerDefaultAction>
<AllowedReferenceRelatedFileExtensions>none</AllowedReferenceRelatedFileExtensions>
</PropertyGroup>
<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/FluentIL.Common/FluentIL.Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
</Project>
31 changes: 31 additions & 0 deletions src/FluentIL.Common/Rule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace FluentIL.Common
{
public enum RuleSeverity
{
Hidden,
Info,
Warning,
Error
}

public class Rule
{

public Rule(string id, string title, string message, RuleSeverity severity, string description, string helpLinkUri)
{
Id = id;
Title = title;
Message = message;
Severity = severity;
Description = description;
HelpLinkUri = helpLinkUri;
}

public string Id { get; }
public string Title { get; }
public string Message { get; }
public RuleSeverity Severity { get; }
public string Description { get; }
public string HelpLinkUri { get; }
}
}
94 changes: 94 additions & 0 deletions src/FluentIL/Cuts/Arrays.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using FluentIL.Extensions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;

namespace FluentIL
{
public static class Arrays
{
public static Cut CreateArray(this in Cut cut, TypeReference elementType, params PointCut[] elements)
{
var pc = cut
.Write(OpCodes.Ldc_I4, elements.Length)
.Write(OpCodes.Newarr, elementType);

for (var i = 0; i < elements.Length; i++)
{
pc = pc.Write(OpCodes.Dup);
pc = SetByIndex(pc, elementType, i, elements[i]);
}

return pc;
}

public static Cut GetByIndex(this in Cut pc, TypeReference elementType, int index)
{
return pc
.Write(OpCodes.Ldc_I4, index)
.Write(GetLoadOpcode(elementType));
}

public static Cut SetByIndex(this in Cut pc, TypeReference elementType, int index, PointCut value)
{
return pc
.Write(OpCodes.Ldc_I4, index)
.Here(value)
.Write(GetStoreOpcode(elementType));
}

private static OpCode GetLoadOpcode(TypeReference elementType)
{
switch (elementType.MetadataType)
{
case MetadataType.Class: return OpCodes.Ldelem_Ref;
case MetadataType.Object: return OpCodes.Ldelem_Ref;
case MetadataType.Double: return OpCodes.Ldelem_R8;
case MetadataType.Single: return OpCodes.Ldelem_R4;
case MetadataType.Int64: return OpCodes.Ldelem_I8;
case MetadataType.UInt64: return OpCodes.Ldelem_I8;
case MetadataType.Int32: return OpCodes.Ldelem_I4;
case MetadataType.UInt32: return OpCodes.Ldelem_U4;
case MetadataType.Int16: return OpCodes.Ldelem_I2;
case MetadataType.UInt16: return OpCodes.Ldelem_U2;
case MetadataType.Byte: return OpCodes.Ldelem_U1;
case MetadataType.SByte: return OpCodes.Ldelem_I1;
case MetadataType.Boolean: return OpCodes.Ldelem_I1;
case MetadataType.String: return OpCodes.Ldelem_Ref;
}

throw new NotSupportedException($"No instruction for {elementType.MetadataType}");
}


private static OpCode GetStoreOpcode(TypeReference elementType)
{
if(elementType.IsValueType && !elementType.IsPrimitive)
{
var r = elementType.Resolve();
if (r.IsEnum)
elementType = r.GetEnumType();
}

switch (elementType.MetadataType)
{
case MetadataType.Class: return OpCodes.Stelem_Ref;
case MetadataType.Object: return OpCodes.Stelem_Ref;
case MetadataType.Double: return OpCodes.Stelem_R8;
case MetadataType.Single: return OpCodes.Stelem_R4;
case MetadataType.Int64: return OpCodes.Stelem_I8;
case MetadataType.UInt64: return OpCodes.Stelem_I8;
case MetadataType.Int32: return OpCodes.Stelem_I4;
case MetadataType.UInt32: return OpCodes.Stelem_I4;
case MetadataType.Int16: return OpCodes.Stelem_I2;
case MetadataType.UInt16: return OpCodes.Stelem_I2;
case MetadataType.Byte: return OpCodes.Stelem_I1;
case MetadataType.SByte: return OpCodes.Stelem_I1;
case MetadataType.Boolean: return OpCodes.Stelem_I1;
case MetadataType.String: return OpCodes.Stelem_Ref;
}

throw new NotSupportedException($"No instruction for {elementType.MetadataType}");
}
}
}
79 changes: 79 additions & 0 deletions src/FluentIL/Cuts/Statements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using FluentIL.Extensions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;

namespace FluentIL
{
public static class Statements
{
public static Cut Return(this in Cut cut)
{
return cut.Write(OpCodes.Ret);
}

public static Cut Call(this in Cut cut, MethodReference method, PointCut args = null)
{
var cur_cut = cut;

if (!method.IsCallCompatible())
throw new ArgumentException($"Uninitialized generic call reference: {method}");

if (args != null) cur_cut = cur_cut.Here(args);

var methodDef = method.Resolve();

var code = OpCodes.Call;
if (methodDef.IsConstructor) code = OpCodes.Newobj;
else if (methodDef.IsVirtual) code = OpCodes.Callvirt;

return cur_cut.Write(code, method);
}

public static Cut IfEqual(this in Cut pc, PointCut left, PointCut right, PointCut pos = null, PointCut neg = null)
{
if (pos != null && neg != null)
return Compare(pc, left, right, OpCodes.Ceq, pos, neg);

if (pos != null)
return Compare(pc, left, right, OpCodes.Ceq, OpCodes.Brfalse, pos);

if (neg != null)
return Compare(pc, left, right, OpCodes.Ceq, OpCodes.Brtrue, neg);

return pc;
}

private static Cut Compare(in Cut cut, PointCut left, PointCut right, OpCode cmp, PointCut pos, PointCut neg)
{
var pc = cut
.Here(left)
.Here(right)
.Write(cmp);

var pe = pc.Here(pos);
var ne = pe.Here(neg);

var exit = ne.Write(OpCodes.Nop);

pc.Write(OpCodes.Brfalse, pe.Next());
pe.Write(OpCodes.Br, exit);

return exit;
}

private static Cut Compare(in Cut cut, PointCut left, PointCut right, OpCode cmp, OpCode brexit, PointCut action)
{
var pc = cut
.Here(left)
.Here(right)
.Write(cmp);

var exit = pc.Write(OpCodes.Nop);

pc.Write(brexit, exit).Here(action);

return exit;
}
}
}
86 changes: 86 additions & 0 deletions src/FluentIL/Cuts/TypeMembers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using FluentIL.Extensions;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;

namespace FluentIL
{
public static class TypeMembers
{
public static Cut ThisOrStatic(this in Cut cut) =>
cut.Method.HasThis ? cut.This() : cut;

public static Cut ThisOrNull(this in Cut cut) =>
cut.Method.HasThis ? cut.This() : cut.Null();

public static Cut This(this in Cut cut)
{
if (cut.Method.HasThis) return cut.Write(OpCodes.Ldarg_0);
else throw new InvalidOperationException("Attempt to load 'this' on static method.");
}

public static Cut Load(this in Cut cut, VariableDefinition variable) => cut
.Write(OpCodes.Ldloc, variable);

public static Cut Load(this in Cut cut, ParameterReference par) => cut
.Write(OpCodes.Ldarg, par.Resolve());

public static Cut Load(this in Cut cut, FieldReference field)
{
if(!field.IsCallCompatible())
throw new ArgumentException($"Uninitialized generic call reference: {field}");

var fieldDef = field.Resolve();

return cut.Write(fieldDef.IsStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, field);
}

public static Cut LoadRef(this in Cut cut, VariableDefinition variable) => cut
.Write(OpCodes.Ldloca, variable);

public static Cut LoadRef(this in Cut cut, ParameterReference par) => cut
.Write(OpCodes.Ldarga, par.Resolve());

public static Cut LoadRef(this in Cut cut, FieldReference field)
{
if (!field.IsCallCompatible())
throw new ArgumentException($"Uninitialized generic call reference: {field}");

var fieldDef = field.Resolve();

return cut.Write(fieldDef.IsStatic ? OpCodes.Ldsflda : OpCodes.Ldflda, field);
}

public static Cut Store(this in Cut cut, FieldReference field, PointCut value = null)
{
if (!field.IsCallCompatible())
throw new ArgumentException($"Uninitialized generic call reference: {field}");

var fieldDef = field.Resolve();

return cut
.Here(value)
.Write(fieldDef.IsStatic ? OpCodes.Stsfld : OpCodes.Stfld, field);
}

public static Cut Store(this in Cut cut, VariableDefinition variable, PointCut value = null) => cut
.Here(value)
.Write(OpCodes.Stloc, variable);

public static Cut Store(this in Cut cut, ParameterReference par, PointCut value = null)
{
if (par.ParameterType.IsByReference)
{
return cut
.Load(par)
.Here(value);
}
else
{
return cut
.Here(value)
.Write(OpCodes.Starg, par.Resolve());
}
}
}
}
Loading

0 comments on commit 40e6285

Please sign in to comment.