Save mangled name in module graph

This commit is contained in:
nub31
2026-02-15 02:55:11 +01:00
parent ee485e4119
commit c342b2e102
5 changed files with 63 additions and 30 deletions

View File

@@ -1,4 +1,5 @@
using System.Text;
using System.Diagnostics;
using System.Text;
namespace Compiler;
@@ -45,7 +46,7 @@ public class Generator
{
if (info is Module.TypeInfoStruct s)
{
writer.WriteLine($"struct {Name.Create(module.Name, name, NubTypeStruct.Get(module.Name, name))};");
writer.WriteLine($"struct {NameMangler.Mangle(module.Name, name, NubTypeStruct.Get(module.Name, name))};");
}
}
}
@@ -64,7 +65,7 @@ public class Generator
writer.Write("__attribute__((__packed__)) ");
}
writer.WriteLine(Name.Create(module.Name, name, NubTypeStruct.Get(module.Name, name)));
writer.WriteLine(NameMangler.Mangle(module.Name, name, NubTypeStruct.Get(module.Name, name)));
writer.WriteLine("{");
using (writer.Indent())
{
@@ -86,14 +87,14 @@ public class Generator
{
if (info.Type is NubTypeFunc fn)
{
if (!functions.Any(x => x.GetMangledName() == Name.Create(module.Name, name, info.Type)))
if (info.External)
writer.Write("extern ");
writer.WriteLine($"{CType(fn.ReturnType, Name.Create(module.Name, name, info.Type))}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});");
writer.WriteLine($"{CType(fn.ReturnType, info.MangledName)}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});");
}
else
{
writer.WriteLine($"{CType(info.Type, Name.Create(module.Name, name, info.Type))};");
writer.WriteLine($"{CType(info.Type, info.MangledName)};");
}
}
}
@@ -102,12 +103,13 @@ public class Generator
if (!compileLib)
{
var main = functions.First(x => x.Module == "main" && x.Name.Ident == "main");
if (!moduleGraph.TryResolveIdentifier("main", "main", true, out var info))
throw new UnreachableException("func main::main() is not defined. This should have been caught earlier");
writer.WriteLine($$"""
int main(int argc, char *argv[])
{
return {{main.GetMangledName()}}();
return {{info.MangledName}}();
}
""");
}
@@ -116,8 +118,11 @@ public class Generator
foreach (var function in functions)
{
if (!moduleGraph.TryResolveIdentifier(function.Module, function.Name.Ident, true, out var info))
throw new UnreachableException($"Module graph does not have info about the function {function.Module}::{function.Name.Ident}. This should have been caught earlier");
var parameters = function.Parameters.Select(x => CType(x.Type, x.Name.Ident));
writer.WriteLine($"{CType(function.ReturnType, function.GetMangledName())}({string.Join(", ", parameters)})");
writer.WriteLine($"{CType(function.ReturnType, info.MangledName)}({string.Join(", ", parameters)})");
writer.WriteLine("{");
using (writer.Indent())
{
@@ -249,7 +254,7 @@ public class Generator
TypedNodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression),
TypedNodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression),
TypedNodeExpressionLocalIdent expression => expression.Value.Ident,
TypedNodeExpressionModuleIdent expression => Name.Create(expression.Module.Ident, expression.Value.Ident, expression.Type),
TypedNodeExpressionModuleIdent expression => EmitExpressionModuleIdent(expression),
TypedNodeExpressionFuncCall expression => EmitExpressionFuncCall(expression),
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
};
@@ -304,9 +309,8 @@ public class Generator
}
var initializerStrings = initializerValues.Select(x => $".{x.Key} = {x.Value}");
var structType = (NubTypeStruct)expression.Type;
return $"(struct {Name.Create(structType.Module, structType.Name, structType)}){{ {string.Join(", ", initializerStrings)} }}";
return $"({CType(expression.Type)}){{ {string.Join(", ", initializerStrings)} }}";
}
private string EmitExpressionMemberAccess(TypedNodeExpressionMemberAccess expression)
@@ -315,6 +319,14 @@ public class Generator
return $"{target}.{expression.Name.Ident}";
}
private string EmitExpressionModuleIdent(TypedNodeExpressionModuleIdent expression)
{
if (!moduleGraph.TryResolveIdentifier(expression.Module.Ident, expression.Value.Ident, true, out var info))
throw new UnreachableException($"Module graph does not have info about identifier {expression.Module.Ident}::{expression.Value.Ident}. This should have been caught earlier");
return info.MangledName;
}
private string EmitExpressionFuncCall(TypedNodeExpressionFuncCall expression)
{
var name = EmitExpression(expression.Target);
@@ -328,7 +340,7 @@ public class Generator
{
NubTypeVoid => "void" + (varName != null ? $" {varName}" : ""),
NubTypeBool => "bool" + (varName != null ? $" {varName}" : ""),
NubTypeStruct type => $"struct {Name.Create(type.Module, type.Name, type)}" + (varName != null ? $" {varName}" : ""),
NubTypeStruct type => $"struct {NameMangler.Mangle(type.Module, type.Name, type)}" + (varName != null ? $" {varName}" : ""),
NubTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""),
NubTypeUInt type => $"uint{type.Width}_t" + (varName != null ? $" {varName}" : ""),
NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),