union definitions
This commit is contained in:
@@ -85,11 +85,21 @@ public class ModuleGraph
|
||||
switch (type)
|
||||
{
|
||||
case Manifest.Module.TypeInfoStruct s:
|
||||
{
|
||||
var info = new Module.TypeInfoStruct(Module.DefinitionKind.External, s.Packed);
|
||||
var fields = s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList();
|
||||
info.SetFields(fields);
|
||||
module.AddType(name, info);
|
||||
break;
|
||||
}
|
||||
case Manifest.Module.TypeInfoEnum s:
|
||||
{
|
||||
var info = new Module.TypeInfoEnum(Module.DefinitionKind.External);
|
||||
var variants = s.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name, v.Fields.Select(x => new Module.TypeInfoEnum.Variant.Field(x.Name, x.Type)).ToList())).ToList();
|
||||
info.SetVariants(variants);
|
||||
module.AddType(name, info);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(type));
|
||||
}
|
||||
@@ -111,6 +121,12 @@ public class ModuleGraph
|
||||
var kind = structDef.Exported ? Module.DefinitionKind.Exported : Module.DefinitionKind.Internal;
|
||||
module.AddType(structDef.Name.Ident, new Module.TypeInfoStruct(kind, structDef.Packed));
|
||||
}
|
||||
|
||||
foreach (var enumDef in ast.Definitions.OfType<NodeDefinitionEnum>())
|
||||
{
|
||||
var kind = enumDef.Exported ? Module.DefinitionKind.Exported : Module.DefinitionKind.Internal;
|
||||
module.AddType(enumDef.Name.Ident, new Module.TypeInfoEnum(kind));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var ast in asts)
|
||||
@@ -128,6 +144,18 @@ public class ModuleGraph
|
||||
structType.SetFields(fields);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var enumDef in ast.Definitions.OfType<NodeDefinitionEnum>())
|
||||
{
|
||||
if (!module.TryResolveType(enumDef.Name.Ident, true, out var typeInfo))
|
||||
throw new UnreachableException($"{nameof(typeInfo)} should always be registered");
|
||||
|
||||
if (typeInfo is Module.TypeInfoEnum enumType)
|
||||
{
|
||||
var variants = enumDef.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name.Ident, v.Fields.Select(f => new Module.TypeInfoEnum.Variant.Field(f.Name.Ident, ResolveType(f.Type, module.Name))).ToList())).ToList();
|
||||
enumType.SetVariants(variants);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var ast in asts)
|
||||
@@ -188,6 +216,7 @@ public class ModuleGraph
|
||||
return customType switch
|
||||
{
|
||||
Module.TypeInfoStruct => NubTypeStruct.Get(type.Module.Ident, type.Name.Ident),
|
||||
Module.TypeInfoEnum => NubTypeEnum.Get(type.Module.Ident, type.Name.Ident),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(customType))
|
||||
};
|
||||
}
|
||||
@@ -301,4 +330,28 @@ public class Module(string name)
|
||||
public NubType Type { get; } = type;
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeInfoEnum(DefinitionKind kind) : TypeInfo(kind)
|
||||
{
|
||||
private IReadOnlyList<Variant>? variants;
|
||||
|
||||
public IReadOnlyList<Variant> Variants => variants ?? throw new InvalidOperationException("Fields has not been set yet");
|
||||
|
||||
public void SetVariants(IReadOnlyList<Variant> variants)
|
||||
{
|
||||
this.variants = variants;
|
||||
}
|
||||
|
||||
public class Variant(string name, List<Variant.Field> fields)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public List<Field> Fields { get; } = fields;
|
||||
|
||||
public class Field(string name, NubType type)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public NubType Type { get; } = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user