Fixes for types sometimes choosing type they inherit from instead of themselves

This commit is contained in:
Deukhoofd 2019-01-22 18:32:29 +01:00
parent 8bd5f7d0af
commit 4062d2f140
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
6 changed files with 51 additions and 18 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Upsilon.BoundTypes;
namespace Upsilon.BaseTypes
@ -103,17 +104,17 @@ namespace Upsilon.BaseTypes
{
private System.Type RealType { get; set; }
protected UndefinedUserDataTypeContainer(Type t) : base(t)
{
}
public UndefinedUserDataTypeContainer(System.Type realType) : base("unset")
{
if (realType == null)
throw new Exception("Type can't be null");
RealType = realType;
}
public UndefinedUserDataTypeContainer(Type t, System.Type realType) : base(t, "unset")
{
if (realType == null)
throw new Exception("Type can't be null");
RealType = realType;
}

View File

@ -174,10 +174,7 @@ namespace Upsilon.BaseTypes
if (t == typeof(ScriptType))
return Type.Unknown;
var boundType = BoundTypeHandler.GetTypeName(t);
if (boundType == null)
return new UndefinedUserDataTypeContainer(t);
return new TypeContainer(BoundTypeHandler.GetTypeName(t));
}
}

View File

@ -16,8 +16,6 @@ namespace Upsilon.BaseTypes.UserData
public static void LoadType(System.Type t, string name)
{
var info = new UserDataType(t);
Types.AddOrUpdate(t, info, (type, dataType) => dataType);
UserDataBoundTypeDefinition boundType;
if (t.IsEnum)
{
@ -28,6 +26,8 @@ namespace Upsilon.BaseTypes.UserData
boundType = UserDataBoundTypeDefinition.Create(t, name);
}
BoundTypeHandler.LoadUserDataTypeDefinition(boundType);
var info = new UserDataType(t);
Types.AddOrUpdate(t, info, (type, dataType) => dataType);
}
public static void LoadAssembly(Assembly assembly)

View File

@ -378,6 +378,12 @@ namespace Upsilon.Binder
diagnostics?.LogWarning($"Can't resolve type '{bDefProperty.ToString()}'", fullStopIndexExpression.Span);
return new VariableSymbol(fullStopIndexExpression.Index, Type.Unknown, true);
}
else
{
diagnostics?.LogWarning(
$"Can't resolve property '{fullStopIndexExpression.Index}' on type {parent.Name}",
fullStopIndexExpression.Span);
}
}
else if (indexerVariable.TypeContainer == Type.Unknown)
{
@ -492,9 +498,17 @@ namespace Upsilon.Binder
}
}
else if (assignment.Type == Type.UserData)
{
var ud = assignment.Type.UserData;
if (ud == null)
{
variable = new VariableSymbol(name, Type.Unknown, isLocal);
}
else
{
variable = new UserDataVariableSymbol(name,
BoundTypeHandler.GetTypeDefinition(assignment.Type.UserData), isLocal);
BoundTypeHandler.GetTypeDefinition(ud), isLocal);
}
}
else
{

View File

@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes;
@ -17,12 +18,21 @@ namespace Upsilon.BoundTypes
dic.TryAdd("function", new BoundTypeDefinition(Type.Function, new System.Type[0]));
}
private static readonly ConcurrentDictionary<System.Type, string> TypeLookup =
new ConcurrentDictionary<System.Type, string>();
private static readonly ConcurrentDictionary<string, BoundTypeDefinition> TypeDefinitions = Reset();
public static ConcurrentDictionary<string, BoundTypeDefinition> Reset()
{
var dic = new ConcurrentDictionary<string, BoundTypeDefinition>();
AddDefaults(dic);
foreach (var boundTypeDefinition in dic)
{
foreach (var valueValidInternalType in boundTypeDefinition.Value.ValidInternalTypes)
{
TypeLookup.TryAdd(valueValidInternalType, boundTypeDefinition.Key);
}
}
return dic;
}
@ -43,14 +53,28 @@ namespace Upsilon.BoundTypes
public static string GetTypeName(System.Type type)
{
return TypeDefinitions.FirstOrDefault(x =>
x.Value.ValidInternalTypes.Any(validType => validType.IsAssignableFrom(type))).Key;
if (TypeLookup.TryGetValue(type, out var bDefKey))
{
return bDefKey;
}
var bDef = TypeDefinitions.FirstOrDefault(x =>
x.Value.ValidInternalTypes.Any(validType => validType.IsAssignableFrom(type)));
if (!bDef.Equals(default(KeyValuePair<string, BoundTypeDefinition>)))
{
TypeLookup.TryAdd(type, bDef.Key);
}
return null;
}
public static void LoadUserDataTypeDefinition(UserDataBoundTypeDefinition def)
{
var key = def.Name.ToLowerInvariant();
TypeDefinitions.AddOrUpdate(key, def, (s, definition) => definition);
foreach (var valueValidInternalType in def.ValidInternalTypes)
{
TypeLookup.TryAdd(valueValidInternalType, key);
}
}
}
}

View File

@ -196,10 +196,7 @@ namespace Upsilon.StandardLibraries
if (type == typeof(ScriptType))
// allows every type
return (Type) 255;
var boundType = BoundTypeHandler.GetTypeName(type);
if (boundType != null)
return new TypeContainer(boundType);
return Type.Unknown;
return new UndefinedUserDataTypeContainer(Type.UserData, type);
}
}
}