Files
c3c/test/test_suite/generic/generic_lambda_complex.c3t

256 lines
6.5 KiB
C

// #target: macos-x64
/**
* @require Type.kindof == STRUCT
**/
module abc(<Type>);
import std::io;
import std::collections::list;
def TextTagList = List(<TextTag>);
fault TextError
{
UNTERMINATED_TAG,
EMPTY_TAG,
MISSING_TAG,
UNSUPPORTED_TAG,
}
enum TextTagKind: char
{
STRING,
TEMPLATE,
}
struct TextTemplate
{
Allocator* allocator;
String template;
TextTag[] tags;
Type data;
}
struct TextTag
{
usz start;
usz end;
TextTagKind kind;
union {
String* data;
Stream template;
}
}
/**
* @require self.tags.len == 0 "template already initialized"
* @require tag_start != tag_end
**/
fn void! TextTemplate.init(&self, String template, String tag_start = "{{", String tag_end = "}}", Allocator* using = mem::heap())
{
TextTagList tags;
String tmpl = template;
uptr data = (uptr)&self.data;
while (true)
{
usz! start = tmpl.index_of(tag_start);
if (catch start) break;
tmpl = tmpl[start + tag_start.len..];
usz! end = tmpl.index_of(tag_end);
if (catch end) return TextError.UNTERMINATED_TAG?;
String name = tmpl[:end].trim();
if (name == "") return TextError.EMPTY_TAG?;
// Check that the tag exists in the data struct.
TextTag tag = {|
$foreach ($m : Type.membersof)
if (name == $m.nameof)
{
$switch ($m.typeid)
$case String.typeid:
return TextTag{
.kind = STRING,
.data = (String*)(data + $m.offsetof),
};
$default:
$if $checks(self.data.$eval($m.nameof).as_stream()):
return TextTag{
.kind = TEMPLATE,
.template = self.data.$eval($m.nameof).as_stream(),
};
$endif
$endswitch
//return TextError.UNSUPPORTED_TAG?;
}
$endforeach
return TextError.MISSING_TAG?;
|}!;
tmpl = tmpl[end + tag_end.len..];
tag.start = start;
tag.end = start + tag_start.len + end + tag_end.len;
tags.push(tag);
}
*self = { .allocator = using, .template = template, .tags = tags.array_view() };
}
fn void! TextTemplate.free(&self)
{
self.allocator.free(self.tags);
*self = {};
}
fn StreamWrapper TextTemplate.as_stream(&self)
{
return { .stream.fns = &texttemplate_interface, .data = self };
}
StreamInterface texttemplate_interface @private = {
.write_stream_fn = fn (s, out) => ((TextTemplate*)(((StreamWrapper*)s).data)).write_to(out),
};
fn usz! TextTemplate.write_to(&self, Stream* writer)
{
usz n;
usz pos;
foreach (tag : self.tags)
{
n += writer.write(self.template[pos:tag.start])!;
pos += tag.end;
n += tag.write(writer)!;
}
n += writer.write(self.template[pos..])!;
return n;
}
fn usz! TextTag.write(&self, Stream* writer)
{
switch (self.kind)
{
case STRING:
return writer.write(*self.data);
case TEMPLATE:
return self.template.write_to(writer);
}
}
module text_test;
import abc;
import std::io;
def Tmpl = TextTemplate(<Data>);
struct Data
{
String user;
String world;
bool ok;
}
def FooTmpl = TextTemplate(<Foo>);
def BarTmpl = TextTemplate(<Bar>);
struct Foo
{
String foo;
BarTmpl* bar;
}
struct Bar
{
String bar;
}
fn void! main()
{
String foo_tmpl = "<<{{foo}} | {{bar}}>>";
FooTmpl ft;
ft.init(foo_tmpl, .using = mem::temp())!;
defer ft.free()!!;
}
/* #expect: text_test.ll
; Function Attrs: nounwind
define i64 @text_test.main() #0 {
entry:
%foo_tmpl = alloca %"char[]", align 8
%ft = alloca %TextTemplate, align 8
%error_var = alloca i64, align 8
%indirectarg = alloca %"char[]", align 8
%indirectarg1 = alloca %"any*", align 8
%error_var2 = alloca i64, align 8
%varargslots = alloca [1 x %"any*"], align 16
%indirectarg6 = alloca %"any*[]", align 8
store %"char[]" { ptr @.str, i64 21 }, ptr %foo_tmpl, align 8
call void @llvm.memset.p0.i64(ptr align 8 %ft, i8 0, i64 72, i1 false)
%0 = load ptr, ptr @std.core.mem.thread_temp_allocator, align 8
%not = icmp eq ptr %0, null
br i1 %not, label %if.then, label %if.exit
if.then: ; preds = %entry
call void @std.core.mem.init_default_temp_allocators()
br label %if.exit
if.exit: ; preds = %if.then, %entry
%1 = load ptr, ptr @std.core.mem.thread_temp_allocator, align 8
%2 = insertvalue %"any*" undef, ptr %1, 0
%3 = insertvalue %"any*" %2, i64 ptrtoint (ptr @"$ct.std.core.mem.allocator.TempAllocator" to i64), 1
%4 = getelementptr inbounds %"char[]", ptr %foo_tmpl, i32 0, i32 0
%lo = load ptr, ptr %4, align 8
%5 = getelementptr inbounds %"char[]", ptr %foo_tmpl, i32 0, i32 1
%hi = load i64, ptr %5, align 8
store %"char[]" { ptr @.str.2, i64 2 }, ptr %indirectarg, align 8
store %"any*" %3, ptr %indirectarg1, align 8
%6 = call i64 @"abc$text_test.Foo$.TextTemplate.init"(ptr %ft, ptr %lo, i64 %hi, ptr @.str.1, i64 2, ptr byval(%"char[]") align 8 %indirectarg, ptr byval(%"any*") align 8 %indirectarg1)
%not_err = icmp eq i64 %6, 0
%7 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
br i1 %7, label %after_check, label %assign_optional
assign_optional: ; preds = %if.exit
store i64 %6, ptr %error_var, align 8
br label %guard_block
after_check: ; preds = %if.exit
br label %noerr_block
guard_block: ; preds = %assign_optional
%8 = load i64, ptr %error_var, align 8
ret i64 %8
noerr_block: ; preds = %after_check
%9 = call i64 @"abc$text_test.Foo$.TextTemplate.free"(ptr %ft)
%not_err3 = icmp eq i64 %9, 0
%10 = call i1 @llvm.expect.i1(i1 %not_err3, i1 true)
br i1 %10, label %after_check5, label %assign_optional4
assign_optional4: ; preds = %noerr_block
store i64 %9, ptr %error_var2, align 8
br label %panic_block
after_check5: ; preds = %noerr_block
br label %noerr_block7
panic_block: ; preds = %assign_optional4
%11 = insertvalue %"any*" undef, ptr %error_var2, 0
%12 = insertvalue %"any*" %11, i64 ptrtoint (ptr @"$ct.anyfault" to i64), 1
%13 = getelementptr inbounds [1 x %"any*"], ptr %varargslots, i64 0, i64 0
store %"any*" %12, ptr %13, align 16
%14 = insertvalue %"any*[]" undef, ptr %varargslots, 0
%"$$temp" = insertvalue %"any*[]" %14, i64 1, 1
store %"any*[]" %"$$temp", ptr %indirectarg6, align 8
call void @std.core.builtin.panicf(ptr @.panic_msg, i64 36, ptr @.file, i64 25, ptr @.func, i64 4, i32 173, ptr byval(%"any*[]") align 8 %indirectarg6)
unreachable
noerr_block7: ; preds = %after_check5
ret i64 0
}