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

View File

@ -174,10 +174,7 @@ namespace Upsilon.BaseTypes
if (t == typeof(ScriptType)) if (t == typeof(ScriptType))
return Type.Unknown; return Type.Unknown;
var boundType = BoundTypeHandler.GetTypeName(t); return new UndefinedUserDataTypeContainer(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) public static void LoadType(System.Type t, string name)
{ {
var info = new UserDataType(t);
Types.AddOrUpdate(t, info, (type, dataType) => dataType);
UserDataBoundTypeDefinition boundType; UserDataBoundTypeDefinition boundType;
if (t.IsEnum) if (t.IsEnum)
{ {
@ -28,6 +26,8 @@ namespace Upsilon.BaseTypes.UserData
boundType = UserDataBoundTypeDefinition.Create(t, name); boundType = UserDataBoundTypeDefinition.Create(t, name);
} }
BoundTypeHandler.LoadUserDataTypeDefinition(boundType); BoundTypeHandler.LoadUserDataTypeDefinition(boundType);
var info = new UserDataType(t);
Types.AddOrUpdate(t, info, (type, dataType) => dataType);
} }
public static void LoadAssembly(Assembly assembly) 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); diagnostics?.LogWarning($"Can't resolve type '{bDefProperty.ToString()}'", fullStopIndexExpression.Span);
return new VariableSymbol(fullStopIndexExpression.Index, Type.Unknown, true); 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) else if (indexerVariable.TypeContainer == Type.Unknown)
{ {
@ -493,8 +499,16 @@ namespace Upsilon.Binder
} }
else if (assignment.Type == Type.UserData) else if (assignment.Type == Type.UserData)
{ {
variable = new UserDataVariableSymbol(name, var ud = assignment.Type.UserData;
BoundTypeHandler.GetTypeDefinition(assignment.Type.UserData), isLocal); if (ud == null)
{
variable = new VariableSymbol(name, Type.Unknown, isLocal);
}
else
{
variable = new UserDataVariableSymbol(name,
BoundTypeHandler.GetTypeDefinition(ud), isLocal);
}
} }
else else
{ {

View File

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