Skip to content

Commit

Permalink
Merge pull request #37 from RobinKa/feature/typegraph
Browse files Browse the repository at this point in the history
[WIP] Add type pins and nodes
  • Loading branch information
RobinKa authored Mar 19, 2019
2 parents cf20715 + e8f8af1 commit c19f7f7
Show file tree
Hide file tree
Showing 36 changed files with 1,000 additions and 427 deletions.
12 changes: 7 additions & 5 deletions NetPrints/Core/MethodSpecifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,24 @@ public override string ToString()

if (Modifiers.HasFlag(MethodModifiers.Static))
{
methodString += "Static ";
methodString += $"{DeclaringType.ShortName}.";
}

string argTypeString = string.Join(", ", Arguments);
methodString += Name;

methodString += $"{Name}({argTypeString})";
string argTypeString = string.Join(", ", Arguments.Select(a => a.Value.ShortName));

methodString += $"({argTypeString})";

if(GenericArguments.Count > 0)
{
string genArgTypeString = string.Join(", ", GenericArguments);
string genArgTypeString = string.Join(", ", GenericArguments.Select(s => s.ShortName));
methodString += $"<{genArgTypeString}>";
}

if(ReturnTypes.Count > 0)
{
string returnTypeString = string.Join(", ", ReturnTypes);
string returnTypeString = string.Join(", ", ReturnTypes.Select(s => s.ShortName));
methodString += $" : {returnTypeString}";
}

Expand Down
34 changes: 0 additions & 34 deletions NetPrints/Core/TypeGraph.cs

This file was deleted.

31 changes: 31 additions & 0 deletions NetPrints/Core/TypeSpecifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,37 @@ public override string ToString()
return s;
}

/// <summary>
/// Constructs this type by replacing all its generic arguments with the given types.
/// </summary>
/// <param name="typeSpecifiers">
/// Specifiers for the types to replace the generic
/// type arguments with
/// </param>
/// <returns>Constructed type with generic type arguments replaced by the given ones.</returns>
public TypeSpecifier Construct(IReadOnlyDictionary<GenericType, BaseType> typeSpecifiers)
{
if (GenericArguments.Count != typeSpecifiers.Count())
{
throw new ArgumentException("Need to replace all generic arguments when constructing type.");
}

// Replace by dictionary
var newGenericArgs = new List<BaseType>(GenericArguments);
for (int i = 0; i < newGenericArgs.Count; i++)
{
if (newGenericArgs[i] is GenericType oldGenericType &&
typeSpecifiers.TryGetValue(oldGenericType, out BaseType newType))
{
newGenericArgs[i] = newType;
}
}

// TODO: Make sure all dictionary values were used.

return new TypeSpecifier(Name, IsEnum, IsInterface, newGenericArgs);
}

public static bool operator ==(TypeSpecifier a, TypeSpecifier b)
{
if (ReferenceEquals(b, null))
Expand Down
117 changes: 78 additions & 39 deletions NetPrints/Graph/CallMethodNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ public string MethodName
{
get => MethodSpecifier.Name;
}

/// <summary>
/// Name of the method with generic arguments fully expanded as it
/// would appear in code. (eg. SomeMethod&lt;System.Object, System.Int32&gt;).
/// </summary>
public string BoundMethodName
{
get
{
string boundName = MethodSpecifier.Name;

if (InputTypePins.Count > 0)
{
boundName += $"<{string.Join(",", InputTypePins.Select(p => p.InferredType?.Value?.FullCodeName ?? p.Name))}>";
}

return boundName;
}
}

/// <summary>
/// Whether the method is static.
Expand All @@ -51,30 +70,23 @@ public TypeSpecifier DeclaringType
/// </summary>
public IReadOnlyList<BaseType> ArgumentTypes
{
get => MethodSpecifier.ArgumentTypes;
get => InputDataPins.Select(p => p.PinType.Value).ToList();
}

// <summary>
/// List of named type specifiers the method takes.
/// </summary>
public IList<Named<BaseType>> Arguments => MethodSpecifier.Arguments;

/// <summary>
/// List of type specifiers the method returns.
/// </summary>
public IList<BaseType> ReturnTypes
public IReadOnlyList<Named<BaseType>> Arguments
{
get => MethodSpecifier.ReturnTypes;
get => InputDataPins.Select(p => new Named<BaseType>(p.Name, p.PinType.Value)).ToList();
}

/// <summary>
/// List of generic arguments the method takes.
/// List of type specifiers the method returns.
/// </summary>
[DataMember]
public IList<BaseType> GenericArgumentTypes
public IReadOnlyList<BaseType> ReturnTypes
{
get;
private set;
get => OutputDataPins.Select(p => p.PinType.Value).ToList();
}

/// <summary>
Expand Down Expand Up @@ -134,21 +146,10 @@ public CallMethodNode(Method method, MethodSpecifier methodSpecifier,
{
MethodSpecifier = methodSpecifier;

// TODO: Check that genericArgumentTypes fullfils GenericArguments constraints
if (MethodSpecifier.GenericArguments.Count > 0 && genericArgumentTypes != null
|| (genericArgumentTypes != null &&
MethodSpecifier.GenericArguments.Count != genericArgumentTypes.Count))
{
throw new ArgumentException(nameof(genericArgumentTypes));
}

if (genericArgumentTypes == null)
{
GenericArgumentTypes = new List<BaseType>();
}
else
// Add type pins for each generic argument of the method type parameters.
foreach (var genericArg in MethodSpecifier.GenericArguments.OfType<GenericType>())
{
GenericArgumentTypes = genericArgumentTypes;
AddInputTypePin(genericArg.Name);
}

if (!IsStatic)
Expand All @@ -159,36 +160,74 @@ public CallMethodNode(Method method, MethodSpecifier methodSpecifier,
AddOutputDataPin("Exception", TypeSpecifier.FromType<Exception>());
AddOutputExecPin("Catch");

foreach (Named<BaseType> argument in Arguments)
foreach (Named<BaseType> argument in MethodSpecifier.Arguments)
{
AddInputDataPin(argument.Name, argument.Value);
}

foreach (BaseType returnType in ReturnTypes)
foreach (BaseType returnType in MethodSpecifier.ReturnTypes)
{
AddOutputDataPin(returnType.ShortName, returnType);
}

// TODO: Set the correct types to begin with.
UpdateTypes();
}

public override string ToString()
protected override void OnInputTypeChanged(object sender, EventArgs eventArgs)
{
string s = IsStatic ? "Static " : "";
base.OnInputTypeChanged(sender, eventArgs);

if (OperatorUtil.TryGetOperatorInfo(MethodSpecifier, out OperatorInfo operatorInfo))
UpdateTypes();
}

private void UpdateTypes()
{
for (int i = 0; i < MethodSpecifier.Arguments.Count; i++)
{
s += $"Operator {operatorInfo.DisplayName}";
BaseType type = MethodSpecifier.Arguments[i];

// Construct type with generic arguments replaced by our input type pins
BaseType constructedType = GenericsHelper.ConstructWithTypePins(type, InputTypePins);

if (ArgumentPins[i].PinType.Value != constructedType)
{
ArgumentPins[i].PinType.Value = constructedType;
}
}
else

for (int i = 0; i < MethodSpecifier.ReturnTypes.Count; i++)
{
s += $"Call {MethodName}";
BaseType type = MethodSpecifier.ReturnTypes[i];

// Construct type with generic arguments replaced by our input type pins
BaseType constructedType = GenericsHelper.ConstructWithTypePins(type, InputTypePins);

// +1 because the first pin is the exception pin
if (ReturnValuePins[i].PinType.Value != constructedType)
{
ReturnValuePins[i].PinType.Value = constructedType;
}
}
}

if (GenericArgumentTypes.Count > 0)
public override string ToString()
{
if (OperatorUtil.TryGetOperatorInfo(MethodSpecifier, out OperatorInfo operatorInfo))
{
s += $"<{string.Join(", ", GenericArgumentTypes.Select(type => type.ShortName))}>";
return $"Operator {operatorInfo.DisplayName}";
}
else
{
string s = "";

if (IsStatic)
{
s += $"{MethodSpecifier.DeclaringType.ShortName}.";
}

return s;
return s + MethodSpecifier.Name;
}
}
}
}
56 changes: 48 additions & 8 deletions NetPrints/Graph/ConstructorNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NetPrints.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace NetPrints.Graph
Expand All @@ -24,17 +25,17 @@ public ConstructorSpecifier ConstructorSpecifier
/// <summary>
/// Specifier for the type this constructor creates.
/// </summary>
public TypeSpecifier ClassType
public BaseType ClassType
{
get => ConstructorSpecifier.DeclaringType;
get => OutputDataPins[0].PinType.Value;
}

/// <summary>
/// List of specifiers for the types this constructor takes.
/// List of type specifiers the constructor takes.
/// </summary>
public IList<TypeSpecifier> ArgumentTypes
public IReadOnlyList<BaseType> ArgumentTypes
{
get => ConstructorSpecifier.Arguments;
get => ArgumentPins.Select(p => p.PinType.Value).ToList();
}

/// <summary>
Expand All @@ -50,17 +51,56 @@ public ConstructorNode(Method method, ConstructorSpecifier specifier)
{
ConstructorSpecifier = specifier;

foreach(TypeSpecifier argumentType in ArgumentTypes)
// Add type pins for each generic arguments of the type being constructed.
foreach (var genericArg in ConstructorSpecifier.DeclaringType.GenericArguments.OfType<GenericType>())
{
AddInputTypePin(genericArg.Name);
}

foreach (TypeSpecifier argumentType in ArgumentTypes)
{
AddInputDataPin(argumentType.ShortName, argumentType);
}

AddOutputDataPin(ClassType.ShortName, ClassType);
AddOutputDataPin(ConstructorSpecifier.DeclaringType.ShortName, ConstructorSpecifier.DeclaringType);

// TODO: Set the correct types to begin with.
UpdateTypes();
}

protected override void OnInputTypeChanged(object sender, EventArgs eventArgs)
{
base.OnInputTypeChanged(sender, eventArgs);

UpdateTypes();
}

private void UpdateTypes()
{
// Construct data input
for (int i = 0; i < ConstructorSpecifier.Arguments.Count; i++)
{
BaseType type = ConstructorSpecifier.Arguments[i];

// Construct type with generic arguments replaced by our input type pins
BaseType constructedType = GenericsHelper.ConstructWithTypePins(type, InputTypePins);

if (InputDataPins[i].PinType.Value != constructedType)
{
InputDataPins[i].PinType.Value = constructedType;
}
}

// Construct data output
{
BaseType constructedType = GenericsHelper.ConstructWithTypePins(ConstructorSpecifier.DeclaringType, InputTypePins);
OutputDataPins[0].PinType.Value = constructedType;
}
}

public override string ToString()
{
return $"Construct New {ClassType}";
return $"Construct {ClassType.ShortName}";
}
}
}
2 changes: 1 addition & 1 deletion NetPrints/Graph/EntryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void SetArgumentTypes(IEnumerable<BaseType> parameterTypes)
{
// Remember pins with same type as before
int i = OutputDataPins.IndexOf(pin);
if (i < parameterTypes.Count() && pin.PinType == parameterTypes.ElementAt(i))
if (i < parameterTypes.Count() && pin.PinType.Value == parameterTypes.ElementAt(i))
{
oldConnections.Add(i, new List<NodeInputDataPin>(pin.OutgoingPins));
}
Expand Down
Loading

0 comments on commit c19f7f7

Please sign in to comment.