Better handling of C# function parameter type checking

This commit is contained in:
2019-01-16 12:07:40 +01:00
parent 2ef06b3fd7
commit b5bfb7997b
2 changed files with 48 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Reflection;
using Upsilon.BaseTypes;
using Upsilon.Binder;
using Upsilon.Binder.VariableSymbols;
using Upsilon.StandardLibraries;
using Type = Upsilon.BaseTypes.Type;
@@ -135,7 +136,8 @@ namespace Upsilon.BoundTypes
Properties.Add(valueName, new UserDataBoundProperty()
{
Name = valueName,
Type = Type.UserData
Type = Type.UserData,
ActualType = name
});
}
}
@@ -185,6 +187,24 @@ namespace Upsilon.BoundTypes
$"Expected one of the following: {functionParameter.Type.ToString()}, got: '{callingParameter.Type}'",
callingParameter);
}
if (functionParameter.Type.HasFlag(Type.UserData))
{
var variable = Binder.Binder.ResolveVariable(callingParameter, null);
if (variable != null && variable.Type == Type.UserData)
{
var parent =
(UserDataBoundTypeDefinition) ((UserDataVariableSymbol) variable).BoundTypeDefinition;
if (functionParameter.ActualType != null &&
!string.Equals(functionParameter.ActualType, parent.Name, StringComparison.InvariantCultureIgnoreCase))
{
return (false,
$"Unexpected variable passed to internal function at variable {i + 1}. " +
$"Expected to be the following: {functionParameter.ActualType}, got: '{parent.Name}'",
callingParameter);
}
}
}
}
return (true, null, null);