Fixes for types sometimes choosing type they inherit from instead of themselves
This commit is contained in:
parent
8bd5f7d0af
commit
4062d2f140
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
return new UndefinedUserDataTypeContainer(t);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
@ -493,8 +499,16 @@ namespace Upsilon.Binder
|
|||
}
|
||||
else if (assignment.Type == Type.UserData)
|
||||
{
|
||||
variable = new UserDataVariableSymbol(name,
|
||||
BoundTypeHandler.GetTypeDefinition(assignment.Type.UserData), isLocal);
|
||||
var ud = assignment.Type.UserData;
|
||||
if (ud == null)
|
||||
{
|
||||
variable = new VariableSymbol(name, Type.Unknown, isLocal);
|
||||
}
|
||||
else
|
||||
{
|
||||
variable = new UserDataVariableSymbol(name,
|
||||
BoundTypeHandler.GetTypeDefinition(ud), isLocal);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue