using System.Diagnostics; using System.Text; namespace Compiler; public class Generator { public static string Emit(List functions, ModuleGraph moduleGraph, string? entryPoint) { return new Generator(functions, moduleGraph, entryPoint).Emit(); } private Generator(List functions, ModuleGraph moduleGraph, string? entryPoint) { this.functions = functions; this.moduleGraph = moduleGraph; this.entryPoint = entryPoint; } private readonly List functions; private readonly ModuleGraph moduleGraph; private readonly string? entryPoint; private IndentedTextWriter writer = new(); private readonly HashSet referencedTypes = []; private readonly Dictionary referencedStringLiterals = []; private readonly HashSet emittedTypes = []; private readonly Stack scopes = new(); private int tmpNameIndex = 0; private string Emit() { if (entryPoint != null) { writer.WriteLine($$""" int main(int argc, char *argv[]) { return {{entryPoint}}(); } """); writer.WriteLine(); } 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"); if (info.Source == Module.DefinitionSource.Imported || info.Extern) writer.Write("extern "); else if (info.Source == Module.DefinitionSource.Internal && !info.Extern && !info.Exported) writer.Write("static "); var parameters = function.Parameters.Select(x => CType(x.Type, x.Name.Ident)); writer.WriteLine($"{CType(function.ReturnType, info.MangledName)}({string.Join(", ", parameters)})"); writer.WriteLine("{"); using (writer.Indent()) { PushScope(); EmitStatement(function.Body); PopScope(); } writer.WriteLine("}"); writer.WriteLine(); } var implementations = writer.ToString(); writer = new IndentedTextWriter(); foreach (var module in moduleGraph.GetModules()) { foreach (var (name, info) in module.GetIdentifiers()) { if (info.Source == Module.DefinitionSource.Internal || info.Exported) { if (info.Source == Module.DefinitionSource.Imported || info.Extern) writer.Write("extern "); else if (info.Source == Module.DefinitionSource.Internal && !info.Extern && !info.Exported) writer.Write("static "); if (info.Type is NubTypeFunc fn) writer.WriteLine($"{CType(fn.ReturnType, info.MangledName)}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});"); else writer.WriteLine($"{CType(info.Type, info.MangledName)};"); } } } var declarations = writer.ToString(); writer = new IndentedTextWriter(); writer.WriteLine( """ #include #include #include #include typedef struct { char *data; size_t length; uint32_t ref; uint32_t flags; } string; typedef uint8_t u8; typedef int8_t i8; typedef uint16_t u16; typedef int16_t i16; typedef uint32_t u32; typedef int32_t i32; typedef uint64_t u64; typedef int64_t i64; #define FLAG_STRING_LITERAL 1 #define STRING_DEBUG 1 #if STRING_DEBUG #define STR_DBG(fmt, ...) fprintf(stderr, "[STR] " fmt "\n", ##__VA_ARGS__) #else #define STR_DBG(fmt, ...) #endif #if STRING_DEBUG static size_t string_allocs = 0; static size_t string_frees = 0; __attribute__((destructor)) static void string_debug_report(void) { fprintf(stderr, "[STR] REPORT allocs=%zu frees=%zu leaks=%zu\n", string_allocs, string_frees, string_allocs - string_frees); } #endif static inline void string_rc_inc(string *str) { if (str == NULL) { STR_DBG("INC null string"); return; } if (str->flags & FLAG_STRING_LITERAL) return; str->ref += 1; STR_DBG("INC: str=%p ref=%u \"%s\"", (void *)str, str->ref, str->data); } static inline void string_rc_dec(string *str) { if (str == NULL) { STR_DBG("DEC null string"); return; } if (str->flags & FLAG_STRING_LITERAL) return; if (str->ref == 0) { STR_DBG("ERROR: DEC on zero refcount str=%p", (void *)str); return; } str->ref -= 1; STR_DBG("DEC: str=%p ref=%u \"%s\"", (void *)str, str->ref, str->data); if (str->ref == 0) { STR_DBG("FREE: str=%p data=%p \"%s\"", (void *)str, (void *)str->data, str->data); #if STRING_DEBUG string_frees++; #endif free(str->data); free(str); } } static inline string *string_concat(string *left, string *right) { size_t new_length = left->length + right->length; string *result = (string *)malloc(sizeof(string)); result->data = (char *)malloc(new_length + 1); memcpy(result->data, left->data, left->length); memcpy(result->data + left->length, right->data, right->length); result->data[new_length] = '\0'; result->length = new_length; result->ref = 1; result->flags = 0; #if STRING_DEBUG string_allocs++; STR_DBG("NEW concat: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data); #endif return result; } static inline string *string_from_cstr(char *cstr) { size_t len = strlen(cstr); string *result = (string *)malloc(sizeof(string)); result->data = (char *)malloc(len + 1); memcpy(result->data, cstr, len + 1); result->length = len; result->ref = 1; result->flags = 0; #if STRING_DEBUG string_allocs++; STR_DBG("NEW from_cstr: str=%p ref=%u \"%s\"", (void *)result, result->ref, result->data); #endif return result; } """ ); while (referencedTypes.Count != 0) { var type = referencedTypes.ElementAt(0); EmitTypeDefinitionIfNotEmitted(type); referencedTypes.Remove(type); } foreach (var (name, value) in referencedStringLiterals) { writer.WriteLine ( $$""" static string {{name}} = (string){ .data = "{{value}}", .length = {{Encoding.UTF8.GetByteCount(value)}}, .ref = 0, .flags = FLAG_STRING_LITERAL }; """ ); } var header = writer.ToString(); return $"{header}\n{declarations}\n{implementations}"; } private void EmitTypeDefinitionIfNotEmitted(NubType type) { if (emittedTypes.Contains(type)) return; emittedTypes.Add(type); switch (type) { case NubTypeArray arrayType: { EmitTypeDefinitionIfNotEmitted(arrayType.ElementType); writer.WriteLine("typedef struct"); writer.WriteLine("{"); using (writer.Indent()) { writer.WriteLine("size_t count;"); writer.WriteLine("size_t capacity;"); writer.WriteLine($"{CType(arrayType.ElementType)} *items;"); } writer.WriteLine($"}} {typeNames[arrayType]};"); writer.WriteLine(); break; } case NubTypeStruct structType: { if (!moduleGraph.TryResolveType(structType.Module, structType.Name, true, out var info) || info is not Module.TypeInfoStruct structInfo) throw new UnreachableException(); foreach (var field in structInfo.Fields) EmitTypeDefinitionIfNotEmitted(field.Type); if (structInfo.Packed) writer.Write("__attribute__((__packed__)) "); writer.WriteLine("typedef struct"); writer.WriteLine("{"); using (writer.Indent()) { foreach (var field in structInfo.Fields) { writer.WriteLine($"{CType(field.Type, field.Name)};"); } } writer.WriteLine($"}} {typeNames[structType]};"); writer.WriteLine(); break; } case NubTypeAnonymousStruct anonymousStructType: { foreach (var field in anonymousStructType.Fields) EmitTypeDefinitionIfNotEmitted(field.Type); writer.WriteLine("typedef struct"); writer.WriteLine("{"); using (writer.Indent()) { foreach (var field in anonymousStructType.Fields) { writer.WriteLine($"{CType(field.Type, field.Name)};"); } } writer.WriteLine($"}} {typeNames[anonymousStructType]};"); writer.WriteLine(); break; } case NubTypeEnum enumType: { if (!moduleGraph.TryResolveType(enumType.Module, enumType.Name, true, out var info) || info is not Module.TypeInfoEnum enumInfo) throw new UnreachableException(); foreach (var variant in enumInfo.Variants) { if (variant.Type is not null) { EmitTypeDefinitionIfNotEmitted(variant.Type); } } writer.WriteLine("typedef struct"); writer.WriteLine("{"); using (writer.Indent()) { writer.WriteLine("uint32_t tag;"); writer.WriteLine("union"); writer.WriteLine("{"); using (writer.Indent()) { foreach (var variant in enumInfo.Variants) { if (variant.Type is not null) { writer.WriteLine($"{CType(variant.Type, variant.Name)};"); } } } writer.WriteLine("};"); } writer.WriteLine($"}} {typeNames[enumType]};"); writer.WriteLine(); break; } case NubTypeEnumVariant variantType: { EmitTypeDefinitionIfNotEmitted(variantType.EnumType); break; } } } private void EmitStatement(TypedNodeStatement node) { if (scopes.Peek().Unreachable) return; switch (node) { case TypedNodeStatementBlock statement: EmitStatementBlock(statement); break; case TypedNodeStatementFuncCall statement: EmitStatementFuncCall(statement); break; case TypedNodeStatementReturn statement: EmitStatementReturn(statement); break; case TypedNodeStatementVariableDeclaration statement: EmitStatementVariableDeclaration(statement); break; case TypedNodeStatementAssignment statement: EmitStatementAssignment(statement); break; case TypedNodeStatementIf statement: EmitStatementIf(statement); break; case TypedNodeStatementWhile statement: EmitStatementWhile(statement); break; case TypedNodeStatementMatch statement: EmitStatementMatch(statement); break; default: throw new ArgumentOutOfRangeException(nameof(node), node, null); } } private void EmitStatementBlock(TypedNodeStatementBlock node) { writer.WriteLine("{"); using (writer.Indent()) { PushScope(); foreach (var statement in node.Statements) EmitStatement(statement); PopScope(); } writer.WriteLine("}"); } private void EmitStatementFuncCall(TypedNodeStatementFuncCall node) { var name = EmitExpression(node.Target); var parameterValues = node.Parameters.Select(EmitExpression).ToList(); writer.WriteLine($"{name}({string.Join(", ", parameterValues)});"); } private void EmitStatementReturn(TypedNodeStatementReturn statement) { if (statement.Value != null) { var value = EmitExpression(statement.Value); EmitCleanupAllScopes(); writer.WriteLine($"return {value};"); } else { EmitCleanupAllScopes(); writer.WriteLine($"return;"); } scopes.Peek().Unreachable = true; } private void EmitStatementVariableDeclaration(TypedNodeStatementVariableDeclaration statement) { var value = EmitExpression(statement.Value); EmitCopyConstructor(value, statement.Value.Type); writer.WriteLine($"{CType(statement.Type, statement.Name.Ident)} = {value};"); scopes.Peek().DeconstructableNames.Add((statement.Name.Ident, statement.Type)); } private void EmitStatementAssignment(TypedNodeStatementAssignment statement) { var target = EmitExpression(statement.Target); EmitCopyDestructor(target, statement.Target.Type); var value = EmitExpression(statement.Value); EmitCopyConstructor(value, statement.Value.Type); writer.WriteLine($"{target} = {value};"); } private void EmitStatementIf(TypedNodeStatementIf statement) { var condition = EmitExpression(statement.Condition); writer.WriteLine($"if ({condition})"); writer.WriteLine("{"); using (writer.Indent()) { PushScope(); EmitStatement(statement.ThenBlock); PopScope(); } writer.WriteLine("}"); if (statement.ElseBlock != null) { writer.Write("else"); if (statement.ElseBlock is TypedNodeStatementIf) writer.Write(" "); else writer.WriteLine(); writer.WriteLine("{"); using (writer.Indent()) { PushScope(); EmitStatement(statement.ElseBlock); PopScope(); } writer.WriteLine("}"); } } private void EmitStatementWhile(TypedNodeStatementWhile statement) { var condition = EmitExpression(statement.Condition); writer.WriteLine($"while ({condition})"); writer.WriteLine("{"); using (writer.Indent()) { PushScope(); EmitStatement(statement.Body); PopScope(); } writer.WriteLine("}"); } private void EmitStatementMatch(TypedNodeStatementMatch statement) { var target = EmitExpression(statement.Target); var enumType = (NubTypeEnum)statement.Target.Type; if (!moduleGraph.TryResolveType(enumType.Module, enumType.Name, true, out var info)) throw new UnreachableException(); var enumInfo = (Module.TypeInfoEnum)info; writer.WriteLine($"switch ({target}.tag)"); writer.WriteLine("{"); using (writer.Indent()) { foreach (var @case in statement.Cases) { var variantInfo = enumInfo.Variants.First(x => x.Name == @case.Variant.Ident); var tag = enumInfo.Variants.ToList().FindIndex(x => x.Name == @case.Variant.Ident); writer.WriteLine($"case {tag}:"); writer.WriteLine("{"); using (writer.Indent()) { PushScope(); if (@case.VariableName != null) { Debug.Assert(variantInfo.Type is not null); writer.WriteLine($"{CType(variantInfo.Type, @case.VariableName.Ident)} = {target}.{@case.Variant.Ident};"); } EmitStatement(@case.Body); PopScope(); writer.WriteLine("break;"); } writer.WriteLine("}"); } } writer.WriteLine("}"); } private string EmitExpression(TypedNodeExpression node) { return node switch { TypedNodeExpressionBinary expression => EmitExpressionBinary(expression), TypedNodeExpressionUnary expression => EmitExpressionUnary(expression), TypedNodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false", TypedNodeExpressionIntLiteral expression => expression.Value.Value.ToString(), TypedNodeExpressionStringLiteral expression => EmitExpressionStringLiteral(expression), TypedNodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression), TypedNodeExpressionEnumLiteral expression => EmitExpressionEnumLiteral(expression), TypedNodeExpressionStringConstructor expression => EmitExpressionStringConstructor(expression), TypedNodeExpressionStructMemberAccess expression => EmitExpressionMemberAccess(expression), TypedNodeExpressionStringLength expression => EmitExpressionStringLength(expression), TypedNodeExpressionStringPointer expression => EmitExpressionStringPointer(expression), TypedNodeExpressionArrayCount expression => EmitExpressionArrayCount(expression), TypedNodeExpressionArrayPointer expression => EmitExpressionArrayPointer(expression), TypedNodeExpressionLocalIdent expression => expression.Name, TypedNodeExpressionGlobalIdent expression => EmitNodeExpressionGlobalIdent(expression), TypedNodeExpressionFuncCall expression => EmitExpressionFuncCall(expression), _ => throw new ArgumentOutOfRangeException(nameof(node), node, null) }; } private string EmitExpressionBinary(TypedNodeExpressionBinary expression) { var left = EmitExpression(expression.Left); var right = EmitExpression(expression.Right); var name = Tmp(); if (expression.Operation == TypedNodeExpressionBinary.Op.Add && expression.Left.Type is NubTypeString && expression.Right.Type is NubTypeString) { scopes.Peek().DeconstructableNames.Add((name, expression.Type)); writer.WriteLine($"{CType(NubTypeString.Instance, name)} = nub_core_string_concat({left}, {right});"); return name; } var op = expression.Operation switch { TypedNodeExpressionBinary.Op.Add => $"({left} + {right})", TypedNodeExpressionBinary.Op.Subtract => $"({left} - {right})", TypedNodeExpressionBinary.Op.Multiply => $"({left} * {right})", TypedNodeExpressionBinary.Op.Divide => $"({left} / {right})", TypedNodeExpressionBinary.Op.Modulo => $"({left} % {right})", TypedNodeExpressionBinary.Op.Equal => $"({left} == {right})", TypedNodeExpressionBinary.Op.NotEqual => $"({left} != {right})", TypedNodeExpressionBinary.Op.LessThan => $"({left} < {right})", TypedNodeExpressionBinary.Op.LessThanOrEqual => $"({left} <= {right})", TypedNodeExpressionBinary.Op.GreaterThan => $"({left} > {right})", TypedNodeExpressionBinary.Op.GreaterThanOrEqual => $"({left} >= {right})", TypedNodeExpressionBinary.Op.LeftShift => $"({left} << {right})", TypedNodeExpressionBinary.Op.RightShift => $"({left} >> {right})", TypedNodeExpressionBinary.Op.LogicalAnd => $"({left} && {right})", TypedNodeExpressionBinary.Op.LogicalOr => $"({left} || {right})", _ => throw new ArgumentOutOfRangeException() }; writer.WriteLine($"{CType(expression.Type, name)} = {op};"); return name; } private string EmitExpressionUnary(TypedNodeExpressionUnary expression) { var target = EmitExpression(expression.Target); var name = Tmp(); var op = expression.Operation switch { TypedNodeExpressionUnary.Op.Negate => $"(-{target})", TypedNodeExpressionUnary.Op.Invert => $"(!{target})", _ => throw new ArgumentOutOfRangeException() }; writer.WriteLine($"{CType(expression.Type, name)} = {op};"); return name; } private string EmitExpressionStringLiteral(TypedNodeExpressionStringLiteral expression) { var name = Tmp(); referencedStringLiterals.Add(name, expression.Value.Value); return $"(&{name})"; } private string EmitExpressionStructLiteral(TypedNodeExpressionStructLiteral expression) { var name = Tmp(); scopes.Peek().DeconstructableNames.Add((name, expression.Type)); var initializerValues = new Dictionary(); foreach (var initializer in expression.Initializers) { var value = EmitExpression(initializer.Value); EmitCopyConstructor(value, initializer.Value.Type); initializerValues[initializer.Name.Ident] = value; } var initializerStrings = initializerValues.Select(x => $".{x.Key} = {x.Value}"); writer.WriteLine($"{CType(expression.Type, name)} = ({CType(expression.Type)}){{ {string.Join(", ", initializerStrings)} }};"); return name; } private string EmitExpressionEnumLiteral(TypedNodeExpressionEnumLiteral expression) { var name = Tmp(); scopes.Peek().DeconstructableNames.Add((name, expression.Type)); var enumVariantType = (NubTypeEnumVariant)expression.Type; if (!moduleGraph.TryResolveType(enumVariantType.EnumType.Module, enumVariantType.EnumType.Name, true, out var info)) throw new UnreachableException(); var enumInfo = (Module.TypeInfoEnum)info; var tag = enumInfo.Variants.ToList().FindIndex(x => x.Name == enumVariantType.Variant); string? value = null; if (expression.Value != null) { value = EmitExpression(expression.Value); EmitCopyConstructor(value, expression.Value.Type); } writer.Write($"{CType(expression.Type, name)} = ({CType(expression.Type)}){{ .tag = {tag}"); if (value != null) writer.WriteLine($", .{enumVariantType.Variant} = {value} }};"); else writer.WriteLine(" }};"); return name; } private string EmitExpressionStringConstructor(TypedNodeExpressionStringConstructor expression) { var name = Tmp(); scopes.Peek().DeconstructableNames.Add((name, expression.Type)); var value = EmitExpression(expression.Value); writer.WriteLine($"{CType(expression.Type, name)} = nub_core_string_from_cstr({value});"); return name; } private string EmitExpressionMemberAccess(TypedNodeExpressionStructMemberAccess expression) { var target = EmitExpression(expression.Target); return $"{target}.{expression.Name.Ident}"; } private string EmitExpressionStringLength(TypedNodeExpressionStringLength expression) { var target = EmitExpression(expression.Target); return $"{target}->length"; } private string EmitExpressionStringPointer(TypedNodeExpressionStringPointer expression) { var target = EmitExpression(expression.Target); return $"{target}->data"; } private string EmitExpressionArrayCount(TypedNodeExpressionArrayCount expression) { var target = EmitExpression(expression.Target); return $"{target}.count"; } private string EmitExpressionArrayPointer(TypedNodeExpressionArrayPointer expression) { var target = EmitExpression(expression.Target); return $"{target}.items"; } private string EmitNodeExpressionGlobalIdent(TypedNodeExpressionGlobalIdent expression) { if (!moduleGraph.TryResolveIdentifier(expression.Module, expression.Name, true, out var info)) throw new UnreachableException($"Module graph does not have info about identifier {expression.Module}::{expression.Name}. This should have been caught earlier"); return info.MangledName; } private string EmitExpressionFuncCall(TypedNodeExpressionFuncCall expression) { var name = EmitExpression(expression.Target); var parameterValues = expression.Parameters.Select(EmitExpression).ToList(); var tmp = Tmp(); writer.WriteLine($"{CType(expression.Type, tmp)} = {name}({string.Join(", ", parameterValues)});"); return tmp; } public string CType(NubType node, string? varName = null) { referencedTypes.Add(node); return node switch { NubTypeVoid => "void" + (varName != null ? $" {varName}" : ""), NubTypeBool => "bool" + (varName != null ? $" {varName}" : ""), NubTypeStruct type => CTypeNamed(type, varName), NubTypeAnonymousStruct type => CTypeNamed(type, varName), NubTypeEnum type => CTypeNamed(type, varName), NubTypeEnumVariant type => CType(type.EnumType, varName), NubTypeSInt type => $"i{type.Width}" + (varName != null ? $" {varName}" : ""), NubTypeUInt type => $"u{type.Width}" + (varName != null ? $" {varName}" : ""), NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"), NubTypeString type => "string" + (varName != null ? $" *{varName}" : "*"), NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})", NubTypeArray type => CTypeNamed(type, varName), _ => throw new ArgumentOutOfRangeException(nameof(node), node, null) }; } private readonly Dictionary typeNames = []; private string CTypeNamed(NubType type, string? varName) { if (!typeNames.TryGetValue(type, out var name)) { name = Tmp(); typeNames[type] = name; } return $"{name}{(varName != null ? $" {varName}" : "")}"; } private string Tmp() { return $"_tmp{tmpNameIndex++}"; } private void EmitCleanupAllScopes() { foreach (var scope in scopes.Reverse()) { for (int i = scope.DeconstructableNames.Count - 1; i >= 0; i--) { var (name, type) = scope.DeconstructableNames[i]; EmitCopyDestructor(name, type); } } } private void EmitCleanupCurrentScope(Scope scope) { for (int i = scope.DeconstructableNames.Count - 1; i >= 0; i--) { var (name, type) = scope.DeconstructableNames[i]; EmitCopyDestructor(name, type); } } private void EmitCopyConstructor(string value, NubType type) { switch (type) { case NubTypeString: { writer.WriteLine($"string_rc_inc({value});"); break; } case NubTypeStruct structType: { if (!moduleGraph.TryResolveType(structType.Module, structType.Name, true, out var info) || info is not Module.TypeInfoStruct structInfo) throw new UnreachableException(); foreach (var field in structInfo.Fields) { EmitCopyConstructor($"{value}.{field.Name}", field.Type); } break; } case NubTypeAnonymousStruct anonymousStructType: { foreach (var field in anonymousStructType.Fields) { EmitCopyConstructor($"{value}.{field.Name}", field.Type); } break; } case NubTypeEnum enumType: { if (!moduleGraph.TryResolveType(enumType.Module, enumType.Name, true, out var info) || info is not Module.TypeInfoEnum enumInfo) throw new UnreachableException(); writer.WriteLine($"switch ({value}.tag)"); writer.WriteLine("{"); using (writer.Indent()) { for (int i = 0; i < enumInfo.Variants.Count; i++) { Module.TypeInfoEnum.Variant variant = enumInfo.Variants[i]; if (variant.Type is not null) { writer.WriteLine($"case {i}:"); writer.WriteLine("{"); using (writer.Indent()) { EmitCopyConstructor($"{value}.{variant.Name}", variant.Type); writer.WriteLine("break;"); } writer.WriteLine("}"); } } } writer.WriteLine("}"); break; } case NubTypeEnumVariant enumVariantType: { if (!moduleGraph.TryResolveType(enumVariantType.EnumType.Module, enumVariantType.EnumType.Name, true, out var info) || info is not Module.TypeInfoEnum enumInfo) throw new UnreachableException(); var variant = enumInfo.Variants.First(x => x.Name == enumVariantType.Variant); if (variant.Type is not null) EmitCopyConstructor($"{value}.{variant.Name}", variant.Type); break; } } } private void EmitCopyDestructor(string value, NubType type) { switch (type) { case NubTypeString: { writer.WriteLine($"string_rc_dec({value});"); break; } case NubTypeStruct structType: { if (!moduleGraph.TryResolveType(structType.Module, structType.Name, true, out var info) || info is not Module.TypeInfoStruct structInfo) throw new UnreachableException(); foreach (var field in structInfo.Fields) { EmitCopyDestructor($"{value}.{field.Name}", field.Type); } break; } case NubTypeAnonymousStruct anonymousStructType: { foreach (var field in anonymousStructType.Fields) { EmitCopyDestructor($"{value}.{field.Name}", field.Type); } break; } case NubTypeEnum enumType: { if (!moduleGraph.TryResolveType(enumType.Module, enumType.Name, true, out var info) || info is not Module.TypeInfoEnum enumInfo) throw new UnreachableException(); writer.WriteLine($"switch ({value}.tag)"); writer.WriteLine("{"); using (writer.Indent()) { for (int i = 0; i < enumInfo.Variants.Count; i++) { var variant = enumInfo.Variants[i]; if (variant.Type is not null) { writer.WriteLine($"case {i}:"); writer.WriteLine("{"); using (writer.Indent()) { EmitCopyDestructor($"{value}.{variant.Name}", variant.Type); writer.WriteLine("break;"); } writer.WriteLine("}"); } } } writer.WriteLine("}"); break; } case NubTypeEnumVariant enumVariantType: { if (!moduleGraph.TryResolveType(enumVariantType.EnumType.Module, enumVariantType.EnumType.Name, true, out var info) || info is not Module.TypeInfoEnum enumInfo) throw new UnreachableException(); var variant = enumInfo.Variants.First(x => x.Name == enumVariantType.Variant); if (variant.Type is not null) EmitCopyDestructor($"{value}.{variant.Name}", variant.Type); break; } } } private void PushScope() { scopes.Push(new Scope()); } private void PopScope() { var scope = scopes.Pop(); if (!scope.Unreachable) EmitCleanupCurrentScope(scope); } private class Scope { public List<(string Name, NubType Type)> DeconstructableNames { get; } = []; public bool Unreachable { get; set; } } } internal class IndentedTextWriter { private readonly StringBuilder builder = new(); private int indentLevel; public IDisposable Indent() { indentLevel++; return new IndentScope(this); } public void WriteLine(string text) { WriteIndent(); builder.AppendLine(text); } public void Write(string text) { WriteIndent(); builder.Append(text); } public void WriteLine() { builder.AppendLine(); } public override string ToString() { return builder.ToString(); } private void WriteIndent() { if (builder.Length > 0) { var lastChar = builder[^1]; if (lastChar != '\n' && lastChar != '\r') return; } for (var i = 0; i < indentLevel; i++) { builder.Append(" "); } } private class IndentScope(IndentedTextWriter writer) : IDisposable { private bool disposed; public void Dispose() { if (disposed) return; writer.indentLevel--; disposed = true; } } }