...
This commit is contained in:
@@ -18,6 +18,13 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
return module != null;
|
||||
}
|
||||
|
||||
public Manifest CreateManifest()
|
||||
{
|
||||
return new Manifest(1, GetModules().Select(x => x.CreateManifestModule()).ToList());
|
||||
}
|
||||
|
||||
public record Manifest(int Version, IReadOnlyList<Module.ManifestModule> Modules);
|
||||
|
||||
public sealed class Module(string name)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
@@ -72,37 +79,105 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
return false;
|
||||
}
|
||||
|
||||
public void AddCustomType(string name, NubType type, bool exported)
|
||||
public void AddCustomType(string name, CustomTypeInfo info)
|
||||
{
|
||||
customTypes.Add(name, new CustomTypeInfo(type, exported));
|
||||
customTypes.Add(name, info);
|
||||
}
|
||||
|
||||
public void AddIdentifier(string name, NubType type, bool exported)
|
||||
public void AddIdentifier(string name, IdentifierInfo info)
|
||||
{
|
||||
identifierTypes.Add(name, new IdentifierInfo(type, exported));
|
||||
identifierTypes.Add(name, info);
|
||||
}
|
||||
|
||||
private record CustomTypeInfo(NubType Type, bool Exported);
|
||||
private record IdentifierInfo(NubType Type, bool Exported);
|
||||
public ManifestModule CreateManifestModule()
|
||||
{
|
||||
var manifestCustomTypes = customTypes.ToDictionary(x => x.Key, x => x.Value.CreateManifestCustomTypeInfo());
|
||||
var manifestIdentifiers = identifierTypes.ToDictionary(x => x.Key, x => x.Value.CreateManifestIdentifierInfo());
|
||||
return new ManifestModule(Name, manifestCustomTypes, manifestIdentifiers);
|
||||
}
|
||||
|
||||
public enum Source
|
||||
{
|
||||
Ast,
|
||||
Lib,
|
||||
}
|
||||
|
||||
public class CustomTypeInfo(NubType type, bool exported, Source source)
|
||||
{
|
||||
public NubType Type { get; } = type;
|
||||
public bool Exported { get; } = exported;
|
||||
public Source Source { get; } = source;
|
||||
|
||||
public ManifestCustomTypeInfo CreateManifestCustomTypeInfo()
|
||||
{
|
||||
return new ManifestCustomTypeInfo(Type, Exported);
|
||||
}
|
||||
}
|
||||
|
||||
public class IdentifierInfo(NubType type, bool exported, Source source)
|
||||
{
|
||||
public NubType Type { get; } = type;
|
||||
public bool Exported { get; } = exported;
|
||||
public Source Source { get; } = source;
|
||||
|
||||
public ManifestIdentifierInfo CreateManifestIdentifierInfo()
|
||||
{
|
||||
return new ManifestIdentifierInfo(Type, Exported);
|
||||
}
|
||||
}
|
||||
|
||||
public record ManifestModule(string Name, Dictionary<string, ManifestCustomTypeInfo> CustomTypes, Dictionary<string, ManifestIdentifierInfo> Identifiers);
|
||||
public record ManifestCustomTypeInfo(NubType Type, bool Exported);
|
||||
public record ManifestIdentifierInfo(NubType Type, bool Exported);
|
||||
}
|
||||
|
||||
public class Builder
|
||||
{
|
||||
private readonly List<Ast> asts = [];
|
||||
private readonly List<Manifest> manifests = [];
|
||||
|
||||
public void AddAst(Ast ast)
|
||||
{
|
||||
asts.Add(ast);
|
||||
}
|
||||
|
||||
public void AddManifest(Manifest manifest)
|
||||
{
|
||||
manifests.Add(manifest);
|
||||
}
|
||||
|
||||
public ModuleGraph? Build(out List<Diagnostic> diagnostics)
|
||||
{
|
||||
diagnostics = [];
|
||||
|
||||
var astModuleCache = new Dictionary<Ast, Module>();
|
||||
var modules = new Dictionary<string, Module>();
|
||||
|
||||
// First pass: Register modules
|
||||
// First pass: Register libraries
|
||||
foreach (var manifest in manifests)
|
||||
{
|
||||
foreach (var manifestModule in manifest.Modules)
|
||||
{
|
||||
if (!modules.TryGetValue(manifestModule.Name, out var module))
|
||||
{
|
||||
module = new Module(manifestModule.Name);
|
||||
modules.Add(manifestModule.Name, module);
|
||||
}
|
||||
|
||||
foreach (var customType in manifestModule.CustomTypes)
|
||||
{
|
||||
module.AddCustomType(customType.Key, new Module.CustomTypeInfo(customType.Value.Type, customType.Value.Exported, Module.Source.Lib));
|
||||
}
|
||||
|
||||
foreach (var customType in manifestModule.Identifiers)
|
||||
{
|
||||
module.AddIdentifier(customType.Key, new Module.IdentifierInfo(customType.Value.Type, customType.Value.Exported, Module.Source.Lib));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var astModuleCache = new Dictionary<Ast, Module>();
|
||||
|
||||
// First pass: Register modules from ast
|
||||
foreach (var ast in asts)
|
||||
{
|
||||
if (!modules.ContainsKey(ast.ModuleName.Ident))
|
||||
@@ -119,7 +194,11 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
var module = astModuleCache[ast];
|
||||
|
||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct(module.Name, structDef.Name.Ident, structDef.Packed), structDef.Exported);
|
||||
{
|
||||
var type = new NubTypeStruct(module.Name, structDef.Name.Ident, structDef.Packed);
|
||||
var info = new Module.CustomTypeInfo(type, structDef.Exported, Module.Source.Ast);
|
||||
module.AddCustomType(structDef.Name.Ident, info);
|
||||
}
|
||||
}
|
||||
|
||||
// Third pass: Resolve struct fields
|
||||
@@ -147,13 +226,15 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
|
||||
var returnType = ResolveType(funcDef.ReturnType, module.Name);
|
||||
var funcType = NubTypeFunc.Get(parameters, returnType);
|
||||
module.AddIdentifier(funcDef.Name.Ident, funcType, funcDef.Exported);
|
||||
var info = new Module.IdentifierInfo(funcType, funcDef.Exported, Module.Source.Ast);
|
||||
module.AddIdentifier(funcDef.Name.Ident, info);
|
||||
}
|
||||
|
||||
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionGlobalVariable>())
|
||||
{
|
||||
var type = ResolveType(globalVariable.Type, module.Name);
|
||||
module.AddIdentifier(globalVariable.Name.Ident, type, globalVariable.Exported);
|
||||
var info = new Module.IdentifierInfo(type, globalVariable.Exported, Module.Source.Ast);
|
||||
module.AddIdentifier(globalVariable.Name.Ident, info);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user