diff --git a/src/main/scala/fred/Translator.scala b/src/main/scala/fred/Translator.scala index 270492b..2c46e5f 100644 --- a/src/main/scala/fred/Translator.scala +++ b/src/main/scala/fred/Translator.scala @@ -16,18 +16,28 @@ object Translator { .flatMap(gen => file.typeDefs.map(td => (gen.decl(td), gen.impl(td)))) .unzip val (fnDecls, fnImpls) = file.fns.map(helper.fnToC).unzip + val ctorDecls = file.typeDefs.flatMap(typ => + typ.cases.map(variant => s"${ctorSig(typ, variant)};") + ) + val ctorImpls = file.typeDefs.flatMap { typ => + typ.cases.map { variant => + s"${ctorSig(typ, variant)} {\n${indent(1)(ctorImpl(typ, variant))}\n}" + } + } val generated = file.typeDefs.map(helper.translateType).mkString("", "\n", "\n") + genDecls.mkString("", "\n", "\n") + + ctorDecls.mkString("", "\n", "\n") + fnDecls.mkString("", "\n", "\n") + genImpls.mkString("", "\n", "\n") + + ctorImpls.mkString("", "\n", "\n") + fnImpls.mkString("", "\n", "\n") "#include \"runtime.h\"\n\n" + generated .replaceAll(raw"\n(\s|\n)*\n", "\n") .strip() + "\n" } - trait GeneratedFn(unmangledName: String) { + private trait GeneratedFn(unmangledName: String) { def returnType: String def body(typ: TypeDef)(using Bindings, Cycles): String @@ -269,6 +279,39 @@ object Translator { } } + private def ctorName(variant: EnumCase): String = + s"new$$${variant.name.value}" + + private def ctorSig(typ: TypeDef, variant: EnumCase): String = { + val params = variant.fields.toSeq + .sortBy(_.name.value) + .map(field => s"${typeRefToC(field.typ.name)} ${field.name.value}") + .mkString(", ") + s"${typeRefToC(typ.name)} ${ctorName(variant)}($params)" + } + + private def ctorImpl(typ: TypeDef, variant: EnumCase)(using + bindings: Bindings + ): String = { + val resVar = "$res" + val setFields = variant.fields + .map { field => + val fieldAccess = + s"$resVar->${cFieldName(field.name.value, typ, variant)}" + s"""|$fieldAccess = ${field.name.value}; + |${incrRc(fieldAccess, bindings.getType(field.typ))}""".stripMargin + } + .mkString("\n") + s"""|${typeRefToC(typ.name)} $resVar = malloc(sizeof (struct ${typ.name})); + |$resVar->rc = 0; + |$resVar->color = kBlack; + |$resVar->addedPCR = 0; + |$resVar->print = ${Printer.name(typ)}; + |$resVar->kind = ${tagName(variant.name.value)}; + |$setFields + |return $resVar;""".stripMargin + } + private def switch(expr: String, typ: TypeDef)( createArm: EnumCase => String ): String = { @@ -544,36 +587,17 @@ object Translator { s"${mangleFnName(fnName.value)}(${argsToC.mkString(", ")})", teardowns.mkString("\n") ) - case CtorCall(ctorName, values, span) => - val (typ, variant) = bindings.ctors(ctorName.value) - val resVar = newVar("ctorres") - val valueSetups = values - .map { case (fieldName, value) => - val fieldType = bindings.types( - variant.fields - .find(_.name.value == fieldName.value) - .get - .typ - .name - ) - val (valueSetup, valueToC, valueTeardown) = exprToC(value) - val fieldAccess = - s"$resVar->${cFieldName(fieldName.value, typ, variant)}" - s"""|$valueSetup - |$fieldAccess = $valueToC; - |${incrRc(fieldAccess, fieldType)} - |$valueTeardown""".stripMargin - } - .mkString("\n") - val setup = - s"""|${typeRefToC(typ.name)} $resVar = malloc(sizeof (struct ${typ.name})); - |$resVar->rc = 0; - |$resVar->color = kBlack; - |$resVar->addedPCR = 0; - |$resVar->print = ${Printer.name(typ)}; - |$resVar->kind = ${tagName(ctorName.value)}; - |$valueSetups""".stripMargin - (setup, resVar, "") + case CtorCall(ctorNameSpanned, values, span) => + val (typ, variant) = bindings.ctors(ctorNameSpanned.value) + val (setups, args, teardowns) = values.toSeq + .sortBy((fieldName, _) => fieldName.value) + .map((_, value) => exprToC(value)) + .unzip3 + ( + setups.mkString("\n"), + s"${ctorName(variant)}(${args.mkString(", ")})", + teardowns.mkString("\n") + ) case matchExpr @ MatchExpr(obj, arms, _) => val (objSetup, objToC, objTeardown) = exprToC(obj) val objType = typer.types(obj).asInstanceOf[TypeDef] diff --git a/src/test/resources/snapshot/exec/basic-cycle.c b/src/test/resources/snapshot/exec/basic-cycle.c index e9f6a1d..fe73b03 100644 --- a/src/test/resources/snapshot/exec/basic-cycle.c +++ b/src/test/resources/snapshot/exec/basic-cycle.c @@ -39,6 +39,9 @@ void $collectWhite_Option(struct Option* this); void $collectWhite_List(struct List* this); void $print_Option(struct Option* this); void $print_List(struct List* this); +struct Option* new$None(); +struct Option* new$Some(struct List* value); +struct List* new$List(struct Option* next, int value); int main(); void $free_Option(struct Option* this) { fprintf(stderr, "Freeing Option\n"); @@ -235,59 +238,51 @@ void $print_List(struct List* this) { break; } } +struct Option* new$None() { + struct Option* $res = malloc(sizeof (struct Option)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Option; + $res->kind = None_tag; + return $res; +} +struct Option* new$Some(struct List* value) { + struct Option* $res = malloc(sizeof (struct Option)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Option; + $res->kind = Some_tag; + $res->value_Some = value; + $res->value_Some->rc ++; + return $res; +} +struct List* new$List(struct Option* next, int value) { + struct List* $res = malloc(sizeof (struct List)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_List; + $res->kind = List_tag; + $res->value = value; + $res->next = next; + $res->next->rc ++; + return $res; +} int main() { - struct List* ctorres$0 = malloc(sizeof (struct List)); - ctorres$0->rc = 0; - ctorres$0->color = kBlack; - ctorres$0->addedPCR = 0; - ctorres$0->print = $print_List; - ctorres$0->kind = List_tag; - ctorres$0->value = 1; - struct Option* ctorres$1 = malloc(sizeof (struct Option)); - ctorres$1->rc = 0; - ctorres$1->color = kBlack; - ctorres$1->addedPCR = 0; - ctorres$1->print = $print_Option; - ctorres$1->kind = None_tag; - ctorres$0->next = ctorres$1; - ctorres$0->next->rc ++; - struct List* a = ctorres$0; + struct List* a = new$List(new$None(), 1); a->rc ++; - struct List* ctorres$2 = malloc(sizeof (struct List)); - ctorres$2->rc = 0; - ctorres$2->color = kBlack; - ctorres$2->addedPCR = 0; - ctorres$2->print = $print_List; - ctorres$2->kind = List_tag; - ctorres$2->value = 2; - struct Option* ctorres$3 = malloc(sizeof (struct Option)); - ctorres$3->rc = 0; - ctorres$3->color = kBlack; - ctorres$3->addedPCR = 0; - ctorres$3->print = $print_Option; - ctorres$3->kind = Some_tag; - ctorres$3->value_Some = a; - ctorres$3->value_Some->rc ++; - ctorres$2->next = ctorres$3; - ctorres$2->next->rc ++; - struct List* b = ctorres$2; + struct List* b = new$List(new$Some(a), 2); b->rc ++; - struct Option* ctorres$4 = malloc(sizeof (struct Option)); - ctorres$4->rc = 0; - ctorres$4->color = kBlack; - ctorres$4->addedPCR = 0; - ctorres$4->print = $print_Option; - ctorres$4->kind = Some_tag; - ctorres$4->value_Some = b; - ctorres$4->value_Some->rc ++; $decr_Option(a->next); - a->next = ctorres$4; + a->next = new$Some(b); a->next->rc ++; a->next; printf("%d\n", a->value + b->value); - int ret$5 = 0; + int ret$0 = 0; $decr_List(b); $decr_List(a); processAllPCRs(); - return ret$5; + return ret$0; } diff --git a/src/test/resources/snapshot/exec/basic-main.c b/src/test/resources/snapshot/exec/basic-main.c index 26173db..8e97b9a 100644 --- a/src/test/resources/snapshot/exec/basic-main.c +++ b/src/test/resources/snapshot/exec/basic-main.c @@ -19,6 +19,8 @@ void $scan_List(struct List* this); void $scanBlack_List(struct List* this); void $collectWhite_List(struct List* this); void $print_List(struct List* this); +struct List* new$Nil(); +struct List* new$Cons(struct List* next, int value); int fn$sum(struct List* list); int main(); void $free_List(struct List* this) { @@ -121,6 +123,27 @@ void $print_List(struct List* this) { break; } } +struct List* new$Nil() { + struct List* $res = malloc(sizeof (struct List)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_List; + $res->kind = Nil_tag; + return $res; +} +struct List* new$Cons(struct List* next, int value) { + struct List* $res = malloc(sizeof (struct List)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_List; + $res->kind = Cons_tag; + $res->value_Cons = value; + $res->next_Cons = next; + $res->next_Cons->rc ++; + return $res; +} int fn$sum(struct List* list) { list->rc ++; struct List* matchobj$0 = list; @@ -142,44 +165,11 @@ int fn$sum(struct List* list) { return ret$2; } int main() { - struct List* ctorres$3 = malloc(sizeof (struct List)); - ctorres$3->rc = 0; - ctorres$3->color = kBlack; - ctorres$3->addedPCR = 0; - ctorres$3->print = $print_List; - ctorres$3->kind = Cons_tag; - ctorres$3->value_Cons = 1; - struct List* ctorres$4 = malloc(sizeof (struct List)); - ctorres$4->rc = 0; - ctorres$4->color = kBlack; - ctorres$4->addedPCR = 0; - ctorres$4->print = $print_List; - ctorres$4->kind = Cons_tag; - ctorres$4->value_Cons = 2; - struct List* ctorres$5 = malloc(sizeof (struct List)); - ctorres$5->rc = 0; - ctorres$5->color = kBlack; - ctorres$5->addedPCR = 0; - ctorres$5->print = $print_List; - ctorres$5->kind = Cons_tag; - ctorres$5->value_Cons = 4; - struct List* ctorres$6 = malloc(sizeof (struct List)); - ctorres$6->rc = 0; - ctorres$6->color = kBlack; - ctorres$6->addedPCR = 0; - ctorres$6->print = $print_List; - ctorres$6->kind = Nil_tag; - ctorres$5->next_Cons = ctorres$6; - ctorres$5->next_Cons->rc ++; - ctorres$4->next_Cons = ctorres$5; - ctorres$4->next_Cons->rc ++; - ctorres$3->next_Cons = ctorres$4; - ctorres$3->next_Cons->rc ++; - struct List* list = ctorres$3; + struct List* list = new$Cons(new$Cons(new$Cons(new$Nil(), 4), 2), 1); list->rc ++; printf("%d\n", fn$sum(list)); - int ret$7 = 0; + int ret$3 = 0; $decr_List(list); processAllPCRs(); - return ret$7; + return ret$3; } diff --git a/src/test/resources/snapshot/exec/contrived-needs-sorting.c b/src/test/resources/snapshot/exec/contrived-needs-sorting.c index 4b82f55..94459ba 100644 --- a/src/test/resources/snapshot/exec/contrived-needs-sorting.c +++ b/src/test/resources/snapshot/exec/contrived-needs-sorting.c @@ -115,6 +115,14 @@ void $print_FileList(struct FileList* this); void $print_File(struct File* this); void $print_ExprList(struct ExprList* this); void $print_Expr(struct Expr* this); +struct CtxRef* new$CtxRef(struct Context* ref); +struct Context* new$Context(struct FileList* files, char* name); +struct FileList* new$FileNil(); +struct FileList* new$FileCons(struct Context* ctx, struct File* head, struct FileList* tail); +struct File* new$File(struct ExprList* exprs); +struct ExprList* new$ExprNil(); +struct ExprList* new$ExprCons(struct Expr* head, struct ExprList* tail); +struct Expr* new$Expr(struct File* file); int main(); void $free_CtxRef(struct CtxRef* this) { fprintf(stderr, "Freeing CtxRef\n"); @@ -694,114 +702,122 @@ void $print_Expr(struct Expr* this) { break; } } +struct CtxRef* new$CtxRef(struct Context* ref) { + struct CtxRef* $res = malloc(sizeof (struct CtxRef)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_CtxRef; + $res->kind = CtxRef_tag; + $res->ref = ref; + $res->ref->rc ++; + return $res; +} +struct Context* new$Context(struct FileList* files, char* name) { + struct Context* $res = malloc(sizeof (struct Context)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Context; + $res->kind = Context_tag; + $res->name = name; + $res->files = files; + $res->files->rc ++; + return $res; +} +struct FileList* new$FileNil() { + struct FileList* $res = malloc(sizeof (struct FileList)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_FileList; + $res->kind = FileNil_tag; + return $res; +} +struct FileList* new$FileCons(struct Context* ctx, struct File* head, struct FileList* tail) { + struct FileList* $res = malloc(sizeof (struct FileList)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_FileList; + $res->kind = FileCons_tag; + $res->ctx_FileCons = ctx; + $res->ctx_FileCons->rc ++; + $res->head_FileCons = head; + $res->head_FileCons->rc ++; + $res->tail_FileCons = tail; + $res->tail_FileCons->rc ++; + return $res; +} +struct File* new$File(struct ExprList* exprs) { + struct File* $res = malloc(sizeof (struct File)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_File; + $res->kind = File_tag; + $res->exprs = exprs; + $res->exprs->rc ++; + return $res; +} +struct ExprList* new$ExprNil() { + struct ExprList* $res = malloc(sizeof (struct ExprList)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_ExprList; + $res->kind = ExprNil_tag; + return $res; +} +struct ExprList* new$ExprCons(struct Expr* head, struct ExprList* tail) { + struct ExprList* $res = malloc(sizeof (struct ExprList)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_ExprList; + $res->kind = ExprCons_tag; + $res->head_ExprCons = head; + $res->head_ExprCons->rc ++; + $res->tail_ExprCons = tail; + $res->tail_ExprCons->rc ++; + return $res; +} +struct Expr* new$Expr(struct File* file) { + struct Expr* $res = malloc(sizeof (struct Expr)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Expr; + $res->kind = Expr_tag; + $res->file = file; + $res->file->rc ++; + return $res; +} int main() { - struct CtxRef* ctorres$0 = malloc(sizeof (struct CtxRef)); - ctorres$0->rc = 0; - ctorres$0->color = kBlack; - ctorres$0->addedPCR = 0; - ctorres$0->print = $print_CtxRef; - ctorres$0->kind = CtxRef_tag; - struct Context* ctorres$1 = malloc(sizeof (struct Context)); - ctorres$1->rc = 0; - ctorres$1->color = kBlack; - ctorres$1->addedPCR = 0; - ctorres$1->print = $print_Context; - ctorres$1->kind = Context_tag; - ctorres$1->name = "foo"; - struct FileList* ctorres$2 = malloc(sizeof (struct FileList)); - ctorres$2->rc = 0; - ctorres$2->color = kBlack; - ctorres$2->addedPCR = 0; - ctorres$2->print = $print_FileList; - ctorres$2->kind = FileNil_tag; - ctorres$1->files = ctorres$2; - ctorres$1->files->rc ++; - ctorres$0->ref = ctorres$1; - ctorres$0->ref->rc ++; - struct CtxRef* ctx = ctorres$0; + struct CtxRef* ctx = new$CtxRef(new$Context(new$FileNil(), "foo")); ctx->rc ++; - struct File* ctorres$3 = malloc(sizeof (struct File)); - ctorres$3->rc = 0; - ctorres$3->color = kBlack; - ctorres$3->addedPCR = 0; - ctorres$3->print = $print_File; - ctorres$3->kind = File_tag; - struct ExprList* ctorres$4 = malloc(sizeof (struct ExprList)); - ctorres$4->rc = 0; - ctorres$4->color = kBlack; - ctorres$4->addedPCR = 0; - ctorres$4->print = $print_ExprList; - ctorres$4->kind = ExprNil_tag; - ctorres$3->exprs = ctorres$4; - ctorres$3->exprs->rc ++; - struct File* file = ctorres$3; + struct File* file = new$File(new$ExprNil()); file->rc ++; struct Context* actualCtx = ctx->ref; actualCtx->rc ++; - struct FileList* ctorres$5 = malloc(sizeof (struct FileList)); - ctorres$5->rc = 0; - ctorres$5->color = kBlack; - ctorres$5->addedPCR = 0; - ctorres$5->print = $print_FileList; - ctorres$5->kind = FileCons_tag; - ctorres$5->ctx_FileCons = ctx->ref; - ctorres$5->ctx_FileCons->rc ++; - ctorres$5->head_FileCons = file; - ctorres$5->head_FileCons->rc ++; - ctorres$5->tail_FileCons = ctx->ref->files; - ctorres$5->tail_FileCons->rc ++; $decr_FileList(actualCtx->files); - actualCtx->files = ctorres$5; + actualCtx->files = new$FileCons(ctx->ref, file, ctx->ref->files); actualCtx->files->rc ++; - struct Expr* ctorres$6 = malloc(sizeof (struct Expr)); - ctorres$6->rc = 0; - ctorres$6->color = kBlack; - ctorres$6->addedPCR = 0; - ctorres$6->print = $print_Expr; - ctorres$6->kind = Expr_tag; - ctorres$6->file = file; - ctorres$6->file->rc ++; - struct Expr* expr = ctorres$6; + struct Expr* expr = new$Expr(file); expr->rc ++; - struct ExprList* ctorres$7 = malloc(sizeof (struct ExprList)); - ctorres$7->rc = 0; - ctorres$7->color = kBlack; - ctorres$7->addedPCR = 0; - ctorres$7->print = $print_ExprList; - ctorres$7->kind = ExprCons_tag; - ctorres$7->head_ExprCons = expr; - ctorres$7->head_ExprCons->rc ++; - ctorres$7->tail_ExprCons = file->exprs; - ctorres$7->tail_ExprCons->rc ++; $decr_ExprList(file->exprs); - file->exprs = ctorres$7; + file->exprs = new$ExprCons(expr, file->exprs); file->exprs->rc ++; actualCtx->files; - struct Context* ctorres$8 = malloc(sizeof (struct Context)); - ctorres$8->rc = 0; - ctorres$8->color = kBlack; - ctorres$8->addedPCR = 0; - ctorres$8->print = $print_Context; - ctorres$8->kind = Context_tag; - ctorres$8->name = "other context"; - struct FileList* ctorres$9 = malloc(sizeof (struct FileList)); - ctorres$9->rc = 0; - ctorres$9->color = kBlack; - ctorres$9->addedPCR = 0; - ctorres$9->print = $print_FileList; - ctorres$9->kind = FileNil_tag; - ctorres$8->files = ctorres$9; - ctorres$8->files->rc ++; $decr_Context(ctx->ref); - ctx->ref = ctorres$8; + ctx->ref = new$Context(new$FileNil(), "other context"); ctx->ref->rc ++; drop((void *) file->exprs, (void *) $decr_ExprList); drop((void *) ctx->ref, (void *) $decr_Context); - int ret$10 = 0; + int ret$0 = 0; $decr_Expr(expr); $decr_Context(actualCtx); $decr_File(file); $decr_CtxRef(ctx); - processAllPCRs(); - return ret$10; + return ret$0; } diff --git a/src/test/resources/snapshot/exec/immediate-drop.c b/src/test/resources/snapshot/exec/immediate-drop.c index 82b9cb5..e26e583 100644 --- a/src/test/resources/snapshot/exec/immediate-drop.c +++ b/src/test/resources/snapshot/exec/immediate-drop.c @@ -18,6 +18,7 @@ void $scan_Foo(struct Foo* this); void $scanBlack_Foo(struct Foo* this); void $collectWhite_Foo(struct Foo* this); void $print_Foo(struct Foo* this); +struct Foo* new$Foo(); int main(); void $free_Foo(struct Foo* this) { fprintf(stderr, "Freeing Foo\n"); @@ -90,15 +91,18 @@ void $print_Foo(struct Foo* this) { break; } } +struct Foo* new$Foo() { + struct Foo* $res = malloc(sizeof (struct Foo)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Foo; + $res->kind = Foo_tag; + return $res; +} int main() { - struct Foo* ctorres$0 = malloc(sizeof (struct Foo)); - ctorres$0->rc = 0; - ctorres$0->color = kBlack; - ctorres$0->addedPCR = 0; - ctorres$0->print = $print_Foo; - ctorres$0->kind = Foo_tag; - drop((void *) ctorres$0, (void *) $decr_Foo); - int ret$1 = 0; + drop((void *) new$Foo(), (void *) $decr_Foo); + int ret$0 = 0; processAllPCRs(); - return ret$1; + return ret$0; } diff --git a/src/test/resources/snapshot/gen/complex-liudr567.c b/src/test/resources/snapshot/gen/complex-liudr567.c index b4ddb1d..94ac5c9 100644 --- a/src/test/resources/snapshot/gen/complex-liudr567.c +++ b/src/test/resources/snapshot/gen/complex-liudr567.c @@ -19,6 +19,8 @@ void $scan_Foo(struct Foo* this); void $scanBlack_Foo(struct Foo* this); void $collectWhite_Foo(struct Foo* this); void $print_Foo(struct Foo* this); +struct Foo* new$Bar(int x, int y); +struct Foo* new$Baz(char* a, int b); int fn$foo(struct Foo* foo); int main(); void $free_Foo(struct Foo* this) { @@ -120,6 +122,28 @@ void $print_Foo(struct Foo* this) { break; } } +struct Foo* new$Bar(int x, int y) { + struct Foo* $res = malloc(sizeof (struct Foo)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Foo; + $res->kind = Bar_tag; + $res->x_Bar = x; + $res->y_Bar = y; + return $res; +} +struct Foo* new$Baz(char* a, int b) { + struct Foo* $res = malloc(sizeof (struct Foo)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Foo; + $res->kind = Baz_tag; + $res->a_Baz = a; + $res->b_Baz = b; + return $res; +} int fn$foo(struct Foo* foo) { foo->rc ++; struct Foo* matchobj$0 = foo; @@ -141,18 +165,10 @@ int fn$foo(struct Foo* foo) { return ret$2; } int main() { - struct Foo* ctorres$3 = malloc(sizeof (struct Foo)); - ctorres$3->rc = 0; - ctorres$3->color = kBlack; - ctorres$3->addedPCR = 0; - ctorres$3->print = $print_Foo; - ctorres$3->kind = Bar_tag; - ctorres$3->x_Bar = 1; - ctorres$3->y_Bar = 2; - struct Foo* foo = ctorres$3; + struct Foo* foo = new$Bar(1, 2); foo->rc ++; - int ret$4 = fn$foo(foo); + int ret$3 = fn$foo(foo); $decr_Foo(foo); processAllPCRs(); - return ret$4; + return ret$3; } diff --git a/src/test/resources/snapshot/gen/good-scc-jifs893.c b/src/test/resources/snapshot/gen/good-scc-jifs893.c index e28a28f..4061083 100644 --- a/src/test/resources/snapshot/gen/good-scc-jifs893.c +++ b/src/test/resources/snapshot/gen/good-scc-jifs893.c @@ -19,6 +19,7 @@ void $scan_Rec(struct Rec* this); void $scanBlack_Rec(struct Rec* this); void $collectWhite_Rec(struct Rec* this); void $print_Rec(struct Rec* this); +struct Rec* new$Rec(struct Rec* rec); void $free_Rec(struct Rec* this) { fprintf(stderr, "Freeing Rec\n"); switch (this->kind) { @@ -100,3 +101,14 @@ void $print_Rec(struct Rec* this) { break; } } +struct Rec* new$Rec(struct Rec* rec) { + struct Rec* $res = malloc(sizeof (struct Rec)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Rec; + $res->kind = Rec_tag; + $res->rec = rec; + $res->rec->rc ++; + return $res; +} diff --git a/src/test/resources/snapshot/gen/set-field-sfdu29.c b/src/test/resources/snapshot/gen/set-field-sfdu29.c index e0290be..87e8c5c 100644 --- a/src/test/resources/snapshot/gen/set-field-sfdu29.c +++ b/src/test/resources/snapshot/gen/set-field-sfdu29.c @@ -19,6 +19,7 @@ void $scan_Foo(struct Foo* this); void $scanBlack_Foo(struct Foo* this); void $collectWhite_Foo(struct Foo* this); void $print_Foo(struct Foo* this); +struct Foo* new$Foo(int field); int fn$bar(struct Foo* f); void $free_Foo(struct Foo* this) { fprintf(stderr, "Freeing Foo\n"); @@ -94,6 +95,16 @@ void $print_Foo(struct Foo* this) { break; } } +struct Foo* new$Foo(int field) { + struct Foo* $res = malloc(sizeof (struct Foo)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Foo; + $res->kind = Foo_tag; + $res->field = field; + return $res; +} int fn$bar(struct Foo* f) { f->rc ++; f->field = 5; diff --git a/src/test/resources/snapshot/gen/type-aslid7fy.c b/src/test/resources/snapshot/gen/type-aslid7fy.c index d0b3699..1f1ab0a 100644 --- a/src/test/resources/snapshot/gen/type-aslid7fy.c +++ b/src/test/resources/snapshot/gen/type-aslid7fy.c @@ -20,6 +20,8 @@ void $scan_Foo(struct Foo* this); void $scanBlack_Foo(struct Foo* this); void $collectWhite_Foo(struct Foo* this); void $print_Foo(struct Foo* this); +struct Foo* new$Bar(char* common, struct Foo* foo, char* notcommon); +struct Foo* new$Baz(char* blech, char* common, int gah, int notcommon); char* fn$foo(struct Foo* param); void $free_Foo(struct Foo* this) { fprintf(stderr, "Freeing Foo\n"); @@ -143,6 +145,32 @@ void $print_Foo(struct Foo* this) { break; } } +struct Foo* new$Bar(char* common, struct Foo* foo, char* notcommon) { + struct Foo* $res = malloc(sizeof (struct Foo)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Foo; + $res->kind = Bar_tag; + $res->foo_Bar = foo; + $res->foo_Bar->rc ++; + $res->common = common; + $res->notcommon_Bar = notcommon; + return $res; +} +struct Foo* new$Baz(char* blech, char* common, int gah, int notcommon) { + struct Foo* $res = malloc(sizeof (struct Foo)); + $res->rc = 0; + $res->color = kBlack; + $res->addedPCR = 0; + $res->print = $print_Foo; + $res->kind = Baz_tag; + $res->blech_Baz = blech; + $res->gah_Baz = gah; + $res->common = common; + $res->notcommon_Baz = notcommon; + return $res; +} char* fn$foo(struct Foo* param) { param->rc ++; char* ret$0 = param->common;