tmp...
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using Microsoft.VisualBasic;
|
||||
|
||||
namespace Compiler;
|
||||
|
||||
@@ -36,8 +34,24 @@ public class Generator
|
||||
writer.WriteLine($$"""
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
return {{entryPoint}}();
|
||||
struct nub_dynamic_array_52747d8c11b7118c args = (struct nub_dynamic_array_52747d8c11b7118c){0};
|
||||
|
||||
for (int i = 0; i < argc; ++i)
|
||||
{
|
||||
auto nubstring = nub_core_string_from_cstr(argv[i]);
|
||||
da_append(&args, nubstring);
|
||||
}
|
||||
|
||||
int returnValue = {{entryPoint}}(args);
|
||||
|
||||
for (int i = 0; i < args.count; ++i)
|
||||
{
|
||||
nub_core_string_rc_dec(args.items[i]);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
""");
|
||||
|
||||
writer.WriteLine();
|
||||
@@ -104,6 +118,143 @@ public class Generator
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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
|
||||
|
||||
struct nub_core_string
|
||||
{
|
||||
char *data;
|
||||
size_t length;
|
||||
uint32_t ref;
|
||||
uint32_t flags;
|
||||
};
|
||||
|
||||
#if STRING_DEBUG
|
||||
static size_t nub_core_string_allocs = 0;
|
||||
static size_t nub_core_string_frees = 0;
|
||||
|
||||
__attribute__((destructor))
|
||||
static void string_debug_report(void)
|
||||
{
|
||||
fprintf(stderr, "[STR] REPORT allocs=%zu frees=%zu leaks=%zu\n", nub_core_string_allocs, nub_core_string_frees, nub_core_string_allocs - nub_core_string_frees);
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline void nub_core_string_rc_inc(struct nub_core_string *string)
|
||||
{
|
||||
if (string == NULL)
|
||||
{
|
||||
STR_DBG("INC null string");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string->flags & FLAG_STRING_LITERAL)
|
||||
return;
|
||||
|
||||
string->ref += 1;
|
||||
|
||||
STR_DBG("INC: str=%p ref=%u \"%s\"", (void*)string, string->ref, string->data);
|
||||
}
|
||||
|
||||
static inline void nub_core_string_rc_dec(struct nub_core_string *string)
|
||||
{
|
||||
if (string == NULL)
|
||||
{
|
||||
STR_DBG("DEC null string");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string->flags & FLAG_STRING_LITERAL)
|
||||
return;
|
||||
|
||||
if (string->ref == 0)
|
||||
{
|
||||
STR_DBG("ERROR: DEC on zero refcount str=%p", (void*)string);
|
||||
return;
|
||||
}
|
||||
|
||||
string->ref -= 1;
|
||||
|
||||
STR_DBG("DEC: str=%p ref=%u \"%s\"", (void*)string, string->ref, string->data);
|
||||
|
||||
if (string->ref == 0)
|
||||
{
|
||||
STR_DBG("FREE: str=%p data=%p \"%s\"", (void*)string, (void*)string->data, string->data);
|
||||
|
||||
char *data_ptr = string->data;
|
||||
struct nub_core_string *str_ptr = string;
|
||||
|
||||
#if STRING_DEBUG
|
||||
nub_core_string_frees++;
|
||||
|
||||
memset(data_ptr, 0xDD, str_ptr->length);
|
||||
memset(str_ptr, 0xDD, sizeof(*str_ptr));
|
||||
#endif
|
||||
|
||||
free(data_ptr);
|
||||
free(str_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
static inline struct nub_core_string *nub_core_string_concat(struct nub_core_string *left, struct nub_core_string *right)
|
||||
{
|
||||
size_t new_length = left->length + right->length;
|
||||
|
||||
struct nub_core_string *result = malloc(sizeof(struct nub_core_string));
|
||||
result->data = 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
|
||||
nub_core_string_allocs++;
|
||||
STR_DBG("NEW concat: str=%p ref=%u \"%s\"", (void*)result, result->ref, result->data);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline struct nub_core_string *nub_core_string_from_cstr(char* cstr)
|
||||
{
|
||||
size_t len = strlen(cstr);
|
||||
|
||||
struct nub_core_string *result = malloc(sizeof(struct nub_core_string));
|
||||
result->data = malloc(len + 1);
|
||||
|
||||
memcpy(result->data, cstr, len + 1);
|
||||
|
||||
result->length = len;
|
||||
result->ref = 1;
|
||||
result->flags = 0;
|
||||
|
||||
#if STRING_DEBUG
|
||||
nub_core_string_allocs++;
|
||||
STR_DBG("NEW from_cstr: str=%p ref=%u \"%s\"", (void*)result, result->ref, result->data);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#define da_append(xs, x) \
|
||||
do { \
|
||||
if ((xs)->count >= (xs)->capacity) { \
|
||||
if ((xs)->capacity == 0) (xs)->capacity = 256; \
|
||||
else (xs)->capacity *= 2; \
|
||||
(xs)->items = realloc((xs)->items, (xs)->capacity*sizeof(*(xs)->items)); \
|
||||
} \
|
||||
(xs)->items[(xs)->count++] = (x); \
|
||||
} while (0)
|
||||
""");
|
||||
|
||||
while (referencedTypes.Count != 0)
|
||||
@@ -143,232 +294,111 @@ public class Generator
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case NubTypeString:
|
||||
{
|
||||
writer.WriteLine
|
||||
(
|
||||
"""
|
||||
#define FLAG_STRING_LITERAL 1
|
||||
#define STRING_DEBUG 1
|
||||
case NubTypeArray arrayType:
|
||||
{
|
||||
var name = NameMangler.Mangle("dynamic", "array", arrayType);
|
||||
|
||||
#if STRING_DEBUG
|
||||
#define STR_DBG(fmt, ...) fprintf(stderr, "[STR] " fmt "\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define STR_DBG(fmt, ...)
|
||||
#endif
|
||||
|
||||
struct nub_core_string
|
||||
writer.WriteLine
|
||||
(
|
||||
$$"""
|
||||
struct {{name}}
|
||||
{
|
||||
char *data;
|
||||
size_t length;
|
||||
uint32_t ref;
|
||||
uint32_t flags;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
{{CType(arrayType.ElementType)}} *items;
|
||||
};
|
||||
|
||||
#if STRING_DEBUG
|
||||
static size_t nub_core_string_allocs = 0;
|
||||
static size_t nub_core_string_frees = 0;
|
||||
|
||||
__attribute__((destructor))
|
||||
static void string_debug_report(void)
|
||||
{
|
||||
fprintf(stderr, "[STR] REPORT allocs=%zu frees=%zu leaks=%zu\n", nub_core_string_allocs, nub_core_string_frees, nub_core_string_allocs - nub_core_string_frees);
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline void nub_core_string_rc_inc(struct nub_core_string *string)
|
||||
{
|
||||
if (string == NULL)
|
||||
{
|
||||
STR_DBG("INC null string");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string->flags & FLAG_STRING_LITERAL)
|
||||
return;
|
||||
|
||||
string->ref += 1;
|
||||
|
||||
STR_DBG("INC: str=%p ref=%u \"%s\"", (void*)string, string->ref, string->data);
|
||||
}
|
||||
|
||||
static inline void nub_core_string_rc_dec(struct nub_core_string *string)
|
||||
{
|
||||
if (string == NULL)
|
||||
{
|
||||
STR_DBG("DEC null string");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string->flags & FLAG_STRING_LITERAL)
|
||||
return;
|
||||
|
||||
if (string->ref == 0)
|
||||
{
|
||||
STR_DBG("ERROR: DEC on zero refcount str=%p", (void*)string);
|
||||
return;
|
||||
}
|
||||
|
||||
string->ref -= 1;
|
||||
|
||||
STR_DBG("DEC: str=%p ref=%u \"%s\"", (void*)string, string->ref, string->data);
|
||||
|
||||
if (string->ref == 0)
|
||||
{
|
||||
STR_DBG("FREE: str=%p data=%p \"%s\"", (void*)string, (void*)string->data, string->data);
|
||||
|
||||
char *data_ptr = string->data;
|
||||
struct nub_core_string *str_ptr = string;
|
||||
|
||||
#if STRING_DEBUG
|
||||
nub_core_string_frees++;
|
||||
|
||||
memset(data_ptr, 0xDD, str_ptr->length);
|
||||
memset(str_ptr, 0xDD, sizeof(*str_ptr));
|
||||
#endif
|
||||
|
||||
free(data_ptr);
|
||||
free(str_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
static inline struct nub_core_string *nub_core_string_concat(struct nub_core_string *left, struct nub_core_string *right)
|
||||
{
|
||||
size_t new_length = left->length + right->length;
|
||||
|
||||
struct nub_core_string *result = malloc(sizeof(struct nub_core_string));
|
||||
result->data = 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
|
||||
nub_core_string_allocs++;
|
||||
STR_DBG("NEW concat: str=%p ref=%u \"%s\"", (void*)result, result->ref, result->data);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline struct nub_core_string *nub_core_string_from_cstr(char* cstr)
|
||||
{
|
||||
size_t len = strlen(cstr);
|
||||
|
||||
struct nub_core_string *result = malloc(sizeof(struct nub_core_string));
|
||||
result->data = malloc(len + 1);
|
||||
|
||||
memcpy(result->data, cstr, len + 1);
|
||||
|
||||
result->length = len;
|
||||
result->ref = 1;
|
||||
result->flags = 0;
|
||||
|
||||
#if STRING_DEBUG
|
||||
nub_core_string_allocs++;
|
||||
STR_DBG("NEW from_cstr: str=%p ref=%u \"%s\"", (void*)result, result->ref, result->data);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
"""
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
);
|
||||
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);
|
||||
|
||||
writer.Write("struct ");
|
||||
|
||||
if (structInfo.Packed)
|
||||
writer.Write("__attribute__((__packed__)) ");
|
||||
|
||||
writer.WriteLine(NameMangler.Mangle(structType.Module, structType.Name, structType));
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
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)
|
||||
{
|
||||
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
||||
}
|
||||
}
|
||||
writer.WriteLine("};");
|
||||
writer.WriteLine();
|
||||
EmitTypeDefinitionIfNotEmitted(field.Type);
|
||||
|
||||
break;
|
||||
}
|
||||
case NubTypeAnonymousStruct anonymousStructType:
|
||||
{
|
||||
foreach (var field in anonymousStructType.Fields)
|
||||
EmitTypeDefinitionIfNotEmitted(field.Type);
|
||||
writer.Write("struct ");
|
||||
|
||||
writer.WriteLine($"struct {NameMangler.Mangle("anonymous", "struct", anonymousStructType)}");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
foreach (var field in anonymousStructType.Fields)
|
||||
{
|
||||
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
||||
}
|
||||
}
|
||||
writer.WriteLine("};");
|
||||
writer.WriteLine();
|
||||
if (structInfo.Packed)
|
||||
writer.Write("__attribute__((__packed__)) ");
|
||||
|
||||
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($"struct {NameMangler.Mangle(enumType.Module, enumType.Name, enumType)}");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
writer.WriteLine("uint32_t tag;");
|
||||
writer.WriteLine("union");
|
||||
writer.WriteLine(NameMangler.Mangle(structType.Module, structType.Name, structType));
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
foreach (var variant in enumInfo.Variants)
|
||||
foreach (var field in structInfo.Fields)
|
||||
{
|
||||
if (variant.Type is not null)
|
||||
{
|
||||
writer.WriteLine($"{CType(variant.Type, variant.Name)};");
|
||||
}
|
||||
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
||||
}
|
||||
}
|
||||
writer.WriteLine("};");
|
||||
}
|
||||
writer.WriteLine("};");
|
||||
writer.WriteLine();
|
||||
writer.WriteLine();
|
||||
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NubTypeAnonymousStruct anonymousStructType:
|
||||
{
|
||||
foreach (var field in anonymousStructType.Fields)
|
||||
EmitTypeDefinitionIfNotEmitted(field.Type);
|
||||
|
||||
writer.WriteLine($"struct {NameMangler.Mangle("anonymous", "struct", anonymousStructType)}");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
foreach (var field in anonymousStructType.Fields)
|
||||
{
|
||||
writer.WriteLine($"{CType(field.Type, field.Name)};");
|
||||
}
|
||||
}
|
||||
writer.WriteLine("};");
|
||||
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($"struct {NameMangler.Mangle(enumType.Module, enumType.Name, enumType)}");
|
||||
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("};");
|
||||
writer.WriteLine();
|
||||
|
||||
break;
|
||||
}
|
||||
case NubTypeEnumVariant variantType:
|
||||
{
|
||||
EmitTypeDefinitionIfNotEmitted(variantType.EnumType);
|
||||
break;
|
||||
}
|
||||
{
|
||||
EmitTypeDefinitionIfNotEmitted(variantType.EnumType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -563,6 +593,8 @@ public class Generator
|
||||
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),
|
||||
@@ -584,7 +616,7 @@ public class Generator
|
||||
return name;
|
||||
}
|
||||
|
||||
var op = expression.Operation switch
|
||||
var op = expression.Operation switch
|
||||
{
|
||||
TypedNodeExpressionBinary.Op.Add => $"({left} + {right})",
|
||||
TypedNodeExpressionBinary.Op.Subtract => $"({left} - {right})",
|
||||
@@ -712,6 +744,18 @@ public class Generator
|
||||
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))
|
||||
@@ -747,6 +791,7 @@ public class Generator
|
||||
NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
|
||||
NubTypeString type => "struct nub_core_string" + (varName != null ? $" *{varName}" : "*"),
|
||||
NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
|
||||
NubTypeArray type => $"struct {NameMangler.Mangle("dynamic", "array", type)}" + (varName != null ? $" {varName}" : ""),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
|
||||
};
|
||||
}
|
||||
@@ -787,69 +832,69 @@ public class Generator
|
||||
switch (type)
|
||||
{
|
||||
case NubTypeString:
|
||||
{
|
||||
writer.WriteLine($"nub_core_string_rc_inc({value});");
|
||||
break;
|
||||
}
|
||||
{
|
||||
writer.WriteLine($"nub_core_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();
|
||||
{
|
||||
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++)
|
||||
foreach (var field in structInfo.Fields)
|
||||
{
|
||||
Module.TypeInfoEnum.Variant variant = enumInfo.Variants[i];
|
||||
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();
|
||||
|
||||
if (variant.Type is not null)
|
||||
writer.WriteLine($"switch ({value}.tag)");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
for (int i = 0; i < enumInfo.Variants.Count; i++)
|
||||
{
|
||||
writer.WriteLine($"case {i}:");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
Module.TypeInfoEnum.Variant variant = enumInfo.Variants[i];
|
||||
|
||||
if (variant.Type is not null)
|
||||
{
|
||||
EmitCopyConstructor($"{value}.{variant.Name}", variant.Type);
|
||||
writer.WriteLine("break;");
|
||||
writer.WriteLine($"case {i}:");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
EmitCopyConstructor($"{value}.{variant.Name}", variant.Type);
|
||||
writer.WriteLine("break;");
|
||||
}
|
||||
writer.WriteLine("}");
|
||||
}
|
||||
writer.WriteLine("}");
|
||||
}
|
||||
}
|
||||
writer.WriteLine("}");
|
||||
break;
|
||||
}
|
||||
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();
|
||||
{
|
||||
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);
|
||||
var variant = enumInfo.Variants.First(x => x.Name == enumVariantType.Variant);
|
||||
if (variant.Type is not null)
|
||||
EmitCopyConstructor($"{value}.{variant.Name}", variant.Type);
|
||||
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -858,68 +903,68 @@ public class Generator
|
||||
switch (type)
|
||||
{
|
||||
case NubTypeString:
|
||||
{
|
||||
writer.WriteLine($"nub_core_string_rc_dec({value});");
|
||||
break;
|
||||
}
|
||||
{
|
||||
writer.WriteLine($"nub_core_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();
|
||||
{
|
||||
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++)
|
||||
foreach (var field in structInfo.Fields)
|
||||
{
|
||||
var variant = enumInfo.Variants[i];
|
||||
if (variant.Type is not null)
|
||||
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++)
|
||||
{
|
||||
writer.WriteLine($"case {i}:");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
var variant = enumInfo.Variants[i];
|
||||
if (variant.Type is not null)
|
||||
{
|
||||
EmitCopyDestructor($"{value}.{variant.Name}", variant.Type);
|
||||
writer.WriteLine("break;");
|
||||
writer.WriteLine($"case {i}:");
|
||||
writer.WriteLine("{");
|
||||
using (writer.Indent())
|
||||
{
|
||||
EmitCopyDestructor($"{value}.{variant.Name}", variant.Type);
|
||||
writer.WriteLine("break;");
|
||||
}
|
||||
writer.WriteLine("}");
|
||||
}
|
||||
writer.WriteLine("}");
|
||||
}
|
||||
}
|
||||
writer.WriteLine("}");
|
||||
break;
|
||||
}
|
||||
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();
|
||||
{
|
||||
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);
|
||||
var variant = enumInfo.Variants.First(x => x.Name == enumVariantType.Variant);
|
||||
if (variant.Type is not null)
|
||||
EmitCopyDestructor($"{value}.{variant.Name}", variant.Type);
|
||||
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user