module exports and name mangling
This commit is contained in:
@@ -21,40 +21,69 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
public sealed class Module(string name)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
private readonly Dictionary<string, NubType> customTypes = new();
|
||||
private readonly Dictionary<string, NubType> identifierTypes = new();
|
||||
private readonly Dictionary<string, CustomTypeInfo> customTypes = new();
|
||||
private readonly Dictionary<string, IdentifierInfo> identifierTypes = new();
|
||||
|
||||
public IReadOnlyList<NubType> GetCustomTypes()
|
||||
{
|
||||
return customTypes.Values.ToList();
|
||||
return customTypes.Values.Select(x => x.Type).ToList();
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, NubType> GetIdentifierTypes()
|
||||
{
|
||||
return identifierTypes;
|
||||
return identifierTypes.ToDictionary(x => x.Key, x => x.Value.Type);
|
||||
}
|
||||
|
||||
public bool TryResolveCustomType(string name, [NotNullWhen(true)] out NubType? customType)
|
||||
public bool TryResolveCustomType(string name, bool searchPrivate, [NotNullWhen(true)] out NubType? customType)
|
||||
{
|
||||
customType = customTypes.GetValueOrDefault(name);
|
||||
return customType != null;
|
||||
var info = customTypes.GetValueOrDefault(name);
|
||||
if (info == null)
|
||||
{
|
||||
customType = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (info.Exported || searchPrivate)
|
||||
{
|
||||
customType = info.Type;
|
||||
return true;
|
||||
}
|
||||
|
||||
customType = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryResolveIdentifierType(string name, [NotNullWhen(true)] out NubType? identifier)
|
||||
public bool TryResolveIdentifierType(string name, bool searchPrivate, [NotNullWhen(true)] out NubType? identifierType)
|
||||
{
|
||||
identifier = identifierTypes.GetValueOrDefault(name);
|
||||
return identifier != null;
|
||||
var info = identifierTypes.GetValueOrDefault(name);
|
||||
if (info == null)
|
||||
{
|
||||
identifierType = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (info.Exported || searchPrivate)
|
||||
{
|
||||
identifierType = info.Type;
|
||||
return true;
|
||||
}
|
||||
|
||||
identifierType = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void AddCustomType(string name, NubType type)
|
||||
public void AddCustomType(string name, NubType type, bool exported)
|
||||
{
|
||||
customTypes.Add(name, type);
|
||||
customTypes.Add(name, new CustomTypeInfo(type, exported));
|
||||
}
|
||||
|
||||
public void AddIdentifier(string name, NubType identifier)
|
||||
public void AddIdentifier(string name, NubType type, bool exported)
|
||||
{
|
||||
identifierTypes.Add(name, identifier);
|
||||
identifierTypes.Add(name, new IdentifierInfo(type, exported));
|
||||
}
|
||||
|
||||
private record CustomTypeInfo(NubType Type, bool Exported);
|
||||
private record IdentifierInfo(NubType Type, bool Exported);
|
||||
}
|
||||
|
||||
public class Builder
|
||||
@@ -91,7 +120,7 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
if (module == null) continue;
|
||||
|
||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct(module.Name, structDef.Name.Ident));
|
||||
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct(module.Name, structDef.Name.Ident, structDef.Packed), structDef.Exported);
|
||||
}
|
||||
|
||||
// Third pass: Resolve struct fields
|
||||
@@ -102,10 +131,10 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
|
||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||
{
|
||||
if (!module.TryResolveCustomType(structDef.Name.Ident, out var customType))
|
||||
if (!module.TryResolveCustomType(structDef.Name.Ident, true, out var customType))
|
||||
throw new UnreachableException($"{nameof(customType)} should always be registered");
|
||||
|
||||
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type))).ToList();
|
||||
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type, true))).ToList();
|
||||
((NubTypeStruct)customType).ResolveFields(fields);
|
||||
}
|
||||
}
|
||||
@@ -118,10 +147,10 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
|
||||
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionFunc>())
|
||||
{
|
||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type)).ToList();
|
||||
var returnType = ResolveType(funcDef.ReturnType);
|
||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, true)).ToList();
|
||||
var returnType = ResolveType(funcDef.ReturnType, true);
|
||||
var funcType = NubTypeFunc.Get(parameters, returnType);
|
||||
module.AddIdentifier(funcDef.Name.Ident, funcType);
|
||||
module.AddIdentifier(funcDef.Name.Ident, funcType, funcDef.Exported);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,14 +159,14 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
|
||||
return new ModuleGraph(modules);
|
||||
|
||||
NubType ResolveType(NodeType node)
|
||||
NubType ResolveType(NodeType node, bool includePrivate)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
NodeTypeBool => NubTypeBool.Instance,
|
||||
NodeTypeCustom type => ResolveCustomType(type),
|
||||
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(ResolveType).ToList(), ResolveType(type.ReturnType)),
|
||||
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To)),
|
||||
NodeTypeCustom type => ResolveCustomType(type, includePrivate),
|
||||
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(x => ResolveType(x, includePrivate)).ToList(), ResolveType(type.ReturnType, includePrivate)),
|
||||
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To, includePrivate)),
|
||||
NodeTypeSInt type => NubTypeSInt.Get(type.Width),
|
||||
NodeTypeUInt type => NubTypeUInt.Get(type.Width),
|
||||
NodeTypeString => NubTypeString.Instance,
|
||||
@@ -146,13 +175,13 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
};
|
||||
}
|
||||
|
||||
NubType ResolveCustomType(NodeTypeCustom type)
|
||||
NubType ResolveCustomType(NodeTypeCustom type, bool includePrivate)
|
||||
{
|
||||
var module = modules.GetValueOrDefault(type.Module.Ident);
|
||||
if (module == null)
|
||||
throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build());
|
||||
|
||||
if (!module.TryResolveCustomType(type.Name.Ident, out var customType))
|
||||
if (!module.TryResolveCustomType(type.Name.Ident, includePrivate, out var customType))
|
||||
throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build());
|
||||
|
||||
return customType;
|
||||
|
||||
Reference in New Issue
Block a user