Skip to content

Commit

Permalink
simplify constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
thegu5 authored and gompoc committed Aug 21, 2024
1 parent b993b3e commit 7191963
Show file tree
Hide file tree
Showing 57 changed files with 253 additions and 542 deletions.
13 changes: 4 additions & 9 deletions Cpp2IL.Core/Exceptions/DllSaveException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Cpp2IL.Core.Exceptions;

public class DllSaveException : Exception
public class DllSaveException(string fullPath, Exception cause)
: Exception($"Fatal Exception writing DLL {fullPath}", cause)
{
public string FullPath { get; }
public Exception Cause { get; }

public DllSaveException(string fullPath, Exception cause) : base($"Fatal Exception writing DLL {fullPath}", cause)
{
FullPath = fullPath;
Cause = cause;
}
public string FullPath { get; } = fullPath;
public Exception Cause { get; } = cause;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@

namespace Cpp2IL.Core.Exceptions;

public class InstructionSetHandlerNotRegisteredException : Exception
{
public InstructionSetHandlerNotRegisteredException(InstructionSetId instructionSetId)
: base($"Instruction set handler not registered for instruction set {instructionSetId.Name}. Please register an instruction set handler using InstructionSetRegistry.RegisterInstructionSet<T>(id)")
{
}
}
public class InstructionSetHandlerNotRegisteredException(InstructionSetId instructionSetId) : Exception(
$"Instruction set handler not registered for instruction set {instructionSetId.Name}. Please register an instruction set handler using InstructionSetRegistry.RegisterInstructionSet<T>(id)");
8 changes: 2 additions & 6 deletions Cpp2IL.Core/Exceptions/LibCpp2ILInitializationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@

namespace Cpp2IL.Core.Exceptions;

public class LibCpp2ILInitializationException : Exception
{
public LibCpp2ILInitializationException(string message, Exception innerException) : base(message, innerException)
{
}
}
public class LibCpp2ILInitializationException(string message, Exception innerException)
: Exception(message, innerException);
7 changes: 1 addition & 6 deletions Cpp2IL.Core/Exceptions/NodeConditionCalculationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@

namespace Cpp2IL.Core.Exceptions;

public class NodeConditionCalculationException : Exception
{
public NodeConditionCalculationException(string message) : base(message)
{
}
}
public class NodeConditionCalculationException(string message) : Exception(message);
26 changes: 6 additions & 20 deletions Cpp2IL.Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,29 @@ public static IEnumerable<T> MaybeAppend<T>(this IEnumerable<T> enumerable, T? i

public static MemoryEnumerator<T> GetEnumerator<T>(this Memory<T> memory) => new(memory);

public class MemoryEnumerable<T> : IEnumerable<T>
public class MemoryEnumerable<T>(Memory<T> memory) : IEnumerable<T>
{
private readonly Memory<T> _memory;

public MemoryEnumerable(Memory<T> memory)
{
_memory = memory;
}

public IEnumerator<T> GetEnumerator() => new MemoryEnumerator<T>(_memory);
public IEnumerator<T> GetEnumerator() => new MemoryEnumerator<T>(memory);

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class MemoryEnumerator<T> : IEnumerator<T>
public class MemoryEnumerator<T>(Memory<T> memory) : IEnumerator<T>
{
private readonly Memory<T> _memory;

private int _index = -1;

public MemoryEnumerator(Memory<T> memory)
{
_memory = memory;
}


public bool MoveNext()
{
_index++;
return _index < _memory.Length;
return _index < memory.Length;
}

public void Reset()
{
_index = -1;
}

public T Current => _memory.Span[_index];
public T Current => memory.Span[_index];

object? IEnumerator.Current => Current;

Expand Down
19 changes: 5 additions & 14 deletions Cpp2IL.Core/Graphs/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Cpp2IL.Core.Graphs;

public class Block
{
public BlockType BlockType { get; set; }
public List<Block> Predecessors;
public List<Block> Successors;
public BlockType BlockType { get; set; } = BlockType.Unknown;
public List<Block> Predecessors = [];
public List<Block> Successors = [];

public List<InstructionSetIndependentInstruction> isilInstructions;
public List<InstructionSetIndependentInstruction> isilInstructions = [];

public int ID { get; set; }
public int ID { get; set; } = -1;

public bool Dirty { get; set; }
public bool Visited = false;
Expand All @@ -31,15 +31,6 @@ public override string ToString() {
return stringBuilder.ToString();
}

public Block()
{
BlockType = BlockType.Unknown;
Predecessors = [];
Successors = [];
isilInstructions = [];
ID = -1;
}

public void AddInstruction(InstructionSetIndependentInstruction instruction)
{
isilInstructions.Add(instruction);
Expand Down
18 changes: 7 additions & 11 deletions Cpp2IL.Core/ISIL/IsilCondition.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
namespace Cpp2IL.Core.ISIL;

public class IsilCondition
public class IsilCondition(
InstructionSetIndependentOperand left,
InstructionSetIndependentOperand right,
InstructionSetIndependentOpCode opCode)
{
public InstructionSetIndependentOperand Left;
public InstructionSetIndependentOperand Right;
public InstructionSetIndependentOpCode OpCode;
public InstructionSetIndependentOperand Left = left;
public InstructionSetIndependentOperand Right = right;
public InstructionSetIndependentOpCode OpCode = opCode;

public bool IsAnd; //E.g. x86 TEST instruction vs CMP

public IsilCondition(InstructionSetIndependentOperand left, InstructionSetIndependentOperand right, InstructionSetIndependentOpCode opCode)
{
Left = left;
Right = right;
OpCode = opCode;
}

public IsilCondition MarkAsAnd()
{
IsAnd = true;
Expand Down
9 changes: 2 additions & 7 deletions Cpp2IL.Core/ISIL/IsilImmediateOperand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace Cpp2IL.Core.ISIL;

public readonly struct IsilImmediateOperand : IsilOperandData
public readonly struct IsilImmediateOperand(IConvertible value) : IsilOperandData
{
public readonly IConvertible Value;

public IsilImmediateOperand(IConvertible value)
{
Value = value;
}
public readonly IConvertible Value = value;

public override string ToString()
{
Expand Down
9 changes: 2 additions & 7 deletions Cpp2IL.Core/ISIL/IsilInstructionStatement.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
namespace Cpp2IL.Core.ISIL;

public struct IsilInstructionStatement : IsilStatement
public struct IsilInstructionStatement(InstructionSetIndependentInstruction instruction) : IsilStatement
{
public readonly InstructionSetIndependentInstruction Instruction;

public IsilInstructionStatement(InstructionSetIndependentInstruction instruction)
{
Instruction = instruction;
}
public readonly InstructionSetIndependentInstruction Instruction = instruction;

public override string ToString() => Instruction.ToString();
}
11 changes: 3 additions & 8 deletions Cpp2IL.Core/ISIL/IsilMethodOperand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Cpp2IL.Core.ISIL;

public readonly struct IsilMethodOperand : IsilOperandData
public readonly struct IsilMethodOperand(MethodAnalysisContext method) : IsilOperandData
{
public readonly MethodAnalysisContext Method { get; }

public IsilMethodOperand(MethodAnalysisContext method)
{
Method = method;
}

public readonly MethodAnalysisContext Method { get; } = method;

public override string ToString() => Method.DeclaringType?.Name + "." + Method.Name;
}
9 changes: 2 additions & 7 deletions Cpp2IL.Core/ISIL/IsilRegisterOperand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
namespace Cpp2IL.Core.ISIL;

public readonly struct IsilRegisterOperand : IsilOperandData
public readonly struct IsilRegisterOperand(string registerName) : IsilOperandData
{
public readonly string RegisterName;

public IsilRegisterOperand(string registerName)
{
RegisterName = registerName;
}
public readonly string RegisterName = registerName;

public override string ToString() => RegisterName;
}
9 changes: 2 additions & 7 deletions Cpp2IL.Core/ISIL/IsilStackOperand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
namespace Cpp2IL.Core.ISIL;

public readonly struct IsilStackOperand : IsilOperandData
public readonly struct IsilStackOperand(int offset) : IsilOperandData
{
public readonly int Offset;

public IsilStackOperand(int offset)
{
Offset = offset;
}
public readonly int Offset = offset;

public override string ToString() => $"stack:0x{Offset:X}";
}
9 changes: 2 additions & 7 deletions Cpp2IL.Core/ISIL/IsilTypeMetadataUsageOperand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Cpp2IL.Core.ISIL;

public readonly struct IsilTypeMetadataUsageOperand : IsilOperandData
public readonly struct IsilTypeMetadataUsageOperand(TypeAnalysisContext typeAnalysisContext) : IsilOperandData
{
public readonly TypeAnalysisContext TypeAnalysisContext;

public IsilTypeMetadataUsageOperand(TypeAnalysisContext typeAnalysisContext)
{
TypeAnalysisContext = typeAnalysisContext;
}
public readonly TypeAnalysisContext TypeAnalysisContext = typeAnalysisContext;

public override string ToString() => "typeof("+TypeAnalysisContext.FullName + ")";
}
19 changes: 8 additions & 11 deletions Cpp2IL.Core/ISIL/IsilVectorRegisterElementOperand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@

namespace Cpp2IL.Core.ISIL;

public readonly struct IsilVectorRegisterElementOperand : IsilOperandData
public readonly struct IsilVectorRegisterElementOperand(
string registerName,
IsilVectorRegisterElementOperand.VectorElementWidth width,
int index)
: IsilOperandData
{
public readonly string RegisterName;
public readonly VectorElementWidth Width;
public readonly int Index;

public IsilVectorRegisterElementOperand(string registerName, VectorElementWidth width, int index)
{
RegisterName = registerName;
Width = width;
Index = index;
}
public readonly string RegisterName = registerName;
public readonly VectorElementWidth Width = width;
public readonly int Index = index;

public override string ToString()
{
Expand Down
18 changes: 5 additions & 13 deletions Cpp2IL.Core/Il2CppArrayUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,11 @@ public static bool IsAtLeastFirstItemPtr(uint offset)
return offset >= FirstItemOffset;
}

public class UsefulOffset
public class UsefulOffset(string name, uint offset, Type type, bool is32Bit)
{
public UsefulOffset(string name, uint offset, Type type, bool is32Bit)
{
this.name = name;
this.offset = offset;
this.type = type;
this.is32Bit = is32Bit;
}

public string name;
public uint offset;
public Type type;
public bool is32Bit;
public string name = name;
public uint offset = offset;
public Type type = type;
public bool is32Bit = is32Bit;
}
}
18 changes: 5 additions & 13 deletions Cpp2IL.Core/Il2CppClassUsefulOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,11 @@ public static bool IsPointerIntoVtable(uint offset)
return UsefulOffsets.FirstOrDefault(o => o.is32Bit == is32Bit && o.offset == offset)?.name;
}

public class UsefulOffset
public class UsefulOffset(string name, uint offset, Type type, bool is32Bit)
{
public UsefulOffset(string name, uint offset, Type type, bool is32Bit)
{
this.name = name;
this.offset = offset;
this.type = type;
this.is32Bit = is32Bit;
}

public string name;
public uint offset;
public Type type;
public bool is32Bit;
public string name = name;
public uint offset = offset;
public Type type = type;
public bool is32Bit = is32Bit;
}
}
18 changes: 5 additions & 13 deletions Cpp2IL.Core/Il2CppMethodDefinitionUsefulOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,11 @@ public static class Il2CppMethodDefinitionUsefulOffsets
return UsefulOffsets.FirstOrDefault(o => o.is32Bit == is32Bit && o.offset == offset)?.name;
}

public class UsefulOffset
public class UsefulOffset(string name, uint offset, Type type, bool is32Bit)
{
public UsefulOffset(string name, uint offset, Type type, bool is32Bit)
{
this.name = name;
this.offset = offset;
this.type = type;
this.is32Bit = is32Bit;
}

public string name;
public uint offset;
public Type type;
public bool is32Bit;
public string name = name;
public uint offset = offset;
public Type type = type;
public bool is32Bit = is32Bit;
}
}
Loading

0 comments on commit 7191963

Please sign in to comment.