Skip to content

Commit

Permalink
added support for different pass types (ref / out / in)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinKa committed Mar 24, 2019
1 parent 09cd773 commit c4c6273
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 21 deletions.
46 changes: 41 additions & 5 deletions NetPrints/Core/MethodSpecifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@

namespace NetPrints.Core
{
public enum MethodParameterPassType
{
Default,
Reference,
Out,
In
}

[DataContract]
public class MethodParameter : Named<BaseType>
{
[DataMember]
public MethodParameterPassType PassType
{
get;
private set;
}

public MethodParameter(string name, BaseType type, MethodParameterPassType passType)
: base(name, type)
{
PassType = passType;
}
}

/// <summary>
/// Specifier describing a method.
/// </summary>
Expand Down Expand Up @@ -40,18 +65,29 @@ public TypeSpecifier DeclaringType
/// Named specifiers for the types this method takes as arguments.
/// </summary>
[DataMember]
public IList<Named<BaseType>> Arguments
public IList<MethodParameter> Parameters
{
get;
private set;
}

[DataMember]
[Obsolete("Use Parameters instead.")]
public IList<Named<BaseType>> Arguments
{
get => Parameters.Cast<Named<BaseType>>().ToList();
private set
{
Parameters = value.Select(arg => new MethodParameter(arg.Name, arg.Value, MethodParameterPassType.Default)).ToList();
}
}

/// <summary>
/// Specifiers for the types this method takes as arguments.
/// </summary>
public IReadOnlyList<BaseType> ArgumentTypes
{
get => Arguments.Select(t => (BaseType)t).ToArray();
get => Parameters.Select(t => (BaseType)t).ToArray();
}

/// <summary>
Expand Down Expand Up @@ -103,13 +139,13 @@ public IList<BaseType> GenericArguments
/// <param name="modifiers">Modifiers of the method.</param>
/// <param name="declaringType">Specifier for the type this method is contained in.</param>
/// <param name="genericArguments">Generic arguments this method takes.</param>
public MethodSpecifier(string name, IEnumerable<Named<BaseType>> arguments,
public MethodSpecifier(string name, IEnumerable<MethodParameter> arguments,
IEnumerable<BaseType> returnTypes, MethodModifiers modifiers, MemberVisibility visibility, TypeSpecifier declaringType,
IList<BaseType> genericArguments)
{
Name = name;
DeclaringType = declaringType;
Arguments = arguments.ToList();
Parameters = arguments.ToList();
ReturnTypes = returnTypes.ToList();
Modifiers = modifiers;
Visibility = visibility;
Expand All @@ -127,7 +163,7 @@ public override string ToString()

methodString += Name;

string argTypeString = string.Join(", ", Arguments.Select(a => a.Value.ShortName));
string argTypeString = string.Join(", ", Parameters.Select(a => a.Value.ShortName));

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

Expand Down
1 change: 1 addition & 0 deletions NetPrints/Core/Named.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NetPrints.Core
/// </summary>
/// <typeparam name="T">Type of the value.</typeparam>
[DataContract]
[KnownType(typeof(MethodParameter))]
public class Named<T>
{
[DataMember]
Expand Down
6 changes: 3 additions & 3 deletions NetPrints/Graph/CallMethodNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public IList<NodeOutputDataPin> ReturnValuePins

AddExceptionPins();

foreach (Named<BaseType> argument in MethodSpecifier.Arguments)
foreach (Named<BaseType> argument in MethodSpecifier.Parameters)
{
AddInputDataPin(argument.Name, argument.Value);
}
Expand Down Expand Up @@ -219,9 +219,9 @@ protected override void OnInputTypeChanged(object sender, EventArgs eventArgs)

private void UpdateTypes()
{
for (int i = 0; i < MethodSpecifier.Arguments.Count; i++)
for (int i = 0; i < MethodSpecifier.Parameters.Count; i++)
{
BaseType type = MethodSpecifier.Arguments[i];
BaseType type = MethodSpecifier.Parameters[i];

// Construct type with generic arguments replaced by our input type pins
BaseType constructedType = GenericsHelper.ConstructWithTypePins(type, InputTypePins);
Expand Down
4 changes: 2 additions & 2 deletions NetPrints/Graph/GenericsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static BaseType ConstructWithTypePins(BaseType type, IEnumerable<NodeInpu
{
if (inputTypePin.InferredType?.Value is BaseType replacementType && !(replacementType is null))
{
GenericType typeToReplace = (GenericType)typeSpecifier.GenericArguments.SingleOrDefault(arg => arg.Name == inputTypePin.Name);
GenericType typeToReplace = typeSpecifier.GenericArguments.SingleOrDefault(arg => arg.Name == inputTypePin.Name) as GenericType;

// If we can not replace all
if (!(typeToReplace is null))
Expand All @@ -59,7 +59,7 @@ public static BaseType ConstructWithTypePins(BaseType type, IEnumerable<NodeInpu
}
else if (type is GenericType genericType)
{
BaseType replacementType = inputTypePins.SingleOrDefault(t => t.Name == type.Name).InferredType?.Value;
BaseType replacementType = inputTypePins.SingleOrDefault(t => t.Name == type.Name)?.InferredType?.Value;
if (replacementType != null)
{
return replacementType;
Expand Down
4 changes: 2 additions & 2 deletions NetPrints/Graph/GraphUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ public static Method AddOverrideMethod(Class cls, MethodSpecifier methodSpecifie
int offsetY = -112;

// Add argument pins, their type nodes and connect them
for (int i = 0; i < methodSpecifier.Arguments.Count; i++)
for (int i = 0; i < methodSpecifier.Parameters.Count; i++)
{
BaseType argType = methodSpecifier.Arguments[i].Value;
BaseType argType = methodSpecifier.Parameters[i].Value;
TypeNode argTypeNode = CreateNestedTypeNode(newMethod, argType, newMethod.EntryNode.PositionX + offsetX, newMethod.EntryNode.PositionY + offsetY * (i+1));

newMethod.EntryNode.AddArgument();
Expand Down
25 changes: 24 additions & 1 deletion NetPrints/Translator/MethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,31 @@ public void TranslateCallMethodNode(CallMethodNode node)
}
}

// Prefix with "out" / "ref" / "in"
string[] argNameArray = argumentNames.ToArray();
Debug.Assert(argNameArray.Length == node.MethodSpecifier.Parameters.Count);

for (int i = 0; i < node.MethodSpecifier.Parameters.Count; i++)
{
MethodParameterPassType passType = node.MethodSpecifier.Parameters[i].PassType;
switch (passType)
{
case MethodParameterPassType.Out:
argNameArray[i] = "out " + argNameArray[i];
break;
case MethodParameterPassType.Reference:
argNameArray[i] = "ref " + argNameArray[i];
break;
case MethodParameterPassType.In:
argNameArray[i] = "in " + argNameArray[i];
break;
default:
break;
}
}

// Write the method call
builder.AppendLine($"{node.BoundMethodName}({string.Join(", ", argumentNames)});");
builder.AppendLine($"{node.BoundMethodName}({string.Join(", ", argNameArray)});");
}

// Assign the real variables from the temporary tuple
Expand Down
2 changes: 1 addition & 1 deletion NetPrintsEditor/Controls/MethodEditorControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void OnGridDrop(object sender, DragEventArgs e)
// TODO: Get this from method directly somehow
// TODO: Get named type specifiers from method
MethodSpecifier methodSpecifier = new MethodSpecifier(method.Name,
method.ArgumentTypes.Select(t => new Named<BaseType>("TODO", t)),
method.ArgumentTypes.Select(t => new MethodParameter("TODO", t, MethodParameterPassType.Default)),
method.ReturnTypes.Cast<TypeSpecifier>(),
method.Modifiers, method.Visibility,
method.Class.Type, Array.Empty<BaseType>());
Expand Down
2 changes: 1 addition & 1 deletion NetPrintsEditor/Converters/MethodSpecifierConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
name = methodSpecifier.Name;
}

string paramTypeNames = string.Join(", ", methodSpecifier.Arguments);
string paramTypeNames = string.Join(", ", methodSpecifier.Parameters);
string s = $"{methodSpecifier.DeclaringType} {name} ({paramTypeNames})";

if(methodSpecifier.ReturnTypes.Count > 0)
Expand Down
19 changes: 16 additions & 3 deletions NetPrintsEditor/Reflection/ReflectionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public static Named<BaseType> NamedBaseTypeSpecifierFromSymbol(IParameterSymbol
return new Named<BaseType>(paramSymbol.Name, BaseTypeSpecifierFromSymbol(paramSymbol.Type));
}

private static readonly Dictionary<RefKind, MethodParameterPassType> refKindToPassType = new Dictionary<RefKind, MethodParameterPassType>()
{
[RefKind.None] = MethodParameterPassType.Default,
[RefKind.Ref] = MethodParameterPassType.Reference,
[RefKind.Out] = MethodParameterPassType.Out,
[RefKind.In] = MethodParameterPassType.In,
};

public static MethodParameter MethodParameterFromSymbol(in IParameterSymbol paramSymbol)
{
return new MethodParameter(paramSymbol.Name, BaseTypeSpecifierFromSymbol(paramSymbol.Type), refKindToPassType[paramSymbol.RefKind]);
}

public static MethodSpecifier MethodSpecifierFromSymbol(IMethodSymbol method)
{
MemberVisibility visibility = roslynToNetprintsVisibility[method.DeclaredAccessibility];
Expand Down Expand Up @@ -137,15 +150,15 @@ public static MethodSpecifier MethodSpecifierFromSymbol(IMethodSymbol method)
new BaseType[] { } :
new BaseType[] { BaseTypeSpecifierFromSymbol(method.ReturnType) };

Named<BaseType>[] parameterTypes = method.Parameters.Select(
p => NamedBaseTypeSpecifierFromSymbol(p)).ToArray();
MethodParameter[] parameters = method.Parameters.Select(
p => MethodParameterFromSymbol(p)).ToArray();

BaseType[] genericArgs = method.TypeParameters.Select(
p => BaseTypeSpecifierFromSymbol(p)).ToArray();

return new MethodSpecifier(
method.Name,
parameterTypes,
parameters,
returnTypes,
modifiers,
visibility,
Expand Down
4 changes: 2 additions & 2 deletions NetPrintsUnitTests/ClassTranslatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public void CreateMainMethod()
Modifiers = MethodModifiers.Static
};

MethodSpecifier stringLengthSpecifier = new MethodSpecifier("StringLength", new Named<BaseType>[0], new List<TypeSpecifier>() { TypeSpecifier.FromType<int>() },
MethodSpecifier stringLengthSpecifier = new MethodSpecifier("StringLength", new MethodParameter[0], new List<TypeSpecifier>() { TypeSpecifier.FromType<int>() },
MethodModifiers.None, MemberVisibility.Public, TypeSpecifier.FromType<string>(), Array.Empty<BaseType>());
//MethodSpecifier writeConsoleSpecifier = typeof(Console).GetMethods().Single(m => m.Name == "WriteLine" && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(string));
TypeSpecifier stringType = TypeSpecifier.FromType<string>();
MethodSpecifier writeConsoleSpecifier = new MethodSpecifier("WriteLine", new Named<BaseType>[] { new Named<BaseType>("argName", stringType) }, new BaseType[0],
MethodSpecifier writeConsoleSpecifier = new MethodSpecifier("WriteLine", new MethodParameter[] { new MethodParameter("argName", stringType, MethodParameterPassType.Default) }, new BaseType[0],
MethodModifiers.None, MemberVisibility.Public, TypeSpecifier.FromType(typeof(Console)), new BaseType[0]);

// Create nodes
Expand Down
6 changes: 5 additions & 1 deletion NetPrintsUnitTests/DelegateTranslatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public void TestDelegate()
}

MethodSpecifier delegateMethodSpecifier = new MethodSpecifier("TestMethod",
new Named<BaseType>[] { new Named<BaseType>("arg1", TypeSpecifier.FromType<int>()), new Named<BaseType>("arg2", TypeSpecifier.FromType<string>()) },
new MethodParameter[]
{
new MethodParameter("arg1", TypeSpecifier.FromType<int>(), MethodParameterPassType.Default),
new MethodParameter("arg2", TypeSpecifier.FromType<string>(), MethodParameterPassType.Default)
},
new BaseType[] { TypeSpecifier.FromType<float>() },
MethodModifiers.Static, MemberVisibility.Public,
TypeSpecifier.FromType<double>(), Array.Empty<BaseType>());
Expand Down

0 comments on commit c4c6273

Please sign in to comment.